httpRequest.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /**
  2. * 常用方法封装 请求,文件上传等
  3. * @author echo.
  4. **/
  5. const tui = {
  6. //接口地址
  7. interfaceUrl: function() {
  8. return 'https://www.abc.com'
  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. showLoading: function(title, mask = true) {
  57. uni.showLoading({
  58. mask: mask,
  59. title: title || '请稍候...'
  60. })
  61. },
  62. /**
  63. * 请求数据处理
  64. * @param string url 请求地址
  65. * @param string method 请求方式
  66. * GET or POST
  67. * @param {*} postData 请求参数
  68. * @param bool isDelay 是否延迟显示loading
  69. * @param bool isForm 数据格式
  70. * true: 'application/x-www-form-urlencoded'
  71. * false:'application/json'
  72. * @param bool hideLoading 是否隐藏loading
  73. * true: 隐藏
  74. * false:显示
  75. */
  76. request: async function(url, method, postData, isDelay, isForm, hideLoading) {
  77. //接口请求
  78. let loadding = false;
  79. tui.delayed && uni.hideLoading();
  80. clearTimeout(tui.delayed);
  81. tui.delayed = null;
  82. if (!hideLoading) {
  83. if (isDelay) {
  84. tui.delayed = setTimeout(() => {
  85. loadding = true
  86. tui.showLoading()
  87. }, 1000)
  88. } else {
  89. loadding = true
  90. tui.showLoading()
  91. }
  92. }
  93. return new Promise((resolve, reject) => {
  94. uni.request({
  95. url: tui.interfaceUrl() + url,
  96. data: postData,
  97. header: {
  98. 'content-type': isForm ? 'application/x-www-form-urlencoded' : 'application/json',
  99. 'Authorization': tui.getToken()
  100. },
  101. method: method, //'GET','POST'
  102. dataType: 'json',
  103. success: (res) => {
  104. clearTimeout(tui.delayed)
  105. tui.delayed = null;
  106. if (loadding && !hideLoading) {
  107. uni.hideLoading()
  108. }
  109. resolve(res.data)
  110. },
  111. fail: (res) => {
  112. clearTimeout(tui.delayed)
  113. tui.delayed = null;
  114. tui.toast("网络不给力,请稍后再试~")
  115. reject(res)
  116. }
  117. })
  118. })
  119. },
  120. /**
  121. * 上传文件
  122. * @param string url 请求地址
  123. * @param string src 文件路径
  124. */
  125. uploadFile: function(url, src) {
  126. tui.showLoading()
  127. return new Promise((resolve, reject) => {
  128. const uploadTask = uni.uploadFile({
  129. url: tui.interfaceUrl() + url,
  130. filePath: src,
  131. name: 'imageFile',
  132. header: {
  133. 'Authorization': tui.getToken()
  134. },
  135. formData: {
  136. // sizeArrayText:""
  137. },
  138. success: function(res) {
  139. uni.hideLoading()
  140. let d = JSON.parse(res.data.replace(/\ufeff/g, "") || "{}")
  141. if (d.code % 100 == 0) {
  142. //返回图片地址
  143. let fileObj = d.data;
  144. resolve(fileObj)
  145. } else {
  146. that.toast(res.msg);
  147. }
  148. },
  149. fail: function(res) {
  150. reject(res)
  151. that.toast(res.msg);
  152. }
  153. })
  154. })
  155. },
  156. tuiJsonp: function(url, callback, callbackname) {
  157. // #ifdef H5
  158. window[callbackname] = callback;
  159. let tuiScript = document.createElement("script");
  160. tuiScript.src = url;
  161. tuiScript.type = "text/javascript";
  162. document.head.appendChild(tuiScript);
  163. document.head.removeChild(tuiScript);
  164. // #endif
  165. },
  166. //设置用户信息
  167. setUserInfo: function(mobile, token) {
  168. uni.setStorageSync("thorui_mobile", mobile)
  169. },
  170. //获取token
  171. getToken() {
  172. return uni.getStorageSync("thorui_token")
  173. },
  174. //判断是否登录
  175. isLogin: function() {
  176. return uni.getStorageSync("thorui_mobile") ? true : false
  177. },
  178. //跳转页面,校验登录状态
  179. href(url, isVerify) {
  180. if (isVerify && !tui.isLogin()) {
  181. uni.navigateTo({
  182. url: '/pages/common/login/login'
  183. })
  184. } else {
  185. uni.navigateTo({
  186. url: url
  187. });
  188. }
  189. }
  190. }
  191. export default tui