App.vue 12 KB

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