App.vue 12 KB

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