request.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //接口地址
  2. // 生产环境,小程序开发,APP开发
  3. // export const baseUrl = 'http://ddsc.hdlkeji.com';
  4. // 开发环境,web跨域用
  5. export const baseUrl = 'http://127.0.0.1:8099';
  6. export const request = ({
  7. url = '/api',
  8. data,
  9. success,
  10. fail,
  11. complete,
  12. showLoading = true,
  13. ...option
  14. }) => {
  15. // console.log(`${baseUrl}${url}`);
  16. let session_key = '';
  17. try {
  18. // session_key = 'L7uJsCD1lGQfhFV2AvDIeA==';
  19. session_key = uni.getStorageSync('session_key') || '';
  20. } catch (e) {
  21. // 异常
  22. }
  23. if (showLoading) {
  24. uni.showLoading({
  25. title: '加载中...'
  26. });
  27. }
  28. return uni.request({
  29. url: `${baseUrl}${url}`,
  30. timeout: 60000,
  31. dataType: 'json',
  32. data,
  33. header: {
  34. // session_key,
  35. 'Authorization': session_key,
  36. },
  37. ...option,
  38. success: (res) => {
  39. const data = res.data; // 请求结果
  40. const errMsg = res.errMsg; // 错误信息
  41. const statusCode = res.statusCode; // 错误码
  42. // 判断是否请求失败
  43. if (statusCode != 200) {
  44. console.error('请求错误:', res);
  45. uni.showToast({
  46. icon: 'none',
  47. title: `statusCode: ${statusCode}`,
  48. duration: 2000,
  49. });
  50. return;
  51. }
  52. // 判断是否登录
  53. if (data.code == -1) {
  54. uni.showModal({
  55. title: '提示',
  56. content: '请登陆后操作',
  57. success: function (res) {
  58. if (res.confirm) {
  59. uni.reLaunch({
  60. url: '/pages/login/login',
  61. });
  62. } else if (res.cancel) {
  63. // uni.navigateBack({
  64. // delta: 1,
  65. // });
  66. }
  67. }
  68. });
  69. // uni.showToast({
  70. // title:"请登陆后操作",
  71. // icon:"none"
  72. // })
  73. // uni.reLaunch({
  74. // url: '/pages/login/login',
  75. // });
  76. return;
  77. }
  78. // 请求成功且登录
  79. success && success(data);
  80. },
  81. fail: (res) => {
  82. uni.showToast({
  83. icon: 'none',
  84. title: '请求错误',
  85. duration: 2000,
  86. });
  87. fail && fail(res);
  88. },
  89. complete: (res) => {
  90. if (showLoading) {
  91. uni.hideLoading();
  92. }
  93. complete && complete(res);
  94. }
  95. });
  96. };
  97. /**
  98. * 上传图片
  99. */
  100. // export function uploadFilesImages({formData, ...option}) {
  101. // let token = '';
  102. // try {
  103. // token = uni.getStorageSync('token') || '';
  104. // } catch (e) {
  105. // // 异常
  106. // }
  107. // return uni.uploadFile({
  108. // url: `${baseUrl}/api/up_files/upload_image`, //仅为示例,非真实的接口地址
  109. // name: 'files',
  110. // formData: {
  111. // token,
  112. // ...formData,
  113. // },
  114. // ...option,
  115. // });
  116. // }
  117. // ==========================开始============================
  118. /**
  119. * 上传文件
  120. */
  121. export function uploadFiles({formData, ...option}) {
  122. let session_key = '';
  123. try {
  124. session_key = uni.getStorageSync('session_key') || '';
  125. } catch (e) {
  126. // 异常
  127. }
  128. return uni.uploadFile({
  129. url: `${baseUrl}/api/Upload/upload`, //仅为示例,非真实的接口地址
  130. name: 'file',
  131. formData: {
  132. session_key,
  133. ...formData,
  134. },
  135. header: {
  136. // session_key,
  137. 'Authorization': session_key,
  138. },
  139. ...option,
  140. });
  141. }
  142. // ==========================结束============================