123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- //接口地址
- // 生产环境,小程序开发,APP开发
- // export const baseUrl = 'http://ddsc.hdlkeji.com';
- // 开发环境,web跨域用
- export const baseUrl = 'http://127.0.0.1:8099';
- export const request = ({
- url = '/api',
- data,
- success,
- fail,
- complete,
- showLoading = true,
- ...option
- }) => {
- // console.log(`${baseUrl}${url}`);
-
- let session_key = '';
- try {
- // session_key = 'L7uJsCD1lGQfhFV2AvDIeA==';
- session_key = uni.getStorageSync('session_key') || '';
- } catch (e) {
- // 异常
- }
- if (showLoading) {
- uni.showLoading({
- title: '加载中...'
- });
- }
- return uni.request({
- url: `${baseUrl}${url}`,
- timeout: 60000,
- dataType: 'json',
- data,
- header: {
- // session_key,
- 'Authorization': session_key,
- },
- ...option,
- success: (res) => {
- const data = res.data; // 请求结果
- const errMsg = res.errMsg; // 错误信息
- const statusCode = res.statusCode; // 错误码
- // 判断是否请求失败
- if (statusCode != 200) {
- console.error('请求错误:', res);
- uni.showToast({
- icon: 'none',
- title: `statusCode: ${statusCode}`,
- duration: 2000,
- });
- return;
- }
- // 判断是否登录
- if (data.code == -1) {
- uni.showModal({
- title: '提示',
- content: '请登陆后操作',
- success: function (res) {
- if (res.confirm) {
- uni.reLaunch({
- url: '/pages/login/login',
- });
- } else if (res.cancel) {
- // uni.navigateBack({
- // delta: 1,
- // });
- }
- }
- });
- // uni.showToast({
- // title:"请登陆后操作",
- // icon:"none"
- // })
- // uni.reLaunch({
- // url: '/pages/login/login',
- // });
- return;
- }
- // 请求成功且登录
- success && success(data);
- },
- fail: (res) => {
- uni.showToast({
- icon: 'none',
- title: '请求错误',
- duration: 2000,
- });
- fail && fail(res);
- },
- complete: (res) => {
- if (showLoading) {
- uni.hideLoading();
- }
- complete && complete(res);
- }
- });
- };
- /**
- * 上传图片
- */
- // export function uploadFilesImages({formData, ...option}) {
- // let token = '';
- // try {
- // token = uni.getStorageSync('token') || '';
- // } catch (e) {
- // // 异常
- // }
- // return uni.uploadFile({
- // url: `${baseUrl}/api/up_files/upload_image`, //仅为示例,非真实的接口地址
- // name: 'files',
- // formData: {
- // token,
- // ...formData,
- // },
- // ...option,
- // });
- // }
- // ==========================开始============================
- /**
- * 上传文件
- */
- export function uploadFiles({formData, ...option}) {
- let session_key = '';
- try {
- session_key = uni.getStorageSync('session_key') || '';
- } catch (e) {
- // 异常
- }
-
- return uni.uploadFile({
- url: `${baseUrl}/api/Upload/upload`, //仅为示例,非真实的接口地址
- name: 'file',
- formData: {
- session_key,
- ...formData,
- },
- header: {
- // session_key,
- 'Authorization': session_key,
- },
- ...option,
- });
- }
- // ==========================结束============================
|