https.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import config from '@/utils/config.js'
  2. module.exports = (param) => {
  3. var url = param.url;
  4. var method = param.method;
  5. var header = param.header || {};
  6. var data = param.data || {};
  7. // 请求方式: GET POST
  8. if(method){
  9. method = method.toUpperCase(); // 小写转成大写
  10. header = {'content-type':'application/x-www-form-urlencoded'}
  11. }
  12. // 发起请求 加载动画
  13. if(!param.hideLoading){
  14. uni.showLoading({title:"加载中..."})
  15. }
  16. // 发起网络请求
  17. uni.request({
  18. url: url,
  19. method:method || "GET",
  20. header:header,
  21. data: data,
  22. success: res => {
  23. // console.log('success', res)
  24. if(res.statusCode && res.statusCode != 200){ // api错误
  25. uni.showModal({
  26. content:res.statusCode + res.data
  27. })
  28. return;
  29. }
  30. if(res.data.code !== config.SUCCESS) {
  31. uni.showToast({
  32. icon:'none',
  33. title:res.data.msg,
  34. duration:2000
  35. })
  36. }
  37. typeof param.success == "function" && param.success(res.data);
  38. },
  39. fail: (e) => {
  40. console.log('fail', e)
  41. uni.showModal({
  42. content: e.errMsg,
  43. showCancel:false
  44. })
  45. typeof param.fail == "function" && param.fail(e.data);
  46. },
  47. complete: () => {
  48. // console.log("网络请求complete");
  49. uni.hideLoading();
  50. typeof param.complete == "function" && param.complete(e.data);
  51. return;
  52. }
  53. })
  54. }