import { encryption } from "./encryption" import { getToken } from '@/utils/storage' import { sm4Key, } from "../network/api.js"; import { SM4Util } from '../util/sm4.js' const s4 = new SM4Util() /** * 配置信息,针对不同的平台进行配置 */ export function request(code, options = {}) { // console.log('接口请求参数:', options) //公参 const Common = { loginSource: '69af303ba2eb4608a099163f0d2a5dbd', orderSource: 'H5' } //Url 地址 // options.url = 'http://' + envs['development'].baseUrl + '/ifzt/api/interfaceMidGroundIn' // options.url = import.meta.env.VITE_APP_BASE_URL + '/api/interfaceMidGroundIn' options.url = '/api/interfaceMidGroundIn' // options.url = 'https://' + 'skx.mynatapp.cc' + '/ifzt/api/interfaceMidGroundIn' //判断baseUri是否为空 if (options.baseUrl) { options.url = options.baseUrl } //默认json数据格式提交` let contentType = 'application/json;charset=UTF-8' //根据type判断数据传输格式 if (options.type && options.type === 2) { contentType = 'application/x-www-form-urlencoded ' } else if (options.type && options.type === 3) { contentType = 'multipart/form-data' } options.header = { 'content-type': contentType } const token = getToken(); if (token) { options.header["Access-Token"] = token; } //默认POST提交 options.method = options.method ? options.method : 'POST' //设置请求超时时间 options.timeout = 60000 //判断code不为空 if (code) { let appId = '' if (import.meta.env.VITE_APP_TYPE === 'production') { appId = '52030131' } else { appId = 'admin001' } //是否显示加载中 if (!options.showLoading) { uni.showLoading({ title: options.showTitle ? options.showTitle : '加载中', mask: true }); } console.time() if (options.noCommon) { // 不携带公参 options.data = encryption(code, { ...options.data, 'appId': appId }, 2); } else { options.data = encryption(code, Object.assign(Common, { ...options.data, 'appId': appId }), 2); } console.timeEnd() } //参数返回 return new Promise((resolve, reject) => { options.success = (res) => { // 请求返回后,隐藏loading(如果请求返回快的话,可能会没有loading) uni.hideLoading() if (res.data.statusCode !== 0) { if(!options.hideShowModal && res.data.statusCode !== options.statusCode) uni.showModal({ title: '提示', content: res.data.errorMsg, success: function(res) { if (res.confirm) {} else if (res.cancel) {} } }); } if (import.meta.env.VITE_APP_TYPE === 'production') { res = s4.decryptData_CBC(res, sm4Key) } //请求成功 resolve(res.data) } options.fail = (err) => { uni.hideLoading() console.log('请求错误', err) //处理请求错误 reject(err) } uni.request(options) }); } // jsonp请求-用于获取公网地址 export function myJsonp(options) { /* 1.首先动态创建script标签 */ var script = document.createElement("script"); //1.1 把数据对象转换为查询字符串 // var qs = resolveData(options.data); // 1.2动态生成函数名 var jsonpName = 'ipCallback'; //1.3将当前文件名放到window里 window[jsonpName] = function(res) { //把结果回掉出去 options.success && options.success(res); //删除window中的函数名 delete window[jsonpName]; }; /* 2.给src设置url和参数 */ script.src = options.url; /* 3.添加到html */ document.querySelector("head").appendChild(script); /* 4.加载完毕后删除script标签 使用监听 */ script.onload = function() { document.querySelector("head").removeChild(script); }; }