| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- /**
- * Request 0.0.7
- * @Class uni-app request网络请求库
- * @Author lu-ch
- * @Date 2019-07-11
- * @Email webwork.s@qq.com
- * http://ext.dcloud.net.cn/plugin?id=392
- * **/
- import { APIHOST } from '@/config'
- // 基础配置
- const constant = {
- hostUrl: APIHOST,
- source: 'mobile'
- }
- class Request {
- config = {
- baseUrl: constant.hostUrl,
- header: {
- 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
- },
- data: {},
- method: 'GET',
- dataType: 'json', /* 如设为json,会对返回的数据做一次 JSON.parse */
- responseType: 'text',
- success() { },
- fail() { },
- complete() { }
- }
- static posUrl(url) { /* 判断url是否为绝对路径 */
- return /(http|https):\/\/([\w.]+\/?)\S*/.test(url)
- }
- interceptor = {
- request: (f) => {
- if (f && typeof f === 'function') {
- this.requestBeforeFun = f
- }
- },
- response: (f) => {
- if (f && typeof f === 'function') {
- this.requestComFun = f
- }
- }
- }
- requestBeforeFun(config, cancel) {
- let token = ''
- try {
- const st = uni.getStorageSync('token')
- if (st) {
- token = st
- }
- } catch (e) {
- console.error(e, '获取token异常')
- }
- config.header = {
- ...config.header
- }
- // 将token 放入header
- if (token) {
- config.header['token'] = token
- }
- config.header['appVersion'] = 2
- config.headers = {
- 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
- }
- const account = uni.getStorageSync('account') || null
- if (account) {
- config.header['account'] = account
- }
- if (constant.source == 'mobile') {
- const shopId = uni.getStorageSync('shopId') || 0
- if (shopId) {
- config.header['shopId'] = shopId
- }
- }
- if (config.config.loading) {
- uni.showLoading({
- title: '',
- mask: true
- })
- }
- return config
- }
- requestComFun(response) {
- if (response.config.config.loading) {
- uni.hideLoading()
- }
- return response
- }
- setConfig(f) {
- this.config = f(this.config)
- }
- request(options = {}, config) {
- options.baseUrl = options.baseUrl || this.config.baseUrl
- options.dataType = options.dataType || this.config.dataType
- options.url = Request.posUrl(options.url) ? options.url : (options.baseUrl + options.url)
- options.data = options.data || this.config.data
- options.header = options.header || this.config.header
- options.method = options.method || this.config.method
-
- return new Promise((resolve, reject) => {
- let next = true
- let _config = null
-
- options.complete = (response) => {
- const statusCode = response.statusCode
- response.config = _config
- response.config.config = config
- response = this.requestComFun(response)
-
- // 全局忽略错误,在单个请求里面自行处理
- if (config && config.hideError) {
- resolve(response.data)
- return
- }
-
- if (statusCode === 200) {
- if (response.data.code === 1) {
- resolve(response.data)
- } else {
- if (response.data.code == 0) {
- if (!config.hideError) {
- uni.showToast({ title: response.data.msg, icon: 'none', mask: false, duration: 2000 })
- }
- reject(response.data)
- } else if (response.data.code && response.data.code == -1) {
- // 特殊处理
- } else if (response.data.code == 2) {
- uni.removeStorageSync('token')
- uni.removeStorageSync('shopUser')
- uni.removeStorageSync('account')
- } else {
- if (!config.hideError) {
- uni.showToast({ title: response.data.msg, icon: 'none', mask: false })
- }
- reject(response.data)
- }
- }
- } else {
- if (!config.hideError) {
- uni.showToast({
- title: response.data.message || '请求失败',
- icon: 'none'
- })
- }
- reject(response)
- }
- }
-
- const cancel = (t = 'handle cancel') => {
- const err = {
- errMsg: t,
- config: afC
- }
- reject(err)
- next = false
- }
-
- const afC = { ...this.config, ...options, config: config }
- _config = { ...afC, ...this.requestBeforeFun(afC, cancel) }
- if (!next) return
- uni.request(_config)
- })
- }
- get(url, data = {}, options = {}, config = {}) {
- return this.request({
- url,
- data,
- method: 'GET',
- ...options
- }, config)
- }
- post(url, data = {}, options = {}, config = {}) {
- //如果有account要带上 ssh 20250812
- let account = uni.getStorageSync('account') || 0;
- url = url+'?account='+account
- return this.request({
- url,
- data,
- method: 'POST',
- ...options
- }, config)
- }
- }
- const https = new Request()
- export default https
|