import { encryption } from "./encryption" /** * 配置信息,针对不同的平台进行配置 */ export const envs = { //开发环境配置 development: { // baseUrl: "222.85.144.89:19002", baseUrl: "192.168.100.63:8087", }, //生产环境配置 production: { baseUrl: "testmedusa.etcjz.cn", } } export function request(code, options = {}) { // console.log('接口请求参数:', options) //公参 const Common = {} //Url 地址 // options.url = 'http://' + envs['development'].baseUrl + '/ifzt/api/interfaceMidGroundIn' options.url = 'https://' + envs['production'].baseUrl + '/ifzt/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 ' } options.header = { 'content-type': contentType } //默认POST提交 options.method = options.method ? options.method : 'POST' //设置请求超时时间 options.timeout = 60000 //判断code不为空 if (code) { options.data = encryption(code, Object.assign(Common, { ...options.data, 'appId': '52030131' }), 2); } //是否显示加载中 if (!options.showLoading) { uni.showLoading({ title: options.showTitle ? options.showTitle : '加载中', mask: true }); } //参数返回 return new Promise((resolve, reject) => { options.success = (res) => { // 请求返回后,隐藏loading(如果请求返回快的话,可能会没有loading) uni.hideLoading() if (res.data.statusCode !== 0) { uni.showModal({ title: '提示', content: res.data.errorMsg, success: function(res) { if (res.confirm) { console.log('用户点击确定'); } else if (res.cancel) { console.log('用户点击取消'); } } }); } //请求成功 resolve(res.data) } options.fail = (err) => { uni.hideLoading() console.log('请求错误', err) //处理请求错误 reject(err) } uni.request(options) }); } export function OrdinaryRequest(options = {}) { // console.log('接口请求参数:', options) var data = new FormData(); uni.showLoading({ title: '正在支付' }); for (let item in options.data) { data.append(item, options.data[item]); } data.append("goodsName", "测试"); //金额 data.append("h5Type", "Wap"); //金额 data.append("type", "H5"); var xhr = new XMLHttpRequest(); xhr.withCredentials = true; xhr.addEventListener("readystatechange", function() { if (this.readyState === 4) { console.log(this.responseText); uni.hideLoading(); } }); xhr.open("POST", "http://192.168.1.3:8080/wxpay/apply"); xhr.send(data); } // 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); }; }