diff --git a/service/request.js b/service/request.js new file mode 100644 index 0000000..1cbc593 --- /dev/null +++ b/service/request.js @@ -0,0 +1,221 @@ + +const serverHost = 'https://api-ugo-web.itheima.net' +function isOutTime(res) { + if (res.data.message === '请先登录') { + uni.showToast('登录信息已过期,请重新登录') + setTimeout(() => { + uni.redirectTo({ + url: '/pages/login/login' + }) + }, 1000) + uni.removeStorageSync('userInfo') + uni.removeStorageSync('token') + } else { + // uni.showToast(res.data.message); + } +} + +const service = { + get(url) { + const header = {} + header['Authorization'] = 'Bearer ' + uni.getStorageSync('token') + header['content-type'] = 'application/json' + return new Promise((resolve, reject) => { + uni.request({ + method: 'get', + url: serverHost + url, + header: header, + success: res => { + // // 调用接口成功 + // if (!res.data.data) { + // isOutTime(res) + // reject(res) + // } + resolve(res) + }, + fail: err => { + // 调用接口失败 + reject(err) + } + }) + }) + }, + + post(url, data, isLogin) { + const header = {} + if (isLogin) { + // header['content-type'] = 'application/x-www-form-urlencoded'; + console.log(isLogin) + header['Authorization'] = '' + } else { + header['Authorization'] = 'Bearer ' + uni.getStorageSync('token') + header['content-type'] = 'application/json' + } + + return new Promise((resolve, reject) => { + uni.request({ + method: 'post', + url: serverHost + url, + data: data, + header: header, + timeout: 30000, + success: res => { + console.log(1111, res) + uni.hideLoading() + // if (res.data.exception === 'UnAuthorizedException') { + // let pages = getCurrentPages() + // let page = (pages[pages.length - 1]).route + // if (!page.includes('login')) { + // uni.navigateTo({ + // url: `/pages/login/login` + // }) + // } + // } + // if (res.data.message) { + // isOutTime(res); + // toast.error(res.data.message) + // reject(res); + // } + + // if (isLogin && !res.data.access_token) { + // reject(res); + // } else { + // resolve(res); + // } + resolve(res) + }, + fail: err => { + console.log('err', err) + uni.hideLoading() + // 调用接口失败 + try { + toast.error(err.data.message) + } catch (errr) { + toast.error('服务器出错') + } + reject(err) + } + }) + }) + }, + put(url, data, isLogin) { + const header = {} + + if (isLogin) { + header['content-type'] = 'application/x-www-form-urlencoded' + } else { + header['Authorization'] = 'Bearer ' + uni.getStorageSync('token') + header['content-type'] = 'application/json' + } + + return new Promise((resolve, reject) => { + uni.request({ + method: 'put', + url: serverHost + url, + header: header, + success: res => { + if (!res.data.flag) { + isOutTime(res) + reject(res) + } + + resolve(res) + }, + data: data, + fail: err => { + // 调用接口失败 + // toast.error(res.data.message) + uni.hideLoading() + reject(err) + } + }) + }) + } + +} +const toastDuration = 1500 +const toast = { + // duration: 1500, + success: (text, dur) => { + // $wuxToptips().success({ + // hidden: false, + // text: text || '成功', + // duration: dur || toastDuration + // }); + uni.showToast({ + title: text || '成功', + duration: dur || toastDuration + }) + }, + warn: text => { + // $wuxToptips().warn({ + // hidden: false, + // text: text || '未知警告', + // duration: toastDuration + // }); + uni.showToast({ + icon: 'none', + title: text || '未知警告', + duration: toastDuration + }) + }, + error: text => { + // $wuxToptips().error({ + // hidden: false, + // text: text || '未知错误', + // duration: toastDuration + // }); + uni.showToast({ + icon: 'none', + title: text || '未知错误', + duration: toastDuration + }) + } +} +const apiService = { + uploadImgUrl: serverHost, + imgUrl: serverHost, + login: data => { + data = Object.assign(data || {}, {}) + const url = `/api` + return new Promise((resolve, reject) => { + resolve(service.post(url, data, true)) + }) + }, + // 获取 + getCategories(data) { + const url = `/api/public/v1/categories` + return new Promise((resolve, reject) => { + resolve(service.get(url, data)) + }) + }, + // 新增 + addOutboundOrder(data) { + const url = `/api` + return new Promise((resolve, reject) => { + resolve(service.post(url, data)) + }) + }, + // 提交 + deliveryOrderConfirm(data) { + const url = `/api` + return new Promise((resolve, reject) => { + resolve(service.post(url, data)) + }) + }, + // 解密 + decrypt(data) { + const url = `/api` + return new Promise((resolve, reject) => { + resolve(service.post(url, { data })) + }) + }, + // 国密sm4加密 + sm4Encrypt(data) { + const url = `/api` + return new Promise((resolve, reject) => { + resolve(service.sm(url, data)) + }) + } +} +export { apiService, toast }