index.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. import app from "@/app.js"
  2. let settings = {
  3. method: "get", // 默认请求方法
  4. contentType: "application/json", // 传参格式
  5. dataType: "json", // 返回值类型
  6. baseUrl: app.baseUrl + '/api/app', // 请求根地址
  7. }
  8. let loadNum = 0; //加载框的数量
  9. let loadingShow = () => {
  10. loadNum++
  11. uni.showLoading({
  12. title: 'loading...'
  13. });
  14. }
  15. let loadingHide = () => {
  16. loadNum--
  17. if (loadNum <= 0) {
  18. uni.hideLoading();
  19. }
  20. }
  21. function x(options = null) {
  22. // 返回当前实例对象 无需手动return
  23. return new x.fn.init(options);
  24. }
  25. x.fn = x.prototype = {
  26. constructor: x,
  27. config(options) {
  28. // 解构并设置默认值
  29. let {
  30. baseUrl,
  31. url,
  32. data,
  33. method,
  34. contentType,
  35. dataType
  36. } = options;
  37. // 请求头参数 写入token和lang
  38. let auth = null;
  39. if (uni.getStorageSync('token')) {
  40. auth = uni.getStorageSync('token');
  41. }
  42. const header = auth ? {
  43. 'X-Requested-With': 'XMLHttpRequest',
  44. lang: uni.getStorageSync('lang') || 'tw',
  45. authorization: `bearer ${auth}`
  46. } : {
  47. 'X-Requested-With': 'XMLHttpRequest',
  48. lang: uni.getStorageSync('lang') || 'tw',
  49. };
  50. this.header = header;
  51. // 请求地址解析
  52. if (url.startsWith('http')) { // 外部链接
  53. this.url = url;
  54. } else { // 本地相对路径
  55. this.url = baseUrl + url;
  56. }
  57. if (data) this.data = data;
  58. if (method) this.method = method;
  59. if (contentType) this.contentType = contentType;
  60. if (dataType) this.dataType = dataType;
  61. },
  62. init(options) {
  63. // 将用户参数 写入配置信息
  64. this.config(Object.assign(settings, options));
  65. let { config = {} } = options
  66. // 提示状态
  67. if (config.loading) {
  68. loadingShow()
  69. }
  70. return new Promise((resolve, reject) => {
  71. uni.request({
  72. url: this.url,
  73. data: this.data,
  74. method: this.method,
  75. header: this.header,
  76. dataType: this.dataType,
  77. sslVerify: false,
  78. success: (res) => {
  79. let message = res.data.message
  80. let code = res.data.code
  81. if (code != 200) {
  82. switch (code) {
  83. case 1003: // 登陆失效 清除状态 重新登陆
  84. // 清除session
  85. uni.removeStorageSync('token');
  86. uni.redirectTo({
  87. url: "/pages/login/index",
  88. });
  89. break;
  90. case 1021:
  91. resolve(res.data);
  92. break;
  93. default:
  94. reject(message);
  95. break;
  96. }
  97. if (config.toast !== false&&message) {
  98. uni.showToast({
  99. title: message,
  100. duration: 2000,
  101. icon: 'none'
  102. });
  103. }
  104. } else {
  105. if(res.data) {
  106. resolve(res.data); // 直接返回数据
  107. }else {
  108. console.log('dd')
  109. resolve('success');
  110. }
  111. if (config.toast&&message) {
  112. uni.showToast({
  113. title: message,
  114. duration: 2000,
  115. icon: 'none'
  116. });
  117. }
  118. }
  119. },
  120. fail: (err) => {
  121. reject(err)
  122. if (config.toast !== false) {
  123. uni.showToast({
  124. title: 'error reload!',
  125. icon: "none"
  126. });
  127. }
  128. if (err) {
  129. throw new Error();
  130. }
  131. },
  132. complete: () => {
  133. loadingHide()
  134. }
  135. })
  136. })
  137. },
  138. // 使用promise封装同步化的确认框
  139. confirmSync(content, fullfilled, rejected = null) {
  140. let showCancel = false;
  141. if (rejected instanceof Function) {
  142. showCancel = true;
  143. }
  144. return new Promise(function (resolve, reject) {
  145. uni.showModal({
  146. content,
  147. showCancel,
  148. success(res) { // confirm or cancel
  149. if (res.confirm) {
  150. resolve(fullfilled()); // 执行动作 需要返回值 则标记到resolve中
  151. } else if (res.cancel && rejected) {
  152. reject(rejected()); // 执行动作 需要返回值 则标记到reject中
  153. }
  154. }
  155. })
  156. })
  157. },
  158. get(url, data = null, config = {}) {
  159. return x({
  160. method: "get",
  161. url,
  162. data,
  163. config
  164. })
  165. },
  166. post(url, data, config = {}) {
  167. return x({
  168. method: "post",
  169. url,
  170. data,
  171. config
  172. })
  173. },
  174. // data 为uni的chooseImage
  175. uploadFile(url, data, config = {}) {
  176. let auth = null;
  177. if (uni.getStorageSync('token')) {
  178. auth = uni.getStorageSync('token');
  179. }
  180. let header = {
  181. 'X-Requested-With': 'XMLHttpRequest',
  182. lang: "cn",
  183. }
  184. if (auth) header.authorization = `bearer ${auth}`;
  185. if (config.loading !== false) {
  186. loadingShow()
  187. }
  188. return new Promise((resolve, reject) => {
  189. uni.uploadFile({
  190. url: settings.baseUrl + url, //仅为示例,非真实的接口地址
  191. filePath: data.tempFilePaths[0],
  192. name: 'image',
  193. formData: {},
  194. sslVerify: false,
  195. header,
  196. success: (res) => {
  197. resolve(JSON.parse(res.data))
  198. },
  199. fail: () => {
  200. reject()
  201. },
  202. complete: () => {
  203. loadingHide()
  204. }
  205. });
  206. })
  207. },
  208. head() {
  209. },
  210. put() {
  211. },
  212. // ...
  213. }
  214. x.fn.init.prototype = x.fn, x.extend = x.fn.extend = function (obj, prop) {
  215. if (!prop) { //如果未设置prop 则表示给this扩展一个对象的内容
  216. prop = obj;
  217. obj = this;
  218. }
  219. for (var i in prop) obj[i] = prop[i];
  220. }, x.extend(x.fn);
  221. export default x;