restful.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. //#region 教师端
  17. //获取全国地址
  18. getOrganizeAll: '/organize/all',
  19. //获取学校信息和班级信息(根据type判断)
  20. getSchoolOrClass: '/school',
  21. //手机号登录注册
  22. phoneRegister: '/tch/user/register',
  23. //获取手机验证码
  24. getVerifyCode: '/tch/user/verifyCode',
  25. //账号密码登录
  26. pwdPhoneLogin: '/tch/user/login',
  27. //修改密码
  28. updatePassWord: '/tch/user/updatePassword',
  29. //更新个人信息
  30. updateUserInfo: '/tch/user',
  31. //根据userId获取用户信息
  32. getUserById: '/tch/user/getUserById',
  33. //根据deviceId获取设备信息(后面拼接deviceId)
  34. getDeviceById: '/tch/device/',
  35. //根据地区,学校,班级获取设备ID
  36. getDevideIdByRSC: '/tch/device/site',
  37. //提交设备报修
  38. postDeviceFault: '/tch/fault',
  39. //上传单个图片
  40. uploadImageList: '/file/upload',
  41. //根据userId获取提交的报修记录
  42. getDeviceFault: '/tch/fault',
  43. //根据故障ID获取故障详情(后面拼接ID)
  44. getFaultInfoByDevId: '/tch/fault/',
  45. //开始上课(创建文件库)(获取文件库get方法)(删除文件delete方法)
  46. createLess: '/tch/lesson/',
  47. //获取文件夹所有文件接口(拼接ID)(删除文件delete方法)
  48. getDocumentFile: '/tch/document',
  49. //#endregion
  50. //提交评价
  51. postComment: '/tch/faultProcess',
  52. }
  53. function fun(url: String, type: any, data: any, otherType: any) {
  54. if (!TOKEN) {
  55. //这里可以做一个请求的拦截 看有没有携带token
  56. console.log("Token:", TOKEN)
  57. }
  58. let promise = new Promise((resolve, reject) => {
  59. wx.request({
  60. url: HOST + url,
  61. method: type,
  62. data: type == 'GET' ? data : JSON.stringify(data),
  63. header: {
  64. 'content-type': otherType ? APPLICATION_JSON_UTF8_FORM : APPLICATION_JSON_UTF8_VALUE,
  65. 'access-token': wx.getStorageSync('accessToken') == null ? '' : wx.getStorageSync('accessToken'),
  66. 'userId': httpData.userId
  67. },
  68. success: function (res: any) {
  69. wx.stopPullDownRefresh();
  70. if (res) {
  71. resolve(res)
  72. } else {
  73. wx.showToast({
  74. title: res.data.subMsg ? res.data.subMsg : res.data.msg,
  75. icon: 'none',
  76. duration: 2000
  77. })
  78. }
  79. },
  80. fail: function (res: any) {
  81. wx.stopPullDownRefresh();
  82. reject(res)
  83. }
  84. })
  85. })
  86. return promise
  87. }
  88. function uploadFile(url: String, filePath: string, fileName: string, formData: any) {
  89. let promise = new Promise((resolve, reject) => {
  90. wx.uploadFile({
  91. url: HOST + url,
  92. filePath: filePath,
  93. name: fileName,
  94. formData: formData,
  95. success(res: any) {
  96. resolve(res)
  97. // const data = res.data
  98. //do something
  99. },
  100. fail(res: any) {
  101. reject(res)
  102. }
  103. })
  104. })
  105. return promise;
  106. }
  107. function downLoadFile(url: string, onProgress: any) {
  108. let promise = new Promise((resolve, reject) => {
  109. let download = wx.downloadFile({
  110. url: url,
  111. success(res: any) {
  112. resolve(res)
  113. // const data = res.data
  114. //do something
  115. },
  116. fail(res: any) {
  117. reject(res)
  118. }
  119. })
  120. download.onProgressUpdate((res) => {
  121. onProgress(res)
  122. })
  123. })
  124. return promise;
  125. }
  126. export const httpUtil = {
  127. httpData,
  128. interfaces,
  129. wxGet: function (url: String, data: any) {
  130. return fun(url, 'GET', data, null)
  131. },
  132. wxPost: function (url: String, data: any) {
  133. return fun(url, 'POST', data, null)
  134. },
  135. wxFormPost: function (url: String, data: any) {
  136. return fun(url, 'POST', data, 'formPost')
  137. },
  138. wxPut: function (url: String, data: any) {
  139. return fun(url, 'PUT', data, null)
  140. },
  141. wxDel: function (url: String, data: any) {
  142. return fun(url, 'DELETE', data, null)
  143. },
  144. wxUploadFile: function (url: String, filePath: string, fileName: string, formData: any) {
  145. return uploadFile(url, filePath, fileName, formData)
  146. },
  147. wxDownLoadFile: function (url: string, onProgress: any) {
  148. return downLoadFile(url, onProgress)
  149. }
  150. }