chat.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <template>
  2. <view>
  3. <uni-card :is-shadow="false" is-full>
  4. <text class="uni-h6">此示例展示了聊天列表的使用场景。</text>
  5. </uni-card>
  6. <uni-section title="圆头像且不显示分割线" type="line">
  7. <uni-list :border="false">
  8. <uni-list-chat v-for="item in listData" :avatar-circle="true" :key="item.id" :title="item.author_name" :avatar="item.cover"
  9. :note="item.title" :time="item.published_at" :clickable="false"></uni-list-chat>
  10. </uni-list>
  11. </uni-section>
  12. <uni-section title="带圆点" type="line">
  13. <uni-list>
  14. <uni-list-chat v-for="item in listData" :key="item.id" :title="item.author_name" :avatar="item.cover" :note="item.title"
  15. :time="item.published_at" :badge-text="item.text" :clickable="false" badge-positon="left" badge-text="dot"></uni-list-chat>
  16. </uni-list>
  17. </uni-section>
  18. <uni-section title="自定义右侧内容" type="line">
  19. <uni-list>
  20. <uni-list-chat v-for="item in listData" :key="item.id" :title="item.author_name" :avatar="item.cover" :note="item.title"
  21. badge-positon="left" :badge-text="item.text">
  22. <view class="chat-custom-right">
  23. <text class="chat-custom-text">刚刚</text>
  24. <uni-icons type="star-filled" color="#999" size="18"></uni-icons>
  25. </view>
  26. </uni-list-chat>
  27. </uni-list>
  28. </uni-section>
  29. <uni-section title="带通知角标的单头像聊天列表" type="line">
  30. <uni-list>
  31. <uni-list-chat v-for="item in listData" :key="item.id" :title="item.author_name" :avatar="item.cover" :note="item.title"
  32. :time="item.published_at" :clickable="true" :badge-text="item.text" @click="onClick"></uni-list-chat>
  33. </uni-list>
  34. </uni-section>
  35. <uni-section title="带通知角标的多头像聊天列表" type="line">
  36. <uni-list>
  37. <uni-list-chat v-for="(item,index) in listData" :key="item.id" :title="item.author_name" :avatar="item.cover" :note="item.title"
  38. :time="item.published_at" :clickable="true" :avatarList="avatar(index+1)" :badge-text="item.text" @click="onClick"></uni-list-chat>
  39. </uni-list>
  40. </uni-section>
  41. </view>
  42. </template>
  43. <script>
  44. export default {
  45. components: {},
  46. data() {
  47. return {
  48. UNITS: {
  49. '年': 31557600000,
  50. '月': 2629800000,
  51. '天': 86400000,
  52. '小时': 3600000,
  53. '分钟': 60000,
  54. '秒': 1000
  55. },
  56. listData: [],
  57. avatarList: [{
  58. url: '/static/logo.png'
  59. }, {
  60. url: '/static/logo.png'
  61. }, {
  62. url: '/static/logo.png'
  63. }]
  64. }
  65. },
  66. onLoad() {
  67. this.getList()
  68. },
  69. methods: {
  70. onClick() {
  71. uni.showToast({
  72. title: '列表被点击'
  73. })
  74. },
  75. avatar(count) {
  76. let arr = []
  77. this.avatarList.forEach((item, index) => {
  78. if (index < count) {
  79. arr.push(item)
  80. }
  81. })
  82. return arr
  83. },
  84. getList() {
  85. var data = {
  86. column: 'id,post_id,title,author_name,cover,published_at' //需要的字段名
  87. };
  88. uni.request({
  89. url: 'https://unidemo.dcloud.net.cn/api/news',
  90. data: data,
  91. success: data => {
  92. if (data.statusCode == 200) {
  93. let list = this.setTime(data.data);
  94. list = this.reload ? list : this.listData.concat(list);
  95. list.map(item => {
  96. item.text = Math.floor(Math.random() * (1 - 20) + 20)
  97. return item
  98. })
  99. this.listData = this.getRandomArrayElements(list, 3)
  100. }
  101. },
  102. fail: (data, code) => {
  103. console.log('fail' + JSON.stringify(data));
  104. }
  105. });
  106. },
  107. getRandomArrayElements(arr, count) {
  108. var shuffled = arr.slice(0),
  109. i = arr.length,
  110. min = i - count,
  111. temp, index;
  112. while (i-- > min) {
  113. index = Math.floor((i + 1) * Math.random());
  114. temp = shuffled[index];
  115. shuffled[index] = shuffled[i];
  116. shuffled[i] = temp;
  117. }
  118. return shuffled.slice(min);
  119. },
  120. setTime(items) {
  121. var newItems = [];
  122. items.forEach(e => {
  123. newItems.push({
  124. author_name: e.author_name,
  125. cover: e.cover,
  126. id: e.id,
  127. post_id: e.post_id,
  128. published_at: this.format(e.published_at),
  129. title: e.title
  130. });
  131. });
  132. return newItems;
  133. },
  134. format(dateStr) {
  135. var date = this.parse(dateStr)
  136. var diff = Date.now() - date.getTime();
  137. if (diff < this.UNITS['天']) {
  138. return this.humanize(diff);
  139. }
  140. var _format = function(number) {
  141. return (number < 10 ? ('0' + number) : number);
  142. };
  143. return date.getFullYear() + '-' + _format(date.getMonth() + 1) + '-' + _format(date.getDate()) + ' ' +
  144. _format(date.getHours()) + ':' + _format(date.getMinutes());
  145. },
  146. parse(str) { //将"yyyy-mm-dd HH:MM:ss"格式的字符串,转化为一个Date对象
  147. var a = str.split(/[^0-9]/);
  148. return new Date(a[0], a[1] - 1, a[2], a[3], a[4], a[5]);
  149. },
  150. }
  151. }
  152. </script>
  153. <style lang="scss" scoped>
  154. .chat-custom-right {
  155. flex: 1;
  156. /* #ifndef APP-NVUE */
  157. display: flex;
  158. /* #endif */
  159. flex-direction: column;
  160. justify-content: space-between;
  161. align-items: flex-end;
  162. }
  163. .chat-custom-text {
  164. font-size: 12px;
  165. color: #999;
  166. }
  167. </style>