restful.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. //提交设备报修
  35. postDeviceFault: '/tch/fault'
  36. }
  37. function fun(url: String, type: any, data: any, otherType: any) {
  38. if (!TOKEN) {
  39. //这里可以做一个请求的拦截 看有没有携带token
  40. console.log("Token:", TOKEN)
  41. }
  42. let promise = new Promise((resolve, reject) => {
  43. wx.request({
  44. url: HOST + url,
  45. method: type,
  46. data: type == 'GET' ? data : JSON.stringify(data),
  47. header: {
  48. 'content-type': otherType ? APPLICATION_JSON_UTF8_FORM : APPLICATION_JSON_UTF8_VALUE,
  49. 'access-token': wx.getStorageSync('accessToken') == null ? '' : wx.getStorageSync('accessToken'),
  50. 'userId': httpData.userId
  51. },
  52. success: function (res: any) {
  53. wx.stopPullDownRefresh();
  54. if (res) {
  55. resolve(res)
  56. } else {
  57. wx.showToast({
  58. title: res.data.subMsg ? res.data.subMsg : res.data.msg,
  59. icon: 'none',
  60. duration: 2000
  61. })
  62. }
  63. },
  64. fail: function (res: any) {
  65. wx.stopPullDownRefresh();
  66. reject(res)
  67. }
  68. })
  69. })
  70. return promise
  71. }
  72. export const httpUtil = {
  73. httpData,
  74. interfaces,
  75. wxGet: function (url: String, data: any) {
  76. return fun(url, 'GET', data, null)
  77. },
  78. wxPost: function (url: String, data: any) {
  79. return fun(url, 'POST', data, null)
  80. },
  81. wxFormPost: function (url: String, data: any) {
  82. return fun(url, 'POST', data, 'formPost')
  83. },
  84. wxPut: function (url: String, data: any) {
  85. return fun(url, 'PUT', data, null)
  86. },
  87. wxDel: function (url: String, data: any) {
  88. return fun(url, 'DELETE', data, null)
  89. }
  90. }