123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- import md5 from "./md5"
- import CryptoJS from "crypto-js";
- // uni.baseUrl = "https://atest.zhongkaishengda.com/"
- uni.baseUrl = "https://www.gyshucang.art/"
- // uni.baseUrl = "https://sx077.sxqichuangkeji.com/"
- /**
- * 获取数据(get请求,不带缓存)
- * @author NanQi
- * @param {String} url 请求的URL
- * @param {Object} data 请求参数
- * @param {Boolean} toast 是否显示toast
- * @return {Promise} Promise对象
- */
- const request = (url = '', data = {}, type = "POST", header = {}) => {
- // console.log(data)
- // console.log(type)
- return new Promise((resolve, reject) => {
- // uni.showLoading({
- // title:"加载中...",
- // icon:"loading"
- // })
- //从localstorage拿到token
- var token;
- try {
- if (uni.getStorageSync('token') == null || uni.getStorageSync('token') == undefined) {
- token = ""
- } else {
- token = uni.getStorageSync('token')
- }
- //运行代码
- } catch (err) {
- token = ""
- //处理错误
- }
- var header = {
- "api-token": token,
- 'Content-Type': 'application/x-www-form-urlencoded'
- }
- let option = {
- url: url,
- data: data,
- method: type,
- header: header,
- dataType:'json',
- }
- // console.log(options)
- let options = Object.assign({}, option)
- // console.log(options)
- options.url = uni.baseUrl + options.url;
- // console.log(options.data)
- options.data = form_str(options);
- // console.log(options.data)
- //获取时间戳 放到sign里
- const times = new Date().getTime()
- //全局携带参数时间戳
- data.time = times
- //signature 该方法第一个参数请求参数 第二个定义关键字
- let sign = signature(data, 'woshijiamijiekou');
- //全局携带sign
- data.sign = sign;
- // method: type,
- // url: uni.baseUrl + url,
- // // url: "/apis/" + url,
- // data: data,
- // header: header,
- // dataType: 'json',
- // options
- uni.request(options).then((response) => {
- let [error, res] = response;
- // console.log(response)
- if (res.data.code == 0 && res.data.info) {
- uni.showToast({
- title: res.data.info,
- icon: "none"
- })
- }
- //401时 用户登录超时
- if (res.data.code == 401) {
- uni.showToast({
- title: res.data.info,
- icon: "none"
- })
- setTimeout(() => {
- uni.redirectTo({
- url: "/pages/login/index"
- })
- }, 500)
- }
- resolve(res.data);
- }).catch(error => {
- let [err, res] = error;
- reject(err)
- })
- });
- }
- //md5字段排序加密 data当前传递的参数 key后面自定义加密规则
- function signature(data, key) {
- //先判断data里有没有sign 有的话 清除之前的数据
- if (data.sign) {
- delete data.sign;
- }
- //objNull 自定义方法 判断对象里面是否为空
- data = objNull(data)
- var n = null,
- d = {},
- str = '',
- s = ''
- //根据key值排序
- n = Object.keys(data).sort()
- // console.log(n)
- for (var i in n) {
- if (isJSON(data[n[i]])) continue
- d[n[i]] = data[n[i]]
- }
- //拼接参数为字符串
- for (var k in d) {
- if (str != '') str += '&'
- if (isContainChinese(d[k])) {
- str += k + '=' + encodeURIComponent(d[k])
- } else {
- str += k + '=' + encodeURI(d[k])
- }
- // console.log("拼接"+ d[k])
- }
- str += '&key=' + key
- // console.log("拼接之后未加密的字符串"+str.trim())
- s = md5.hex_md5(str).toUpperCase() // 这儿是进行MD5加密并转大写
- // console.log("拼接之后加密的字符串"+s)
- return s
- }
- // 用来清楚对象里面的数据
- function objNull(obj) {
- Object.keys(obj).forEach(item => {
- // console.log(obj[item])
- if (!obj[item]) {
- obj[item] = ""
- } else {
- obj[item] = obj[item].toString().trim()
- }
- })
- return obj;
- }
- //判断字符串是否是json
- function isJSON(str) {
- if (typeof str == 'string') {
- try {
- var obj = JSON.parse(str);
- if (typeof obj == 'object' && obj) {
- return true;
- } else {
- return false;
- }
- } catch (e) {
- return false;
- }
- } else {
- return false;
- }
- }
- //判断字符串是否包含中文
- function isContainChinese(val) {
- if (/.*[\u4e00-\u9fa5]+.*$/.test(val)) {
- return true;
- }
- return false;
- }
- // aes: "1234123412ABCDEF",
- // iv: "ABCDEF1234123412"
- function encryptByAES(str) {
- let key = CryptoJS.enc.Utf8.parse('1234123412ABCDEF'); // 密钥:一个常量,前后端协定后一个字符串即可
- let iv = CryptoJS.enc.Utf8.parse('ABCDEF1234123412'); // 偏移量:一个常量,前后端协定后一个字符串,前后端一致即可
- let srcs = CryptoJS.enc.Utf8.parse(str);
- var encrypted = CryptoJS.AES.encrypt(srcs, key, {
- iv: iv,
- // mode: CryptoJS.mode.ECB,
- mode: CryptoJS.mode.CBC, // mode 与后台一致。有多个模式可选
- padding: CryptoJS.pad.Pkcs7, //
- });
- // 需要返回base64格式的加密结果,使用此句
- return CryptoJS.enc.Base64.stringify(encrypted.ciphertext);
- // 需要返回hex格式的加密结果,使用此句
- // return encrypted.ciphertext.toString().toUpperCase();
- }
- function Decrypt(str) {
- let key = CryptoJS.enc.Utf8.parse('1234123412ABCDEF'); // 密钥:一个常量,前后端协定后一个字符串即可
- let iv = CryptoJS.enc.Utf8.parse('ABCDEF1234123412'); // 偏移量:一个常量,前后端协定后一个字符串,前后端一致即可
- let base64 = CryptoJS.enc.Base64.parse(str);
- let src = CryptoJS.enc.Base64.stringify(base64);
- var decrypt = CryptoJS.AES.decrypt(src, key, {
- iv: iv,
- // mode: CryptoJS.mode.ECB,
- mode: CryptoJS.mode.CBC, // mode 与后台一致。有多个模式可选
- padding: CryptoJS.pad.Pkcs7
- });
- var decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
- return JSON.parse(decryptedStr.toString());
- }
- function encryptor_fn(str) {
- const encryptor = new uni.$jsencrypt();
- const publicKey =
- "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsYWp5UANmyoaXbhza0N23fzh2ciKYl/u2J30VhCzTFG4tldFTLaYOmTNFRWstUFPRaHp0vALjAW2espTuSpQQVawy2rqpQcIMcMS+W9cDtgvcVp/L3yFge2RdEQKXK3lxv7Iu8H4mA9SKfDRKjyb/pu2nVi4bcwdJidPW7NKnAZ+ejjiZesVAZ9/FWPseHg5w9qzGSY8arPJSGHzaeDSIq9oEZo0A3/N5xLyQEzp2a1jktuukCPZB2+9z9eGzAoD2vHK3WEypBbmg8jYVNPPWZ1p1of0h/2I+hTH7ZTW6jePvDHuGSU/U1Ql5DuBh2pmyqFN8FyTUfv12MihNzEwhwIDAQAB";
- encryptor.setPublicKey(publicKey);
- return encryptor.encrypt(str);
- }
- function form_str(obj) {
- // if(uni.conf.debug==true){
- // console.log(obj)
- // }
- // console.log(obj,666)
- obj.data.version="1.0";
- let new_obj = {
- objEncrypt: encryptByAES(JSON.stringify(obj.data)),
- sign: encryptor_fn(obj.data),
- }
- if (obj.method == "GET") {
- return new_obj;
- } else {
- return new_obj;
- // return JSON.stringify(new_obj);
- }
- }
- export default request
|