| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- /**
- * Request 0.0.5
- * @Class uni-app request网络请求库
- * @Author lu-ch
- * @Date 2019-06-05
- * @Email webwork.s@qq.com
- * **/
- export default class Request {
- config = {
- baseUrl: '',
- header: {
- 'Content-Type': 'application/json;charset=UTF-8'
- },
- method: 'GET',
- dataType: 'json',
- responseType: 'text',
- success () {
- },
- fail () {
- },
- complete () {
- }
- }
- static posUrl (url) { /* 判断url是否为绝对路径 */
- return /(http|https):\/\/([\w.]+\/?)\S*/.test(url)
- }
- interceptor = {
- request (f) {
- if (f) {
- Request.requestBeforeFun = f
- }
- },
- response (f) {
- if (f) {
- Request.requestComFun = f
- }
- }
- }
- static requestBeforeFun (config) {
- return config
- }
- static requestComFun (response) {
- return response
- }
- setConfig (f) {
- this.config = f(this.config)
- }
- request (options = {}) {
- 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 || {}
- 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) => {
- let statusCode = response.statusCode
- response.config = _config
- response = Request.requestComFun(response)
- if (statusCode === 200) { // 成功
- resolve(response)
- } else {
- reject(response)
- }
- }
- let cancel = (t = 'handle cancel') => {
- let err = {
- errMsg: t,
- config: afC
- }
- reject(err)
- next = false
- }
- let afC = { ...this.config, ...options }
- _config = { ...afC, ...Request.requestBeforeFun(afC, cancel) }
- if (!next) return
- uni.request(_config)
- })
- }
- get (url, data, options = {}) {
- options.url = url
- options.data = data
- options.method = 'GET'
- return this.request(options)
- }
- post (url, data, options = {}) {
- options.url = url
- options.data = data
- options.method = 'POST'
- return this.request(options)
- }
- }
|