123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- import {baseApiUrl} from './config.js'
- import {apiWechatLogin} from '@/api/user'
- import CryptoJS from './crypto'
- import store from '@/store/index'
- const logger = require('./log.js')
- const ENV=uni.getAccountInfoSync().miniProgram
- // 请求错误消息提示
- const showError=error=>{
- let errMsg=''
- switch(error.code){
- case 400:
- errMsg=error.msg;
- break;
- default:
- errMsg=error.msg;
- break;
- }
- setTimeout(()=>{
- uni.showToast({
- title:errMsg,
- icon:'none'
- })
- },0)
-
- }
- // 微信登录
- const wechatLogin=()=>{
- return new Promise((resolve,reject)=>{
- uni.login({
- provider: 'weixin',
- success: function (loginRes) {
- apiWechatLogin({code:loginRes.code}).then(res=>{
- if(res.code===200){
- store.commit('setToken', res.data.authorization)
- store.dispatch('getUserInfo')
- resolve(res.data)
- }
- })
- },
- fail:function(loginErr){
- uni.showToast({
- title:"微信登录失败",
- icon:"none"
- })
- }
- })
- })
-
- }
- let requestList=[]//存放token失效时请求队列
- let isRefreshing=false//是否正在刷新token
- /**
- * 刷新token
- */
- const refreshToken=async (url,params,method,resolve)=>{
- requestList.push(()=>{resolve(http(url,params,method))})
- if(!isRefreshing){
- isRefreshing=true
- const wechatLoginRes=await wechatLogin()
- console.log(wechatLoginRes);
- // if(!wechatLoginRes.is_bind){
- // uni.reLaunch({
- // url:'/pages/login'
- // })
- // isRefreshing=false
- // return
- // }
- // 重新请求队列
- requestList.map(MT=>{MT()})
- requestList=[]
- isRefreshing=false
- }
- }
- let LOADINGCOUNT = 0;// 请求数
- const http=(url,params,method)=>{
- // 设置loading
- if(url!='/public/get_share_poster'){
- if (LOADINGCOUNT === 0) {
- uni.showLoading({
- title:'加载中...'
- })
- }
- LOADINGCOUNT++;
- }
-
-
- return new Promise((resolve,reject)=>{
- uni.request({
- url:baseApiUrl+url,
- data:params,
- method:method,
- header:{
- Authorization:store.state.user.token,
- version:'yb11.9',
- thirdCode:store.state.user.thirdCode
- },
- success(e) {
- // 接口404
- if(e.statusCode===404){
- setTimeout(()=>{
- uni.showToast({
- title:'network:404',
- icon:'none'
- })
- },0)
- return
- }
- let res
- if(ENV.envVersion==='release'){
- res=JSON.parse(CryptoJS.Des3Decrypt(e.data));//解密
- }else{
- res=e.data
- }
- if(res.code!==200&&res.code!==403&&res.code!==4001&&res.code!==401){
- showError(res)
- }
- // 401 token失效
- if(res.code===401){
- refreshToken(url,params,method,resolve)
- return
- }
-
- resolve(res)
- },
- fail(error) {
- console.log(error);
- if(ENV.envVersion==='release'){
- logger.error('接口请求fail',error)
- }
- setTimeout(()=>{
- uni.showToast({
- title:'网络异常,稍后重试!',
- icon:'none'
- })
- },0)
- },
- complete() {
- // 关闭loading
- LOADINGCOUNT--;
- if (LOADINGCOUNT === 0) {
- uni.hideLoading()
- }
- }
- })
- })
- }
- // get 请求
- export const httpGet=(url,params)=>{
- return http(url,params,'GET')
- }
- // post 请求
- export const httpPost=(url,params)=>{
- return http(url,params,'POST')
- }
|