request.js 2.8 KB

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