request.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. let is = 2
  2. var baseURL = 'https://api.hxxfb.com/gateway/' // API访问的地址
  3. if(is==1){
  4. baseURL = 'https://api.hxxfb.com/gateway/';//测试环境
  5. }else if(is==2){
  6. // baseURL = 'https://api.hebeiyhc.com/gateway/'; //生产环境
  7. // baseURL = 'https://api.hebeiyhc.com/gateway/'; //生产环境
  8. baseURL = 'https://testapi.hxxfb.com/gateway/'; //预生产环境
  9. // baseURL = '/gateway/'; //预生产环境
  10. }else{
  11. baseURL = 'https://api.hxxfb.com/gateway/'; //预生产环境
  12. }
  13. let token = ''; // 用户的token
  14. let volunteerToken = ''; // DES+B64加密
  15. import time from '@/commit/item.js';
  16. import {
  17. encryptDes,
  18. decryptDes
  19. } from '@/encryption/index.js' // 引用路径根据自己的文件结构而定
  20. import
  21. Base64
  22. from 'base-64'
  23. var d = new Date(); //标准时间
  24. let Fzitem = time.dateFormat("yyyy-MM-dd HH:mm:SS", d) //2022-08-08 14:02:59
  25. let shijian = '123456789/' + Fzitem //定义时间 + 封装时间
  26. let jmi = encryptDes(shijian, "bjtdba123")
  27. let bas = Base64.encode(jmi + "," + Fzitem)
  28. export const myRequest = (options) => {
  29. return new Promise((resolve, reject) => {
  30. uni.request({
  31. url: baseURL + options.url, //接口地址:前缀+方法中传入的地址
  32. method: options.method || 'GET', //请求方法:传入的方法或者默认是“GET”
  33. data: options.data || {}, //传递参数:传入的参数或者默认传递空集合
  34. header: {
  35. // 'Content-Type': 'application/x-www-form-urlencoded',
  36. // "Content-Type":"application/json; charset=utf-8",
  37. 'X-token': uni.getStorageSync('authorization'), //请求头信息
  38. 'volunteerdes': bas, // DES+B64加密
  39. // responseType:'json'
  40. },
  41. success: (res) => {
  42. console.log('url = ',baseURL + options.url)
  43. console.log('params = ',options.data)
  44. // debugger
  45. resolve(res, "查看")
  46. //返回的数据(不固定,看后端接口,这里是做了一个判断,如果不为true,用uni.showToast方法提示获取数据失败)
  47. // 如果不满足上述判断就输出数据
  48. if (res.data.status == 200) { //接口返回200走一下操作
  49. resolve(res)
  50. } else if (res.data.status == 500) {//接口返回500走一下操作
  51. return uni.showToast({
  52. title: res.data.error,
  53. icon: 'none'
  54. })
  55. } else { //除了500都会走以下操作
  56. return uni.showToast({
  57. title: res.data.message,
  58. icon: 'none'
  59. })
  60. }
  61. },
  62. // 这里的接口请求,如果出现问题就输出接口请求失败
  63. fail: (err) => {
  64. reject(err)
  65. }
  66. })
  67. })
  68. }