App.vue 13 KB

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