123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- import {
- publicUrl
- } from './config'
- export const request = (options) => {
- return new Promise((resolve, reject) => {
- let Token = uni.getStorageSync('token')
- let TenantId = uni.getStorageSync('tenantId')
- let userId = uni.getStorageSync('userId')
- const isToken = (options.isToken === false)
- const isLogin = options.isLogin
- console.log("开始提交信息、", options)
- console.log("获取到的tokey信息、", Token)
- console.log("获取到的租户信息、", TenantId)
- console.log("获取到的用户信息、", userId)
- console.log("是否拼接请求地址", options.isAddress)
- // #ifdef APP-PLUS || MP-WEIXIN
- console.log("当前URL、", (publicUrl + options.url))
- // #endif
- // #ifndef APP-PLUS || MP-WEIXIN
- console.log("当前URL、", ( options.url))
- // #endif
- // if (options.isAddress) {
- // options.url = '/admin' + options.url;
-
- // }
- let ContentType = '';
- let Authorization = '';
- if (isToken) {
- if (isLogin) {
- ContentType = 'application/x-www-form-urlencoded;charset=utf-8',
- Authorization = 'Basic cGlnOnBpZw=='
- } else {
- ContentType = 'application/json;charset=utf-8',
- Authorization = 'Basic cGlnOnBpZw=='
- }
- } else {
- if (isLogin) {
- ContentType = 'application/x-www-form-urlencoded;charset=utf-8',
- Authorization = 'Bearer ' + Token
- } else {
- ContentType = 'application/json;charset=utf-8',
- Authorization = 'Bearer ' + Token
- }
- }
- let header = {
- 'Content-Type': ContentType,
- 'Authorization': Authorization,
- 'TENANT-ID': TenantId || ''
- }
- if (Token || isToken) {
- uni.request({
- // #ifdef APP-PLUS || MP-WEIXIN
- url: publicUrl + options.url,
- // #endif
- // #ifndef APP-PLUS || MP-WEIXIN
- url: options.url,
- // #endif
- method: options.method || 'GET',
- data: options.data || {},
- dataType: 'json',
- header: header,
- success: (res) => {
- const status = Number(res.statusCode) || 200
- console.log("返回状态", status);
- console.log("返回值", res);
- if (status == 424 || status == 401) {
- //#ifdef MP-WEIXIN
- wx.login({
- success(res) {
- console.log("开始准备微信登录", res.code);
- wx.request({
- url: publicUrl +
- '/auth/mobile/token/social?grant_type=mobil&mobile=E_MINI@' +
- res.code + '&code=' + res.code,
- method: 'post',
- header: {
- Authorization: 'Basic cGlnOnBpZw==',
- },
- success(res) {
- console.log("微信登录返回成功、", res);
- if (res.statusCode === 401 || res.statusCode === 404) {
- console.log(res.data);
- uni.showModal({
- // title: '提示',
- content: '登录【'+res.statusCode +'】'+(res.statusCode === 401?'已过期':'无法访问接口')+'请重新登录',
- showCancel: true,
- success: function (res) {
- if (res.confirm) {
- uni.removeStorageSync('token')
- uni.removeStorageSync('tenantId')
- uni.removeStorageSync('userId')
- uni.removeStorageSync('userName')
- uni.removeStorageSync('userInfo')
- uni.removeStorageSync('userType')
- uni.removeStorageSync('tenantInfo')
- uni.removeStorageSync('enterpriseInfo')
- uni.reLaunch({
- url: "/pages/login/login"
- })
- console.log('用户点击确定');
- } else if (res.cancel) {
- console.log('用户点击取消');
- }
- }
- });
- } else if (res.statusCode === 200) {
- console.log("微信登录后信息、", res);
- uni.setStorageSync('userId', res
- .data.user_info.id)
- uni.setStorageSync('userName', res
- .data.user_info.username)
- uni.setStorageSync('token', res.data
- .access_token)
- } else if (res.statusCode != 200 || res
- .data.code === 1) {
- uni.showToast({
- title: res.data.msg,
- icon: 'none',
- duration: 2000
- })
- }
- },
- });
- },
- });
- // #endif
- // #ifndef MP-WEIXIN
- uni.showModal({
- // title: '提示',
- content: '登录【401】已过期,请重新登录',
- showCancel: true,
- success: function (res) {
- if (res.confirm) {
- uni.removeStorageSync('token')
- uni.removeStorageSync('tenantId')
- uni.removeStorageSync('userId')
- uni.removeStorageSync('userName')
- uni.removeStorageSync('userInfo')
- uni.removeStorageSync('userType')
- uni.removeStorageSync('tenantInfo')
- uni.reLaunch({
- url: "/pages/login/login"
- })
- console.log('用户点击确定');
- } else if (res.cancel) {
- console.log('用户点击取消');
- }
- }
- });
- // #endif
- } else if (status != 200) {
- uni.showModal({
- content: res.data.msg || "接口【" + status + "】,没有返回信息",
- showCancel: false,
- })
- }
- resolve(res)
- },
- fail: (err) => {
- console.log(err)
- uni.showToast({
- title: '请求接口失败',
- icon: 'none',
- duration: 2000
- })
- reject(err)
- }
- })
- } else {
- uni.showModal({
- // title: '提示',
- content: '暂未登录,请登录',
- showCancel: true,
- success: function (res) {
- if (res.confirm) {
- uni.removeStorageSync('token')
- uni.removeStorageSync('tenantId')
- uni.removeStorageSync('userId')
- uni.removeStorageSync('userName')
- uni.removeStorageSync('userInfo')
- uni.removeStorageSync('userType')
- uni.removeStorageSync('tenantInfo')
- uni.reLaunch({
- url: "/pages/login/login"
- })
- console.log('用户点击确定');
- } else if (res.cancel) {
- console.log('用户点击取消');
- }
- }
- });
- }
- })
- }
|