App.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <script>
  2. import {
  3. checkLogin
  4. } from "./libs/login";
  5. import {
  6. HTTP_REQUEST_URL
  7. } from './config/app';
  8. import Auth from './libs/wechat.js';
  9. import Routine from './libs/routine.js';
  10. import Apps from './libs/apps.js';
  11. import {
  12. mapActions
  13. } from 'vuex'
  14. export default {
  15. globalData: {
  16. spid: 0,
  17. code: 0,
  18. isLogin: false,
  19. userInfo: {},
  20. MyMenus: [],
  21. windowHeight: 0,
  22. id: 0
  23. },
  24. onLaunch: function(option) {
  25. let that = this;
  26. // #ifdef H5
  27. uni.getSystemInfo({
  28. success: function(res) {
  29. // 首页没有title获取的整个页面的高度,里面的页面有原生标题要减掉就是视口的高度
  30. // 状态栏是动态的可以拿到 标题栏是固定写死的是44px
  31. let height = res.windowHeight - res.statusBarHeight - 44
  32. // #ifdef H5
  33. that.globalData.windowHeight = res.windowHeight + 'px'
  34. // #endif
  35. }
  36. });
  37. // #endif
  38. // #ifdef MP
  39. if (HTTP_REQUEST_URL == '') {
  40. console.error(
  41. "请配置根目录下的config.js文件中的 'HTTP_REQUEST_URL'\n\n请修改开发者工具中【详情】->【AppID】改为自己的Appid\n\n请前往后台【小程序】->【小程序配置】填写自己的 appId and AppSecret"
  42. );
  43. return false;
  44. }
  45. if (option.query.hasOwnProperty('scene')) {
  46. switch (option.scene) {
  47. case 1047: //扫描小程序码
  48. case 1048: //长按图片识别小程序码
  49. case 1049: //手机相册选取小程序码
  50. case 1001: //直接进入小程序
  51. let value = this.$util.getUrlParams(decodeURIComponent(option.query.scene));
  52. let values = value.split(',');
  53. if (values.length === 2) {
  54. let v1 = values[0].split(":");
  55. if (v1[0] === 'pid') {
  56. that.globalData.spid = v1[1];
  57. } else {
  58. that.globalData.id = v1[1];
  59. }
  60. let v2 = values[1].split(":");
  61. if (v2[0] === 'pid') {
  62. that.globalData.spid = v2[1];
  63. } else {
  64. that.globalData.id = v2[1];
  65. }
  66. } else {
  67. that.globalData.spid = values[0].split(":")[1];
  68. }
  69. break;
  70. }
  71. }
  72. // #endif
  73. // 获取导航高度;
  74. uni.getSystemInfo({
  75. success: function(res) {
  76. that.globalData.navHeight = res.statusBarHeight * (750 / res.windowWidth) + 91;
  77. }
  78. });
  79. // #ifdef MP
  80. let menuButtonInfo = uni.getMenuButtonBoundingClientRect();
  81. that.globalData.navH = menuButtonInfo.top * 2 + menuButtonInfo.height / 2;
  82. // #endif
  83. // #ifdef H5
  84. let snsapiBase = 'snsapi_base';
  85. let urlData = location.pathname + location.search;
  86. if (!that.$store.getters.isLogin && Auth.isWeixin()) {
  87. option.query = {
  88. code: this.getParam("code"),
  89. state: this.getParam("state"),
  90. scope: this.getParam("scope")
  91. }
  92. const {
  93. code,
  94. state,
  95. scope
  96. } = option.query;
  97. console.log('option: ', option)
  98. if (code && code != uni.getStorageSync('snsapiCode') && location.pathname.indexOf(
  99. '/pages/users/wechat_login/index') === -1) {
  100. // 存储静默授权code
  101. uni.setStorageSync('snsapiCode', code);
  102. let spread = that.globalData.spid ? that.globalData.spid : 0;
  103. Auth.auth(code, that.$Cache.get('spread'))
  104. .then(res => {
  105. uni.setStorageSync('snRouter', decodeURIComponent(decodeURIComponent(option.query
  106. .back_url)));
  107. if (res.type === 'register') {
  108. this.$Cache.set('snsapiKey', res.key);
  109. }
  110. if (res.type === 'login') {
  111. this.$store.commit('LOGIN', {
  112. token: res.token
  113. });
  114. this.$store.commit("SETUID", res.uid);
  115. location.replace(decodeURIComponent(decodeURIComponent(option.query.back_url)));
  116. }
  117. })
  118. .catch(error => {
  119. // this.$util.Tips({
  120. // title: error
  121. // });
  122. if (!this.$Cache.has('snsapiKey')) {
  123. if (location.pathname.indexOf('/pages/users/wechat_login/index') === -1) {
  124. Auth.oAuth(snsapiBase, option.query.back_url);
  125. }
  126. }
  127. });
  128. } else {
  129. if (!this.$Cache.has('snsapiKey')) {
  130. if (location.pathname.indexOf('/pages/users/wechat_login/index') === -1) {
  131. Auth.oAuth(snsapiBase, urlData);
  132. }
  133. }
  134. }
  135. } else {
  136. if (option.query.back_url) {
  137. location.replace(uni.getStorageSync('snRouter'));
  138. }
  139. }
  140. // #endif
  141. // #ifdef MP
  142. // 小程序静默授权
  143. if (!this.$store.getters.isLogin) {
  144. let spread = that.globalData.spid ? that.globalData.spid : 0;
  145. Routine.getCode()
  146. .then(code => {
  147. Routine.authUserInfo(code, {
  148. 'spread_spid': spread
  149. }).then(res => {
  150. // that.$store.commit('AuthorizeType', res.data.type);
  151. })
  152. })
  153. .catch(res => {
  154. uni.hideLoading();
  155. });
  156. }
  157. // #endif
  158. },
  159. async mounted() {
  160. if (this.$store.getters.isLogin && !this.$Cache.get('USER_INFO')) await this.$store.dispatch('USERINFO');
  161. },
  162. methods: {
  163. getParam(name) {
  164. let local = location.href;
  165. var reg = new RegExp("(^|\\?|&)" + name + "=([^&]*)(\\s|&|$)", "i");
  166. if (reg.test(local))
  167. return unescape(RegExp.$2.replace(/\+/g, " "));
  168. return "";
  169. }
  170. },
  171. onShow: function(option) {
  172. // #ifdef H5
  173. uni.getSystemInfo({
  174. success(e) {
  175. /* 窗口宽度大于420px且不在PC页面且不在移动设备时跳转至 PC.html 页面 */
  176. if (e.windowWidth > 420 && !window.top.isPC && !/iOS|Android/i.test(e.system)) {
  177. // window.location.pathname = 'https://java.crmeb.net/';
  178. /* 若你的项目未设置根目录(默认为 / 时),则使用下方代码 */
  179. window.location.pathname = '/static/html/pc.html';
  180. }
  181. }
  182. })
  183. // #endif
  184. },
  185. onHide: function() {
  186. //console.log('App Hide')
  187. }
  188. }
  189. </script>
  190. <style>
  191. @import url("@/plugin/animate/animate.min.css");
  192. @import 'static/css/base.css';
  193. @import 'static/iconfont/iconfont.css';
  194. @import 'static/css/guildford.css';
  195. @import 'static/css/style.scss';
  196. // #ifdef APP
  197. @import 'static/iconfont/iconfont.css';
  198. @import 'static/iconfont/iconfont-app.css';
  199. // #endif
  200. /* 条件编译,仅在H5平台生效 */
  201. // #ifdef H5
  202. body::-webkit-scrollbar,
  203. html::-webkit-scrollbar {
  204. display: none;
  205. }
  206. // #endif
  207. view {
  208. box-sizing: border-box;
  209. }
  210. .bg-color-red {
  211. background-color: #E93323 !important;
  212. }
  213. .syspadding {
  214. padding-top: var(--status-bar-height);
  215. }
  216. .flex {
  217. display: flex;
  218. }
  219. .uni-scroll-view::-webkit-scrollbar {
  220. /* 隐藏滚动条,但依旧具备可以滚动的功能 */
  221. display: none
  222. }
  223. ::-webkit-scrollbar {
  224. width: 0;
  225. height: 0;
  226. color: transparent;
  227. }
  228. </style>