uni-list-chat.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. <template>
  2. <!-- #ifdef APP-NVUE -->
  3. <cell>
  4. <!-- #endif -->
  5. <view :hover-class="!clickable && !link ? '' : 'uni-list-chat--hover'" class="uni-list-chat" @click.stop="onClick">
  6. <view :class="{ 'uni-list--border': border, 'uni-list-chat--first': isFirstChild }"></view>
  7. <view class="uni-list-chat__container">
  8. <view class="uni-list-chat__header-warp">
  9. <view v-if="avatarCircle || avatarList.length === 0" class="uni-list-chat__header" :class="{ 'header--circle': avatarCircle }">
  10. <image class="uni-list-chat__header-image" :src="avatar" mode="aspectFill"></image>
  11. </view>
  12. <!-- 头像组 -->
  13. <view v-else class="uni-list-chat__header">
  14. <view
  15. v-for="(item, index) in avatarList"
  16. :key="index"
  17. class="uni-list-chat__header-box"
  18. :class="computedAvatar"
  19. :style="{ width: imageWidth + 'px', height: imageWidth + 'px' }"
  20. >
  21. <image class="uni-list-chat__header-image" :style="{ width: imageWidth + 'px', height: imageWidth + 'px' }" :src="item.url" mode="aspectFill"></image>
  22. </view>
  23. </view>
  24. </view>
  25. <view v-if="badgeText && badgePositon === 'left'" class="uni-list-chat__badge uni-list-chat__badge-pos" :class="[isSingle]">
  26. <text class="uni-list-chat__badge-text">{{ badgeText === 'dot' ? '' : badgeText }}</text>
  27. </view>
  28. <view class="uni-list-chat__content">
  29. <view class="uni-list-chat__content-main">
  30. <text class="uni-list-chat__content-title uni-ellipsis">{{ title }}</text>
  31. <text class="uni-list-chat__content-note uni-ellipsis">{{ note }}</text>
  32. </view>
  33. <view class="uni-list-chat__content-extra">
  34. <slot>
  35. <text class="uni-list-chat__content-extra-text">{{ time }}</text>
  36. <view
  37. v-if="badgeText && badgePositon === 'right'"
  38. class="uni-list-chat__badge"
  39. :class="[isSingle, badgePositon === 'right' ? 'uni-list-chat--right' : '']"
  40. >
  41. <text class="uni-list-chat__badge-text">{{ badgeText === 'dot' ? '' : badgeText }}</text>
  42. </view>
  43. </slot>
  44. </view>
  45. </view>
  46. </view>
  47. </view>
  48. <!-- #ifdef APP-NVUE -->
  49. </cell>
  50. <!-- #endif -->
  51. </template>
  52. <script>
  53. // 头像大小
  54. const avatarWidth = 45;
  55. /**
  56. * ListChat 聊天列表
  57. * @description 聊天列表,用于创建聊天类列表
  58. * @tutorial https://ext.dcloud.net.cn/plugin?id=24
  59. * @property {String} title 标题
  60. * @property {String} note 描述
  61. * @property {Boolean} clickable = [true|false] 是否开启点击反馈,默认为false
  62. * @property {String} badgeText 数字角标内容
  63. * @property {String} badgePositon = [left|right] 角标位置,默认为 right
  64. * @property {String} link = [false|navigateTo|redirectTo|reLaunch|switchTab] 是否展示右侧箭头并开启点击反馈,默认为false
  65. * @value false 不开启
  66. * @value navigateTo 同 uni.navigateTo()
  67. * @value redirectTo 同 uni.redirectTo()
  68. * @value reLaunch 同 uni.reLaunch()
  69. * @value switchTab 同 uni.switchTab()
  70. * @property {String | PageURIString} to 跳转目标页面
  71. * @property {String} time 右侧时间显示
  72. * @property {Boolean} avatarCircle = [true|false] 是否显示圆形头像,默认为false
  73. * @property {String} avatar 头像地址,avatarCircle 不填时生效
  74. * @property {Array} avatarList 头像组,格式为 [{url:''}]
  75. * @event {Function} click 点击 uniListChat 触发事件
  76. */
  77. export default {
  78. name: 'UniListChat',
  79. props: {
  80. title: {
  81. type: String,
  82. default: ''
  83. },
  84. note: {
  85. type: String,
  86. default: ''
  87. },
  88. clickable: {
  89. type: Boolean,
  90. default: false
  91. },
  92. link: {
  93. type: [Boolean, String],
  94. default: false
  95. },
  96. to: {
  97. type: String,
  98. default: ''
  99. },
  100. badgeText: {
  101. type: [String, Number],
  102. default: ''
  103. },
  104. badgePositon: {
  105. type: String,
  106. default: 'right'
  107. },
  108. time: {
  109. type: String,
  110. default: ''
  111. },
  112. avatarCircle: {
  113. type: Boolean,
  114. default: false
  115. },
  116. avatar: {
  117. type: String,
  118. default: ''
  119. },
  120. avatarList: {
  121. type: Array,
  122. default() {
  123. return [];
  124. }
  125. }
  126. },
  127. inject: ['list'],
  128. computed: {
  129. isSingle() {
  130. if (this.badgeText === 'dot') {
  131. return 'uni-badge--dot';
  132. } else {
  133. const badgeText = this.badgeText.toString();
  134. if (badgeText.length > 1) {
  135. return 'uni-badge--complex';
  136. } else {
  137. return 'uni-badge--single';
  138. }
  139. }
  140. },
  141. computedAvatar() {
  142. if (this.avatarList.length > 4) {
  143. this.imageWidth = avatarWidth * 0.31;
  144. return 'avatarItem--3';
  145. } else if (this.avatarList.length > 1) {
  146. this.imageWidth = avatarWidth * 0.47;
  147. return 'avatarItem--2';
  148. } else {
  149. this.imageWidth = avatarWidth;
  150. return 'avatarItem--1';
  151. }
  152. }
  153. },
  154. data() {
  155. return {
  156. isFirstChild: false,
  157. border: true,
  158. // avatarList: 3,
  159. imageWidth: 50
  160. };
  161. },
  162. mounted() {
  163. if (!this.list.firstChildAppend) {
  164. this.list.firstChildAppend = true;
  165. this.isFirstChild = true;
  166. }
  167. this.border = this.list.border;
  168. },
  169. methods: {
  170. onClick() {
  171. if (this.to !== '') {
  172. this.openPage();
  173. return;
  174. }
  175. if (this.clickable || this.link) {
  176. this.$emit('click', {
  177. data: {}
  178. });
  179. }
  180. },
  181. openPage() {
  182. if (['navigateTo', 'redirectTo', 'reLaunch', 'switchTab'].indexOf(this.link) !== -1) {
  183. this.pageApi(this.link);
  184. } else {
  185. this.pageApi('navigateTo');
  186. }
  187. },
  188. pageApi(api) {
  189. uni[api]({
  190. url: this.to,
  191. success: res => {
  192. this.$emit('click', {
  193. data: res
  194. });
  195. },
  196. fail: err => {
  197. this.$emit('click', {
  198. data: err
  199. });
  200. console.error(err.errMsg);
  201. }
  202. });
  203. }
  204. }
  205. };
  206. </script>
  207. <style lang="scss" scoped>
  208. $background-color: #fff;
  209. $divide-line-color: #e5e5e5;
  210. $avatar-width: 45px;
  211. $avatar-border-radius: 5px;
  212. $avatar-border-color: #eee;
  213. $avatar-border-width: 1px;
  214. $title-size: 16px;
  215. $title-color: #3b4144;
  216. $title-weight: normal;
  217. $note-size: 12px;
  218. $note-color: #999;
  219. $note-weight: normal;
  220. $right-text-size: 12px;
  221. $right-text-color: #999;
  222. $right-text-weight: normal;
  223. $badge-left: 0px;
  224. $badge-top: 0px;
  225. $dot-width: 10px;
  226. $dot-height: 10px;
  227. $badge-size: 18px;
  228. $badge-font: 12px;
  229. $badge-color: #fff;
  230. $badge-background-color: #ff5a5f;
  231. $badge-space: 6px;
  232. $hover: #f5f5f5;
  233. .uni-list-chat {
  234. font-size: $uni-font-size-lg;
  235. position: relative;
  236. flex-direction: column;
  237. justify-content: space-between;
  238. background-color: $background-color;
  239. }
  240. // .uni-list-chat--disabled {
  241. // opacity: 0.3;
  242. // }
  243. .uni-list-chat--hover {
  244. background-color: $hover;
  245. }
  246. .uni-list--border {
  247. position: relative;
  248. margin-left: $uni-spacing-row-lg;
  249. /* #ifdef APP-PLUS */
  250. border-top-color: $divide-line-color;
  251. border-top-style: solid;
  252. border-top-width: 0.5px;
  253. /* #endif */
  254. }
  255. /* #ifndef APP-NVUE */
  256. .uni-list--border:after {
  257. position: absolute;
  258. top: 0;
  259. right: 0;
  260. left: 0;
  261. height: 1px;
  262. content: '';
  263. -webkit-transform: scaleY(0.5);
  264. transform: scaleY(0.5);
  265. background-color: $divide-line-color;
  266. }
  267. .uni-list-item--first:after {
  268. height: 0px;
  269. }
  270. /* #endif */
  271. .uni-list-chat--first {
  272. border-top-width: 0px;
  273. }
  274. .uni-ellipsis {
  275. /* #ifndef APP-NVUE */
  276. overflow: hidden;
  277. white-space: nowrap;
  278. text-overflow: ellipsis;
  279. /* #endif */
  280. /* #ifdef APP-NVUE */
  281. lines: 1;
  282. /* #endif */
  283. }
  284. .uni-ellipsis-2 {
  285. /* #ifndef APP-NVUE */
  286. overflow: hidden;
  287. text-overflow: ellipsis;
  288. display: -webkit-box;
  289. -webkit-line-clamp: 2;
  290. -webkit-box-orient: vertical;
  291. /* #endif */
  292. /* #ifdef APP-NVUE */
  293. lines: 2;
  294. /* #endif */
  295. }
  296. .uni-list-chat__container {
  297. position: relative;
  298. /* #ifndef APP-NVUE */
  299. display: flex;
  300. /* #endif */
  301. flex-direction: row;
  302. flex: 1;
  303. padding: $uni-spacing-row-base $uni-spacing-row-lg;
  304. position: relative;
  305. overflow: hidden;
  306. }
  307. .uni-list-chat__header-warp {
  308. position: relative;
  309. }
  310. .uni-list-chat__header {
  311. /* #ifndef APP-NVUE */
  312. display: flex;
  313. align-content: center;
  314. /* #endif */
  315. flex-direction: row;
  316. justify-content: center;
  317. align-items: center;
  318. flex-wrap: wrap-reverse;
  319. /* #ifdef APP-NVUE */
  320. width: 50px;
  321. height: 50px;
  322. /* #endif */
  323. /* #ifndef APP-NVUE */
  324. width: $avatar-width;
  325. height: $avatar-width;
  326. /* #endif */
  327. border-radius: $avatar-border-radius;
  328. border-color: $avatar-border-color;
  329. border-width: $avatar-border-width;
  330. border-style: solid;
  331. overflow: hidden;
  332. }
  333. .uni-list-chat__header-box {
  334. /* #ifndef APP-PLUS */
  335. box-sizing: border-box;
  336. display: flex;
  337. width: $avatar-width;
  338. height: $avatar-width;
  339. /* #endif */
  340. /* #ifdef APP-NVUE */
  341. width: 50px;
  342. height: 50px;
  343. /* #endif */
  344. overflow: hidden;
  345. border-radius: 2px;
  346. }
  347. .uni-list-chat__header-image {
  348. margin: 1px;
  349. /* #ifdef APP-NVUE */
  350. width: 50px;
  351. height: 50px;
  352. /* #endif */
  353. /* #ifndef APP-NVUE */
  354. width: $avatar-width;
  355. height: $avatar-width;
  356. /* #endif */
  357. }
  358. /* #ifndef APP-NVUE */
  359. .uni-list-chat__header-image {
  360. display: block;
  361. width: 100%;
  362. height: 100%;
  363. }
  364. .avatarItem--1 {
  365. width: 100%;
  366. height: 100%;
  367. }
  368. .avatarItem--2 {
  369. width: 47%;
  370. height: 47%;
  371. }
  372. .avatarItem--3 {
  373. width: 32%;
  374. height: 32%;
  375. }
  376. /* #endif */
  377. .header--circle {
  378. border-radius: 50%;
  379. }
  380. .uni-list-chat__content {
  381. /* #ifndef APP-NVUE */
  382. display: flex;
  383. /* #endif */
  384. flex-direction: row;
  385. flex: 1;
  386. overflow: hidden;
  387. padding: 2px 0;
  388. }
  389. .uni-list-chat__content-main {
  390. /* #ifndef APP-NVUE */
  391. display: flex;
  392. /* #endif */
  393. flex-direction: column;
  394. justify-content: space-between;
  395. padding-left: $uni-spacing-row-base;
  396. flex: 1;
  397. overflow: hidden;
  398. }
  399. .uni-list-chat__content-title {
  400. font-size: $title-size;
  401. color: $title-color;
  402. font-weight: $title-weight;
  403. overflow: hidden;
  404. }
  405. .uni-list-chat__content-note {
  406. margin-top: 3px;
  407. color: $note-color;
  408. font-size: $note-size;
  409. font-weight: $title-weight;
  410. overflow: hidden;
  411. }
  412. .uni-list-chat__content-extra {
  413. /* #ifndef APP-NVUE */
  414. flex-shrink: 0;
  415. display: flex;
  416. /* #endif */
  417. flex-direction: column;
  418. justify-content: space-between;
  419. align-items: flex-end;
  420. margin-left: 5px;
  421. }
  422. .uni-list-chat__content-extra-text {
  423. color: $right-text-color;
  424. font-size: $right-text-size;
  425. font-weight: $right-text-weight;
  426. overflow: hidden;
  427. }
  428. .uni-list-chat__badge-pos {
  429. position: absolute;
  430. /* #ifdef APP-NVUE */
  431. left: 55px;
  432. top: 3px;
  433. /* #endif */
  434. /* #ifndef APP-NVUE */
  435. left: calc(#{$avatar-width} + 10px - #{$badge-space} + #{$badge-left});
  436. top: calc(#{$uni-spacing-row-base}/ 2 + 1px + #{$badge-top});
  437. /* #endif */
  438. }
  439. .uni-list-chat__badge {
  440. /* #ifndef APP-NVUE */
  441. display: flex;
  442. /* #endif */
  443. justify-content: center;
  444. align-items: center;
  445. border-radius: 100px;
  446. background-color: $badge-background-color;
  447. }
  448. .uni-list-chat__badge-text {
  449. color: $badge-color;
  450. font-size: $badge-font;
  451. }
  452. .uni-badge--single {
  453. /* #ifndef APP-NVUE */
  454. left: calc(#{$avatar-width} + 7px + #{$badge-left});
  455. /* #endif */
  456. width: $badge-size;
  457. height: $badge-size;
  458. }
  459. .uni-badge--complex {
  460. /* #ifdef APP-NVUE */
  461. left: 50px;
  462. /* #endif */
  463. /* #ifndef APP-NVUE */
  464. width: auto;
  465. /* #endif */
  466. height: $badge-size;
  467. padding: 0 $badge-space;
  468. }
  469. .uni-badge--dot {
  470. /* #ifdef APP-NVUE */
  471. left: 60px;
  472. top: 6px;
  473. /* #endif */
  474. /* #ifndef APP-NVUE */
  475. left: calc(#{$avatar-width} + 15px - #{$dot-width}/ 2 + 1px + #{$badge-left});
  476. /* #endif */
  477. width: $dot-width;
  478. height: $dot-height;
  479. padding: 0;
  480. }
  481. .uni-list-chat--right {
  482. /* #ifdef APP-NVUE */
  483. left: 0;
  484. /* #endif */
  485. }
  486. </style>