App.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. <script>
  2. import {
  3. getUpApp
  4. } from './utils/upApp.js';
  5. import {
  6. checkLogin
  7. } from './libs/login';
  8. import {
  9. HTTP_REQUEST_URL
  10. } from './config/app';
  11. import {
  12. getShopConfig,
  13. silenceAuth
  14. } from '@/api/public';
  15. import Auth from '@/libs/wechat.js';
  16. import Routine from './libs/routine.js';
  17. import {
  18. colorChange
  19. } from '@/api/api.js';
  20. import {
  21. mapGetters
  22. } from "vuex"
  23. // #ifdef MP
  24. // let livePlayer = requirePlugin('live-player-plugin')
  25. // #endif
  26. let green =
  27. '--view-theme: #42CA4D;--view-priceColor:#FF7600;--view-minorColor:rgba(108, 198, 94, 0.5);--view-minorColorT:rgba(66, 202, 77, 0.1);--view-bntColor:#FE960F;--view-assistColor:#FE960F;'
  28. let red =
  29. '--view-theme: #e93323;--view-priceColor:#e93323;--view-minorColor:rgba(233, 51, 35, 0.5);--view-minorColorT:rgba(233, 51, 35, 0.1);--view-bntColor:#FE960F;--view-assistColor:#FE960F;'
  30. let blue =
  31. '--view-theme: #1DB0FC;--view-priceColor:#FD502F;--view-minorColor:rgba(58, 139, 236, 0.5);--view-minorColorT:rgba(9, 139, 243, 0.1);--view-bntColor:#22CAFD;--view-assistColor:#C4D9EC;'
  32. let pink =
  33. '--view-theme: #FF448F;--view-priceColor:#FF448F;--view-minorColor:rgba(255, 68, 143, 0.5);--view-minorColorT:rgba(255, 68, 143, 0.1);--view-bntColor:#282828;--view-assistColor:#FEAC41;'
  34. let orange =
  35. '--view-theme: #FE5C2D;--view-priceColor:#FE5C2D;--view-minorColor:rgba(254, 92, 45, 0.5);--view-minorColorT:rgba(254, 92, 45, 0.1);--view-bntColor:#FDB000;--view-assistColor:#FDB000;'
  36. export default {
  37. globalData: {
  38. spid: 0,
  39. code: 0,
  40. isLogin: false,
  41. userInfo: {},
  42. MyMenus: [],
  43. globalData: false,
  44. isIframe: false,
  45. tabbarShow: true,
  46. windowHeight: 0
  47. },
  48. computed: mapGetters(['isLogin', 'cartNum']),
  49. watch: {
  50. isLogin: {
  51. deep: true, //深度监听设置为 true
  52. handler: function(newV, oldV) {
  53. if (newV) {
  54. // this.getCartNum()
  55. } else {
  56. this.$store.commit('indexData/setCartNum', '')
  57. }
  58. }
  59. },
  60. cartNum(newCart, b) {
  61. this.$store.commit('indexData/setCartNum', newCart + '')
  62. if (newCart > 0) {
  63. uni.setTabBarBadge({
  64. index: 3,
  65. text: newCart + ''
  66. })
  67. } else {
  68. uni.hideTabBarRedDot({
  69. index: 3
  70. })
  71. }
  72. }
  73. },
  74. onLaunch: async function(option) {
  75. //#ifdef APP
  76. plus.screen.lockOrientation("portrait-primary");
  77. //#endif
  78. let that = this;
  79. colorChange('color_change').then(res => {
  80. let navigation = res.data.navigation; //判断悬浮导航是否显示
  81. let statusColor = res.data.status; //判断显示啥颜色
  82. uni.setStorageSync('navigation', navigation);
  83. uni.$emit('navOk', navigation);
  84. uni.setStorageSync('statusColor', statusColor);
  85. uni.$emit('colorOk', statusColor);
  86. switch (res.data.status) {
  87. case 1:
  88. uni.setStorageSync('viewColor', blue)
  89. uni.$emit('ok', blue)
  90. break;
  91. case 2:
  92. uni.setStorageSync('viewColor', green)
  93. uni.$emit('ok', green)
  94. break;
  95. case 3:
  96. uni.setStorageSync('viewColor', red)
  97. uni.$emit('ok', red)
  98. break;
  99. case 4:
  100. uni.setStorageSync('viewColor', pink)
  101. uni.$emit('ok', pink)
  102. break;
  103. case 5:
  104. uni.setStorageSync('viewColor', orange)
  105. uni.$emit('ok', orange)
  106. break;
  107. default:
  108. uni.setStorageSync('viewColor', red)
  109. uni.$emit('ok', red)
  110. break
  111. }
  112. });
  113. if (option.query.pid) {
  114. that.$Cache.set('spread', option.query.pid);
  115. }
  116. if (option.query.spread) {
  117. that.$Cache.set('spread', option.query.spread);
  118. that.globalData.spid = option.query.spread;
  119. that.globalData.pid = option.query.spread;
  120. }
  121. // #ifdef APP-PLUS || H5
  122. uni.getSystemInfo({
  123. success: function(res) {
  124. // 首页没有title获取的整个页面的高度,里面的页面有原生标题要减掉就是视口的高度
  125. // 状态栏是动态的可以拿到 标题栏是固定写死的是44px
  126. let height = res.windowHeight - res.statusBarHeight - 44
  127. // #ifdef H5 || APP-PLUS
  128. that.globalData.windowHeight = res.windowHeight + 'px'
  129. // #endif
  130. // // #ifdef APP-PLUS
  131. // that.globalData.windowHeight = height + 'px'
  132. // // #endif
  133. }
  134. });
  135. // #endif
  136. // #ifdef MP
  137. if (HTTP_REQUEST_URL == '') {
  138. console.error(
  139. "请配置根目录下的config.js文件中的 'HTTP_REQUEST_URL'\n\n请修改开发者工具中【详情】->【AppID】改为自己的Appid\n\n请前往后台【小程序】->【小程序配置】填写自己的 appId and AppSecret"
  140. );
  141. return false;
  142. }
  143. if (option.query.hasOwnProperty('scene')) {
  144. switch (option.scene) {
  145. //扫描小程序码
  146. case 1047:
  147. let val = that.$util.getUrlParams(decodeURIComponent(option.query.scene));
  148. that.globalData.code = val.pid === undefined ? val : val.pid;
  149. break;
  150. //长按图片识别小程序码
  151. case 1048:
  152. that.globalData.code = option.query.scene;
  153. break;
  154. //手机相册选取小程序码
  155. case 1049:
  156. that.globalData.code = option.query.scene;
  157. break;
  158. //直接进入小程序
  159. case 1001:
  160. that.globalData.spid = option.query.scene;
  161. break;
  162. }
  163. }
  164. const updateManager = wx.getUpdateManager();
  165. updateManager.onCheckForUpdate(function(res) {
  166. // 请求完新版本信息的回调
  167. });
  168. updateManager.onUpdateReady(function() {
  169. wx.showModal({
  170. title: '更新提示',
  171. content: '新版本已经准备好,是否重启应用?',
  172. success: function(res) {
  173. if (res.confirm) {
  174. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  175. updateManager.applyUpdate();
  176. }
  177. }
  178. });
  179. });
  180. updateManager.onUpdateFailed(function() {
  181. return that.Tips({
  182. title: '新版本下载失败'
  183. });
  184. });
  185. // #endif
  186. // getShopConfig().then(res => {
  187. // this.$store.commit('SETPHONESTATUS', res.data.status);
  188. // });
  189. // 获取导航高度;
  190. uni.getSystemInfo({
  191. success: function(res) {
  192. that.globalData.navHeight = res.statusBarHeight * (750 / res.windowWidth) + 91;
  193. }
  194. });
  195. // #ifdef MP
  196. let menuButtonInfo = uni.getMenuButtonBoundingClientRect();
  197. that.globalData.navH = menuButtonInfo.top * 2 + menuButtonInfo.height / 2;
  198. const version = uni.getSystemInfoSync().SDKVersion
  199. if (Routine.compareVersion(version, '2.21.2') >= 0) {
  200. console.log(version)
  201. that.$Cache.set('MP_VERSION_ISNEW', true)
  202. } else {
  203. that.$Cache.set('MP_VERSION_ISNEW', false)
  204. }
  205. // #endif
  206. // #ifdef H5
  207. // 添加crmeb chat 统计
  208. var __s = document.createElement('script');
  209. __s.src = `${HTTP_REQUEST_URL}/api/get_script`;
  210. document.head.appendChild(__s);
  211. uni.getSystemInfo({
  212. success(e) {
  213. /* 窗口宽度大于420px且不在PC页面且不在移动设备时跳转至 PC.html 页面 */
  214. if (e.windowWidth > 420 && !window.top.isPC && !/iOS|Android/i.test(e.system)) {
  215. window.location.pathname = '/static/html/pc.html';
  216. }
  217. }
  218. });
  219. if (option.query.hasOwnProperty('type')) {
  220. this.globalData.isIframe = true;
  221. } else {
  222. this.globalData.isIframe = false;
  223. }
  224. if (window.location.pathname !== '/' && !this.isWork()) {
  225. let snsapiBase = 'snsapi_base';
  226. let urlData = location.pathname + location.search;
  227. if (!that.$store.getters.isLogin && uni.getStorageSync('authIng')) {
  228. uni.setStorageSync('authIng', false)
  229. }
  230. if (!that.$store.getters.isLogin && Auth.isWeixin()) {
  231. let code,
  232. state,
  233. scope = ''
  234. if (option.query.code instanceof Array) {
  235. code = option.query.code[option.query.code.length - 1]
  236. } else {
  237. code = option.query.code
  238. }
  239. if (code && code != uni.getStorageSync('snsapiCode') && location.pathname.indexOf(
  240. '/pages/users/wechat_login/index') === -1) {
  241. // 存储静默授权code
  242. uni.setStorageSync('snsapiCode', code);
  243. let spread = that.globalData.spid ? that.globalData.spid : '';
  244. try {
  245. let res = await silenceAuth({
  246. code: code,
  247. snsapi: 'snsapi_base',
  248. spread: that.$Cache.get('spread'),
  249. spid: that.globalData.code
  250. });
  251. uni.setStorageSync('snRouter', decodeURIComponent(decodeURIComponent(option.query
  252. .back_url)));
  253. if (res.data.key !== undefined && res.data.key) {
  254. this.$Cache.set('snsapiKey', res.data.key);
  255. } else {
  256. let time = res.data.expires_time - this.$Cache.time();
  257. this.$store.commit('LOGIN', {
  258. token: res.data.token,
  259. time: time
  260. });
  261. this.$store.commit('SETUID', res.data.userInfo.uid);
  262. this.$store.commit('UPDATE_USERINFO', res.data.userInfo);
  263. if (option.query.back_url) {
  264. location.replace(decodeURIComponent(decodeURIComponent(option.query
  265. .back_url)));
  266. }
  267. }
  268. } catch (e) {
  269. let url = ''
  270. if (option.query.back_url instanceof Array) {
  271. url = option.query.back_url[option.query.back_url.length - 1]
  272. } else {
  273. url = option.query.back_url
  274. }
  275. if (!that.$Cache.has('snsapiKey')) {
  276. if (location.pathname.indexOf('/pages/users/wechat_login/index') === -1) {
  277. Auth.oAuth('snsapi_userinfo', url);
  278. }
  279. }
  280. }
  281. // silenceAuth({
  282. // code: code,
  283. // spread: that.$Cache.get('spread'),
  284. // spid: that.globalData.code
  285. // })
  286. // .then(res => {
  287. // uni.setStorageSync('snRouter', decodeURIComponent(decodeURIComponent(option.query
  288. // .back_url)));
  289. // if (res.data.key !== undefined && res.data.key) {
  290. // this.$Cache.set('snsapiKey', res.data.key);
  291. // } else {
  292. // let time = res.data.expires_time - this.$Cache.time();
  293. // this.$store.commit('LOGIN', {
  294. // token: res.data.token,
  295. // time: time
  296. // });
  297. // this.$store.commit('SETUID', res.data.userInfo.uid);
  298. // this.$store.commit('UPDATE_USERINFO', res.data.userInfo);
  299. // if (option.query.back_url) {
  300. // location.replace(decodeURIComponent(decodeURIComponent(option.query
  301. // .back_url)));
  302. // }
  303. // }
  304. // })
  305. // .catch(error => {
  306. // let url = ''
  307. // if (option.query.back_url instanceof Array) {
  308. // url = option.query.back_url[option.query.back_url.length - 1]
  309. // } else {
  310. // url = option.query.back_url
  311. // }
  312. // if (!that.$Cache.has('snsapiKey')) {
  313. // if (location.pathname.indexOf('/pages/users/wechat_login/index') === -1) {
  314. // Auth.oAuth(snsapiBase, url);
  315. // }
  316. // }
  317. // });
  318. } else {
  319. if (!this.$Cache.has('snsapiKey')) {
  320. if (location.pathname.indexOf('/pages/users/wechat_login/index') === -1) {
  321. Auth.oAuth(snsapiBase, urlData);
  322. }
  323. }
  324. }
  325. } else {
  326. if (option.query.back_url) {
  327. location.replace(uni.getStorageSync('snRouter'));
  328. }
  329. }
  330. }
  331. // #endif
  332. // #ifdef MP
  333. // 小程序静默授权
  334. if (!this.$store.getters.isLogin) {
  335. Routine.getCode()
  336. .then(code => {
  337. this.silenceAuth(code);
  338. })
  339. .catch(res => {
  340. uni.hideLoading();
  341. });
  342. }
  343. // #endif
  344. },
  345. onShow(options) {
  346. let that = this;
  347. //直播间分享
  348. // #ifdef MP
  349. // const sceneList = [1007, 1008, 1014, 1044, 1045, 1046, 1047, 1048, 1049, 1073, 1154, 1155];
  350. // if (sceneList.includes(options.scene)) {
  351. // livePlayer.getShareParams()
  352. // .then(res => {
  353. // //记录推广人uid
  354. // if(res.custom_params.pid){
  355. // that.$Cache.set('spread', res.custom_params.pid);
  356. // that.globalData.spid = res.custom_params.pid;
  357. // that.globalData.pid = res.custom_params.pid;
  358. // }
  359. // }).catch(err => {
  360. // })
  361. // }
  362. // #endif
  363. // #ifdef APP-PLUS
  364. // 判断是否升级
  365. getUpApp();
  366. // 获取当前运行系统
  367. let system = uni.getStorageSync('platform') || '';
  368. if (!system) {
  369. uni.setStorage({
  370. key: 'platform',
  371. data: uni.getSystemInfoSync().platform
  372. });
  373. }
  374. // #endif
  375. },
  376. mounted() {
  377. // setTimeout((e) => {
  378. // if (this.$store.getters.isLogin) {
  379. // this.getCartNum()
  380. // }
  381. // }, 100)
  382. },
  383. methods: {
  384. // 小程序静默授权
  385. silenceAuth(code) {
  386. let that = this;
  387. let spread = that.globalData.spid ? that.globalData.spid : '';
  388. silenceAuth({
  389. code: code,
  390. spread_spid: spread,
  391. spread_code: that.globalData.code
  392. })
  393. .then(res => {
  394. if (res.data.token !== undefined && res.data.token) {
  395. uni.hideLoading();
  396. let time = res.data.expires_time - this.$Cache.time();
  397. that.$store.commit('LOGIN', {
  398. token: res.data.token,
  399. time: time
  400. });
  401. that.$store.commit('SETUID', res.data.userInfo.uid);
  402. that.$store.commit('UPDATE_USERINFO', res.data.userInfo);
  403. }
  404. })
  405. .catch(res => {});
  406. },
  407. isWork() {
  408. return navigator.userAgent.toLowerCase().indexOf('wxwork') !== -1 && navigator.userAgent.toLowerCase()
  409. .indexOf("micromessenger") !== -1
  410. }
  411. },
  412. onHide: function() {
  413. }
  414. };
  415. </script>
  416. <style lang="scss">
  417. /* #ifndef APP-PLUS-NVUE || APP-NVUE */
  418. @import url('@/plugin/emoji-awesome/css/tuoluojiang.css');
  419. @import url('@/plugin/animate/animate.min.css');
  420. @import 'static/css/base.css';
  421. @import 'static/iconfont/iconfont.css';
  422. @import 'static/css/guildford.css';
  423. @import 'static/css/style.scss';
  424. view {
  425. box-sizing: border-box;
  426. }
  427. page {
  428. font-family: PingFang SC;
  429. }
  430. .placeholder{
  431. color: #ccc;
  432. }
  433. .bg-color-red {
  434. background-color: var(--view-theme) !important;
  435. }
  436. .syspadding {
  437. padding-top: var(--status-bar-height);
  438. }
  439. .flex {
  440. display: flex;
  441. }
  442. .uni-scroll-view::-webkit-scrollbar {
  443. /* 隐藏滚动条,但依旧具备可以滚动的功能 */
  444. display: none;
  445. }
  446. ::-webkit-scrollbar {
  447. width: 0;
  448. height: 0;
  449. color: transparent;
  450. }
  451. .uni-system-open-location .map-content.fix-position {
  452. height: 100vh;
  453. top: 0;
  454. bottom: 0;
  455. }
  456. /* #endif */
  457. </style>