App.vue 5.5 KB

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