request.js 5.2 KB

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