App.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. } from '@/api/public';
  12. import Auth from '@/libs/wechat.js';
  13. import Routine from './libs/routine.js';
  14. import {
  15. getCartCounts,
  16. } from '@/api/order.js';
  17. import {
  18. colorChange
  19. } from '@/api/api.js';
  20. import {
  21. mapGetters
  22. } from "vuex"
  23. import colors from '@/mixins/color.js';
  24. let green =
  25. '--view-theme: rgba(66,202,77,1);--view-theme-16: #42CA4D;--view-priceColor:#FF7600;--view-minorColor:rgba(108, 198, 94, 0.5);--view-minorColorT:rgba(66, 202, 77, 0.1);--view-bntColor:#FE960F;--view-op-ten: rgba(66,202,77, 0.1);--view-main-start:#70E038; --view-main-over:#42CA4D;--view-op-point-four: rgba(66,202,77, 0.04);'
  26. let red =
  27. '--view-theme: rgba(233,51,35,1);--view-theme-16: #e93323;--view-priceColor:#e93323;--view-minorColor:rgba(233, 51, 35, 0.5);--view-minorColorT:rgba(233, 51, 35, 0.1);--view-bntColor:#FE960F;--view-op-ten: rgba(233,51,35, 0.1);--view-main-start:#FF6151; --view-main-over:#e93323;--view-op-point-four: rgba(233,51,35, 0.04);'
  28. let blue =
  29. '--view-theme: rgba(29,176,252,1);--view-theme-16:#1db0fc;--view-priceColor:#FD502F;--view-minorColor:rgba(58, 139, 236, 0.5);--view-minorColorT:rgba(9, 139, 243, 0.1);--view-bntColor:#22CAFD;--view-op-ten: rgba(29,176,252, 0.1);--view-main-start:#40D1F4; --view-main-over:#1DB0FC;--view-op-point-four: rgba(29,176,252, 0.04);'
  30. let pink =
  31. '--view-theme: rgba(255,68,143,1);--view-theme-16:#ff448f;--view-priceColor:#FF448F;--view-minorColor:rgba(255, 68, 143, 0.5);--view-minorColorT:rgba(255, 68, 143, 0.1);--view-bntColor:#282828;--view-op-ten: rgba(255,68,143, 0.1);--view-main-start:#FF67AD; --view-main-over:#FF448F;--view-op-point-four: rgba(255,68,143, 0.04);'
  32. let orange =
  33. '--view-theme: rgba(254,92,45,1); --view-theme-16:#FE5C2D;--view-priceColor:#FE5C2D;--view-minorColor:rgba(254, 92, 45, 0.5);--view-minorColorT:rgba(254, 92, 45, 0.1);--view-bntColor:#FDB000;--view-op-ten: rgba(254,92,45, 0.1);--view-main-start:#FF9445; --view-main-over:#FE5C2D;--view-op-point-four: rgba(254,92,45, 0.04);'
  34. export default {
  35. globalData: {
  36. spid: 0,
  37. code: 0,
  38. isLogin: false,
  39. userInfo: {},
  40. MyMenus: [],
  41. globalData: false,
  42. isIframe: false,
  43. tabbarShow: true,
  44. windowHeight: 0
  45. },
  46. mixins: [colors],
  47. computed: mapGetters(['isLogin', 'cartNum']),
  48. watch: {
  49. isLogin: {
  50. deep: true, //深度监听设置为 true
  51. handler: function(newV, oldV) {
  52. if (newV) {
  53. // this.getCartNum()
  54. } else {
  55. this.$store.commit('indexData/setCartNum', '')
  56. }
  57. }
  58. },
  59. cartNum(newCart, b) {
  60. this.$store.commit('indexData/setCartNum', newCart + '')
  61. if (newCart > 0) {
  62. uni.setTabBarBadge({
  63. index: Number(uni.getStorageSync('FOOTER_ADDCART')) || 2,
  64. text: newCart + ''
  65. })
  66. } else {
  67. uni.hideTabBarRedDot({
  68. index: Number(uni.getStorageSync('FOOTER_ADDCART')) || 2
  69. })
  70. }
  71. }
  72. },
  73. onLaunch: function(option) {
  74. let that = this;
  75. colorChange('color_change').then(res => {
  76. switch (res.data.status) {
  77. case 1:
  78. uni.setStorageSync('viewColor', blue)
  79. uni.$emit('ok', blue)
  80. break;
  81. case 2:
  82. uni.setStorageSync('viewColor', green)
  83. uni.$emit('ok', green)
  84. break;
  85. case 3:
  86. uni.setStorageSync('viewColor', red)
  87. uni.$emit('ok', red)
  88. break;
  89. case 4:
  90. uni.setStorageSync('viewColor', pink)
  91. uni.$emit('ok', pink)
  92. break;
  93. case 5:
  94. uni.setStorageSync('viewColor', orange)
  95. uni.$emit('ok', orange)
  96. break;
  97. default:
  98. uni.setStorageSync('viewColor', red)
  99. uni.$emit('ok', red)
  100. break
  101. }
  102. });
  103. if (option.query.spread) {
  104. that.$Cache.set('spread', option.query.spread);
  105. that.globalData.spid = option.query.spread;
  106. that.globalData.pid = option.query.spread;
  107. }
  108. // #ifdef APP-PLUS || H5
  109. uni.getSystemInfo({
  110. success: function(res) {
  111. // 首页没有title获取的整个页面的高度,里面的页面有原生标题要减掉就是视口的高度
  112. // 状态栏是动态的可以拿到 标题栏是固定写死的是44px
  113. let height = res.windowHeight - res.statusBarHeight - 44
  114. // #ifdef H5 || APP-PLUS
  115. that.globalData.windowHeight = res.windowHeight + 'px'
  116. // #endif
  117. // // #ifdef APP-PLUS
  118. // that.globalData.windowHeight = height + 'px'
  119. // // #endif
  120. }
  121. });
  122. // #endif
  123. // #ifdef MP
  124. if (HTTP_REQUEST_URL == '') {
  125. console.error(
  126. "请配置根目录下的config.js文件中的 'HTTP_REQUEST_URL'\n\n请修改开发者工具中【详情】->【AppID】改为自己的Appid\n\n请前往后台【小程序】->【小程序配置】填写自己的 appId and AppSecret"
  127. );
  128. return false;
  129. }
  130. if (option.query.hasOwnProperty('scene')) {
  131. switch (option.scene) {
  132. //扫描小程序码
  133. case 1047:
  134. let val = that.$util.getUrlParams(decodeURIComponent(option.query.scene));
  135. that.globalData.code = val.pid === undefined ? val : val.pid;
  136. break;
  137. //长按图片识别小程序码
  138. case 1048:
  139. that.globalData.code = option.query.scene;
  140. break;
  141. //手机相册选取小程序码
  142. case 1049:
  143. that.globalData.code = option.query.scene;
  144. break;
  145. //直接进入小程序
  146. case 1001:
  147. that.globalData.spid = option.query.scene;
  148. break;
  149. }
  150. }
  151. const updateManager = wx.getUpdateManager();
  152. updateManager.onCheckForUpdate(function(res) {
  153. // 请求完新版本信息的回调
  154. });
  155. updateManager.onUpdateReady(function() {
  156. wx.showModal({
  157. title: '更新提示',
  158. content: '新版本已经准备好,是否重启应用?',
  159. success: function(res) {
  160. if (res.confirm) {
  161. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  162. updateManager.applyUpdate();
  163. }
  164. }
  165. });
  166. });
  167. updateManager.onUpdateFailed(function() {
  168. return that.Tips({
  169. title: '新版本下载失败'
  170. });
  171. });
  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. // #endif
  186. // #ifdef H5
  187. uni.getSystemInfo({
  188. success(e) {
  189. /* 窗口宽度大于420px且不在PC页面且不在移动设备时跳转至 PC.html 页面 */
  190. if (e.windowWidth > 420 && !window.top.isPC && !/iOS|Android/i.test(e.system)) {
  191. window.location.pathname = '/static/html/pc.html';
  192. }
  193. }
  194. });
  195. if (option.query.hasOwnProperty('type') && option.query.type == "iframeWindow") {
  196. this.globalData.isIframe = true;
  197. } else {
  198. this.globalData.isIframe = false;
  199. }
  200. if (window.location.pathname !== '/') {
  201. let snsapiBase = 'snsapi_base';
  202. let urlData = location.pathname + location.search;
  203. if (!that.$store.getters.isLogin && uni.getStorageSync('authIng')) {
  204. uni.setStorageSync('authIng', false)
  205. }
  206. if (!that.$store.getters.isLogin && Auth.isWeixin()) {
  207. let code,
  208. state,
  209. scope = ''
  210. if (option.query.code instanceof Array) {
  211. code = option.query.code[option.query.code.length - 1]
  212. } else {
  213. code = option.query.code
  214. }
  215. if (code && code != uni.getStorageSync('snsapiCode') && location.pathname.indexOf(
  216. '/pages/users/wechat_login/index') === -1) {
  217. // 存储静默授权code
  218. uni.setStorageSync('snsapiCode', code);
  219. let spread = that.globalData.spid ? that.globalData.spid : '';
  220. silenceAuth({
  221. code: code,
  222. spread: that.$Cache.get('spread'),
  223. spid: that.globalData.code
  224. })
  225. .then(res => {
  226. uni.setStorageSync('snRouter', decodeURIComponent(decodeURIComponent(option.query
  227. .back_url)));
  228. if (res.data.key !== undefined && res.data.key) {
  229. this.$Cache.set('snsapiKey', res.data.key);
  230. } else {
  231. let time = res.data.expires_time - this.$Cache.time();
  232. this.$store.commit('LOGIN', {
  233. token: res.data.token,
  234. time: time
  235. });
  236. this.$store.commit('SETUID', res.data.userInfo.uid);
  237. this.$store.commit('UPDATE_USERINFO', res.data.userInfo);
  238. if (option.query.back_url) {
  239. location.replace(decodeURIComponent(decodeURIComponent(option.query
  240. .back_url)));
  241. }
  242. }
  243. })
  244. .catch(error => {
  245. let url = ''
  246. if (option.query.back_url instanceof Array) {
  247. url = option.query.back_url[option.query.back_url.length - 1]
  248. } else {
  249. url = option.query.back_url
  250. }
  251. if (!that.$Cache.has('snsapiKey')) {
  252. if (location.pathname.indexOf('/pages/users/wechat_login/index') === -1) {
  253. Auth.oAuth(snsapiBase, url);
  254. }
  255. }
  256. });
  257. } else {
  258. if (!this.$Cache.has('snsapiKey')) {
  259. if (location.pathname.indexOf('/pages/users/wechat_login/index') === -1) {
  260. Auth.oAuth(snsapiBase, urlData);
  261. }
  262. }
  263. }
  264. } else {
  265. if (option.query.back_url) {
  266. location.replace(uni.getStorageSync('snRouter'));
  267. }
  268. }
  269. }
  270. // #endif
  271. // #ifdef MP
  272. // 小程序静默授权
  273. if (!this.$store.getters.isLogin) {
  274. Routine.getCode()
  275. .then(code => {
  276. this.silenceAuth(code);
  277. })
  278. .catch(res => {
  279. uni.hideLoading();
  280. });
  281. }
  282. // #endif
  283. // #ifdef H5
  284. // 添加crmeb chat 统计
  285. var __s = document.createElement('script');
  286. __s.src = `${HTTP_REQUEST_URL}/api/get_script`;
  287. document.head.appendChild(__s);
  288. // #endif
  289. },
  290. mounted() {},
  291. methods: {
  292. // 小程序静默授权
  293. silenceAuth(code) {
  294. let that = this;
  295. let spread = that.globalData.spid ? that.globalData.spid : '';
  296. silenceAuth({
  297. code: code,
  298. spread_spid: spread,
  299. spread_code: that.globalData.code
  300. })
  301. .then(res => {
  302. if (res.data.token !== undefined && res.data.token) {
  303. uni.hideLoading();
  304. let time = res.data.expires_time - this.$Cache.time();
  305. that.$store.commit('LOGIN', {
  306. token: res.data.token,
  307. time: time
  308. });
  309. that.$store.commit('SETUID', res.data.userInfo.uid);
  310. that.$store.commit('UPDATE_USERINFO', res.data.userInfo);
  311. }
  312. })
  313. .catch(res => {});
  314. },
  315. },
  316. onHide: function() {
  317. }
  318. };
  319. </script>
  320. <style>
  321. @import url('@/plugin/emoji-awesome/css/google.min.css');
  322. @import url('@/plugin/animate/animate.min.css');
  323. @import 'static/css/base.css';
  324. @import 'static/iconfont/iconfont.css';
  325. @import 'static/css/guildford.css';
  326. @import 'static/css/style.scss';
  327. view {
  328. box-sizing: border-box;
  329. }
  330. page {
  331. font-family: PingFang SC;
  332. }
  333. .bg-color-red {
  334. background-color: var(--view-theme) !important;
  335. }
  336. .syspadding {
  337. padding-top: var(--status-bar-height);
  338. }
  339. .flex {
  340. display: flex;
  341. }
  342. .uni-scroll-view::-webkit-scrollbar {
  343. /* 隐藏滚动条,但依旧具备可以滚动的功能 */
  344. display: none;
  345. }
  346. ::-webkit-scrollbar {
  347. width: 0;
  348. height: 0;
  349. color: transparent;
  350. }
  351. .uni-system-open-location .map-content.fix-position {
  352. height: 100vh;
  353. top: 0;
  354. bottom: 0;
  355. }
  356. </style>