request.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // const baseUrl =  'http://39.100.230.190:30046'
  2. const baseUrl =  'http://172.18.0.24:30046'
  3. // const baseUrl = 'http://172.18.1.227:30046'
  4. // 文件上传地址
  5. const fileUrl =  'http://172.18.0.24:30045'
  6. // const fileUrl =  'http://39.100.230.190:30045'
  7. // import { showLoading,hideLoading } from './loading.js'
  8. // 方式请求
  9. export function request(method, url, data = {},header={},token=false) {
  10. return new Promise((resolve, reject) => {
  11. // header['content-type']='application/x-www-form-urlencoded;charset=UTF-8'
  12. const t = uni.getStorageSync('token')
  13. if(t){
  14. // header['Authorization']='Bearer '+ t
  15. header['Authorization']= t
  16. } else {
  17. header['Authorization']= ''
  18. }
  19. uni.showLoading()
  20. uni.request({
  21. url: baseUrl + url,
  22. method: method.toUpperCase(),
  23. data: data,
  24. dataType: 'json',
  25. header,
  26. success: (res) => {
  27. if(res.data.status === '40401' || res.data.status === '40415' || res.data.message === '未获得认证信息,请重新登陆') {
  28. uni.hideLoading()
  29. uni.showToast({
  30. title: res.data.message,
  31. duration: 2000,
  32. icon: 'none',
  33. position: 'top'
  34. });
  35. setTimeout(()=>{
  36. uni.navigateTo({
  37. url: '/pages/login/login'
  38. })
  39. },1000)
  40. } else {
  41. uni.hideLoading()
  42. resolve(res.data)
  43. }
  44. },
  45. fail: (error) => {
  46. uni.showToast({
  47. title: error.errMsg,
  48. duration: 20000,
  49. icon: 'none',
  50. position: 'top'
  51. });
  52. setTimeout(()=>{
  53. uni.navigateTo({
  54. url: '/pages/login/login'
  55. })
  56. },1000)
  57. reject(error)
  58. }
  59. });
  60. })
  61. }
  62. //上传图片
  63. export function upload_img(url, imageSrc) {
  64. return new Promise((resolve, reject) => {
  65. uni.uploadFile({
  66. url: fileUrl + url,
  67. filePath: imageSrc,
  68. name: 'file',
  69. header: {
  70. 'Authorization': uni.getStorageSync('token')
  71. },
  72. success: (res) => {
  73. res = JSON.parse(res.data);
  74. resolve(res);
  75. },
  76. fail: (err) => {
  77. reject(err);
  78. }
  79. });
  80. })
  81. }