request.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /**
  2. * 常用方法封装 请求,文件上传等
  3. * @author echo.
  4. **/
  5. const axg = {
  6. shopId: function() {
  7. return "10108618787549";
  8. },
  9. //接口地址
  10. interfaceUrl: function() {
  11. // #ifdef H5
  12. return '/api';
  13. // #endif
  14. // #ifdef MP
  15. return 'https://miniapi.jsshuita.com.cn/api/dy'
  16. // return 'http://127.0.0.1:8190/api/dy'
  17. // #endif
  18. },
  19. toast: function(text, duration, success) {
  20. // #ifdef APP-PLUS
  21. plus.nativeUI.toast(text || "出错啦~");
  22. // #endif
  23. // #ifndef MP
  24. uni.showToast({
  25. title: text || "出错啦~",
  26. icon: success ? 'success' : 'none',
  27. duration: duration || 2000
  28. })
  29. // #endif
  30. },
  31. modal: function(title, content, showCancel, callback, confirmColor, confirmText) {
  32. uni.showModal({
  33. title: title || '提示',
  34. content: content,
  35. showCancel: showCancel,
  36. cancelColor: "#555",
  37. confirmColor: confirmColor || "#5677fc",
  38. confirmText: confirmText || "确定",
  39. success(res) {
  40. if (res.confirm) {
  41. callback && callback(true)
  42. } else {
  43. callback && callback(false)
  44. }
  45. }
  46. })
  47. },
  48. isAndroid: function() {
  49. const res = uni.getSystemInfoSync();
  50. if(res.platform.toLocaleLowerCase() == 'android')
  51. {
  52. return true;
  53. }
  54. return false;
  55. },
  56. isPhoneX: function() {
  57. const res = uni.getSystemInfoSync();
  58. let iphonex = false;
  59. let models=['iphonex','iphonexr','iphonexsmax','iphone11','iphone11pro','iphone11promax']
  60. const model=res.model.replace(/\s/g,"").toLowerCase()
  61. if (models.includes(model)) {
  62. iphonex = true;
  63. }
  64. return iphonex;
  65. },
  66. constNum: function() {
  67. let time = 0;
  68. // #ifdef APP-PLUS
  69. time = this.isAndroid() ? 300 : 0;
  70. // #endif
  71. return time
  72. },
  73. delayed: null,
  74. /**
  75. * 请求数据处理
  76. * @param string url 请求地址
  77. * @param string method 请求方式
  78. * GET or POST
  79. * @param {*} postData 请求参数
  80. * @param bool isDelay 是否延迟显示loading
  81. * @param bool isForm 数据格式
  82. * true: 'application/x-www-form-urlencoded'
  83. * false:'application/json'
  84. * @param bool hideLoading 是否隐藏loading
  85. * true: 隐藏
  86. * false:显示
  87. */
  88. request: function(url, method, postData, isDelay, isForm, hideLoading) {
  89. //接口请求
  90. let loadding = false;
  91. axg.delayed && uni.hideLoading();
  92. clearTimeout(axg.delayed);
  93. axg.delayed = null;
  94. if (!hideLoading) {
  95. axg.delayed = setTimeout(() => {
  96. uni.showLoading({
  97. mask: true,
  98. title: '请求中...',
  99. success(res) {
  100. loadding = true
  101. }
  102. })
  103. }, isDelay ? 1000 : 0)
  104. }
  105. var header = {
  106. 'Content-Type': isForm ? 'application/x-www-form-urlencoded' : 'application/json',
  107. 'authorization': "Bearer "+axg.getToken(),
  108. 'platform': "mini"
  109. }
  110. // postData.shop = axg.shopId();
  111. axg.toast(axg.interfaceUrl() + url)
  112. return new Promise((resolve, reject) => {
  113. uni.request({
  114. url: axg.interfaceUrl() + url,
  115. data: postData,
  116. header: header,
  117. method: method, //'GET','POST'
  118. dataType: 'json',
  119. success: (res) => {
  120. clearTimeout(axg.delayed)
  121. axg.delayed = null;
  122. uni.hideLoading()
  123. if (loadding && !hideLoading) {
  124. uni.hideLoading()
  125. }
  126. // if (res.data && res.data.code == 401) {
  127. // uni.showToast({
  128. // title: res.data.msg,
  129. // icon: 'none',
  130. // mask: true,
  131. // duration: 1500,
  132. // success() {
  133. // // uni.navigateTo({
  134. // // url:"/pages/user/login"
  135. // // });
  136. // uni.clearStorageSync();
  137. // }
  138. // });
  139. // return ;
  140. // }
  141. resolve(res.data)
  142. },
  143. fail: (res) => {
  144. clearTimeout(axg.delayed)
  145. axg.delayed = null;
  146. axg.toast("网络不给力,请稍后再试~")
  147. reject(res)
  148. }
  149. })
  150. })
  151. },
  152. /**
  153. * 上传文件
  154. * @param string url 请求地址
  155. * @param string src 文件路径
  156. */
  157. uploadFile: function(url, src, param) {
  158. return new Promise((resolve, reject) => {
  159. const uploadTask = uni.uploadFile({
  160. url: axg.interfaceUrl() + url,
  161. filePath: src,
  162. name: 'file',
  163. header: {
  164. 'authorization': "Bearer "+axg.getToken(),
  165. 'platform': "mini"
  166. },
  167. formData:param,
  168. success: function(res) {
  169. uni.hideLoading()
  170. let d = JSON.parse(res.data.replace(/\ufeff/g, "") || "{}")
  171. if (d.code == 200) {
  172. //返回图片地址
  173. let fileObj = d.data;
  174. resolve(fileObj)
  175. } else {
  176. axg.toast(res.msg);
  177. }
  178. },
  179. fail: function(res) {
  180. console.log('上传错误:',res)
  181. reject(res)
  182. axg.toast(res.msg);
  183. }
  184. })
  185. })
  186. },
  187. tuiJsonp: function(url, callback, callbackname) {
  188. // #ifdef H5
  189. window[callbackname] = callback;
  190. let tuiScript = document.createElement("script");
  191. tuiScript.src = url;
  192. tuiScript.type = "text/javascript";
  193. document.head.appendChild(tuiScript);
  194. document.head.removeChild(tuiScript);
  195. // #endif
  196. },
  197. //设置用户信息
  198. setUserInfo: function(mobile, token) {
  199. //uni.setStorageSync("thorui_token", token)
  200. uni.setStorageSync("user_token", mobile)
  201. },
  202. //获取token
  203. getToken() {
  204. return uni.getStorageSync("user_token") || 'None'
  205. },
  206. //判断是否登录
  207. isLogin: function() {
  208. return uni.getStorageSync("user_token") ? true : false
  209. },
  210. //跳转页面,校验登录状态
  211. href(url, isVerify) {
  212. if (isVerify && !axg.isLogin()) {
  213. uni.navigateTo({
  214. url: '/pages/user/login/login'
  215. })
  216. } else {
  217. uni.navigateTo({
  218. url: url
  219. });
  220. }
  221. }
  222. }
  223. export default axg