uni-list-chat.vue 14 KB

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