request.js 4.4 KB

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