|
|
@@ -0,0 +1,237 @@
|
|
|
+import { useAppStore } from '../../store/index.js'
|
|
|
+import {
|
|
|
+ encodeRedirectUrl
|
|
|
+} from '../../common/util.js'
|
|
|
+import Request from './request.js'
|
|
|
+import Config from '../config.js'
|
|
|
+import {
|
|
|
+ wechatAutoLogin
|
|
|
+} from '../api/user.js';
|
|
|
+let appStore = null;
|
|
|
+
|
|
|
+function getStore() {
|
|
|
+ if (!appStore)
|
|
|
+ appStore = useAppStore();
|
|
|
+ return appStore;
|
|
|
+}
|
|
|
+
|
|
|
+export const http = new Request();
|
|
|
+
|
|
|
+// 设置全局配置
|
|
|
+http.setConfig((config) => {
|
|
|
+ config.baseUrl = Config.baseUrl;
|
|
|
+ config.header = {
|
|
|
+ 'Content-Type': 'application/json;charset=UTF-8'
|
|
|
+ };
|
|
|
+
|
|
|
+ return config
|
|
|
+})
|
|
|
+
|
|
|
+
|
|
|
+// 请求之前拦截器
|
|
|
+http.interceptor.beforeRequest((config, cancel) => {
|
|
|
+ console.log('请求前拦截', config.url, config)
|
|
|
+ getStore();
|
|
|
+
|
|
|
+ // 登录校验
|
|
|
+ let accessToken = appStore.getAccessToken;
|
|
|
+ let activityId = appStore.getActivityId;
|
|
|
+ console.log('get.accessToken', accessToken, activityId);
|
|
|
+ if (!config.muteLogin) {
|
|
|
+ if (!accessToken) {
|
|
|
+ console.error('needLogin request');
|
|
|
+ cancel('登录过期,尝试重试'); // 取消请求
|
|
|
+ console.info('needLogin request', 'silentReload');
|
|
|
+ silentReload(config.curd === 'save');
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ config.url += '?token=' + accessToken;
|
|
|
+ // config.header['Cookie'] = 'token=' + accessToken; // uploadFile时,不能传header,慎用这种方式
|
|
|
+ }
|
|
|
+ config.url += (config.url.indexOf('?') === -1 ? '?' : '&') + 'activity_id=' + activityId;
|
|
|
+
|
|
|
+ return config;
|
|
|
+})
|
|
|
+
|
|
|
+// 请求之后拦截器
|
|
|
+http.interceptor.afterRequest((response) => {
|
|
|
+ console.log('请求后拦截', response);
|
|
|
+ getStore();
|
|
|
+
|
|
|
+ // 系统错误
|
|
|
+ if (response.statusCode === 500) {
|
|
|
+ console.error('系统错误');
|
|
|
+ setTimeout(() => {
|
|
|
+ uni.showToast({
|
|
|
+ title: '系统错误',
|
|
|
+ icon: 'none',
|
|
|
+ mask: true,
|
|
|
+ duration: 2000
|
|
|
+ })
|
|
|
+ }, 50);
|
|
|
+ return [response];
|
|
|
+ }
|
|
|
+
|
|
|
+ let data = response.data;
|
|
|
+
|
|
|
+ // 网络错误
|
|
|
+ if (!data) {
|
|
|
+ console.error('网络错误');
|
|
|
+ setTimeout(() => {
|
|
|
+ uni.showToast({
|
|
|
+ title: '网络错误',
|
|
|
+ icon: 'none',
|
|
|
+ mask: true,
|
|
|
+ duration: 2000
|
|
|
+ })
|
|
|
+ }, 50)
|
|
|
+ return [response];
|
|
|
+ }
|
|
|
+
|
|
|
+ if (typeof data === 'string') {
|
|
|
+ data = JSON.parse(data)
|
|
|
+ }
|
|
|
+
|
|
|
+ // 正常请求
|
|
|
+ if (data.code === 1) {
|
|
|
+ return [0, data.data || data.msg];
|
|
|
+ }
|
|
|
+
|
|
|
+ // 系统错误
|
|
|
+ console.warn('invalid request', data.msg);
|
|
|
+ switch (data.code) {
|
|
|
+ case 401: // 认证失败
|
|
|
+ console.error('needLogin 401');
|
|
|
+ appStore.delAccessToken(); // 删除token
|
|
|
+ console.info('needLogin 401', 'silentReload');
|
|
|
+ silentReload(response.config.curd === 'save');
|
|
|
+ break;
|
|
|
+ case 402: // 小程序自动登录失败
|
|
|
+ break;
|
|
|
+ default: // 错误提示
|
|
|
+ setTimeout(() => {
|
|
|
+ uni.showToast({
|
|
|
+ title: data.msg || '网络错误',
|
|
|
+ icon: 'none',
|
|
|
+ mask: true,
|
|
|
+ duration: 2000
|
|
|
+ })
|
|
|
+ }, 50)
|
|
|
+ if (data.code === 403) { // 活动无效
|
|
|
+ appStore.delAccessToken(); // 删除token
|
|
|
+ appStore.delActivityId(); // 删除活动ID
|
|
|
+ smartLogin('');
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ return [response];
|
|
|
+})
|
|
|
+
|
|
|
+/**
|
|
|
+ * 获取 login code
|
|
|
+ */
|
|
|
+export function silentReload(warning = false, redirectUrl = null) {
|
|
|
+ getStore();
|
|
|
+ if (warning) {
|
|
|
+ setTimeout(() => {
|
|
|
+ uni.showToast({
|
|
|
+ title: '鉴权失败,请重新操作',
|
|
|
+ icon: 'none',
|
|
|
+ mask: true,
|
|
|
+ duration: 2000
|
|
|
+ })
|
|
|
+ }, 50)
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ if (appStore.isLock('silentReload')) {
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ console.info('needLogin silentReload')
|
|
|
+ appStore.lock('silentReload')
|
|
|
+ if (redirectUrl === null) {
|
|
|
+ const pages = getCurrentPages()
|
|
|
+ const currentPage = pages[pages.length - 1]
|
|
|
+ redirectUrl = encodeRedirectUrl(currentPage.route, currentPage.options)
|
|
|
+ }
|
|
|
+
|
|
|
+ // #ifdef MP-WEIXIN
|
|
|
+ // 微信小程序自动登录
|
|
|
+ uni.reLaunch({
|
|
|
+ url: '/pages/third/yunexamine/pages/user/auto_login?redirect=' + redirectUrl
|
|
|
+ })
|
|
|
+ // #endif
|
|
|
+
|
|
|
+ // #ifdef H5
|
|
|
+ // 跳到登录页面或活动列表页
|
|
|
+ smartLogin(redirectUrl)
|
|
|
+ // #endif
|
|
|
+
|
|
|
+ return true
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 获取 login code
|
|
|
+ */
|
|
|
+export function smartLogin(redirectUrl, loginCheck, ignoreConfig) {
|
|
|
+ getStore();
|
|
|
+ if (appStore.getActivityId) { // 有缓存的活动ID
|
|
|
+ if (!loginCheck) {
|
|
|
+ // 直接到登录页面
|
|
|
+ uni.reLaunch({
|
|
|
+ url: '/pages/third/yunexamine/pages/user/login?redirect=' + redirectUrl
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ const globalConfig = appStore.getGlobalConfig
|
|
|
+
|
|
|
+ // 配置未正确加载
|
|
|
+ if (!globalConfig || Object.keys(globalConfig).length === 0) {
|
|
|
+ if (ignoreConfig) {
|
|
|
+ console.log('load config fail')
|
|
|
+ setTimeout(() => {
|
|
|
+ uni.reLaunch({
|
|
|
+ url: '/pages/third/yunexamine/pages/user/tips?cont=配置加载失败,请尝试重新进入'
|
|
|
+ })
|
|
|
+ }, 50)
|
|
|
+ } else {
|
|
|
+ console.log('wait to load config')
|
|
|
+ setTimeout(() => {
|
|
|
+ smartLogin(redirectUrl, false, true)
|
|
|
+ }, 800)
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 存在默认活动
|
|
|
+ if (globalConfig.default_activity) {
|
|
|
+ if (!loginCheck) {
|
|
|
+ // 直接到登录页面
|
|
|
+ uni.reLaunch({
|
|
|
+ url: '/pages/third/yunexamine/pages/user/login?redirect=' + redirectUrl
|
|
|
+ })
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 可以选择活动
|
|
|
+ if (globalConfig.select_activity) {
|
|
|
+ // 直接到活动列表
|
|
|
+ uni.reLaunch({
|
|
|
+ url: '/pages/third/yunexamine/pages/home/dashboard'
|
|
|
+ })
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 需要扫码进入
|
|
|
+ setTimeout(() => {
|
|
|
+ uni.reLaunch({
|
|
|
+ url: '/pages/third/yunexamine/pages/user/tips'
|
|
|
+ })
|
|
|
+ }, 50)
|
|
|
+
|
|
|
+ return true
|
|
|
+}
|