App.vue 13 KB

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