index.vue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. <template>
  2. <view class="container main">
  3. <view class="one" v-if="total>0">
  4. <scroll-view :scroll-top="scrollTop" scroll-y="true" class="scroll-view" @scrolltolower="toPage"
  5. @scroll="toScroll">
  6. <view class="list-scroll-view">
  7. <view class="list" v-for="(item, index) in list" :key="index">
  8. <view class="value">
  9. <view class="title">内容:</view>
  10. <view class="label"> <text :user-select="true">{{item.content||'暂无'}}</text> </view>
  11. </view>
  12. <view class="value">
  13. <view class="title">通知类型:</view>
  14. <view class="label">{{getDict(item.type,'type')||'暂无'}}</view>
  15. </view>
  16. <view class="value">
  17. <view class="title">是否已读:</view>
  18. <view class="label">{{getDict(item,'is_read')||'暂无'}}</view>
  19. </view>
  20. <view class="value">
  21. <view class="title">发送时间:</view>
  22. <view class="label">{{item.created_time||'暂无'}}</view>
  23. </view>
  24. <view class="bottom">
  25. <button v-if="getDict(item, 'is_read') == '未读'" class="button button_2" type="default"
  26. size="mini" @tap.stop="toRead(item)">已读</button>
  27. </view>
  28. </view>
  29. <view class="is_bottom" v-if="is_bottom">
  30. <text>{{config.bottom_title||'到底了!'}}</text>
  31. </view>
  32. </view>
  33. </scroll-view>
  34. </view>
  35. <o-empty v-else />
  36. </view>
  37. </template>
  38. <script>
  39. export default {
  40. data() {
  41. return {
  42. user: {},
  43. config: {},
  44. list: [],
  45. total: 0,
  46. skip: 0,
  47. limit: 5,
  48. page: 0,
  49. // 数据是否触底
  50. is_bottom: false,
  51. scrollTop: 0,
  52. // 禁止滚动穿透
  53. show: false,
  54. readList: [],
  55. typeList: []
  56. }
  57. },
  58. onLoad: async function(e) {
  59. const that = this;
  60. await that.searchToken();
  61. await that.searchOther();
  62. await that.searchConfig();
  63. },
  64. onShow: async function() {
  65. const that = this;
  66. that.clearPage();
  67. await that.search();
  68. },
  69. onPullDownRefresh: async function() {
  70. const that = this;
  71. that.clearPage();
  72. await that.search();
  73. uni.stopPullDownRefresh();
  74. },
  75. methods: {
  76. // 禁止滚动穿透
  77. change(e) {
  78. const that = this;
  79. that.show = e.show
  80. },
  81. // 用户信息
  82. searchToken() {
  83. const that = this;
  84. try {
  85. const res = uni.getStorageSync('token');
  86. if (res) {
  87. const user = that.$jwt(res);
  88. that.$set(that, `user`, user);
  89. }
  90. } catch (e) {}
  91. },
  92. searchConfig() {
  93. const that = this;
  94. try {
  95. const res = uni.getStorageSync('config');
  96. if (res) that.$set(that, `config`, res);
  97. } catch (e) {}
  98. },
  99. // 查询其他信息
  100. async searchOther() {
  101. const that = this;
  102. let res;
  103. // 查询类型
  104. res = await that.$api(`/dictData`, 'GET', {
  105. code: 'messageIsRead',
  106. is_use: '0',
  107. })
  108. if (res.errcode == '0') that.$set(that, `readList`, res.data);
  109. // 通知类型
  110. res = await that.$api(`/dictData`, 'GET', {
  111. code: 'messageType',
  112. is_use: '0',
  113. })
  114. if (res.errcode == '0') that.$set(that, `typeList`, res.data)
  115. },
  116. // 查询
  117. async search() {
  118. const that = this;
  119. let info = {
  120. skip: that.skip,
  121. limit: that.limit,
  122. 'to.user': that.user.id
  123. }
  124. const res = await that.$api(`/message`, 'GET', info)
  125. if (res.errcode == '0') {
  126. let list = [...that.list, ...res.data];
  127. that.$set(that, `list`, list)
  128. that.$set(that, `total`, res.total)
  129. } else {
  130. uni.showToast({
  131. title: res.errmsg,
  132. icon: 'none'
  133. });
  134. }
  135. },
  136. // 处理字典表
  137. getDict(item, model) {
  138. if(item){
  139. const that = this;
  140. let res
  141. if (model == 'is_read') {
  142. //理论上,to中应该只有这个数据,但是还要处理
  143. const userid = that.user.id
  144. if (!userid) return
  145. const to = item.to || []
  146. const is_readData = to.find((f) => f.user === userid)
  147. if (!is_readData) return
  148. const is_read = is_readData.is_read || '0'
  149. res = that.readList.find((f) => f.value === is_read)
  150. } else if (model == 'type') res = that.typeList.find(i => i.value == item)
  151. if (res) return res.label
  152. else return '暂无'
  153. }
  154. },
  155. // 已读未读
  156. toRead(item) {
  157. const that = this;
  158. uni.showModal({
  159. title: '提示',
  160. content: '您确认已读该数据?',
  161. success: async function(res) {
  162. if (res.confirm) {
  163. const data = JSON.parse(JSON.stringify(item));
  164. const userid = that.user.id
  165. const to = data.to || []
  166. for (const val of to) {
  167. if (val.user == userid) val.is_read = '1'
  168. }
  169. const res = await that.$api(`/message/${item.id}`, 'POST', {
  170. id: item.id,
  171. to
  172. })
  173. if (res.errcode == '0') {
  174. uni.showToast({
  175. title: "已读成功!",
  176. icon: 'none'
  177. });
  178. that.clearPage();
  179. that.search();
  180. } else {
  181. uni.showToast({
  182. title: res.errmsg,
  183. icon: 'none'
  184. });
  185. }
  186. } else if (res.cancel) {
  187. console.log('用户点击取消');
  188. }
  189. }
  190. });
  191. },
  192. // 分页
  193. toPage(e) {
  194. const that = this;
  195. let list = that.list;
  196. let limit = that.limit;
  197. if (that.total > list.length) {
  198. uni.showLoading({
  199. title: '加载中',
  200. mask: true
  201. })
  202. let page = that.page + 1;
  203. that.$set(that, `page`, page)
  204. let skip = page * limit;
  205. that.$set(that, `skip`, skip)
  206. that.search();
  207. uni.hideLoading();
  208. } else that.$set(that, `is_bottom`, true)
  209. },
  210. toScroll(e) {
  211. const that = this;
  212. let up = that.scrollTop;
  213. let num = Math.sign(up - e.detail.scrollTop);
  214. if (num == 1) that.$set(that, `is_bottom`, false);
  215. // 检查滚动位置是否达到div显示的条件
  216. if (e.detail.scrollTop >= 300 && !that.showDiv) {
  217. that.showDiv = true;
  218. } else if (e.detail.scrollTop < 300 && that.showDiv) {
  219. that.showDiv = false;
  220. }
  221. },
  222. // 清空列表
  223. clearPage() {
  224. const that = this;
  225. that.$set(that, `list`, [])
  226. that.$set(that, `skip`, 0)
  227. that.$set(that, `limit`, 5)
  228. that.$set(that, `page`, 0)
  229. }
  230. }
  231. }
  232. </script>
  233. <style lang="scss" scoped>
  234. .main {
  235. .one {
  236. position: relative;
  237. flex-grow: 1;
  238. background-color: var(--f1Color);
  239. .list {
  240. background-color: var(--mainColor);
  241. border-bottom: 1px solid var(--f5Color);
  242. margin: 2vw 2vw 0 2vw;
  243. border-radius: 4px;
  244. padding: 3vw;
  245. .value {
  246. display: flex;
  247. align-items: center;
  248. .title {
  249. min-width: 70px;
  250. font-size: var(--font14Size);
  251. font-weight: bold;
  252. }
  253. .label {
  254. font-size: var(--font12Size);
  255. color: var(--f85Color);
  256. }
  257. }
  258. .bottom {
  259. margin: 2vw 0 0 0;
  260. text-align: center;
  261. .button {
  262. color: var(--mainColor);
  263. font-size: var(--font14Size);
  264. border-radius: 2vw;
  265. }
  266. .button_1 {
  267. margin: 0 2vw 0 0;
  268. background-color: var(--f3CColor);
  269. }
  270. .button_2 {
  271. background-color: var(--fF0Color);
  272. }
  273. }
  274. }
  275. }
  276. }
  277. .scroll-view {
  278. position: absolute;
  279. top: 0;
  280. left: 0;
  281. right: 0;
  282. bottom: 0;
  283. .list-scroll-view {
  284. display: flex;
  285. flex-direction: column;
  286. }
  287. }
  288. .is_bottom {
  289. width: 100%;
  290. text-align: center;
  291. text {
  292. padding: 2vw 0;
  293. display: inline-block;
  294. color: var(--f85Color);
  295. font-size: var(--font14Size);
  296. }
  297. }
  298. </style>