request.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // var siteConfig = {
  2. // uniacid: 19,
  3. // siteroot: "https://ins.iduomi.cc/addons/wike_chatgpt/public/index.php/",
  4. // root: "https://ins.iduomi.cc/addons/wike_chatgpt/public/",
  5. // };
  6. // var siteConfig = {
  7. // uniacid: 2,
  8. // siteroot: "https://dev.iduomi.cc/addons/wike_chatgpt/public/index.php/",
  9. // root: "https://dev.iduomi.cc/addons/wike_chatgpt/public/",
  10. // };
  11. // var siteConfig = {
  12. // uniacid: 5059,
  13. // siteroot: "http://nywhcm.com/addons/wike_aging/public/index.php/",
  14. // root: "http://nywhcm.com/addons/wike_aging/public/",
  15. // };
  16. // var siteConfig = {
  17. // uniacid: 1,
  18. // siteroot: "https://ins.iduomi.cc/addons/wike_chatgpt/public/index.php/",
  19. // root: "https://ins.iduomi.cc/addons/wike_chatgpt/public/",
  20. // };
  21. var siteConfig = {
  22. uniacid: 1,
  23. siteroot: "http://t20.9026.com/",
  24. root: "http://t20.9026.com/"
  25. };
  26. let siteInfo = siteConfig;
  27. let uniacid = 1;
  28. // if (siteInfo.uniacid) {
  29. // uni.setStorageSync('uniacid', siteInfo.uniacid);
  30. // uniacid = siteInfo.uniacid
  31. // } else {
  32. // uniacid = uni.getStorageSync('uniacid')
  33. // }
  34. export const API_ROOT = `${siteInfo.root}`; //后台接口域名
  35. export const API_URL = `${siteInfo.root}`; //后台接口域名
  36. export const UNIACID = `${uniacid}`;
  37. export const IMG_URL = `${siteInfo.root}static/mobile/images`;
  38. export function apiurl(action) {
  39. return 'http://t20.9026.com/api';
  40. }
  41. import platform from '@/common/platform/index';
  42. export default class Request {
  43. constructor() {
  44. // console.log('uniacid:'+uniacid);
  45. // 默认配置
  46. this.config = {
  47. baseUrl: siteInfo.siteroot,
  48. header: {
  49. 'content-type': 'application/json',
  50. 'platform': platform.get(),
  51. 'uniacid': uniacid,
  52. 'Authorization': uni.getStorageSync('token')
  53. },
  54. url: '',
  55. data: {},
  56. params: {},
  57. method: 'GET',
  58. dataType: 'json',
  59. // #ifndef MP-ALIPAY || APP-PLUS
  60. responseType: 'text',
  61. // #endif
  62. custom: {},
  63. // #ifdef APP-PLUS
  64. sslVerify: false
  65. // #endif
  66. }
  67. /* 拦截器 */
  68. this.interceptor = {
  69. request: cb => {
  70. if (cb) {
  71. this.requestBefore = cb
  72. } else {
  73. this.requestBefore = request => request
  74. }
  75. },
  76. response: (cb) => {
  77. if (cb) {
  78. this.requestAfter = cb
  79. } else {
  80. this.requestAfter = response => response
  81. }
  82. }
  83. }
  84. }
  85. /* 判断url是否完整 */
  86. static isUrl(url) {
  87. return true
  88. }
  89. static addQueryString(params) {
  90. let paramsData = ''
  91. Object.keys(params).forEach(key => {
  92. paramsData += key + '=' + encodeURIComponent(params[key]) + '&'
  93. })
  94. return paramsData.substring(0, paramsData.length - 1)
  95. }
  96. /* 请求前 */
  97. static requestBefore(config) {
  98. return config
  99. }
  100. /* 请求后 */
  101. static requestAfter(response) {
  102. return response
  103. }
  104. /*设置全局配置*/
  105. setConfig(func) {
  106. return func(this.config)
  107. }
  108. /**
  109. * @Function
  110. * @param {Object} options - 请求配置项
  111. * @prop {String} options.url - 请求路径
  112. * @prop {Object} options.data - 请求参数
  113. * @prop {Object} [options.responseType = config.responseType] [text|arraybuffer] - 响应的数据类型
  114. * @prop {Object} [options.dataType = config.dataType] - 如果设为 json,会尝试对返回的数据做一次 JSON.parse
  115. * @prop {Object} [options.header = config.header] - 请求header
  116. * @prop {Object} [options.method = config.method] - 请求方法
  117. * @returns {Promise<unknown>}
  118. */
  119. async request(options = {}) {
  120. options = {
  121. ...options,
  122. ...this.config,
  123. ...this.requestBefore(options)
  124. }
  125. return new Promise((resolve, reject) => {
  126. let mergeUrl = 'http://t20.9026.com/api/order/course/list';
  127. if (JSON.stringify(options.params) !== '{}') {
  128. let query = Request.addQueryString(options.params);
  129. mergeUrl += mergeUrl.indexOf('?') === -1 ? `?${query}` : `&${query}`
  130. }
  131. options.url = mergeUrl
  132. options.success = res => {
  133. resolve(this.requestAfter(res.data))
  134. }
  135. options.fail = err => {
  136. reject(this.requestAfter(err))
  137. }
  138. uni.request(options)
  139. })
  140. }
  141. get(url, options = {}) {
  142. return this.request({
  143. url,
  144. method: 'GET',
  145. ...options
  146. })
  147. }
  148. post(url, data, options = {}) {
  149. return this.request({
  150. url,
  151. data,
  152. method: 'POST',
  153. ...options
  154. })
  155. }
  156. }