httpRequest.js 4.9 KB

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