App.vue 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <script>
  2. import { checkLogin } from './libs/login';
  3. import { HTTP_REQUEST_URL } from './config/app';
  4. import { getShopConfig, silenceAuth } from '@/api/public';
  5. import Auth from './libs/wechat.js';
  6. import Routine from './libs/routine.js';
  7. export default {
  8. globalData: {
  9. spid: 0,
  10. code: 0,
  11. isLogin: false,
  12. userInfo: {},
  13. MyMenus: [],
  14. globalData: false,
  15. isIframe: false,
  16. tabbarShow: true,
  17. isWsOpen:false,
  18. },
  19. onLaunch: function(option) {
  20. let that = this;
  21. // #ifdef MP
  22. if (HTTP_REQUEST_URL == '') {
  23. console.error(
  24. "请配置根目录下的config.js文件中的 'HTTP_REQUEST_URL'\n\n请修改开发者工具中【详情】->【AppID】改为自己的Appid\n\n请前往后台【小程序】->【小程序配置】填写自己的 appId and AppSecret"
  25. );
  26. return false;
  27. }
  28. if (option.query.hasOwnProperty('scene')) {
  29. switch (option.scene) {
  30. //扫描小程序码
  31. case 1047:
  32. let val = that.$util.getUrlParams(decodeURIComponent(option.query.scene));
  33. that.globalData.code = val.pid === undefined ? val : val.pid;
  34. break;
  35. //长按图片识别小程序码
  36. case 1048:
  37. that.globalData.code = option.query.scene;
  38. break;
  39. //手机相册选取小程序码
  40. case 1049:
  41. that.globalData.code = option.query.scene;
  42. break;
  43. //直接进入小程序
  44. case 1001:
  45. that.globalData.spid = option.query.scene;
  46. break;
  47. }
  48. }
  49. // #endif
  50. getShopConfig().then(res => {
  51. this.$store.commit('SETPHONESTATUS', res.data.status);
  52. });
  53. // 获取导航高度;
  54. uni.getSystemInfo({
  55. success: function(res) {
  56. that.globalData.navHeight = res.statusBarHeight * (750 / res.windowWidth) + 91;
  57. }
  58. });
  59. // #ifdef H5
  60. if (option.query.hasOwnProperty('type') && option.query.type=="crmebDiy") {
  61. this.globalData.isIframe = true;
  62. } else {
  63. this.globalData.isIframe = false;
  64. }
  65. // try {
  66. // // 静默授权code
  67. // var snsapiCode = uni.getStorageSync('snsapiCode');
  68. // } catch (e) {}
  69. let snsapiBase = 'snsapi_base';
  70. let urlData = location.pathname + location.search;
  71. // if (snsapiCode) {
  72. // return
  73. // } else {
  74. if (!that.$store.getters.isLogin && Auth.isWeixin()) {
  75. const { code, state, scope } = option.query;
  76. if (code && location.pathname.indexOf('/pages/users/wechat_login/index') === -1) {
  77. // 存储静默授权code
  78. uni.setStorageSync('snsapiCode', code);
  79. let spread = that.globalData.spid ? that.globalData.spid : '';
  80. silenceAuth({
  81. code: code,
  82. spread: that.$Cache.get('spread'),
  83. spid: that.globalData.code
  84. })
  85. .then(res => {
  86. uni.setStorageSync('snRouter', decodeURIComponent(decodeURIComponent(option.query.back_url)));
  87. if (res.data.key !== undefined && res.data.key) {
  88. this.$Cache.set('snsapiKey', res.data.key);
  89. } else {
  90. let time = res.data.expires_time - this.$Cache.time();
  91. this.$store.commit('LOGIN', {
  92. token: res.data.token,
  93. time: time
  94. });
  95. this.$store.commit('SETUID', res.data.userInfo.uid);
  96. this.$store.commit('UPDATE_USERINFO', res.data.userInfo);
  97. location.href = decodeURIComponent(decodeURIComponent(option.query.back_url));
  98. }
  99. })
  100. .catch(res => {
  101. this.$util.Tips({
  102. title: error
  103. });
  104. });
  105. } else {
  106. if (!this.$Cache.has('snsapiKey')) {
  107. if (location.pathname.indexOf('/pages/users/wechat_login/index') === -1) {
  108. Auth.oAuth(snsapiBase, urlData);
  109. }
  110. }
  111. }
  112. } else {
  113. if(option.query.back_url){
  114. // alert(uni.getStorageSync('snsapiCode'))
  115. // alert(uni.getStorageSync('snRouter'))
  116. location.href = uni.getStorageSync('snRouter')
  117. }
  118. }
  119. // }
  120. // #endif
  121. // #ifdef MP
  122. // 小程序静默授权
  123. console.log(this.$store.getters.isLogin, 'this.$store');
  124. if (!this.$store.getters.isLogin) {
  125. Routine.getCode()
  126. .then(code => {
  127. this.silenceAuth(code);
  128. })
  129. .catch(res => {
  130. uni.hideLoading();
  131. });
  132. }
  133. // #endif
  134. },
  135. mounted() {},
  136. methods: {
  137. // 小程序静默授权
  138. silenceAuth(code) {
  139. let that = this;
  140. let spread = that.globalData.spid ? that.globalData.spid : '';
  141. silenceAuth({
  142. code: code,
  143. spread_spid: spread,
  144. spread_code: that.globalData.code
  145. })
  146. .then(res => {
  147. if (res.data.token !== undefined && res.data.token) {
  148. uni.hideLoading();
  149. let time = res.data.expires_time - this.$Cache.time();
  150. that.$store.commit('LOGIN', {
  151. token: res.data.token,
  152. time: time
  153. });
  154. that.$store.commit('SETUID', res.data.userInfo.uid);
  155. that.$store.commit('UPDATE_USERINFO', res.data.userInfo);
  156. }
  157. })
  158. .catch(res => {
  159. console.log(res);
  160. });
  161. }
  162. },
  163. onHide: function() {
  164. //console.log('App Hide')
  165. }
  166. };
  167. </script>
  168. <style>
  169. @import url("@/plugin/emoji-awesome/css/google.min.css");
  170. @import url('@/plugin/animate/animate.min.css');
  171. @import 'static/css/base.css';
  172. @import 'static/iconfont/iconfont.css';
  173. @import 'static/css/guildford.css';
  174. @import 'static/css/style.scss';
  175. view {
  176. box-sizing: border-box;
  177. }
  178. .bg-color-red {
  179. background-color: #e93323 !important;
  180. }
  181. .syspadding {
  182. padding-top: var(--status-bar-height);
  183. }
  184. .flex {
  185. display: flex;
  186. }
  187. .uni-scroll-view::-webkit-scrollbar {
  188. /* 隐藏滚动条,但依旧具备可以滚动的功能 */
  189. display: none;
  190. }
  191. ::-webkit-scrollbar {
  192. width: 0;
  193. height: 0;
  194. color: transparent;
  195. }
  196. </style>