整合小程序库
This commit is contained in:
		
							
								
								
									
										29
									
								
								src/components/utils/http.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								src/components/utils/http.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,29 @@
 | 
			
		||||
import axios from 'axios'
 | 
			
		||||
import adapter from 'axios-miniprogram-adapter'
 | 
			
		||||
 | 
			
		||||
const instance = axios.create({
 | 
			
		||||
  timeout: 600000,
 | 
			
		||||
  withCredentials: true,
 | 
			
		||||
  adapter
 | 
			
		||||
})
 | 
			
		||||
const getToken = () => {
 | 
			
		||||
  let vuex = uni.getStorageSync("vuex")
 | 
			
		||||
  return !!vuex ? JSON.parse(vuex).token : null
 | 
			
		||||
}
 | 
			
		||||
const source = axios.CancelToken.source();
 | 
			
		||||
instance.interceptors.request.use(config => {
 | 
			
		||||
  if (config.withoutToken) {
 | 
			
		||||
    return config
 | 
			
		||||
  } else if (getToken()) {
 | 
			
		||||
    config.headers["Authorization"] = getToken()
 | 
			
		||||
  } else {
 | 
			
		||||
    config.cancelToken = source.token
 | 
			
		||||
    source.cancel("用户未验证,取消请求:" + config.url)
 | 
			
		||||
  }
 | 
			
		||||
  return config
 | 
			
		||||
}, err => {
 | 
			
		||||
  console.error(err)
 | 
			
		||||
  return Promise.reject(err)
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
export default instance
 | 
			
		||||
							
								
								
									
										66
									
								
								src/components/utils/modules.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										66
									
								
								src/components/utils/modules.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,66 @@
 | 
			
		||||
import http from "./http";
 | 
			
		||||
import Vue from "vue";
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 用户登录信息
 | 
			
		||||
 */
 | 
			
		||||
export const user = {
 | 
			
		||||
  state: () => ({}),
 | 
			
		||||
  mutations: {
 | 
			
		||||
    setUser(state, user) {
 | 
			
		||||
      for (const key in user) {
 | 
			
		||||
        Vue.set(state, key, user[key])
 | 
			
		||||
      }
 | 
			
		||||
    },
 | 
			
		||||
  },
 | 
			
		||||
  actions: {
 | 
			
		||||
    getUserInfo({commit}) {
 | 
			
		||||
      //获取企业微信后台账号信息
 | 
			
		||||
      //党员认证状态 partyStatusForWX:0、未认证 1、认证中 2、已认证
 | 
			
		||||
      return http.post("/app/appwechatuser/check").then(res => {
 | 
			
		||||
        if (res?.code == 0) {
 | 
			
		||||
          commit('setUser', res.data)
 | 
			
		||||
          return Promise.all([])
 | 
			
		||||
        }
 | 
			
		||||
      })
 | 
			
		||||
    },
 | 
			
		||||
    getCode({dispatch}, count = 0) {
 | 
			
		||||
      if (count > 3) {
 | 
			
		||||
        return Promise.reject("无法获取code")
 | 
			
		||||
      } else return new Promise((resolve, reject) => {
 | 
			
		||||
        uni.login({
 | 
			
		||||
          success: res => {
 | 
			
		||||
            if (res?.code) {
 | 
			
		||||
              resolve(res.code);
 | 
			
		||||
            } else {
 | 
			
		||||
              reject(res);
 | 
			
		||||
            }
 | 
			
		||||
          },
 | 
			
		||||
          fail: () => resolve(dispatch("getCode", ++count))
 | 
			
		||||
        })
 | 
			
		||||
      })
 | 
			
		||||
    },
 | 
			
		||||
    getToken({commit}, code) {
 | 
			
		||||
      if (code) {
 | 
			
		||||
        return http.post("/auth/wechat-con/token", {code}, {
 | 
			
		||||
          headers: {"Authorization": "Basic d2VjaGF0OndlY2hhdA=="},
 | 
			
		||||
          withoutToken: true
 | 
			
		||||
        }).then(res => {
 | 
			
		||||
          if (res?.access_token) {
 | 
			
		||||
            const token = [res?.token_type, res?.access_token].join(" ").trim()
 | 
			
		||||
            commit("setToken", token)
 | 
			
		||||
            return token
 | 
			
		||||
          } else {
 | 
			
		||||
            uni.showToast({title: res?.msg})
 | 
			
		||||
            return Promise.resolve(res?.msg)
 | 
			
		||||
          }
 | 
			
		||||
        })
 | 
			
		||||
      } else return Promise.reject("缺少登录code")
 | 
			
		||||
    },
 | 
			
		||||
    autoLogin({dispatch}, count = 0) {
 | 
			
		||||
      if (count <= 3) {
 | 
			
		||||
        return dispatch("getCode").then(code => dispatch("getToken", code).catch(() => dispatch("autoLogin", ++count)))
 | 
			
		||||
      } else return Promise.reject("登录失败,请联系管理员")
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
@@ -9,13 +9,13 @@ import 'uview-ui/theme.scss'
 | 
			
		||||
 | 
			
		||||
Vue.use(vui)
 | 
			
		||||
Vue.config.productionTip = false
 | 
			
		||||
Vue.prototype.$instance = axios.instance
 | 
			
		||||
Vue.prototype.$instance = axios
 | 
			
		||||
Vue.prototype.$areaId = config.areaId;
 | 
			
		||||
Vue.prototype.$areaName = config.areaName;
 | 
			
		||||
Vue.prototype.$cdn = 'https://cdn.cunwuyun.cn/'
 | 
			
		||||
App.mpType = 'app'
 | 
			
		||||
Object.keys(utils).map(e => Vue.prototype[e] = utils[e])
 | 
			
		||||
utils.$dict.init(axios.instance)
 | 
			
		||||
utils.$dict.init(axios)
 | 
			
		||||
 | 
			
		||||
const app = new Vue({
 | 
			
		||||
  store,
 | 
			
		||||
 
 | 
			
		||||
@@ -30,11 +30,10 @@
 | 
			
		||||
        <div class="appName fill" v-text="app.name"/>
 | 
			
		||||
      </div>
 | 
			
		||||
    </div>
 | 
			
		||||
    <AiLogin ref="login" @success="getAuth()"/>
 | 
			
		||||
  </div>
 | 
			
		||||
</template>
 | 
			
		||||
<script>
 | 
			
		||||
import {mapMutations, mapState} from 'vuex'
 | 
			
		||||
import {mapActions, mapState} from 'vuex'
 | 
			
		||||
 | 
			
		||||
export default {
 | 
			
		||||
  name: "home",
 | 
			
		||||
@@ -57,18 +56,18 @@ export default {
 | 
			
		||||
    },
 | 
			
		||||
  },
 | 
			
		||||
  methods: {
 | 
			
		||||
    ...mapMutations(['getUserInfo']),
 | 
			
		||||
    ...mapActions(['getUserInfo', 'autoLogin']),
 | 
			
		||||
    handleLogin() {
 | 
			
		||||
      if (!this.token) {
 | 
			
		||||
        this.$refs.login.show();
 | 
			
		||||
      }
 | 
			
		||||
        this.autoLogin();
 | 
			
		||||
      } else this.$u.toast("已登录,无需重新登录!")
 | 
			
		||||
    },
 | 
			
		||||
    handleGotoApp(app) {
 | 
			
		||||
      uni.navigateTo({url: `${app.path}`})
 | 
			
		||||
    },
 | 
			
		||||
    getApps() {
 | 
			
		||||
      this.$instance.post("/node/wechatapps/list", null, {
 | 
			
		||||
        baseURL: "http://192.168.1.87:12525", params: {size: 999, type: 'mp'}
 | 
			
		||||
        baseURL: "http://192.168.1.87:12525", params: {size: 999, type: 'mp'}, withoutToken: true
 | 
			
		||||
      }).then(res => {
 | 
			
		||||
        if (res?.data) {
 | 
			
		||||
          this.apps = res.data.records.map(e => {
 | 
			
		||||
@@ -80,14 +79,13 @@ export default {
 | 
			
		||||
        }
 | 
			
		||||
      })
 | 
			
		||||
    },
 | 
			
		||||
    getAuth() {
 | 
			
		||||
      this.$nextTick(() => {
 | 
			
		||||
        this.getUserInfo()
 | 
			
		||||
      })
 | 
			
		||||
    }
 | 
			
		||||
  },
 | 
			
		||||
  onShow() {
 | 
			
		||||
    this.getAuth();
 | 
			
		||||
    if (this.token) {
 | 
			
		||||
      this.getUserInfo();
 | 
			
		||||
    } else {
 | 
			
		||||
      this.autoLogin().then(() => this.getUserInfo())
 | 
			
		||||
    }
 | 
			
		||||
    this.getApps()
 | 
			
		||||
  },
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,97 +1,25 @@
 | 
			
		||||
import Vue from 'vue'
 | 
			
		||||
import Vuex from 'vuex'
 | 
			
		||||
import perState from 'vuex-persistedstate'
 | 
			
		||||
import http from "../utils/axios";
 | 
			
		||||
import * as modules from "dvcp-wui/utils/modules"
 | 
			
		||||
 | 
			
		||||
Vue.use(Vuex)
 | 
			
		||||
 | 
			
		||||
const store = new Vuex.Store({
 | 
			
		||||
  state: {
 | 
			
		||||
    /**
 | 
			
		||||
     * 是否需要强制登录
 | 
			
		||||
     */
 | 
			
		||||
    forcedLogin: false,
 | 
			
		||||
    token: "",
 | 
			
		||||
    userName: "",
 | 
			
		||||
    user: {
 | 
			
		||||
      username: "",
 | 
			
		||||
      avatarUrl: "",
 | 
			
		||||
      nickName: "",
 | 
			
		||||
      gender: "",
 | 
			
		||||
      token: "",
 | 
			
		||||
      phoneNumber: ''
 | 
			
		||||
    },
 | 
			
		||||
    global: {
 | 
			
		||||
      areaId: ""
 | 
			
		||||
    }
 | 
			
		||||
    token: ""
 | 
			
		||||
  },
 | 
			
		||||
  mutations: {
 | 
			
		||||
    getToken(state, params) {
 | 
			
		||||
      if (params?.code) {
 | 
			
		||||
        http.instance.post("/auth/wechat-con/token", params, {
 | 
			
		||||
          headers: {"Authorization": "Basic d2VjaGF0OndlY2hhdA=="},
 | 
			
		||||
          withoutToken: true
 | 
			
		||||
        }).then(res => {
 | 
			
		||||
          if (res?.access_token) {
 | 
			
		||||
            store.commit("setToken", res.token_type + ' ' + res.access_token)
 | 
			
		||||
            params?.then?.(true)
 | 
			
		||||
          } else {
 | 
			
		||||
            uni.showToast({title: res?.msg})
 | 
			
		||||
            params?.then?.(false)
 | 
			
		||||
          }
 | 
			
		||||
        }).finally(err => console.error(err))
 | 
			
		||||
      } else params?.then(false)
 | 
			
		||||
    },
 | 
			
		||||
    setToken(state, token) {
 | 
			
		||||
      state.token = token
 | 
			
		||||
    },
 | 
			
		||||
    setUserInfo(state, user) {
 | 
			
		||||
      state.user = user
 | 
			
		||||
    },
 | 
			
		||||
    getUserInfo(state, cb) {
 | 
			
		||||
      http.instance.post("/app/appwechatuser/check").then(res => {
 | 
			
		||||
        if (res?.data) {
 | 
			
		||||
          store.commit("setUserInfo", res.data)
 | 
			
		||||
          cb && cb(true)
 | 
			
		||||
        } else {
 | 
			
		||||
          cb && cb(false)
 | 
			
		||||
        }
 | 
			
		||||
      }).catch(err => {
 | 
			
		||||
        console.error(err)
 | 
			
		||||
        cb && cb(false)
 | 
			
		||||
      })
 | 
			
		||||
    },
 | 
			
		||||
    login(state, ploy) {
 | 
			
		||||
      state.userName = ploy.userName || '新用户';
 | 
			
		||||
      state.token = ploy.token
 | 
			
		||||
    },
 | 
			
		||||
    logout(state) {
 | 
			
		||||
      state.userName = "";
 | 
			
		||||
    logout(state, showToast) {
 | 
			
		||||
      state.token = ""
 | 
			
		||||
      uni.setStorageSync('userInfo', {})
 | 
			
		||||
      uni.setStorageSync('vuex', {})
 | 
			
		||||
      state.user = {
 | 
			
		||||
        username: "",
 | 
			
		||||
        avatarUrl: "",
 | 
			
		||||
        nickName: "",
 | 
			
		||||
        gender: "",
 | 
			
		||||
        token: "",
 | 
			
		||||
        phoneNumber: ''
 | 
			
		||||
      }
 | 
			
		||||
      uni.showToast({title: `登录失效,请重新登录`, duration: 2000, icon: 'none'})
 | 
			
		||||
      state.user = {}
 | 
			
		||||
      showToast && uni.showToast({title: `登录失效,请重新登录`, duration: 2000, icon: 'none'})
 | 
			
		||||
    },
 | 
			
		||||
    setUser(state, info) {
 | 
			
		||||
      state.user = info
 | 
			
		||||
    },
 | 
			
		||||
    getUser(state, ploy) {
 | 
			
		||||
      state.user.nickName = ploy.nickName;
 | 
			
		||||
      state.user.avatarUrl = ploy.avatarUrl;
 | 
			
		||||
      state.user.gender = ploy.gender;
 | 
			
		||||
    },
 | 
			
		||||
    getUserPhoneNumber(state, ploy) {
 | 
			
		||||
      state.user.phoneNumber = ploy.phoneNumber;
 | 
			
		||||
    }
 | 
			
		||||
  },
 | 
			
		||||
  modules,
 | 
			
		||||
  plugins: [perState({
 | 
			
		||||
    storage: {
 | 
			
		||||
      getItem: key => uni.getStorageSync(key),
 | 
			
		||||
 
 | 
			
		||||
@@ -1,52 +1,26 @@
 | 
			
		||||
import config from '@/utils/config';
 | 
			
		||||
import setting from './config';
 | 
			
		||||
import util from 'dvcp-wui/utils/util';
 | 
			
		||||
import store from "../store"
 | 
			
		||||
 | 
			
		||||
const Fly = require('flyio/dist/npm/wx');
 | 
			
		||||
let instance = new Fly();
 | 
			
		||||
const baseURL = config.baseUrl;
 | 
			
		||||
let count = 0;
 | 
			
		||||
instance.config.timeout = 50000;
 | 
			
		||||
instance.config.baseURL = baseURL;
 | 
			
		||||
 | 
			
		||||
const getToken = () => {
 | 
			
		||||
  if (store.state.token) return store.state.token;
 | 
			
		||||
  else return '';
 | 
			
		||||
};
 | 
			
		||||
import instance from "dvcp-wui/utils/http";
 | 
			
		||||
 | 
			
		||||
instance.interceptors.request.use((config) => {
 | 
			
		||||
  if (!config.withoutToken) {
 | 
			
		||||
    config.headers['Authorization'] = getToken();
 | 
			
		||||
  }
 | 
			
		||||
  config.baseURL = config.baseURL || setting.baseUrl
 | 
			
		||||
  return config;
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
instance.interceptors.response.use(
 | 
			
		||||
    function (response) {
 | 
			
		||||
      util.$hideLoading();
 | 
			
		||||
      if (response.data.code === 1) {
 | 
			
		||||
        util.$toast({title: response.data.msg, duration: 3000});
 | 
			
		||||
      } else if (response.data.code == 2) {
 | 
			
		||||
        //首次静默登录异常不做任何返回
 | 
			
		||||
      } else if (response.data.code == 401) {
 | 
			
		||||
        if (count > 3) {
 | 
			
		||||
          return;
 | 
			
		||||
        }
 | 
			
		||||
        count++;
 | 
			
		||||
        return util.$getLoginCode().then((res) => {
 | 
			
		||||
          store.commit("getToken", {code: res.code})
 | 
			
		||||
        });
 | 
			
		||||
      } else {
 | 
			
		||||
        count = 0;
 | 
			
		||||
        return response.data;
 | 
			
		||||
      }
 | 
			
		||||
    },
 | 
			
		||||
    function (err) {
 | 
			
		||||
      console.log(err);
 | 
			
		||||
    }
 | 
			
		||||
);
 | 
			
		||||
instance.interceptors.response.use(response => {
 | 
			
		||||
  util.$hideLoading();
 | 
			
		||||
  if (response.data.code === 1) {
 | 
			
		||||
    util.$toast({title: response.data.msg, duration: 3000});
 | 
			
		||||
  } else if (response.data.code == 2) {
 | 
			
		||||
    //首次静默登录异常不做任何返回
 | 
			
		||||
  } else if (response.data.code == 401) {
 | 
			
		||||
    return store.commit('logout')
 | 
			
		||||
  } else {
 | 
			
		||||
    return response.data;
 | 
			
		||||
  }
 | 
			
		||||
}, err => {
 | 
			
		||||
  console.error(err);
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
export default {
 | 
			
		||||
  instance,
 | 
			
		||||
  baseURL
 | 
			
		||||
};
 | 
			
		||||
export default instance
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user