request.js 2.8 KB

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