diff --git a/service/request.js b/service/request.js index 0da82d3..d7bb065 100644 --- a/service/request.js +++ b/service/request.js @@ -1,228 +1,261 @@ - const serverHost = 'https://pupil.hchyun.com/api' // 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); - } + 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,data) { - 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, - data:data, - 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' - } + get(url, data) { + 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, + data: data, + 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); - // } + 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 && !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' - } + 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) - } + 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) - } - }) - }) - } + 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 - }) - } + // 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 = `/home/classlist` - return new Promise((resolve, reject) => { - resolve(service.get(url)) - }) - }, - // 搜索发现 热搜 - getDiscover(data) { - const url = `/home/discover` - return new Promise((resolve, reject) => { - resolve(service.get(url)) - }) - }, - // 新增 - 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)) - }) - } + 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() { + const url = `/home/classlist` + return new Promise((resolve, reject) => { + resolve(service.get(url)) + }) + }, + // 获取首页轮播图 + getBanner(data) { + const url = `/home/banner` + return new Promise((resolve, reject) => { + resolve(service.get(url, data)) + }) + }, + // 获取产品列表 + getProducts(data) { + const url = `/home/product` + return new Promise((resolve, reject) => { + resolve(service.get(url, data)) + }) + }, + // 获取产品详情 + getProductDetail(id) { + const url = `/home/pdetail/?productId=${id}` + return new Promise((resolve, reject) => { + resolve(service.get(url, id)) + }) + }, + // 获取行业新闻 + getNews() { + const url = `/news/nlists/?pageSize=3&pageNum=1` + return new Promise((resolve, reject) => { + resolve(service.get(url)) + }) + }, + // 获取行业新闻详情 + getNewsDetail(id) { + const url = `/news/ndetail?newsId=${id}` + return new Promise((resolve, reject) => { + resolve(service.get(url, id)) + }) + }, + // 获取认证厂家列表 + getCertifiedCj() { + const url = `/news/flists?pageSize=3&pageNum=1` + return new Promise((resolve, reject) => { + resolve(service.get(url)) + }) + }, + // 获取认证厂家详情 + getCertifiedCjDetail(id) { + const url = `/news/fdetail?fId=1` + return new Promise((resolve, reject) => { + resolve(service.get(url, id)) + }) + }, + + // 解密 + 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 } -export { apiService, toast }