123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- import tools from "@/service/tools";
- import store from '@/store'
- let BASE_URL =''
- //#ifdef APP-PLUS
- BASE_URL ='https://' + process.uniEnv.baseUrl;
- //#endif
- console.log(BASE_URL)
- /**
- * post请求封装
- */
- export const request = (url, method, data, show = false) => {
- console.log(url)
- let token=uni.getStorageSync('token')
- let userId=uni.getStorageSync('userId')
- // data.userId=userId
- let header={
- 'content-type': 'application/json',
- 'hversion': '1.0',
- 'Authorization': '',
- 'CLIENT-TOC': 'Y',
- }
- if(url==='/auth/oauth2/token'){
- token='Basic YXBwOmFwcA=='
- // header['CLIENT-TOC']='Y'
- url=url+tools.addQueryString(data)
- }else {
- token= 'Bearer '+token
- }
- header.Authorization=token
- // console.log(header)
- // data.token = token;
- // data.m_id = uni.getStorageSync('m_id')
- if (show) {
- tools.showLoading();
- }
- return new Promise((resolve, reject) => {
- // 封装主体:网络请求
- // console.log(BASE_URL+ url)
- // console.log(data)
- uni.request({
- url:BASE_URL+ url,
- data: data,
- method: method || 'post',
- header: header,
- success: (res) => {
- // console.log(res); // 控制台显示数据信息
- if (res.statusCode === 200) {
- // console.log('应答信息-----------------------')
- tools.hideLoading();
- if (res.data.code * 1 === 401) {
- uni.clearStorageSync()
- uni.reLaunch({
- url: '/pages/login/index'
- });
- } else {
- resolve(res.data)
- }
- }else if(res.statusCode === 401){
- // uni.clearStorageSync()
- // uni.reLaunch({
- // url: '/pages/login/index'
- // });
- tools.error(res.data.data)
- } else{
- tools.error('网络错误!!')
- }
- },
- fail: (err) => {
- tools.error('网络错误!!')
- // 返回错误消息
- reject(err)
- tools.hideLoading();
- }
- })
- })
- }
- /**
- * 文件上传
- */
- export const uploadImage = (filePath, imageName, data) => {
- tools.showLoading();
- return new Promise((resolve, reject) => {
- uni.uploadFile({
- url: BASE_URL + '/api/common/upload',
- // header: {
- // 'Authorization': JSON.parse(uni.getStorageSync('userInfo')).token
- // },
- filePath: filePath,
- name: imageName,
- formData: data,
- success: (res) => {
- tools.hideLoading();
- resolve(res)
- },
- fail(err) {
- tools.hideLoading();
- reject(err)
- }
- });
- })
- }
- export const uploadFile = (url, file) => {
- tools.showLoading();
- let token = uni.getStorageSync('token') || '';
- // data.app_version = plus.runtime.version;
- // console.log('请求地址-----------------------')
- // console.log(BASE_URL + url)
- // console.log('请求参数-----------------------')
- // console.log(data)
- console.log(file)
- return new Promise((resolve, reject) => {
- // 封装主体:网络请求
- console.log(BASE_URL + url)
- uni.uploadFile({
- url: BASE_URL + url, //仅为示例,非真实的接口地址
- filePath: file,
- name: 'file',
- header: {
- 'content-type': 'application/json',
- 'apikey': '01ABA77B8BA6F26243D85B5A50FFB88624951C7B3A4078B3F9971983E667D5B8',
- 'hversion': '1.0',
- 'Authorization': token
- },
- success: (res) => {
- tools.hideLoading();
- if (res.statusCode === 200) {
- // console.log('应答信息-----------------------')
- res.data = JSON.parse(res.data)
- // console.log(res.data); // 控制台显示数据信息
- if (res.data.code * 1 === 401) {
- uni.reLaunch({
- url: '/pages/login/login'
- });
- } else {
- resolve(res.data)
- }
- } else {
- tools.error('网络错误!!')
- }
- },
- fail: (err) => {
- tools.error('网络错误!!')
- // 返回错误消息
- reject(err)
- tools.hideLoading();
- }
- });
- })
- }
|