index.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <template>
  2. <mobile-frame :frameStyle="frameStyle" @toPath="toPath">
  3. <scroll-view scroll-y="true" class="scroll-view" @scrolltolower="toPage">
  4. <view class="list-scroll-view">
  5. <view class="main">
  6. <!-- <view class="one">
  7. <input type="text" v-model="searchInfo.name" @input="toInput" placeholder="搜索">
  8. </view> -->
  9. <view class="two">
  10. <view class="list" v-for="(item,index) in list" :key="index" @tap="toInfo(item)">
  11. <image class="image"
  12. :src="item.shop&&item.shop.logo&&item.shop.logo.length>0?item.shop.logo[0].url:''"
  13. mode="">
  14. </image>
  15. <view class="content">
  16. <view class="name textOver">{{item.shop&&item.shop.name||'暂无'}}</view>
  17. <view class="news textOver">{{item.last_chat}}</view>
  18. </view>
  19. <view class="time">
  20. <view class="time_1">{{item.last_time}}</view>
  21. <uni-badge :text="item.not_read" size="small"></uni-badge>
  22. </view>
  23. </view>
  24. </view>
  25. </view>
  26. </view>
  27. </scroll-view>
  28. </mobile-frame>
  29. </template>
  30. <script>
  31. export default {
  32. data() {
  33. return {
  34. frameStyle: {
  35. useBar: true
  36. },
  37. user: {},
  38. searchInfo: {},
  39. list: [],
  40. total: 0,
  41. skip: 0,
  42. limit: 10,
  43. page: 0,
  44. };
  45. },
  46. onLoad: async function() {
  47. const that = this;
  48. await that.watchlogin();
  49. },
  50. onShow: async function() {
  51. const that = this;
  52. that.clearPage();
  53. if (that.user && that.user._id) that.search();
  54. },
  55. onPullDownRefresh: async function() {
  56. const that = this;
  57. that.clearPage();
  58. await that.watchlogin();
  59. uni.stopPullDownRefresh();
  60. },
  61. computed: {
  62. listenWebsocket() {
  63. return this.$store.state.websocketData;
  64. }
  65. },
  66. watch: {
  67. listenWebsocket: function(newstr) {
  68. if (newstr.type == 'chat') {
  69. this.clearPage()
  70. this.search();
  71. }
  72. }
  73. },
  74. methods: {
  75. // 监听用户是否登录
  76. watchlogin() {
  77. const that = this;
  78. uni.getStorage({
  79. key: 'token',
  80. success: function(res) {
  81. let user = that.$jwt(res.data);
  82. if (user) {
  83. that.$set(that, `user`, user)
  84. that.search();
  85. if (that.$store.state.socketTask == null) {
  86. //当websocket收到后端发送的消息时,触发
  87. let config = that.$config.wsUrl;
  88. // 开启websocket
  89. that.$store.dispatch('websocketInit', config +
  90. `/${user._id}`);
  91. }
  92. }
  93. }
  94. })
  95. },
  96. async search() {
  97. const that = this;
  98. let config = that.$config;
  99. let info = {
  100. skip: that.skip,
  101. limit: that.limit,
  102. customer: that.user && that.user._id,
  103. }
  104. let res;
  105. res = await that.$api(`/room`, `GET`, {
  106. ...info,
  107. ...that.searchInfo
  108. }, 'chat');
  109. if (res.errcode == '0') {
  110. let list = [...that.list, ...res.data];
  111. for (let val of list) {
  112. if (val.last_chat) {
  113. val.last_time = val.last_chat.time
  114. let chat = val.last_chat.content.indexOf(config.stompUrl) != -1;
  115. if (chat) val.last_chat = `[图片]`
  116. else val.last_chat = val.last_chat.content
  117. }
  118. }
  119. that.$set(that, `list`, list);
  120. that.$set(that, `total`, res.total)
  121. } else {
  122. uni.showToast({
  123. title: res.errmsg,
  124. icon: 'none'
  125. })
  126. }
  127. },
  128. // 输入框
  129. toInput(e) {
  130. const that = this;
  131. if (that.searchInfo.goods) that.$set(that.searchInfo, `goods`, e.detail.value)
  132. else that.$set(that, `searchInfo`, {})
  133. that.clearPage();
  134. that.search();
  135. },
  136. // 详情
  137. toInfo(e) {
  138. uni.navigateTo({
  139. url: `/pagesMessage/message/info?id=${e._id}`
  140. })
  141. },
  142. // 分页
  143. toPage(e) {
  144. const that = this;
  145. let list = that.marketList;
  146. let limit = that.limit;
  147. if (that.total > list.length) {
  148. uni.showLoading({
  149. title: '加载中',
  150. mask: true
  151. })
  152. let page = that.page + 1;
  153. that.$set(that, `page`, page)
  154. let skip = page * limit;
  155. that.$set(that, `skip`, skip)
  156. that.search();
  157. uni.hideLoading();
  158. }
  159. },
  160. clearPage() {
  161. const that = this;
  162. that.$set(that, `list`, [])
  163. that.$set(that, `skip`, 0)
  164. that.$set(that, `limit`, 10)
  165. that.$set(that, `page`, 0)
  166. },
  167. // 菜单跳转
  168. toPath(e) {
  169. let url = `/${e.route}`;
  170. if (e.type == '0') uni.redirectTo({
  171. url
  172. })
  173. else {
  174. uni.navigateTo({
  175. url
  176. })
  177. }
  178. },
  179. }
  180. }
  181. </script>
  182. <style lang="scss">
  183. .main {
  184. display: flex;
  185. flex-direction: column;
  186. width: 96vw;
  187. padding: 2vw;
  188. background-color: var(--f1Color);
  189. .one {
  190. input {
  191. padding: 2vw;
  192. background-color: #ffffff;
  193. font-size: var(--font14Size);
  194. border-radius: 10px;
  195. }
  196. }
  197. .two {
  198. margin: 1vw 0 0 0;
  199. .list {
  200. display: flex;
  201. padding: 2vw;
  202. margin: 1vw 0 0 0;
  203. background-color: #ffffff;
  204. border-radius: 10px;
  205. .image {
  206. width: 15vw;
  207. height: 15vw;
  208. border-radius: 15vw;
  209. border: 1px solid #C0C0C0;
  210. }
  211. .content {
  212. width: 40vw;
  213. padding: 2vw;
  214. .name {
  215. font-size: 16px;
  216. padding: 1vw 0;
  217. }
  218. .news {
  219. font-size: 12px;
  220. color: #C0C0C0;
  221. }
  222. }
  223. .time {
  224. width: 32vw;
  225. text-align: right;
  226. padding: 2vw 0;
  227. font-size: 12px;
  228. color: #C0C0C0;
  229. .time_1 {
  230. margin: 0 0 1vw 0;
  231. }
  232. }
  233. }
  234. }
  235. }
  236. .scroll-view {
  237. position: absolute;
  238. top: 0;
  239. left: 0;
  240. right: 0;
  241. bottom: 0;
  242. .list-scroll-view {
  243. display: flex;
  244. flex-direction: column;
  245. }
  246. }
  247. </style>