App.vue 14 KB

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