App.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. <script>
  2. import { HTTP_REQUEST_URL } from "./config/app";
  3. import {
  4. getShopConfig,
  5. silenceAuth,
  6. getSystemVersion,
  7. basicConfig,
  8. remoteRegister,
  9. } from "@/api/public";
  10. import Auth from "@/libs/wechat.js";
  11. import Routine from "./libs/routine.js";
  12. import { silenceBindingSpread } from "@/utils";
  13. import { getCrmebCopyRight, getThemeInfo } from "@/api/api.js";
  14. import { getLangJson, getLangVersion } from "@/api/user.js";
  15. import { mapGetters } from "vuex";
  16. import colors from "@/mixins/color.js";
  17. import Cache from "@/utils/cache";
  18. import { debug } from "util";
  19. import { applyTheme } from "@/utils/theme.js";
  20. export default {
  21. globalData: {
  22. spid: 0,
  23. code: 0,
  24. isLogin: false,
  25. userInfo: {},
  26. MyMenus: [],
  27. globalData: false,
  28. isIframe: false,
  29. tabbarShow: true,
  30. windowHeight: 0,
  31. locale: "",
  32. },
  33. mixins: [colors],
  34. computed: mapGetters(["isLogin", "cartNum"]),
  35. watch: {
  36. isLogin: {
  37. deep: true, //深度监听设置为 true
  38. handler: function (newV, oldV) {
  39. if (newV) {
  40. // this.getCartNum()
  41. } else {
  42. this.$store.commit("indexData/setCartNum", "");
  43. }
  44. },
  45. },
  46. cartNum(newCart, b) {
  47. this.$store.commit("indexData/setCartNum", newCart + "");
  48. if (newCart > 0) {
  49. uni.setTabBarBadge({
  50. index: Number(uni.getStorageSync("FOOTER_ADDCART")) || 2,
  51. text: newCart + "",
  52. });
  53. } else {
  54. uni.hideTabBarRedDot({
  55. index: Number(uni.getStorageSync("FOOTER_ADDCART")) || 2,
  56. });
  57. }
  58. },
  59. },
  60. onShow() {
  61. const queryData = uni.getEnterOptionsSync(); // uni-app版本 3.5.1+ 支持
  62. if (queryData.query.spread) {
  63. this.$Cache.set("spread", queryData.query.spread);
  64. this.globalData.spid = queryData.query.spread;
  65. this.globalData.pid = queryData.query.spread;
  66. silenceBindingSpread(this.globalData);
  67. }
  68. if (queryData.query.spid) {
  69. this.$Cache.set("spread", queryData.query.spid);
  70. this.globalData.spid = queryData.query.spid;
  71. this.globalData.pid = queryData.query.spid;
  72. silenceBindingSpread(this.globalData);
  73. }
  74. if (queryData.query.agent_id) {
  75. this.$Cache.set("agent_id", queryData.query.agent_id);
  76. this.globalData.agent_id = queryData.query.agent_id;
  77. silenceBindingSpread(this.globalData);
  78. }
  79. // #ifdef MP
  80. if (queryData.query.scene) {
  81. let param = this.$util.getUrlParams(
  82. decodeURIComponent(queryData.query.scene)
  83. );
  84. if (param.pid) {
  85. this.$Cache.set("spread", param.pid);
  86. this.globalData.spid = param.pid;
  87. this.globalData.pid = param.pid;
  88. } else {
  89. switch (queryData.scene) {
  90. //扫描小程序码
  91. case 1047:
  92. this.globalData.code = queryData.query.scene;
  93. break;
  94. //长按图片识别小程序码
  95. case 1048:
  96. this.globalData.code = queryData.query.scene;
  97. break;
  98. //手机相册选取小程序码
  99. case 1049:
  100. this.globalData.code = queryData.query.scene;
  101. break;
  102. //直接进入小程序
  103. case 1001:
  104. this.globalData.spid = queryData.query.scene;
  105. break;
  106. }
  107. }
  108. silenceBindingSpread(this.globalData);
  109. }
  110. // #endif
  111. },
  112. async onLaunch(option) {
  113. uni.hideTabBar();
  114. let that = this;
  115. basicConfig().then((res) => {
  116. uni.setStorageSync("BASIC_CONFIG", res.data);
  117. });
  118. // #ifdef H5
  119. if (
  120. option.query.hasOwnProperty("mdType") &&
  121. option.query.mdType == "iframeWindow"
  122. ) {
  123. this.globalData.isIframe = true;
  124. } else {
  125. this.globalData.isIframe = false;
  126. }
  127. if (!this.isLogin && option.query.hasOwnProperty("remote_token")) {
  128. this.remoteRegister(option.query.remote_token);
  129. }
  130. // #endif
  131. let previewThemeId = uni.getStorageSync("previewThemeId");
  132. applyTheme(previewThemeId);
  133. getLangVersion().then((res) => {
  134. let version = res.data.version;
  135. if (version != uni.getStorageSync("LANG_VERSION")) {
  136. getLangJson().then((res) => {
  137. let value = Object.keys(res.data)[0];
  138. Cache.set("locale", Object.keys(res.data)[0]);
  139. this.$i18n.setLocaleMessage(value, res.data[value]);
  140. uni.setStorageSync("localeJson", res.data);
  141. });
  142. }
  143. uni.setStorageSync("LANG_VERSION", version);
  144. });
  145. // #ifdef APP-PLUS || H5
  146. uni.getSystemInfo({
  147. success: function (res) {
  148. // 首页没有title获取的整个页面的高度,里面的页面有原生标题要减掉就是视口的高度
  149. // 状态栏是动态的可以拿到 标题栏是固定写死的是44px
  150. let height = res.windowHeight - res.statusBarHeight - 44;
  151. // #ifdef H5 || APP-PLUS
  152. that.globalData.windowHeight = res.windowHeight + "px";
  153. // #endif
  154. // // #ifdef APP-PLUS
  155. // that.globalData.windowHeight = height + 'px'
  156. // // #endif
  157. },
  158. });
  159. // #endif
  160. // #ifdef MP
  161. if (HTTP_REQUEST_URL == "") {
  162. console.error(
  163. "请配置根目录下的config.js文件中的 'HTTP_REQUEST_URL'\n\n请修改开发者工具中【详情】->【AppID】改为自己的Appid\n\n请前往后台【小程序】->【小程序配置】填写自己的 appId and AppSecret"
  164. );
  165. return false;
  166. }
  167. const updateManager = wx.getUpdateManager();
  168. const startParamObj = wx.getEnterOptionsSync();
  169. if (wx.canIUse("getUpdateManager") && startParamObj.scene != 1154) {
  170. const updateManager = wx.getUpdateManager();
  171. updateManager.onCheckForUpdate(function (res) {
  172. if (res.hasUpdate) {
  173. updateManager.onUpdateFailed(function () {
  174. return that.Tips({
  175. title: "新版本下载失败",
  176. });
  177. });
  178. updateManager.onUpdateReady(function () {
  179. wx.showModal({
  180. title: "更新提示",
  181. content: "新版本已经下载好,是否重启当前应用?",
  182. success(res) {
  183. if (res.confirm) {
  184. updateManager.applyUpdate();
  185. }
  186. },
  187. });
  188. });
  189. updateManager.onUpdateFailed(function () {
  190. wx.showModal({
  191. title: "发现新版本",
  192. content: "请删除当前小程序,重启搜索打开...",
  193. });
  194. });
  195. }
  196. });
  197. }
  198. // #endif
  199. // 获取导航高度;
  200. uni.getSystemInfo({
  201. success: function (res) {
  202. that.globalData.navHeight =
  203. res.statusBarHeight * (750 / res.windowWidth) + 91;
  204. },
  205. });
  206. // #ifdef MP
  207. let menuButtonInfo = uni.getMenuButtonBoundingClientRect();
  208. that.globalData.navH = menuButtonInfo.top * 2 + menuButtonInfo.height / 2;
  209. const version = uni.getSystemInfoSync().SDKVersion;
  210. if (Routine.compareVersion(version, "2.21.3") >= 0) {
  211. that.$Cache.set("MP_VERSION_ISNEW", true);
  212. } else {
  213. that.$Cache.set("MP_VERSION_ISNEW", false);
  214. }
  215. // #endif
  216. // #ifdef MP
  217. // 小程序静默授权
  218. // if (!this.$store.getters.isLogin) {
  219. // Routine.getCode()
  220. // .then(code => {
  221. // this.silenceAuth(code);
  222. // })
  223. // .catch(res => {
  224. // uni.hideLoading();
  225. // });
  226. // }
  227. // #endif
  228. // #ifdef H5
  229. // 添加crmeb chat 统计
  230. // var __s = document.createElement('script');
  231. // __s.src = `${HTTP_REQUEST_URL}/api/get_script`;
  232. // document.head.appendChild(__s);
  233. fetch(`${HTTP_REQUEST_URL}/api/get_script`)
  234. .then((response) => response.text())
  235. .then((content) => {
  236. // 尝试解析是否为HTML(带<script>标签)
  237. const isHTML = content.trim().startsWith("<script");
  238. let externalScripts = [];
  239. let inlineScripts = [];
  240. if (isHTML) {
  241. // 情况1:带<script>标签,用DOMParser解析
  242. const parser = new DOMParser();
  243. const doc = parser.parseFromString(content, "text/html");
  244. const scripts = doc.querySelectorAll("script");
  245. externalScripts = Array.from(scripts).filter((script) => script.src);
  246. inlineScripts = Array.from(scripts).filter((script) => !script.src);
  247. } else {
  248. // 情况2:不带<script>标签,直接当作内联脚本处理
  249. inlineScripts = [
  250. {
  251. textContent: content,
  252. },
  253. ];
  254. }
  255. // 1. 先加载所有外部脚本(如果有)
  256. const loadExternalScripts = externalScripts.map((script) => {
  257. return new Promise((resolve, reject) => {
  258. const newScript = document.createElement("script");
  259. newScript.src = script.src;
  260. newScript.onload = resolve;
  261. newScript.onerror = reject;
  262. document.body.appendChild(newScript);
  263. });
  264. });
  265. // 2. 等外部脚本加载完成后,再执行内联脚本
  266. Promise.all(loadExternalScripts)
  267. .then(() => {
  268. inlineScripts.forEach((script) => {
  269. const newScript = document.createElement("script");
  270. newScript.textContent = script.textContent;
  271. document.body.appendChild(newScript);
  272. });
  273. })
  274. .catch((error) =>
  275. console.error("Failed to load external scripts:", error)
  276. );
  277. })
  278. .catch((error) => console.error("Error fetching script:", error));
  279. // #endif
  280. getCrmebCopyRight().then((res) => {
  281. uni.setStorageSync("copyRight", res.data);
  282. });
  283. },
  284. onHide() {
  285. // #ifdef H5
  286. this.$Cache.clear("snsapiKey");
  287. // #endif
  288. this.$Cache.clear("previewThemeId");
  289. },
  290. methods: {
  291. remoteRegister(remote_token) {
  292. remoteRegister({
  293. remote_token,
  294. }).then((res) => {
  295. let data = res.data;
  296. if (data.get_remote_login_url) {
  297. location.href = data.get_remote_login_url;
  298. } else {
  299. this.$store.commit("LOGIN", {
  300. token: data.token,
  301. time: data.expires_time - this.$Cache.time(),
  302. });
  303. this.$store.commit("SETUID", data.userInfo.uid);
  304. location.reload();
  305. }
  306. });
  307. },
  308. // 小程序静默授权
  309. // silenceAuth(code) {
  310. // let that = this;
  311. // let spread = that.globalData.spid ? that.globalData.spid : '';
  312. // silenceAuth({
  313. // code: code,
  314. // spread_spid: spread,
  315. // spread_code: that.globalData.code
  316. // })
  317. // .then(res => {
  318. // if (res.data.token !== undefined && res.data.token) {
  319. // uni.hideLoading();
  320. // let time = res.data.expires_time - this.$Cache.time();
  321. // that.$store.commit('LOGIN', {
  322. // token: res.data.token,
  323. // time: time
  324. // });
  325. // that.$store.commit('SETUID', res.data.userInfo.uid);
  326. // that.$store.commit('UPDATE_USERINFO', res.data.userInfo);
  327. // }
  328. // })
  329. // .catch(res => {});
  330. // },
  331. },
  332. };
  333. </script>
  334. <style>
  335. @import url("@/plugin/emoji-awesome/css/tuoluojiang.css");
  336. @import url("@/plugin/animate/animate.min.css");
  337. @import "static/css/base.css";
  338. @import "static/iconfont/iconfont.css";
  339. @import "static/css/guildford.css";
  340. @import "static/css/style.scss";
  341. @import "static/css/unocss.css";
  342. @import "static/fonts/font.scss";
  343. view {
  344. box-sizing: border-box;
  345. }
  346. page {
  347. font-family: system-ui, -apple-system, BlinkMacSystemFont, Segoe UI, Roboto,
  348. Helvetica Neue, Arial, sans-serif;
  349. }
  350. .bg-color-red {
  351. background-color: var(--view-theme) !important;
  352. }
  353. .syspadding {
  354. padding-top: var(--status-bar-height);
  355. }
  356. .flex {
  357. display: flex;
  358. }
  359. .uni-scroll-view::-webkit-scrollbar {
  360. /* 隐藏滚动条,但依旧具备可以滚动的功能 */
  361. display: none;
  362. }
  363. ::-webkit-scrollbar {
  364. width: 0;
  365. height: 0;
  366. color: transparent;
  367. }
  368. .uni-system-open-location .map-content.fix-position {
  369. height: 100vh;
  370. top: 0;
  371. bottom: 0;
  372. }
  373. .open-location {
  374. width: 100%;
  375. height: 100vh;
  376. }
  377. </style>