request.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. if(options.apiType === 'php'){
  31. baseURL = 'http://testshop.hxxfb.com/'
  32. // baseURL = '/'
  33. // console.log('url = ',baseURL + options.url,"PHP")
  34. }
  35. uni.request({
  36. url: baseURL + options.url, //接口地址:前缀+方法中传入的地址
  37. method: options.method || 'GET', //请求方法:传入的方法或者默认是“GET”
  38. data: options.data || {}, //传递参数:传入的参数或者默认传递空集合
  39. header: {
  40. // 'Content-Type': 'application/x-www-form-urlencoded',
  41. 'X-token': uni.getStorageSync('authorization'), //请求头信息
  42. // 'X-token':'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJhcGkuaHh4ZmIuY29tIiwiYXVkIjoiYXBpLmh4eGZiLmNvbSIsImlhdCI6MTczOTk2NDIxMiwibmJmIjoxNzM5OTY0MjEyLCJleHAiOjE3NTA3NjQyMTIsImp0aSI6WzM1OSwibWVyIl19.IX8wt8Fc05VgshXbUFTusGag4KD__teryi13L-QrJQM',
  43. 'volunteerdes': bas // DES+B64加密
  44. },
  45. success: (res) => {
  46. console.log('url = ',baseURL + options.url)
  47. console.log('params = ',options.data)
  48. // debugger
  49. resolve(res, "查看")
  50. //返回的数据(不固定,看后端接口,这里是做了一个判断,如果不为true,用uni.showToast方法提示获取数据失败)
  51. // 如果不满足上述判断就输出数据
  52. if (res.data.status == 200) { //接口返回200走一下操作
  53. resolve(res)
  54. } else if (res.data.status == 500) {//接口返回500走一下操作
  55. return uni.showToast({
  56. title: res.data.error,
  57. icon: 'none'
  58. })
  59. } else { //除了500都会走以下操作
  60. return uni.showToast({
  61. title: res.data.message,
  62. icon: 'none'
  63. })
  64. }
  65. },
  66. // 这里的接口请求,如果出现问题就输出接口请求失败
  67. fail: (err) => {
  68. reject(err)
  69. }
  70. })
  71. })
  72. }