index.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. import md5 from "./md5"
  2. import CryptoJS from "crypto-js";
  3. // uni.baseUrl = "https://atest.zhongkaishengda.com/"
  4. uni.baseUrl = "https://www.gyshucang.art/"
  5. // uni.baseUrl = "https://sx077.sxqichuangkeji.com/"
  6. /**
  7. * 获取数据(get请求,不带缓存)
  8. * @author NanQi
  9. * @param {String} url 请求的URL
  10. * @param {Object} data 请求参数
  11. * @param {Boolean} toast 是否显示toast
  12. * @return {Promise} Promise对象
  13. */
  14. const request = (url = '', data = {}, type = "POST", header = {}) => {
  15. // console.log(data)
  16. // console.log(type)
  17. return new Promise((resolve, reject) => {
  18. // uni.showLoading({
  19. // title:"加载中...",
  20. // icon:"loading"
  21. // })
  22. //从localstorage拿到token
  23. var token;
  24. try {
  25. if (uni.getStorageSync('token') == null || uni.getStorageSync('token') == undefined) {
  26. token = ""
  27. } else {
  28. token = uni.getStorageSync('token')
  29. }
  30. //运行代码
  31. } catch (err) {
  32. token = ""
  33. //处理错误
  34. }
  35. var header = {
  36. "api-token": token,
  37. 'Content-Type': 'application/x-www-form-urlencoded'
  38. }
  39. let option = {
  40. url: url,
  41. data: data,
  42. method: type,
  43. header: header,
  44. dataType:'json',
  45. }
  46. // console.log(options)
  47. let options = Object.assign({}, option)
  48. // console.log(options)
  49. options.url = uni.baseUrl + options.url;
  50. // console.log(options.data)
  51. options.data = form_str(options);
  52. // console.log(options.data)
  53. //获取时间戳 放到sign里
  54. const times = new Date().getTime()
  55. //全局携带参数时间戳
  56. data.time = times
  57. //signature 该方法第一个参数请求参数 第二个定义关键字
  58. let sign = signature(data, 'woshijiamijiekou');
  59. //全局携带sign
  60. data.sign = sign;
  61. // method: type,
  62. // url: uni.baseUrl + url,
  63. // // url: "/apis/" + url,
  64. // data: data,
  65. // header: header,
  66. // dataType: 'json',
  67. // options
  68. uni.request(options).then((response) => {
  69. let [error, res] = response;
  70. // console.log(response)
  71. if (res.data.code == 0 && res.data.info) {
  72. uni.showToast({
  73. title: res.data.info,
  74. icon: "none"
  75. })
  76. }
  77. //401时 用户登录超时
  78. if (res.data.code == 401) {
  79. uni.showToast({
  80. title: res.data.info,
  81. icon: "none"
  82. })
  83. setTimeout(() => {
  84. uni.redirectTo({
  85. url: "/pages/login/index"
  86. })
  87. }, 500)
  88. }
  89. resolve(res.data);
  90. }).catch(error => {
  91. let [err, res] = error;
  92. reject(err)
  93. })
  94. });
  95. }
  96. //md5字段排序加密 data当前传递的参数 key后面自定义加密规则
  97. function signature(data, key) {
  98. //先判断data里有没有sign 有的话 清除之前的数据
  99. if (data.sign) {
  100. delete data.sign;
  101. }
  102. //objNull 自定义方法 判断对象里面是否为空
  103. data = objNull(data)
  104. var n = null,
  105. d = {},
  106. str = '',
  107. s = ''
  108. //根据key值排序
  109. n = Object.keys(data).sort()
  110. // console.log(n)
  111. for (var i in n) {
  112. if (isJSON(data[n[i]])) continue
  113. d[n[i]] = data[n[i]]
  114. }
  115. //拼接参数为字符串
  116. for (var k in d) {
  117. if (str != '') str += '&'
  118. if (isContainChinese(d[k])) {
  119. str += k + '=' + encodeURIComponent(d[k])
  120. } else {
  121. str += k + '=' + encodeURI(d[k])
  122. }
  123. // console.log("拼接"+ d[k])
  124. }
  125. str += '&key=' + key
  126. // console.log("拼接之后未加密的字符串"+str.trim())
  127. s = md5.hex_md5(str).toUpperCase() // 这儿是进行MD5加密并转大写
  128. // console.log("拼接之后加密的字符串"+s)
  129. return s
  130. }
  131. // 用来清楚对象里面的数据
  132. function objNull(obj) {
  133. Object.keys(obj).forEach(item => {
  134. // console.log(obj[item])
  135. if (!obj[item]) {
  136. obj[item] = ""
  137. } else {
  138. obj[item] = obj[item].toString().trim()
  139. }
  140. })
  141. return obj;
  142. }
  143. //判断字符串是否是json
  144. function isJSON(str) {
  145. if (typeof str == 'string') {
  146. try {
  147. var obj = JSON.parse(str);
  148. if (typeof obj == 'object' && obj) {
  149. return true;
  150. } else {
  151. return false;
  152. }
  153. } catch (e) {
  154. return false;
  155. }
  156. } else {
  157. return false;
  158. }
  159. }
  160. //判断字符串是否包含中文
  161. function isContainChinese(val) {
  162. if (/.*[\u4e00-\u9fa5]+.*$/.test(val)) {
  163. return true;
  164. }
  165. return false;
  166. }
  167. // aes: "1234123412ABCDEF",
  168. // iv: "ABCDEF1234123412"
  169. function encryptByAES(str) {
  170. let key = CryptoJS.enc.Utf8.parse('1234123412ABCDEF'); // 密钥:一个常量,前后端协定后一个字符串即可
  171. let iv = CryptoJS.enc.Utf8.parse('ABCDEF1234123412'); // 偏移量:一个常量,前后端协定后一个字符串,前后端一致即可
  172. let srcs = CryptoJS.enc.Utf8.parse(str);
  173. var encrypted = CryptoJS.AES.encrypt(srcs, key, {
  174. iv: iv,
  175. // mode: CryptoJS.mode.ECB,
  176. mode: CryptoJS.mode.CBC, // mode 与后台一致。有多个模式可选
  177. padding: CryptoJS.pad.Pkcs7, //
  178. });
  179. // 需要返回base64格式的加密结果,使用此句
  180. return CryptoJS.enc.Base64.stringify(encrypted.ciphertext);
  181. // 需要返回hex格式的加密结果,使用此句
  182. // return encrypted.ciphertext.toString().toUpperCase();
  183. }
  184. function Decrypt(str) {
  185. let key = CryptoJS.enc.Utf8.parse('1234123412ABCDEF'); // 密钥:一个常量,前后端协定后一个字符串即可
  186. let iv = CryptoJS.enc.Utf8.parse('ABCDEF1234123412'); // 偏移量:一个常量,前后端协定后一个字符串,前后端一致即可
  187. let base64 = CryptoJS.enc.Base64.parse(str);
  188. let src = CryptoJS.enc.Base64.stringify(base64);
  189. var decrypt = CryptoJS.AES.decrypt(src, key, {
  190. iv: iv,
  191. // mode: CryptoJS.mode.ECB,
  192. mode: CryptoJS.mode.CBC, // mode 与后台一致。有多个模式可选
  193. padding: CryptoJS.pad.Pkcs7
  194. });
  195. var decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
  196. return JSON.parse(decryptedStr.toString());
  197. }
  198. function encryptor_fn(str) {
  199. const encryptor = new uni.$jsencrypt();
  200. const publicKey =
  201. "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsYWp5UANmyoaXbhza0N23fzh2ciKYl/u2J30VhCzTFG4tldFTLaYOmTNFRWstUFPRaHp0vALjAW2espTuSpQQVawy2rqpQcIMcMS+W9cDtgvcVp/L3yFge2RdEQKXK3lxv7Iu8H4mA9SKfDRKjyb/pu2nVi4bcwdJidPW7NKnAZ+ejjiZesVAZ9/FWPseHg5w9qzGSY8arPJSGHzaeDSIq9oEZo0A3/N5xLyQEzp2a1jktuukCPZB2+9z9eGzAoD2vHK3WEypBbmg8jYVNPPWZ1p1of0h/2I+hTH7ZTW6jePvDHuGSU/U1Ql5DuBh2pmyqFN8FyTUfv12MihNzEwhwIDAQAB";
  202. encryptor.setPublicKey(publicKey);
  203. return encryptor.encrypt(str);
  204. }
  205. function form_str(obj) {
  206. // if(uni.conf.debug==true){
  207. // console.log(obj)
  208. // }
  209. // console.log(obj,666)
  210. obj.data.version="1.0";
  211. let new_obj = {
  212. objEncrypt: encryptByAES(JSON.stringify(obj.data)),
  213. sign: encryptor_fn(obj.data),
  214. }
  215. if (obj.method == "GET") {
  216. return new_obj;
  217. } else {
  218. return new_obj;
  219. // return JSON.stringify(new_obj);
  220. }
  221. }
  222. export default request