uni-list-chat.vue 13 KB

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