123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- import store from '../store';
- import {
- getUpimgKey
- } from '@/api/login.js';
- let service = {
- baseURL: store.state.baseURL, // 请求头
- header: {
- 'Content-Type': "application/x-www-form-urlencoded"
- },
- // 请求拦截器
- interceptors: {
- // 安装请求前拦截
- request: function(success, iserror) {
- service.request.success = success;
- service.request.error = iserror;
- },
- // 安装请求完成后拦截
- response: function(success, iserror) {
- service.response.success = success;
- service.response.error = iserror;
- }
- },
- // 请求前拦截
- request: {
- // 回调成功执行方法
- success: function(e) {
- return e
- },
- // 回调失败
- error: function(e) {
- return e
- }
- },
- // 请求完成后拦截
- response: {
- // 回调成功执行方法
- success: function(e) {
- return e
- },
- // 回调失败
- error: function(e) {
- return e
- }
- },
- // 开始请求
- open: async function(data) {
- return await new Promise(function(resolve, reject) {
- let requestData = ''; //保存处理完成后请求数据
- let url = '' //保存链接地址
- //console.log(data);
- // 请求前数据处理
- requestData = service.request.success(data);
- // #ifdef H5
- if (process.env.NODE_ENV === 'development') {
- url = requestData.url;
- console.log('开发环境')
- } else {
- url = service.baseURL + requestData.url;
- console.log('生产环境')
- }
- // #endif
- // #ifndef H5
- url = service.baseURL + requestData.url;
- // #endif
- console.log(url, '请求地址信息');
- // 数据复制用于请求
- uni.request({
- url: url,
- method: requestData.method || 'GET',
- data: requestData.data,
- header: data.header || service.header,
- success: res => {
- if (res.statusCode == 200) {
- try {
- resolve(service.response.success(res));
- } catch (e) {
- service.response.error(e)
- }
- }
- },
- // 请求失败数据处理
- fail: (err) => {
- service.response.error(err)
- }
- });
- })
- },
- // 上传文件
- upFilse: async function(data) {
- return await new Promise(async function(resolve, reject) {
- let requestData = ''; //保存处理完成后请求数据
- let url = '' //保存请求地址
- try {
- // 请求前数据处理
- requestData = service.request.success(data);
- } catch (e) {
- // 请求报错拦截
- service.request.error(e);
- }
- // #ifdef H5
- if (process.env.NODE_ENV === 'development') {
- url = requestData.url;
- console.log('开发环境')
- } else {
- url = service.baseURL + requestData.url;
- console.log('生产环境')
- }
- // #endif
- // #ifndef H5
- url = service.baseURL + requestData.url;
- // #endif
- let config = {
- // 上传图片的API
- upload_img_url: url,
- data: requestData.data,
- file_name: requestData.data.file_name || 'user/' + store.state.user.userInfo.uid
- }
- // uni.showLoading({
- // title: '请求key中',
- // mark: true,
- // })
- try {
- // const {
- // data
- // } = await getUpimgKey({
- // file_name: config.file_name
- // })
- // uni.hideLoading()
- // setTimeout(()=>{
- uni.chooseImage({
- count: 1,
- sizeType: ['compressed'],
- sourceType: ['camera', 'album'],
- success(res) {
- uni.showLoading({
- title: '上传中',
- mark: true,
- })
- try {
- // console.log(res, '选中图片');
- const time = (new Date()).getTime();
- // #ifdef H5
- const formData = {
- // "policy": data.sign.policy,
- // "OSSAccessKeyId": data.sign.accessid,
- // 'success_action_status': 200,
- // "signature": data.sign.signature,
- "key": config.file_name + '/' + time + res.tempFiles[0]
- .name
- }
- // #endif
- // #ifndef H5
- const formData = {
- // "policy": data.sign.policy,
- // "OSSAccessKeyId": data.sign.accessid,
- // 'success_action_status': 200,
- // "signature": data.sign.signature,
- "key": config.file_name + '/' + time + '.png'
- }
- // #endif
- // console.log(formData,'formData');
- uni.uploadFile({
- url: url, //仅为示例,非真实的接口地址
- filePath: res.tempFilePaths[0],
- formData,
- name: 'file',
- header: {
- "Authori-zation": 'Bearer ' + uni
- .getStorageSync('token')
- },
- success: (suc) => {
- if (suc.statusCode == 200) {
- suc.data = JSON.parse(suc.data).data;
- // #ifdef H5
- const imgurl = [{
- url:suc.data.url
- }];
- // #endif
- // #ifndef H5
- const imgurl = [{
- url:suc.data.url
- }];
- // #endif
- resolve(imgurl)
- }
- uni.hideLoading()
- },
- file(res) {
- uni.hideLoading()
- // console.log(res, '图片上传错误');
- reject(res)
- }
- });
- } catch (e) {
- uni.showModal({
- title: '错误',
- content: e.message,
- showCancel: false
- });
- }
- },
- fail(err) {
- if (err.errCode == 0) {
- uni.showToast({
- title: '已取消',
- icon: "error"
- });
- } else if (err.errCode == 12) {
- uni.showToast({
- title: '已返回',
- icon: "error"
- });
- } else {
- console.log(err, '图片上传错误');
- uni.showToast({
- title: '请重新上传',
- icon: "error"
- });
- }
- uni.hideLoading()
- reject(err)
- }
- })
- // })
- } catch (e) {
- uni.hideLoading()
- uni.showToast({
- title: e.message,
- icon: "error"
- });
- reject(res)
- }
- })
- },
- };
- export default service
|