restful.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. const APPLICATION_JSON_UTF8_VALUE = "application/json;charset=UTF-8";
  2. const APPLICATION_JSON_UTF8_FORM = 'application/x-www-form-urlencoded;charset=UTF-8';
  3. const RESPONSE_OK = 20000;
  4. const TOKEN_BEOVERDUE = 40001;
  5. const TOKEN = wx.getStorageSync('accessToken') == null ? '' : wx.getStorageSync('accessToken');
  6. //测试环境
  7. const HOST = 'https://wordpad-api-test.efunbox.cn';
  8. //正式环境
  9. // const HOST = 'https://wordpad-api-test.efunbox.cn';
  10. const httpData = {
  11. userId: 'test'
  12. }
  13. //API接口
  14. const interfaces = {
  15. //这里边放入请求的接口
  16. //获取全国地址
  17. getOrganizeAll: '/organize/all',
  18. //获取学校信息和班级信息(根据type判断)
  19. getSchoolOrClass: '/school',
  20. //手机号登录注册
  21. phoneRegister: '/tch/user/register',
  22. //获取手机验证码
  23. getVerifyCode: '/tch/user/verifyCode',
  24. //账号密码登录
  25. pwdPhoneLogin: '/tch/user/login',
  26. //修改密码
  27. updatePassWord: '/tch/user/updatePassword',
  28. //更新个人信息
  29. updateUserInfo: '/tch/user',
  30. //根据userId获取用户信息
  31. getUserById: '/tch/user/getUserById',
  32. //根据deviceId获取设备信息(后面拼接deviceId)
  33. getDeviceById: '/tch/device/',
  34. //根据地区,学校,班级获取设备ID
  35. getDevideIdByRSC: '/tch/device/site',
  36. //提交设备报修
  37. postDeviceFault: '/tch/fault',
  38. //根据userId获取提交的报修记录
  39. getDeviceFault: '/tch/fault',
  40. }
  41. function fun(url: String, type: any, data: any, otherType: any) {
  42. if (!TOKEN) {
  43. //这里可以做一个请求的拦截 看有没有携带token
  44. console.log("Token:", TOKEN)
  45. }
  46. let promise = new Promise((resolve, reject) => {
  47. wx.request({
  48. url: HOST + url,
  49. method: type,
  50. data: type == 'GET' ? data : JSON.stringify(data),
  51. header: {
  52. 'content-type': otherType ? APPLICATION_JSON_UTF8_FORM : APPLICATION_JSON_UTF8_VALUE,
  53. 'access-token': wx.getStorageSync('accessToken') == null ? '' : wx.getStorageSync('accessToken'),
  54. 'userId': httpData.userId
  55. },
  56. success: function (res: any) {
  57. wx.stopPullDownRefresh();
  58. if (res) {
  59. resolve(res)
  60. } else {
  61. wx.showToast({
  62. title: res.data.subMsg ? res.data.subMsg : res.data.msg,
  63. icon: 'none',
  64. duration: 2000
  65. })
  66. }
  67. },
  68. fail: function (res: any) {
  69. wx.stopPullDownRefresh();
  70. reject(res)
  71. }
  72. })
  73. })
  74. return promise
  75. }
  76. export const httpUtil = {
  77. httpData,
  78. interfaces,
  79. wxGet: function (url: String, data: any) {
  80. return fun(url, 'GET', data, null)
  81. },
  82. wxPost: function (url: String, data: any) {
  83. return fun(url, 'POST', data, null)
  84. },
  85. wxFormPost: function (url: String, data: any) {
  86. return fun(url, 'POST', data, 'formPost')
  87. },
  88. wxPut: function (url: String, data: any) {
  89. return fun(url, 'PUT', data, null)
  90. },
  91. wxDel: function (url: String, data: any) {
  92. return fun(url, 'DELETE', data, null)
  93. }
  94. }