httpRequest.js 4.9 KB

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