处理微信小程序 Babel regenerator 的 "a" is read-only 问题
This commit is contained in:
+20
-19
@@ -28,14 +28,13 @@ function toQueryString(params) {
|
||||
* {boolean} loading - 是否显示 loading,默认 true
|
||||
* {boolean} toast - 是否在失败时弹 toast,默认 true
|
||||
*/
|
||||
export function request(url, options = {}) {
|
||||
const {
|
||||
method = 'GET',
|
||||
data,
|
||||
header = {},
|
||||
loading = true,
|
||||
toast = true
|
||||
} = options
|
||||
export function request(url, options) {
|
||||
const opts = options || {}
|
||||
const method = opts.method || 'GET'
|
||||
const data = opts.data
|
||||
const header = opts.header || {}
|
||||
const loading = opts.loading !== false
|
||||
const toast = opts.toast !== false
|
||||
|
||||
const userStore = useUserStore()
|
||||
const headers = {
|
||||
@@ -47,22 +46,22 @@ export function request(url, options = {}) {
|
||||
}
|
||||
|
||||
// GET: 手动拼 query string(不依赖 uni.request 各平台差异)
|
||||
let fullUrl = `${baseUrl}${url}`
|
||||
let fullUrl = baseUrl + url
|
||||
if (method === 'GET' && data) {
|
||||
fullUrl += toQueryString(data)
|
||||
fullUrl = fullUrl + toQueryString(data)
|
||||
}
|
||||
|
||||
if (loading) {
|
||||
uni.showLoading({ title: '加载中...', mask: true })
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
return new Promise(function (resolve, reject) {
|
||||
uni.request({
|
||||
url: fullUrl,
|
||||
method,
|
||||
method: method,
|
||||
data: method === 'POST' ? data : undefined,
|
||||
header: headers,
|
||||
success: (res) => {
|
||||
success: function (res) {
|
||||
if (loading) uni.hideLoading()
|
||||
const body = res.data
|
||||
if (body.code !== 1) {
|
||||
@@ -81,7 +80,7 @@ export function request(url, options = {}) {
|
||||
}
|
||||
resolve(body)
|
||||
},
|
||||
fail: (err) => {
|
||||
fail: function (err) {
|
||||
if (loading) uni.hideLoading()
|
||||
uni.showToast({ title: '网络连接失败', icon: 'none' })
|
||||
reject(err)
|
||||
@@ -95,18 +94,20 @@ export function request(url, options = {}) {
|
||||
* 兼容两种调用方式:
|
||||
* get('/path?key=val') 旧风格(URL 自带参数)
|
||||
* get('/path', { key: 'val' }) 新风格(参数对象,自动编码)
|
||||
* 不可对形参 url 做 += 重赋值,正式版 Babel 会把形参编译为只读,触发 "a" is read-only
|
||||
*/
|
||||
export const get = (url, params) => {
|
||||
const fullUrl = params ? `${url}${toQueryString(params)}` : url
|
||||
export function get(url, params) {
|
||||
let fullUrl = url
|
||||
if (params) {
|
||||
fullUrl = url + toQueryString(params)
|
||||
}
|
||||
return request(fullUrl, { method: 'GET' })
|
||||
}
|
||||
|
||||
/**
|
||||
* POST 快捷方法
|
||||
*/
|
||||
export const post = (url, data) => {
|
||||
return request(url, { method: 'POST', data })
|
||||
export function post(url, data) {
|
||||
return request(url, { method: 'POST', data: data })
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user