123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- import {
- appId,
- envs
- } from "./api";
- import {
- encryption
- } from "./encryption";
- import {
- setItem,
- getItem,
- StorageKeys
- } from "../storage";
- import {
- sm4Key
- } from "../network/api.js";
- import {
- SM4Util
- } from '../util/sm4.js'
- import {
- updateToken
- } from "@/utils/network/api";
- const s4 = new SM4Util()
-
- /* 刷新token */
- function updateGetToken() {
- const options = {
- type: 2,
- data: {
- openId: getItem("openId"),
- accessToken: getItem(StorageKeys.Token)
- },
- method: "POST",
- showLoading: false,
- };
- //刷新token
- return new Promise(async (resolve, reject) => {
- const res = await request(updateToken, options);
- const data = JSON.parse(res.bizContent);
- console.log("data", data)
- setItem('accessToken', data.accessToken);
- setItem(StorageKeys.Token, data.accessToken);
- setItem('openId', data.openId);
- resolve(data);
- }).catch((error) => {
- reject(error);
- });
- }
-
- //请求
- export function request(code, options = {}, start = false) {
- //公参
- const Common = {
- agentId: "52010106004",
- channelId: "5201010200601130001",
- channelType: "1",
- staffId: "54623263cb4d4a289dccbc983b22a4af",
- terminalId: "999999999999",
- loginSource: getItem("loginSource"),
- rbacSource: 'MINI_PROGRAM',
- accessToken: getItem(StorageKeys.Token)
- }
- // options.url = envs[process.env.NODE_ENV].baseUrl + '/api/interfaceMidGroundIn'
- options.url = envs[process.env.NODE_ENV].baseUrl + '/ifzt/api/interfaceMidGroundIn'
- //默认json数据格式提交`
- let contentType = 'application/x-www-form-urlencoded'
-
- //判断baseUri是否为空
- if (options.baseUrl) {
- options.url = options.baseUrl
- }
- //根据type判断数据传输格式
- if (options.type && options.type === 2) {
- contentType = 'application/json;charset=UTF-8'
- }
- //默认POST提交
- options.method = options.method ? options.method : 'POST'
-
- //设置请求超时时间
- options.timeout = 60000
-
- options.header = {
- 'content-type': contentType,
- 'Access-Token': getItem(StorageKeys.Token)
- }
- console.log('Token', getItem(StorageKeys.Token))
- if (!start) {
- //判断code不为空
- if (code) {
- options.data = encryption(code, Object.assign(Common, {
- ...options.data,
- 'appId': appId
- }), 2);
- }
- }
- //是否显示加载中
- if (options.showLoading) {
- uni.showLoading({
- title: '请稍后',
- mask: true
- });
- }
- console.log('请求参数:', options.data)
- //参数返回
- return new Promise((resolve, reject) => {
- options.success = (res) => {
- console.log('请求成功返回参数:', res)
- // 请求返回后,隐藏loading(如果请求返回快的话,可能会没有loading)
- uni.hideLoading()
- if (res.data.statusCode !== 0) {
- if (res.data.statusCode == 600) {
- resolve(res.data)
- } else if (res.data.statusCode == 401) {
- uni.showModal({
- title: '提示',
- content: res.data.errorMsg,
- confirmText: '去登录',
- showCancel: false,
- success: function(res) {
- if (res.confirm) {
- uni.navigateBack({
- delta: 1
- })
- uni.navigateTo({
- url: '/login/login',
- })
- }
- }
- });
- } else if (res.data.statusCode == 99999) {
- if (!start) {
- updateGetToken().then((data) => {
- console.log("token刷新", data);
- request(code, options, true)
- })
- } else {
- uni.showModal({
- title: '提示',
- content: res.data.errorMsg,
- success: function(res) {
- if (res.confirm) {
- console.log('用户点击确定');
- } else if (res.cancel) {
- console.log('用户点击取消');
- }
- }
- });
- }
- } else {
- uni.showModal({
- title: '提示',
- content: res.data.errorMsg,
- success: function(res) {
- if (res.confirm) {
- console.log('用户点击确定');
- } else if (res.cancel) {
- console.log('用户点击取消');
- }
- }
- });
- }
- reject(res.data.errorMsg)
- return
- } else {
- let content = res
- // let content = s4.decryptData_CBC(res, sm4Key)
- // console.log(content);
- // content.data.bizContent = JSON.stringify(content.data.bizContent)
- // console.log(content.data);
- resolve(content.data)
- }
- }
- options.fail = (err) => {
- // console.log('请求失败返回参数:', res)
- uni.hideLoading()
- console.log('请求错误', err)
- // if (err == 'openId无效,请核实传参!') {
- // setItem(StorageKeys.OpenId, openId);
- // }
- //处理请求错误
- reject(err)
- }
- uni.getNetworkType({
- success: function(res) {
- if (res.networkType == 'none') {
- uni.showModal({
- title: '提示',
- content: "网络异常",
- success: function(res) {
- if (res.confirm) {
- console.log('用户点击确定');
- } else if (res.cancel) {
- console.log('用户点击取消');
- }
- }
- });
- } else {
- uni.request(options)
- }
- console.log("res.networkType", res.networkType);
- }
- });
- });
- }
|