request.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import app from '../../App.vue';
  2. // http://117.ci2.cn:8080/
  3. //http://api.order.ci2.cn/ //98
  4. //http://api.test.order.ci2.cn/
  5. const baseurl="http://api.order.ci2.cn/";
  6. console.log(app.globalData)
  7. const request = (url, options) => {
  8. return new Promise((resolve, reject) => {
  9. wx.request({
  10. url: `${baseurl}${url}`,
  11. method: options.method,
  12. // data: options.method === 'GET' ? options.data : JSON.stringify(options.data),
  13. data:options.data,
  14. header: {
  15. 'content-Type': 'application/json',
  16. "Authorization": uni.getStorageSync('userInfo').Authorization ? uni.getStorageSync('userInfo').Authorization:''
  17. },
  18. success(res) {
  19. if (res.data.code === "success"||res.data.status) {
  20. // 返回data
  21. resolve(res.data)
  22. } else {
  23. if(res.data.code=='U204'){
  24. uni.reLaunch({
  25. url:'/pages/login/newlogin'
  26. })
  27. }
  28. reject(res.data)
  29. }
  30. },
  31. fail(err) {
  32. console.log(err)
  33. reject(err.data)
  34. }
  35. })
  36. })
  37. }
  38. const get = (url, options = {}) => {
  39. return request(url, { method: 'GET', data: options })
  40. }
  41. const post = (url, options) => {
  42. return request(url, { method: 'POST', data: options })
  43. }
  44. const put = (url, options) => {
  45. return request(url, { method: 'PUT', data: options })
  46. }
  47. // 不能声明DELETE(关键字)
  48. const remove = (url, options) => {
  49. return request(url, { method: 'DELETE', data: options })
  50. }
  51. module.exports = {
  52. get,
  53. post,
  54. put,
  55. remove
  56. }