App.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. export default {
  15. globalData: {
  16. spid: 0,
  17. code: 0,
  18. isLogin: false,
  19. userInfo: {},
  20. MyMenus: [],
  21. globalData: false,
  22. isIframe: false,
  23. tabbarShow: true
  24. },
  25. onLaunch: function(option) {
  26. let that = this;
  27. // #ifdef MP
  28. if (HTTP_REQUEST_URL == '') {
  29. console.error(
  30. "请配置根目录下的config.js文件中的 'HTTP_REQUEST_URL'\n\n请修改开发者工具中【详情】->【AppID】改为自己的Appid\n\n请前往后台【小程序】->【小程序配置】填写自己的 appId and AppSecret"
  31. );
  32. return false;
  33. }
  34. if (option.query.hasOwnProperty('scene')) {
  35. switch (option.scene) {
  36. //扫描小程序码
  37. case 1047:
  38. let val = that.$util.getUrlParams(decodeURIComponent(option.query.scene));
  39. that.globalData.code = val.pid === undefined ? val : val.pid;
  40. break;
  41. //长按图片识别小程序码
  42. case 1048:
  43. that.globalData.code = option.query.scene;
  44. break;
  45. //手机相册选取小程序码
  46. case 1049:
  47. that.globalData.code = option.query.scene;
  48. break;
  49. //直接进入小程序
  50. case 1001:
  51. that.globalData.spid = option.query.scene;
  52. break;
  53. }
  54. }
  55. const updateManager = uni.getUpdateManager();
  56. updateManager.onCheckForUpdate(function(res) {
  57. // 请求完新版本信息的回调
  58. });
  59. updateManager.onUpdateReady(function() {
  60. uni.showModal({
  61. title: '更新提示',
  62. content: '新版本已经准备好,是否重启应用?',
  63. success: function(res) {
  64. if (res.confirm) {
  65. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  66. updateManager.applyUpdate();
  67. }
  68. }
  69. });
  70. });
  71. updateManager.onUpdateFailed(function() {
  72. return that.Tips({
  73. title: '新版本下载失败'
  74. });
  75. });
  76. // #endif
  77. getShopConfig().then(res => {
  78. this.$store.commit('SETPHONESTATUS', res.data.status);
  79. });
  80. // 获取导航高度;
  81. uni.getSystemInfo({
  82. success: function(res) {
  83. that.globalData.navHeight = res.statusBarHeight * (750 / res.windowWidth) + 91;
  84. }
  85. });
  86. // #ifdef H5
  87. uni.getSystemInfo({
  88. success(e) {
  89. /* 窗口宽度大于420px且不在PC页面且不在移动设备时跳转至 PC.html 页面 */
  90. if (e.windowWidth > 420 && !window.top.isPC && !/iOS|Android/i.test(e.system)) {
  91. window.location.pathname = '/static/html/pc.html';
  92. }
  93. }
  94. })
  95. if (option.query.hasOwnProperty('type') && option.query.type == "iframeMakkMinkkJuan") {
  96. this.globalData.isIframe = true;
  97. } else {
  98. this.globalData.isIframe = false;
  99. }
  100. // try {
  101. // // 静默授权code
  102. // var snsapiCode = uni.getStorageSync('snsapiCode');
  103. // } catch (e) {}
  104. if(window.location.pathname !== '/'){
  105. let snsapiBase = 'snsapi_base';
  106. let urlData = location.pathname + location.search;
  107. // if (snsapiCode) {
  108. // return
  109. // } else {
  110. if (!that.$store.getters.isLogin && Auth.isWeixin()) {
  111. const {
  112. code,
  113. state,
  114. scope
  115. } = option.query;
  116. if (code && code != uni.getStorageSync('snsapiCode') && location.pathname.indexOf(
  117. '/pages/users/wechat_login/index') === -1) {
  118. // 存储静默授权code
  119. uni.setStorageSync('snsapiCode', code);
  120. let spread = that.globalData.spid ? that.globalData.spid : '';
  121. silenceAuth({
  122. code: code,
  123. spread: that.$Cache.get('spread'),
  124. spid: that.globalData.code
  125. })
  126. .then(res => {
  127. uni.setStorageSync('snRouter', decodeURIComponent(decodeURIComponent(option.query
  128. .back_url)));
  129. if (res.data.key !== undefined && res.data.key) {
  130. this.$Cache.set('snsapiKey', res.data.key);
  131. } else {
  132. let time = res.data.expires_time - this.$Cache.time();
  133. this.$store.commit('LOGIN', {
  134. token: res.data.token,
  135. time: time
  136. });
  137. this.$store.commit('SETUID', res.data.userInfo.uid);
  138. this.$store.commit('UPDATE_USERINFO', res.data.userInfo);
  139. // location.href = decodeURIComponent(decodeURIComponent(option.query.back_url));
  140. }
  141. })
  142. .catch(res => {
  143. let url = ''
  144. if (option.query.back_url instanceof Array) {
  145. url = option.query.back_url[option.query.back_url.length - 1]
  146. } else {
  147. url = option.query.back_url
  148. }
  149. if (!that.$Cache.has('snsapiKey')) {
  150. if (location.pathname.indexOf('/pages/users/wechat_login/index') === -1) {
  151. Auth.oAuth(snsapiBase, url);
  152. }
  153. }
  154. });
  155. } else {
  156. if (!this.$Cache.has('snsapiKey')) {
  157. if (location.pathname.indexOf('/pages/users/wechat_login/index') === -1) {
  158. Auth.oAuth(snsapiBase, urlData);
  159. }
  160. }
  161. }
  162. } else {
  163. if (option.query.back_url) {
  164. // alert(uni.getStorageSync('snsapiCode'))
  165. // alert(uni.getStorageSync('snRouter'))
  166. location.href = uni.getStorageSync('snRouter')
  167. }
  168. }
  169. }
  170. // }
  171. // #endif
  172. // #ifdef MP
  173. // 小程序静默授权
  174. console.log(this.$store.getters.isLogin, 'this.$store');
  175. if (!this.$store.getters.isLogin) {
  176. Routine.getCode()
  177. .then(code => {
  178. this.silenceAuth(code);
  179. })
  180. .catch(res => {
  181. uni.hideLoading();
  182. });
  183. }
  184. // #endif
  185. },
  186. mounted() {},
  187. methods: {
  188. // 小程序静默授权
  189. silenceAuth(code) {
  190. let that = this;
  191. let spread = that.globalData.spid ? that.globalData.spid : '';
  192. silenceAuth({
  193. code: code,
  194. spread_spid: spread,
  195. spread_code: that.globalData.code
  196. })
  197. .then(res => {
  198. if (res.data.token !== undefined && res.data.token) {
  199. uni.hideLoading();
  200. let time = res.data.expires_time - this.$Cache.time();
  201. that.$store.commit('LOGIN', {
  202. token: res.data.token,
  203. time: time
  204. });
  205. that.$store.commit('SETUID', res.data.userInfo.uid);
  206. that.$store.commit('UPDATE_USERINFO', res.data.userInfo);
  207. }
  208. })
  209. .catch(res => {
  210. console.log(res);
  211. });
  212. }
  213. },
  214. onHide: function() {
  215. //console.log('App Hide')
  216. }
  217. };
  218. </script>
  219. <style>
  220. @import url("@/plugin/emoji-awesome/css/google.min.css");
  221. @import url('@/plugin/animate/animate.min.css');
  222. @import 'static/css/base.css';
  223. @import 'static/iconfont/iconfont.css';
  224. @import 'static/css/guildford.css';
  225. @import 'static/css/style.scss';
  226. /* #ifdef H5 */
  227. body::-webkit-scrollbar,
  228. html::-webkit-scrollbar {
  229. display: none;
  230. }
  231. /* #endif */
  232. view {
  233. box-sizing: border-box;
  234. }
  235. .bg-color-red {
  236. background-color: #e93323 !important;
  237. }
  238. .syspadding {
  239. padding-top: var(--status-bar-height);
  240. }
  241. .flex {
  242. display: flex;
  243. }
  244. .uni-scroll-view::-webkit-scrollbar {
  245. /* 隐藏滚动条,但依旧具备可以滚动的功能 */
  246. display: none;
  247. }
  248. ::-webkit-scrollbar {
  249. width: 0;
  250. height: 0;
  251. color: transparent;
  252. }
  253. </style>