123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- import { API_BASE_URL } from './util'
- const BASE_URL = `${API_BASE_URL}`;
- function request(url: String, options: any) {
- return new Promise((resolve, reject) => {
- let token = wx.getStorageSync('token')
- wx.request(Object.assign({
- url: url.indexOf('://') == -1 ? BASE_URL + url : url,
- method: options.method,
- data: options.data,
- header: Object.assign({ token: token }, options.header),
- success(res: any) {
- if (res.data.code == 0 || res.data.code == 200 || res.data.errno === 0) {
- resolve(res.data)
- } else {
- reject(res)
- }
- },
- fail(err: any) {
- console.log(err, '请求失败')
- reject(err)
- }
- }, options))
- setTimeout(() => reject('time out'), 5000)
- })
- }
- function get(url: String, data: any, options: any = {}) {
- options.method = 'GET'
- options.data = data
- return request(url, options)
- }
- function post(url: String, data: any = {}, options: any = {}) {
- options.method = 'POST'
- options.data = Object.assign({}, data)
- return request(url, options)
- }
- export default {
- request,
- get,
- post
- }
|