uni-list-chat.vue 12 KB

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