123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- const devBaseUrl = 'uav.djiplant.com:3100'
- const proBaseUrl = 'fly.djiplant.com:3300'
- const env = wx.getAccountInfoSync();
- const {
- envVersion
- } = env.miniProgram
- export const baseUrl = envVersion === 'release' ? proBaseUrl : devBaseUrl
- let is401 = false
- const defaultOptions = {
- loading: true,
- timeout: 5000,
- successMessage: true,
- errorMessage: true,
- header: {
- "content-type": "application/json"
- },
- }
- export function upLoadRequest(
- url,
- file = "",
- options = {}
- ) {
- return new Promise((resolve, reject) => {
- const { token } = wx.getStorageSync('userInfo') || {}
- options = Object.assign(defaultOptions, options)
- if (is401) reject()
- if (options.loading) wx.showToast({
- title: 'loading...',
- icon: 'none'
- })
- if (token) {
- options.header['X-Access-Token'] = token
- options.header['Authorization'] = token
- }
- wx.uploadFile({
- url: `https://${baseUrl}/hisun-boot${url}`, // 仅为示例,非真实的接口地址
- filePath: file.url,
- name: 'file',
- header: options.header,
- success(res) {
-
- if (typeof(res.data)=='string') {
- let _res =JSON.parse(res.data)
- if (_res.code==200) {
- resolve(_res.result)
- }
-
-
- }else{
- if (res.data==200) {
- resolve(res.data)
- }
- }
-
- console.log(res);
- },
- });
- })
-
- }
- export function request(
- url,
- method = 'get',
- data = {},
- options = {}
- ) {
- return new Promise((resolve, reject) => {
- const { token } = wx.getStorageSync('userInfo') || {}
- options = Object.assign(defaultOptions, options)
- if (is401) reject()
- if (options.loading) wx.showToast({
- title: 'loading...',
- icon: 'none'
- })
- if (token) {
- options.header['X-Access-Token'] = token
- options.header['Authorization'] = token
- }
- wx.request({
- url: `https://${baseUrl}/hisun-boot${url}`,
- method: method.toLocaleUpperCase(),
- data: data,
- header: options.header,
- timeout: options.timeout,
- // 成功回调
- success: res => {
- console.log('request::success', res);
- wx.hideToast()
- if (res?.data?.code === 401) {
- is401 = true
- wx.showToast({
- title: '登录过期, 请重新登录!',
- icon: 'error',
- mask: true,
- duration: 2000,
- complete() {
- is401 = false
- wx.clearStorageSync()
- wx.reLaunch({
- url: '/pages/may/index',
- })
- }
- })
- } else if ([200, 0].includes(res?.data?.code)) {
- options.success && wx.showToast({
- title: '提交成功!',
- icon: 'success',
- duration: 1000
- })
- resolve(res.data.result)
- } else {
- options.errorMessage && wx.showToast({
- title: res?.data?.message || '网络错误!',
- icon: 'error',
- duration: 1000
- })
- reject()
- }
- },
- // 错误回调
- fail: err => {
- console.log('request::error', err);
- wx.hideToast()
- options.errorMessage && wx.showToast({
- title: err.errMsg || '网络错误!',
- icon: 'error',
- duration: 1000
- })
- reject()
- },
- });
- })
- }
|