App.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <script setup>
  2. import { watch, onBeforeUnmount } from "vue";
  3. import { onLaunch, onShow, onHide } from "@dcloudio/uni-app";
  4. import {
  5. useSystemStore,
  6. useShopStore,
  7. useVideoStore,
  8. useSocketStore,
  9. useMessageStore,
  10. useUserStore,
  11. useTabbarStore,
  12. } from "@/store";
  13. import { storeToRefs } from "pinia";
  14. import { setLan, findLang } from "@/locale";
  15. import { parseUrl } from "@/utils";
  16. import appUpdate from "./utils/appUpdate";
  17. import { USER_INIT } from "@/api";
  18. const useSystem = useSystemStore();
  19. const useShop = useShopStore();
  20. const useVideo = useVideoStore();
  21. const useScoket = useSocketStore();
  22. const useMessage = useMessageStore();
  23. const useUser = useUserStore();
  24. const useTabbar = useTabbarStore();
  25. const { isFirstConnectNetwork, networkStatus, isHomeFirstConnect } =
  26. storeToRefs(useSystem);
  27. function initFunc() {
  28. useSystem.setWinnotice();
  29. useSystem.setRateList();
  30. useShop.setCartList();
  31. useShop.setHotLink();
  32. useScoket.connect();
  33. // useVideo.setVideoList();
  34. }
  35. onLaunch(async () => {
  36. useSystem.checkNetworkStatus();
  37. // 监听网络状态
  38. listenerNetworkStatus();
  39. // initFunc();
  40. // #ifdef APP-PLUS
  41. appUpdate();
  42. initPushService();
  43. clearBadge();
  44. // #endif
  45. // #ifdef H5
  46. setInit();
  47. // #endif
  48. });
  49. onShow(() => {
  50. clearBadge();
  51. // #ifdef H5
  52. getLang();
  53. // #endif
  54. });
  55. onHide(() => {});
  56. onBeforeUnmount(() => {
  57. uni.$off("$onMessage");
  58. stopWatch();
  59. });
  60. const stopWatch = watch(
  61. [isFirstConnectNetwork, networkStatus], // 监听多个响应式变量
  62. ([newIsFirst, newNetStatus], [oldIsFirst, oldNetStatus]) => {
  63. // console.log("联动变化:", {
  64. // 首次连接标记: `${oldIsFirst}→${newIsFirst}`,
  65. // 网络状态: `${oldNetStatus ? "已连接" : "断开"}→${
  66. // newNetStatus ? "已连接" : "断开"
  67. // }`,
  68. // });
  69. if (newNetStatus) {
  70. initFunc();
  71. useSystem.setFirstConnectStatus(false);
  72. }
  73. }
  74. );
  75. function listenerNetworkStatus() {
  76. uni.onNetworkStatusChange((res) => {
  77. const { isConnected, networkType } = res;
  78. if (isConnected && networkType != "none") {
  79. if (isFirstConnectNetwork.value) {
  80. initFunc();
  81. }
  82. useSystem.setNetworkStatus(true);
  83. } else {
  84. useSystem.setNetworkStatus(false);
  85. }
  86. });
  87. }
  88. const getLang = () => {
  89. let urlObj = parseUrl(window.location.href);
  90. let lang = urlObj["lang"];
  91. let langValue = findLang(lang) && findLang(lang).value;
  92. if (langValue) {
  93. useSystem.setLan(langValue);
  94. setLan(langValue, () => {
  95. useShop.setHotLink();
  96. });
  97. }
  98. };
  99. function initPushService() {
  100. uni.getPushClientId({
  101. success: (res) => {
  102. setInit({ push_cid: res.cid });
  103. },
  104. fail: (err) => {
  105. setInit();
  106. console.error("获取推送ID失败:", err);
  107. },
  108. });
  109. }
  110. const setInit = async (para) => {
  111. try {
  112. const res = await USER_INIT(para);
  113. useUser.setInitId(res.data.auth_id);
  114. // #ifdef APP-PLUS
  115. useSystem.setAppinfo(res.data.appinfo);
  116. // #endif
  117. if (res.data.token) {
  118. useUser.setUserInfo(res.data);
  119. }
  120. } catch (error) {}
  121. };
  122. // 清除角标的方法
  123. function clearBadge() {
  124. // #ifdef APP-PLUS
  125. useMessage.getTotalUnreadCount();
  126. // #endif
  127. }
  128. uni.$on("$onMessage", ({ event, channel, data }) => {
  129. // console.log('event, channel, data', event, channel, data)
  130. useScoket.checkStatus();
  131. if (
  132. channel &&
  133. channel.includes("private-call") &&
  134. data.extend.message_event == "new_message"
  135. ) {
  136. const msgInfo = JSON.parse(data.message);
  137. if (msgInfo.send == 1) return;
  138. useMessage.updateMessageList(
  139. channel,
  140. {
  141. ...msgInfo,
  142. msg: JSON.parse(msgInfo.msg),
  143. },
  144. "push"
  145. );
  146. useMessage.updateGlobalMapLasttime(
  147. `${msgInfo.type}Channel`,
  148. msgInfo.indate
  149. );
  150. useMessage.updateUnreadCount(channel);
  151. } else if (
  152. channel &&
  153. channel.includes("private-user") &&
  154. data.extend.message_event == "new_message"
  155. ) {
  156. const msgInfo = JSON.parse(data.message);
  157. // #ifdef APP-PLUS
  158. // const cont = JSON.parse(msgInfo.lastmsg || '{}')
  159. // uni.createPushMessage({
  160. // content: cont.text || '测试数据', success: res => {
  161. // console.log('通知栏调用成功', res);
  162. // }, fail: err => {
  163. // console.error('通知栏调用失败', err);
  164. // }
  165. // })
  166. // #endif
  167. useMessage.updateGlobalMapUnreadCount(`${msgInfo.type}Channel`);
  168. }
  169. });
  170. const onPush = () => {
  171. uni.onPushMessage((res) => {
  172. const data = res.data;
  173. console.log(res);
  174. if (res.type === "receive") {
  175. // 推送到通知栏
  176. uni.createPushMessage({
  177. title: data?.title,
  178. content: data?.content || "收到新消息",
  179. payload: data?.payload,
  180. success: (r) => {
  181. console.log("推送成功", r);
  182. },
  183. fail: (err) => {
  184. console.error("推送失败", err);
  185. },
  186. });
  187. }
  188. if (res.type == "click") {
  189. console.log("通知栏", data);
  190. }
  191. });
  192. };
  193. // #ifdef APP-PLUS
  194. onPush();
  195. // #endif
  196. </script>
  197. <style>
  198. @import url("./static/fonts.css");
  199. @import url("static/css/main.css");
  200. /* @font-face {
  201. font-family: 'MyCustomFont';
  202. src: url('../static/fonts/SourceHanSansCN-VF.otf') format('opentype');
  203. font-weight: normal;
  204. font-style: normal;
  205. } */
  206. @font-face {
  207. font-family: "HarmonyOS_Sans";
  208. src: url("../static/fonts/HarmonyOS_Sans_SC.ttf") format("truetype");
  209. font-weight: normal;
  210. font-style: normal;
  211. }
  212. body {
  213. /* font-family: 'MyCustomFont'; */
  214. }
  215. </style>
  216. <style lang="scss">
  217. @import "@/uni_modules/uview-plus/index.scss";
  218. .container {
  219. padding: 15px;
  220. }
  221. button {
  222. margin-bottom: 15px;
  223. }
  224. </style>