newRequest.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. import store from '../store'
  2. import md5 from './md5.js'
  3. import sha1 from './sha1.js'
  4. let service = {
  5. // baseURL: store.state.baseURL, // 请求头
  6. baseURL: '', // 请求头
  7. header: {
  8. // 'Accept':'application/json',
  9. // 'Content-Type': 'application/json; charset=utf-8'
  10. 'Content-Type': "application/x-www-form-urlencoded"
  11. },
  12. // 请求拦截器
  13. interceptors: {
  14. // 安装请求前拦截
  15. request: function(success, iserror) {
  16. service.request.success = success;
  17. service.request.error = iserror;
  18. },
  19. // 安装请求完成后拦截
  20. response: function(success, iserror) {
  21. service.response.success = success;
  22. service.response.error = iserror;
  23. }
  24. },
  25. // 请求前拦截
  26. request: {
  27. // 回调成功执行方法
  28. success: function(e) {
  29. return e
  30. },
  31. // 回调失败
  32. error: function(e) {
  33. return e
  34. }
  35. },
  36. // 请求完成后拦截
  37. response: {
  38. // 回调成功执行方法
  39. success: function(e) {
  40. return e
  41. },
  42. // 回调失败
  43. error: function(e) {
  44. return e
  45. }
  46. },
  47. // 开始请求
  48. open: async function(data) {
  49. return await new Promise(function(resolve, reject) {
  50. let requestData = ''; //保存处理完成后请求数据
  51. //console.log(data);
  52. // 请求前数据处理
  53. requestData = service.request.success(data);
  54. // 保存请求地址;
  55. let url;
  56. if (requestData.url.indexOf('http') > -1) {
  57. url = requestData.url
  58. } else {
  59. url = service.baseURL + requestData.url
  60. }
  61. // 数据复制用于请求
  62. uni.request({
  63. url: url,
  64. method: requestData.method || 'GET',
  65. data: requestData.data,
  66. header: data.header || service.header,
  67. success: res => {
  68. console.log(res,'newrequest res+++++++++++++++++')
  69. if (res.statusCode == 200 || res.statusCode == 401) {
  70. try {
  71. resolve(service.response.success(res));
  72. } catch (e) {
  73. service.response.error(e)
  74. }
  75. }
  76. },
  77. // 请求失败数据处理
  78. fail: (err) => {
  79. service.response.error(err)
  80. }
  81. });
  82. })
  83. },
  84. // 上传文件
  85. upFilse: async function(data) {
  86. return await new Promise(function(resolve, reject) {
  87. let requestData = ''; //保存处理完成后请求数据
  88. try {
  89. // 请求前数据处理
  90. requestData = service.request.success(data);
  91. } catch (e) {
  92. // 请求报错拦截
  93. service.request.error(e);
  94. }
  95. let config = {
  96. // 上传图片的API
  97. upload_img_url: service.baseURL + requestData.url
  98. }
  99. let Uploader = {
  100. // 选择图片
  101. choose(num) {
  102. return new Promise((resolve, reject) => {
  103. uni.chooseImage({
  104. count: num,
  105. sourceType: ['album'],
  106. sizeType: ['compressed'],
  107. success(res) {
  108. // console.log(res);
  109. // 缓存文件路径
  110. resolve(res.tempFilePaths)
  111. },
  112. fail(err) {
  113. console.log(err)
  114. reject(err)
  115. }
  116. })
  117. })
  118. },
  119. // 上传图片
  120. upload_one(path) {
  121. return new Promise((resolve, reject) => {
  122. uni.showLoading({
  123. title: '上传中'
  124. })
  125. let token = uni.getStorageSync('token') || '';
  126. let sign_time = new Date().getTime();
  127. let sys_appid = store.state.sys_appid
  128. let sys_appsecret = store.state.sys_appsecret
  129. // let sys_appid = 'xgred20211207'
  130. // let sys_appsecret = '70211202dergxxgred20211207'
  131. let sys_token = md5(md5(sys_appid + sys_appsecret) + sign_time + sha1(sys_appsecret + sign_time))
  132. uni.uploadFile({
  133. url: config.upload_img_url, //仅为示例,非真实的接口地址
  134. filePath: path,
  135. name: 'file',
  136. header: {
  137. "sys-appid": sys_appid,
  138. "sign-time": sign_time,
  139. "sys-token": sys_token,
  140. "token": token
  141. },
  142. success: (uploadFileRes) => {
  143. if ("string" === typeof uploadFileRes.data) {
  144. resolve(JSON.parse(uploadFileRes.data).data)
  145. } else {
  146. resolve(uploadFileRes.data.data)
  147. }
  148. },
  149. complete() {
  150. uni.hideLoading()
  151. }
  152. });
  153. })
  154. },
  155. // 上传多个图片
  156. upload(path_arr) {
  157. let num = path_arr.length;
  158. return new Promise(async (resolve, reject) => {
  159. let img_urls = []
  160. for (let i = 0; i < num; i++) {
  161. let img_url = await this.upload_one(path_arr[i]);
  162. img_urls.push(img_url)
  163. };
  164. resolve(img_urls)
  165. })
  166. },
  167. // 触发上传图片事件
  168. choose_and_upload(num) {
  169. return new Promise(async (resolve, reject) => {
  170. // 选择图片
  171. let path_arr = await this.choose(num);
  172. // 上传图片
  173. await this.upload(path_arr).then((e) => {
  174. resolve(e);
  175. }).catch((e) => {
  176. reject(e)
  177. });
  178. })
  179. }
  180. };
  181. Uploader.choose_and_upload(1).then((e) => {
  182. resolve(e);
  183. }).catch((e) => {
  184. console.log(e);
  185. })
  186. })
  187. },
  188. };
  189. export default service