123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285 |
- import {
- baseUrl,
- photoUrl
- } from "./config.js"
- import CryptoJS from '../utils/aes_util.js';
- class Request {
- config = {
- baseUrl: baseUrl,
- business: 'data',
- }
- static posUrl(url) {
- return /(http|https):\/\/([\w.]+\/?)\S*/.test(url)
- }
- static getUrl(config) {
- let url = config.url || ''
- let abs = Request.posUrl(url);
- if (!abs) {
- let f = config.slashAbsoluteUrl
- if (f) {
- abs = /^\/([\w.]+\/?)\S*/.test(url)
- }
- }
- return abs ? url : (config.baseUrl + url)
- }
- static getContentType(config) {
- var type = config.contentType || 'json'
- var charset = config.encoding || 'UTF-8'
- if (type === 'json') {
- return 'application/json;charset=' + charset
- } else if (type === 'form') {
- return 'application/x-www-form-urlencoded;charset=' + charset
- } else if (type === 'file') {
- return 'multipart/form-data;charset=' + charset
- } else if (type === 'text') {
- return 'text/plain;charset=' + charset
- } else if (type === 'html') {
- return 'text/html;charset=' + charset
- } else {
- throw new Error('unsupported content type : ' + type)
- }
- }
- interceptor = {
- request: undefined,
- response: undefined,
- fail: undefined,
- complete: undefined // since 1.2.0
- }
- setConfig(config) {
- this.config = Object.assign(this.config, config)
- }
- request(options = {}) {
- var that = this;
- if (options.data === undefined) {
- options.data = {}
- }
- if (options.header === undefined) {
- options.header = {}
- }
- let _options = Object.assign({}, this.config, options)
- _options = Object.assign(options, _options)
- _options.url = Request.getUrl(_options)
- if (!_options.header['Content-Type']) {
- _options.header['Content-Type'] = Request.getContentType(_options)
- let setUerInfo = uni.getStorageSync('setUerInfo');
- let token = setUerInfo ? setUerInfo.token : undefined;
- _options.header["Authorization"] = `Bearer ${token}`
- }
- let _config = _options
- if (that.interceptor.request && typeof that.interceptor.request === 'function') {
- _config = that.interceptor.request(_options)
- }
- let task = undefined
- let promise = new Promise((resolve, reject) => {
- let extras = {}
- that._prepare(that, _config, extras)
- if (_config.contentType === 'file') {
- task = uni.uploadFile({
- ..._config,
- success: res => {
- that._success(that, _config, res, resolve, reject)
- },
- fail: res => {
- that._fail(that, _config, res, resolve, reject)
- },
- complete: (res) => {
- that._complete(that, _config, res, extras)
- }
- })
- if (_config.progress && typeof _config.progress === 'function') {
- task.onProgressUpdate(_res => {
- _config.progress(_res, task)
- })
- }
- } else {
- console.log(_config)
- task = uni.request({
- ..._config,
- success: res => {
- that._success(that, _config, res, resolve, reject)
- },
- fail: res => {
- console.log("----2222--->公用方法_config放回数据", res)
- that._fail(that, _config, res, resolve, reject)
- },
- complete: (res) => {
- that._complete(that, _config, res, extras)
- }
- })
- }
- })
- if (_config.success || _config.fail || _config.complete) {
- return task;
- }
- return promise;
- }
- aa() {
- console.log('掉到了')
- }
- get(options = {}) {
- options.method = 'GET'
- return this.request(options)
- }
- post(options = {}) {
- options.method = 'POST'
- let data = {
- data: CryptoJS.AesEncrypt(JSON.stringify(options.data))
- }
- options.data = data;
- return this.request(options)
- }
- put(options = {}) {
- options.method = 'PUT'
- return this.request(options)
- }
- delete(options = {}) {
- options.method = 'DELETE'
- return this.request(options)
- }
- upload(options = {}) {
- options.method = 'POST'
- options.contentType = 'file'
- options.baseUrl = photoUrl
- return this.request(options)
- }
- _success = function(that, _config, res, resolve, reject) {
- if (res.data.code == "401") {
- that._fail(that, _config, res, resolve, reject);
- return
- }
- let re = null;
- if (_config.contentType == 'file') {
- re = JSON.parse(res?.data);
- } else {
- re = JSON.parse(CryptoJS.AesDecrypt(res?.data))
- }
- // console.log("登录返回信息2123", re)
- if (re.code == "200" || re.code == 200) {
- console.log("解密--------->", re)
- // var result = re
- // console.log("---12result---->公用方法放回数据", result)
- // var parseFileJson = _config.contentType === 'file' && typeof result === 'string' && (_config
- // .dataType ===
- // undefined || _config.dataType === 'json')
- // if (parseFileJson) {
- // result = re.data;
- // }
- // var skip = _config.skipInterceptorResponse
- // if (that.interceptor.response && typeof that.interceptor.response === 'function' && !skip) {
- // result = that.interceptor.response(result, _config)
- // if (_config.businessSuccess) {
- // var _data = _config.business ? result[_config.business] : result;
- // _config.success ? _config.success(_data) : resolve(_data)
- // return;
- // }
- // } else {
- // _config.success ? _config.success(result) : resolve(result)
- // return;
- // }
- return _config.success ? _config.success(re) : resolve(re)
- }
- console.log("---12---->公用方法放回数据", res)
- that._fail(that, _config, res, resolve, reject)
- }
- _fail = function(that, _config, re, resolve, reject) {
- let res = CryptoJS.AesDecrypt(re?.data)
- console.log("----数据失败返回信息", res)
- // console.log("----parBase64Decode--->", parBase64Decode);
- if (res.errMsg === 'request:fail') {
- return
- }
- var result = res
- if (that.interceptor.fail && typeof that.interceptor.fail === 'function') {
- result = that.interceptor.fail(res, _config)
- }
- _config.fail ? _config.fail(result) : reject(result)
- }
- _prepare = function(that, _config, obj = {}) {
- if (that.interceptor.prepare && typeof that.interceptor.prepare === 'function') {
- that.interceptor.prepare(_config, obj)
- return
- }
- obj.startTime = Date.now()
- if (_config.loadingTip) {
- uni.showLoading({
- title: _config.loadingTip
- })
- }
- if (_config.contentType === 'file') {
- if (_config.formData === undefined || _config.formData === null) {
- _config.formData = _config.data
- delete _config.data
- }
- delete _config.header['Content-Type']
- delete _config.header['Referer']
- _config.method = 'POST'
- }
- }
- _complete = function(that, _config, res, obj = {}) {
- if (that.interceptor.complete && typeof that.interceptor.complete === 'function') {
- that.interceptor.complete(_config, obj, res)
- return
- }
- obj.endTime = Date.now()
- if (_config.loadingTip) {
- let diff = obj.endTime - obj.startTime;
- let duration = _config.loadingDuration || 500
- if (diff < duration) {
- diff = duration - diff
- } else {
- diff = 0
- }
- setTimeout(function() {
- uni.hideLoading()
- }, diff)
- }
- if (_config.complete) {
- _config.complete(res)
- }
- }
- }
- var request = new Request()
- request.setConfig({
- baseUrl: baseUrl,
- debug: true
- })
- export default request
- //云支付 https://blog.csdn.net/qq_32340877/article/details/125461425
|