request.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import config from "../config/config.js";
  2. import store from "../../store/index.js"
  3. import msg from './message.js'
  4. import statusCode from './statusCode.js'
  5. import utils from './util.js'
  6. import storage from './storage.js'
  7. export default{
  8. config:{
  9. header: {
  10. TargetCode: 3
  11. },
  12. data: {},
  13. url: config.baseUrl,
  14. method: "POST",
  15. timeout: 10000
  16. },
  17. async request(options = {}){
  18. let token = await storage.getStorageSync('token') || ''
  19. options.data.token = token
  20. !options.data.isUnShowLoading && msg.showLoading(); // isUnShowLoading为true时不显示加载
  21. return new Promise((resolve,reject)=>{
  22. options.header = options.header || this.config.header
  23. options.timeout = options.timeout || this.config.timeout
  24. options.method = options.method || this.config.method
  25. options.data = options.data || this.config.data
  26. options.url = options.url
  27. options.success = (response) => {
  28. !options.data.isUnShowLoading && msg.hideLoading()
  29. if (response.statusCode === 404) {
  30. console.log(options.url);
  31. return msg.showToast('未知的请求!')
  32. }
  33. let res = response.data
  34. // 状态码处理
  35. // if(res.code == statusCode.OCCUPY_CODE) {
  36. // msg.showToast('账号信息已过期,请重新登录!')
  37. // storage.removeStorageSync('token')
  38. // return utils.openReLaunch('/pages/login/login')
  39. // }else if(res.code == statusCode.UNKNOW_CODE) {
  40. // if(res.msg == 'The token field is required.') {
  41. // if(options.url.indexOf('api/Account/UpdatePhoneOnline') === -1) {
  42. // msg.showToast('登录信息有误,请重新登录!')
  43. // }
  44. // storage.removeStorageSync('token')
  45. // return utils.openReLaunch('/pages/login/login')
  46. // }else {
  47. // console.log(options.url);
  48. // console.log(res)
  49. // return msg.showToast(`字段错误!\n${res.msg}`)
  50. // }
  51. // }else if(res.code !== 0) {
  52. // msg.showToast(`${res.msg}`)
  53. // return resolve(res);
  54. // }
  55. return resolve(res);
  56. }
  57. options.fail = (err) => {
  58. console.log('请求失败')
  59. console.log(err)
  60. // console.log(options)
  61. msg.showToast('请求失败,请检查网络是否正常!')
  62. msg.hideLoading()
  63. return reject(err)
  64. }
  65. uni.request(options)
  66. });
  67. },
  68. post2(url,data,options={}){
  69. // 于测试切换ip
  70. options.url = config.baseUrl + url;
  71. // console.log(options.url, 'url');
  72. // options.url = config.baseUrl + url;
  73. options.data = data;
  74. options.method = 'POST';
  75. return this.request(options)
  76. },
  77. post(url,data,options={}){
  78. // 于测试切换ip
  79. options.url = config.baseUrl + url;
  80. // console.log(options.url, 'url');
  81. // options.url = config.baseUrl + url;
  82. options.data = data;
  83. options.method = 'POST';
  84. return this.request(options)
  85. },
  86. get(url,data,options={}){
  87. options.url = config.baseUrl + url;
  88. options.method = 'GET';
  89. return this.request(options)
  90. }
  91. }