index.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <template>
  2. <view class="page_container" :class="{ ios_page: isIos }">
  3. <z-paging
  4. ref="paging"
  5. :fixed="false"
  6. loading-full-fixed
  7. default-page-size="20"
  8. refresher-background="#05263c"
  9. v-model="momentsList"
  10. :show-loading-more-no-more-view="false"
  11. @query="queryList"
  12. @scroll="onScroll">
  13. <moments-header
  14. :needTransparent="needTransparent"
  15. @topbarClick="topbarClick"
  16. />
  17. <moments-item
  18. v-for="item in momentsList"
  19. :key="item.workMomentID"
  20. :moments="item"
  21. @startComent="startComent"
  22. />
  23. <view slot="loading" class="loading_wrap">
  24. <u-loading-icon text="加载中"></u-loading-icon>
  25. </view>
  26. <comment-input
  27. slot="bottom"
  28. ref="commentInputRef"
  29. v-show="commenting"
  30. :loading="commentLoading"
  31. :commenting="commenting"
  32. :placeholder="placeholder"
  33. @completeInput="sendComment"
  34. @hiddenInput="commenting = false"
  35. />
  36. </z-paging>
  37. </view>
  38. </template>
  39. <script>
  40. import { v4 as uuidv4 } from "uuid";
  41. import IMSDK from "openim-uniapp-polyfill";
  42. import CustomNavBar from "../../../components/CustomNavBar/index.vue";
  43. import { commentMoments, getSelfMomentsSplit } from "../../../api/moments";
  44. import MomentsHeader from "./components/MomentsHeader.vue";
  45. import MomentsItem from "./components/MomentsItem.vue";
  46. import CommentInput from "./components/CommentInput.vue";
  47. import { PageEvents } from "../../../constant";
  48. export default {
  49. components: {
  50. MomentsHeader,
  51. MomentsItem,
  52. CustomNavBar,
  53. CommentInput,
  54. },
  55. data() {
  56. return {
  57. commenting: false,
  58. isFocus: false,
  59. needTransparent: true,
  60. momentsList: [],
  61. commentState: {
  62. workMomentID: "",
  63. comment: {},
  64. },
  65. commentLoading: false,
  66. isIos: uni.$u.os() === "ios",
  67. };
  68. },
  69. computed: {
  70. placeholder() {
  71. if (this.commentState.comment.userID) {
  72. return `回复${this.commentState.comment.nickname}`;
  73. }
  74. return "评论";
  75. },
  76. },
  77. onLoad() {
  78. this.setPageListener();
  79. uni.addInterceptor("navigateTo", {
  80. success: () => {
  81. if (this.commenting) {
  82. this.commentState.workMomentID = "";
  83. this.commentState.comment = {};
  84. this.commenting = false;
  85. }
  86. },
  87. });
  88. IMSDK.subscribe(
  89. IMSDK.IMEvents.OnRecvCustomBusinessMessage,
  90. this.customMessageHandler,
  91. );
  92. },
  93. onUnload() {
  94. this.disposePageListener();
  95. IMSDK.unsubscribe(
  96. IMSDK.IMEvents.OnRecvCustomBusinessMessage,
  97. this.customMessageHandler,
  98. );
  99. },
  100. onShow() {
  101. this.$nextTick(() => this.$refs.commentInputRef.setKeyboardListener());
  102. },
  103. onHide() {
  104. this.$refs.commentInputRef.disposeKeyboardListener();
  105. uni.showTabBar();
  106. },
  107. methods: {
  108. customMessageHandler({ data }) {
  109. uni.$emit(PageEvents.RefreshMomentsUnreadCount);
  110. console.log("customMessageHandler", data);
  111. if (data.key.includes("wm_")) {
  112. const moments = JSON.parse(data.data).body;
  113. const tempArr = this.momentsList.map((item) => {
  114. if (item.workMomentID === moments.workMomentID) {
  115. item = { ...item, ...moments };
  116. }
  117. if (!item.content.metas) {
  118. item.content.metas = [];
  119. }
  120. if (!item.likeUsers) {
  121. item.likeUsers = [];
  122. }
  123. if (!item.comments) {
  124. item.comments = [];
  125. }
  126. if (!item.permissionUsers) {
  127. item.permissionUsers = [];
  128. }
  129. if (!item.atUsers) {
  130. item.atUsers = [];
  131. }
  132. return item;
  133. });
  134. console.log(tempArr);
  135. this.momentsList = tempArr;
  136. }
  137. },
  138. queryList(pageNo, pageSize) {
  139. console.log(pageNo, pageSize);
  140. getSelfMomentsSplit(pageNo)
  141. .then((res) => {
  142. console.log(res);
  143. this.$refs.paging.complete(res.workMoments);
  144. })
  145. .catch((err) => uni.$u.toast("获取工作圈数据失败!"));
  146. },
  147. startComent(workMomentID, comment) {
  148. this.commentState.workMomentID = workMomentID;
  149. this.commentState.comment = comment || {};
  150. this.commenting = true;
  151. this.$nextTick(() => (this.isFocus = true));
  152. },
  153. sendComment(content) {
  154. this.commentLoading = true;
  155. commentMoments(
  156. this.commentState.workMomentID,
  157. content,
  158. this.commentState.comment.userID,
  159. )
  160. .then(() => {
  161. const newComment = {
  162. userID: this.$store.getters.storeCurrentUserID,
  163. nickname: this.$store.getters.storeSelfInfo.nickname,
  164. replyUserID: this.commentState.comment.userID,
  165. replyNickname: this.commentState.comment.nickname,
  166. commentID: uuidv4(),
  167. content,
  168. };
  169. this.pushNewComment(newComment);
  170. })
  171. .catch((err) => uni.$u.toast("评论失败!"))
  172. .finally(() => {
  173. this.commentLoading = false;
  174. this.commentState.workMomentID = "";
  175. this.commentState.comment = {};
  176. this.commenting = false;
  177. });
  178. },
  179. pushNewComment(newComment, workMomentID) {
  180. const idx = this.momentsList.findIndex(
  181. (moments) =>
  182. moments.workMomentID ===
  183. (workMomentID || this.commentState.workMomentID),
  184. );
  185. if (idx > -1) {
  186. const tmpObj = {
  187. ...this.momentsList[idx],
  188. };
  189. tmpObj.comments = [...tmpObj.comments, newComment];
  190. this.momentsList[idx] = {
  191. ...tmpObj,
  192. };
  193. console.log(this.momentsList[idx]);
  194. this.$refs.paging.updateCache();
  195. }
  196. },
  197. onScroll(e) {
  198. this.needTransparent = e.detail.scrollTop < 200;
  199. },
  200. topbarClick() {
  201. uni.$u.route("/pages/moments/designatedMoments/index", {
  202. baseUserInfo: JSON.stringify({
  203. userID: this.$store.getters.storeSelfInfo.userID,
  204. nickname: this.$store.getters.storeSelfInfo.nickname,
  205. faceURL: this.$store.getters.storeSelfInfo.faceURL,
  206. }),
  207. });
  208. },
  209. deleteMomentsHandler(workMomentID) {
  210. const tmpList = [...this.momentsList];
  211. const idx = tmpList.findIndex(
  212. (moments) => moments.workMomentID === workMomentID,
  213. );
  214. if (idx > -1) {
  215. tmpList.splice(idx, 1);
  216. this.momentsList = [...tmpList];
  217. this.$refs.paging.updateCache();
  218. }
  219. },
  220. deleteCommentHandler(workMomentID, commentID) {
  221. const idx = this.momentsList.findIndex(
  222. (moments) => moments.workMomentID === workMomentID,
  223. );
  224. if (idx > -1) {
  225. const tmpObj = {
  226. ...this.momentsList[idx],
  227. };
  228. const commentIdx = tmpObj.comments.findIndex(
  229. (comment) => comment.commentID === commentID,
  230. );
  231. tmpObj.comments.splice(commentIdx, 1);
  232. this.momentsList[idx] = {
  233. ...tmpObj,
  234. };
  235. this.$refs.paging.updateCache();
  236. }
  237. },
  238. operateLikeHandler(workMomentID, isLiked) {
  239. console.log(workMomentID, isLiked);
  240. const idx = this.momentsList.findIndex(
  241. (moments) => moments.workMomentID === workMomentID,
  242. );
  243. if (idx === -1) return;
  244. const tmpObj = {
  245. ...this.momentsList[idx],
  246. };
  247. if (isLiked) {
  248. const likedIdx = tmpObj.likeUsers.findIndex(
  249. (user) => user.userID === this.$store.getters.storeCurrentUserID,
  250. );
  251. if (likedIdx > -1) {
  252. tmpObj.likeUsers.splice(likedIdx, 1);
  253. }
  254. } else {
  255. tmpObj.likeUsers.push({
  256. userID: this.$store.getters.storeCurrentUserID,
  257. nickname: this.$store.getters.storeSelfInfo.nickname,
  258. });
  259. }
  260. this.momentsList[idx] = {
  261. ...tmpObj,
  262. };
  263. console.log(this.momentsList[idx]);
  264. this.$refs.paging.updateCache();
  265. },
  266. refershHandler() {
  267. this.$refs.paging.reload();
  268. },
  269. setPageListener() {
  270. uni.$on(PageEvents.PushNewComment, this.pushNewComment);
  271. uni.$on(PageEvents.DeleteComment, this.deleteCommentHandler);
  272. uni.$on(PageEvents.DeleteMoments, this.deleteMomentsHandler);
  273. uni.$on(PageEvents.OperateLike, this.operateLikeHandler);
  274. uni.$on(PageEvents.RefreshMoments, this.refershHandler);
  275. },
  276. disposePageListener() {
  277. uni.$off(PageEvents.PushNewComment, this.pushNewComment);
  278. uni.$off(PageEvents.DeleteComment, this.deleteCommentHandler);
  279. uni.$off(PageEvents.DeleteMoments, this.deleteMomentsHandler);
  280. uni.$off(PageEvents.OperateLike, this.operateLikeHandler);
  281. uni.$off(PageEvents.RefreshMoments, this.refershHandler);
  282. },
  283. },
  284. onBackPress() {
  285. if (this.commenting) {
  286. this.commenting = false;
  287. return true;
  288. }
  289. return false;
  290. },
  291. };
  292. </script>
  293. <style lang="scss" scoped>
  294. .page_container {
  295. background-color: #f8f8f8;
  296. position: relative;
  297. // height: calc(100vh - 55px);
  298. .loading_wrap {
  299. @include centerBox();
  300. height: 100%;
  301. }
  302. }
  303. .ios_page {
  304. .input_bar {
  305. position: absolute;
  306. left: 0;
  307. right: 0;
  308. }
  309. }
  310. </style>