App.vue 13 KB

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