index.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. "use strict";
  2. const common_vendor = require("../../common/vendor.js");
  3. const submit_1 = () => "./common/submit_1.js";
  4. const _sfc_main = {
  5. components: {
  6. submit_1
  7. },
  8. data() {
  9. return {
  10. config: {},
  11. user: {},
  12. // 聊天记录
  13. msgList: [],
  14. total: 0,
  15. skip: 0,
  16. limit: 6,
  17. page: 0,
  18. // 判断是否跳到最后一条
  19. is_bottom: true,
  20. // 判断是否下拉刷新复位
  21. triggered: false,
  22. scrollToView: "",
  23. //滑动最后一条信息
  24. // 判断是否是选择图片刷新
  25. is_img: false
  26. };
  27. },
  28. onShow: async function() {
  29. const that = this;
  30. that.searchToken();
  31. that.searchConfig();
  32. if (!that.is_img) {
  33. await that.clearPage();
  34. await that.search();
  35. }
  36. },
  37. onPullDownRefresh: async function() {
  38. const that = this;
  39. that.clearPage();
  40. await that.search();
  41. common_vendor.index.stopPullDownRefresh();
  42. },
  43. methods: {
  44. searchToken() {
  45. const that = this;
  46. try {
  47. const res = common_vendor.index.getStorageSync("token");
  48. if (res)
  49. that.$set(that, `user`, res);
  50. } catch (e) {
  51. common_vendor.index.showToast({
  52. title: err.errmsg,
  53. icon: "error",
  54. duration: 2e3
  55. });
  56. }
  57. },
  58. searchConfig() {
  59. const that = this;
  60. try {
  61. const res = common_vendor.index.getStorageSync("config");
  62. if (res)
  63. that.$set(that, `config`, res);
  64. } catch (e) {
  65. common_vendor.index.showToast({
  66. title: err.errmsg,
  67. icon: "error",
  68. duration: 2e3
  69. });
  70. }
  71. },
  72. async search() {
  73. const that = this;
  74. if (!that.user._id)
  75. return;
  76. let info = {
  77. skip: that.skip,
  78. limit: that.limit,
  79. user: that.user._id
  80. };
  81. const res = await that.$api(`/chat`, "GET", {
  82. ...info
  83. });
  84. if (res.errcode == "0") {
  85. let list = [...res.data.reverse(), ...that.msgList];
  86. that.$set(that, `msgList`, list);
  87. that.$set(that, `total`, res.total);
  88. } else {
  89. common_vendor.index.showToast({
  90. title: res.errmsg
  91. });
  92. }
  93. that.goBottom();
  94. },
  95. // 进行图片的预览
  96. previewImg(e) {
  97. common_vendor.index.previewImage({
  98. current: 0,
  99. urls: [e],
  100. longPressActions: {
  101. itemList: ["发送给朋友", "保存图片", "收藏"],
  102. success: function(data) {
  103. console.log("选中了第" + (data.tapIndex + 1) + "个按钮,第" + (data.index + 1) + "张图片");
  104. },
  105. fail: function(err2) {
  106. console.log(err2.errMsg);
  107. }
  108. }
  109. });
  110. },
  111. // 判断是否是选择图片刷新
  112. choseImg(e) {
  113. const that = this;
  114. that.$set(that, `is_img`, e);
  115. },
  116. //接受输入内容
  117. async inputs(e) {
  118. const that = this;
  119. let user = that.user;
  120. if (user._id) {
  121. let data = {
  122. "user": user._id,
  123. "speaker": user._id,
  124. "content": e.message,
  125. "time": common_vendor.hooks().format("YYYY-MM-DD HH:mm:ss"),
  126. "msg_type": e.type
  127. };
  128. let res = await that.$api(`/chat`, `POST`, data);
  129. if (res.errcode == "0") {
  130. that.msgList.push(res.data);
  131. } else {
  132. common_vendor.index.showToast({
  133. title: res.errmsg,
  134. icon: "none"
  135. });
  136. }
  137. that.goBottom();
  138. } else {
  139. common_vendor.index.navigateTo({
  140. url: `/pagesIndex/login/index`
  141. });
  142. }
  143. },
  144. //输入框高度
  145. heights(e) {
  146. const that = this;
  147. that.goBottom();
  148. },
  149. // 滚动到底部
  150. async goBottom() {
  151. const that = this;
  152. that.scrollToView = "";
  153. that.$nextTick(function() {
  154. that.scrollToView = "msg123456789";
  155. });
  156. },
  157. // 下拉刷新分页
  158. getFresh(e) {
  159. const that = this;
  160. that.$set(that, `triggered`, true);
  161. that.$set(that, `is_img`, false);
  162. let msgList = that.msgList;
  163. let limit = that.limit;
  164. setTimeout(() => {
  165. if (that.total > msgList.length) {
  166. common_vendor.index.showLoading({
  167. title: "加载中",
  168. mask: true
  169. });
  170. let page = that.page + 1;
  171. that.$set(that, `page`, page);
  172. let skip = page * limit;
  173. that.$set(that, `skip`, skip);
  174. that.$set(that, `is_bottom`, false);
  175. that.search();
  176. common_vendor.index.hideLoading();
  177. } else {
  178. common_vendor.index.showToast({
  179. title: `没有更多聊天记录了`,
  180. icon: "none"
  181. });
  182. }
  183. that.triggered = false;
  184. }, 1e3);
  185. },
  186. // 分页
  187. toPage(e) {
  188. const that = this;
  189. let list = that.list;
  190. let limit = that.limit;
  191. if (that.total > list.length) {
  192. common_vendor.index.showLoading({
  193. title: "加载中",
  194. mask: true
  195. });
  196. let page = that.page + 1;
  197. that.$set(that, `page`, page);
  198. let skip = page * limit;
  199. that.$set(that, `skip`, skip);
  200. that.search();
  201. common_vendor.index.hideLoading();
  202. } else
  203. that.$set(that, `is_bottom`, true);
  204. },
  205. // 清空列表
  206. clearPage() {
  207. const that = this;
  208. that.$set(that, `msgList`, []);
  209. that.$set(that, `skip`, 0);
  210. that.$set(that, `limit`, 6);
  211. that.$set(that, `page`, 0);
  212. }
  213. }
  214. };
  215. if (!Array) {
  216. const _component_submit_1 = common_vendor.resolveComponent("submit_1");
  217. _component_submit_1();
  218. }
  219. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  220. return {
  221. a: common_vendor.f($data.msgList, (item, index, i0) => {
  222. return common_vendor.e({
  223. a: item.time != ""
  224. }, item.time != "" ? {
  225. b: common_vendor.t(item.time)
  226. } : {}, {
  227. c: item.speaker != $data.user._id
  228. }, item.speaker != $data.user._id ? common_vendor.e({
  229. d: $data.config.logo_url && $data.config.logo_url.length > 0 ? $data.config.logo_url[0].url : "",
  230. e: item.msg_type == "0"
  231. }, item.msg_type == "0" ? {
  232. f: common_vendor.t(item.content)
  233. } : item.msg_type == "1" ? {
  234. h: item.content,
  235. i: common_vendor.o(($event) => $options.previewImg(item.content), index)
  236. } : {}, {
  237. g: item.msg_type == "1"
  238. }) : item.speaker == $data.user._id ? common_vendor.e({
  239. k: $data.user.logo && $data.user.logo.length > 0 ? $data.user.logo[0].url : "",
  240. l: item.msg_type == "0"
  241. }, item.msg_type == "0" ? {
  242. m: common_vendor.t(item.content)
  243. } : item.msg_type == "1" ? {
  244. o: item.content,
  245. p: common_vendor.o(($event) => $options.previewImg(item.content), index)
  246. } : {}, {
  247. n: item.msg_type == "1"
  248. }) : {}, {
  249. j: item.speaker == $data.user._id,
  250. q: index,
  251. r: "msg" + item._id
  252. });
  253. }),
  254. b: $data.scrollToView,
  255. c: $data.triggered,
  256. d: common_vendor.o((...args) => $options.getFresh && $options.getFresh(...args)),
  257. e: common_vendor.o($options.choseImg),
  258. f: common_vendor.o($options.inputs),
  259. g: common_vendor.o($options.heights)
  260. };
  261. }
  262. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__scopeId", "data-v-22cb3e28"], ["__file", "D:/project/赋强公证/notarization_applet/pagesHome/customer/index.vue"]]);
  263. wx.createPage(MiniProgramPage);