restful.ts 5.2 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. function fun(url: String, type: any, data: any, otherType: any) {
  52. if (!TOKEN) {
  53. //这里可以做一个请求的拦截 看有没有携带token
  54. console.log("Token:", TOKEN)
  55. }
  56. let promise = new Promise((resolve, reject) => {
  57. wx.request({
  58. url: HOST + url,
  59. method: type,
  60. data: type == 'GET' ? data : JSON.stringify(data),
  61. header: {
  62. 'content-type': otherType ? APPLICATION_JSON_UTF8_FORM : APPLICATION_JSON_UTF8_VALUE,
  63. 'access-token': wx.getStorageSync('accessToken') == null ? '' : wx.getStorageSync('accessToken'),
  64. 'userId': httpData.userId
  65. },
  66. success: function (res: any) {
  67. wx.stopPullDownRefresh();
  68. if (res) {
  69. resolve(res)
  70. } else {
  71. wx.showToast({
  72. title: res.data.subMsg ? res.data.subMsg : res.data.msg,
  73. icon: 'none',
  74. duration: 2000
  75. })
  76. }
  77. },
  78. fail: function (res: any) {
  79. wx.stopPullDownRefresh();
  80. reject(res)
  81. }
  82. })
  83. })
  84. return promise
  85. }
  86. function uploadFile(url: String, filePath: string, fileName: string, formData: any) {
  87. let promise = new Promise((resolve, reject) => {
  88. wx.uploadFile({
  89. url: HOST + url,
  90. filePath: filePath,
  91. name: fileName,
  92. formData: formData,
  93. success(res: any) {
  94. resolve(res)
  95. // const data = res.data
  96. //do something
  97. },
  98. fail(res: any) {
  99. reject(res)
  100. }
  101. })
  102. })
  103. return promise;
  104. }
  105. function downLoadFile(url: string, onProgress: any) {
  106. let promise = new Promise((resolve, reject) => {
  107. let download = wx.downloadFile({
  108. url: url,
  109. success(res: any) {
  110. resolve(res)
  111. // const data = res.data
  112. //do something
  113. },
  114. fail(res: any) {
  115. reject(res)
  116. }
  117. })
  118. download.onProgressUpdate((res) => {
  119. onProgress(res)
  120. console.log('下载进度', res.progress)
  121. console.log('已经下载的数据长度', res.totalBytesWritten)
  122. console.log('预期需要下载的数据总长度', res.totalBytesExpectedToWrite)
  123. })
  124. })
  125. return promise;
  126. }
  127. export const httpUtil = {
  128. httpData,
  129. interfaces,
  130. wxGet: function (url: String, data: any) {
  131. return fun(url, 'GET', data, null)
  132. },
  133. wxPost: function (url: String, data: any) {
  134. return fun(url, 'POST', data, null)
  135. },
  136. wxFormPost: function (url: String, data: any) {
  137. return fun(url, 'POST', data, 'formPost')
  138. },
  139. wxPut: function (url: String, data: any) {
  140. return fun(url, 'PUT', data, null)
  141. },
  142. wxDel: function (url: String, data: any) {
  143. return fun(url, 'DELETE', data, null)
  144. },
  145. wxUploadFile: function (url: String, filePath: string, fileName: string, formData: any) {
  146. return uploadFile(url, filePath, fileName, formData)
  147. },
  148. wxDownLoadFile: function (url: string, onProgress: any) {
  149. return downLoadFile(url, onProgress)
  150. }
  151. }