372 lines
11 KiB
JavaScript
372 lines
11 KiB
JavaScript
/**
|
||
* @BaseUR 请求域名
|
||
* @HEADERTOKENNAME 请求token键名
|
||
* @Bearer 是否需要在token前拼接 Bearer字符
|
||
* */
|
||
import Vue from 'vue';
|
||
let $URL = require('urijs');
|
||
// 进入首页之后会请求
|
||
let login = false;
|
||
import state from '@/store/index';
|
||
import {
|
||
loginAPI
|
||
} from '@/request/index/index.js';
|
||
import { HTTP_REQUEST_URL } from '@/config/app.js';
|
||
|
||
let bm_id = 0;
|
||
const LoginPromis = () => {
|
||
return new Promise(resolve => {
|
||
console.log(loginAPI, 'loginAPI');
|
||
// 开发模式下浏览器不支持画卷云API所以开发模式下登录用户写死
|
||
if (process.env.NODE_ENV == 'development') {
|
||
loginAPI({
|
||
// 剑南白马ID
|
||
// bm_id: 17693,
|
||
// 张奇镇白马ID
|
||
bm_id: 4,
|
||
// 柯南白马ID
|
||
// bm_id: 620439,
|
||
// 李美娜白马ID
|
||
// bm_id: 637853,
|
||
}).then(res => {
|
||
// bm_id = 620439;
|
||
console.log(res, '登录信息');
|
||
// location.reload();
|
||
});
|
||
return
|
||
}
|
||
// 提示用户登录
|
||
uni.showLoading({
|
||
title: '正在登录...'
|
||
});
|
||
// 获取APP登录状态
|
||
getToken().then((res) => {
|
||
console.log('检测TOKEN');
|
||
if (res) {
|
||
getH5UserInfo().then((res) => {
|
||
console.log('获取用户信息');
|
||
uni.hideLoading();
|
||
let userInfo = JSON.parse(res)
|
||
uni.setStorageSync('userInfo', res);
|
||
bm_id = userInfo.id;
|
||
loginAPI({
|
||
bm_id: userInfo.id
|
||
// userInfo.id
|
||
}).then(res => {
|
||
console.log(res)
|
||
// location.reload()
|
||
})
|
||
// showToast(`当前用户为:${JSON.parse(res)}`)
|
||
})
|
||
}
|
||
})
|
||
})
|
||
}
|
||
|
||
// // #ifdef H5
|
||
// import HLOGIN from './h5Login.js';
|
||
// // #endif
|
||
class Request {
|
||
responseType = 'test';
|
||
constructor({
|
||
BaseUR = '',
|
||
HEADERTOKENNAME = 'token',
|
||
Bearer = false
|
||
} = {}) {
|
||
// setTimeout(()=>{
|
||
// // LoginPromis();
|
||
// console.log(loginAPI,'loginAPI');
|
||
|
||
// },200)
|
||
this['BaseUR'] = BaseUR;
|
||
this['HEADERTOKENNAME'] = HEADERTOKENNAME;
|
||
this['Bearer'] = Bearer;
|
||
}
|
||
|
||
url(value) {
|
||
return /http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w-.\/?%&=]*)?/.test(value)
|
||
}
|
||
/**
|
||
* @param {String} method请求类型
|
||
* @param {String} method请求方式
|
||
* @param {String} Content-Type请求类型
|
||
* @param {String} url请求地址
|
||
*/
|
||
Request(config = {}) {
|
||
// 是否存在url
|
||
try {
|
||
if (!config['url']) {
|
||
throw '请求缺少URL参数';
|
||
}
|
||
} catch (e) {
|
||
throw new Error(e);
|
||
};
|
||
// 请求体
|
||
return new Promise((resolve, reject) => {
|
||
// config['url'] = `/jc_service${config['url']}`
|
||
// 创建请求头对象
|
||
let header = {}
|
||
// 请求类型
|
||
config['method'] = config?.method ? config['method'].toUpperCase() : 'GET';
|
||
// 正常情况下
|
||
// GET请求使用 'application/x-www-form-urlencoded'
|
||
// POST请求使用 'application/json'
|
||
// 列外情况下会与之相反,所以将Content-Type写为根据手动传输或根据请求类型判断,目前只写入GET与POST后续可扩展
|
||
header['Content-Type'] = config['ContentType'] ? config['ContentType'] : this.method(
|
||
config['method']);
|
||
config['header'] = header;
|
||
// url活动判断
|
||
if (!this.url(config['url'])) {
|
||
config['url'] = `${this['BaseUR']}${config['url']}`
|
||
};
|
||
config['complete'] = (infor) => {
|
||
this.responseintercept(infor);
|
||
if (infor.data['status'] == 200 || infor.statusCode == 200) {
|
||
// arraybuffer格式返回是不带code的
|
||
// 判断是否是arraybuffer
|
||
if (this['responseType'] == 'arraybuffer') {
|
||
console.log(infor['data'], 'arraybuffer1-1')
|
||
resolve(infor['data']);
|
||
return
|
||
}
|
||
if (infor['data']['status'] != 200 && infor['data']['code'] != 7777) {
|
||
// #ifdef H5
|
||
if (infor['data']['status'] == 110002) {
|
||
window.location.href = `${HTTP_REQUEST_URL}/JXH5/pages/users/h5_login/index&redirect_uri=${window.location.protocol + "//" + window.location.host}/privilege/pages/restaurant/home/shop_home/shop_home`;
|
||
}
|
||
// #endif
|
||
|
||
if (infor['data']['msg'] == '产品错误' || infor['data']['msg'] == '产品不存在') {
|
||
uni.showToast({
|
||
title: '当前餐品不可用',
|
||
icon: 'none'
|
||
});
|
||
reject(infor['data']);
|
||
return;
|
||
} else {
|
||
uni.showToast({
|
||
title: infor['data']['msg'],
|
||
icon: 'none'
|
||
});
|
||
reject(infor['data']);
|
||
return;
|
||
}
|
||
}
|
||
resolve(infor['data']);
|
||
|
||
} else {
|
||
uni.showToast({
|
||
title: '网络错误',
|
||
icon: 'none'
|
||
})
|
||
reject(infor['data']);
|
||
}
|
||
};
|
||
// config['header'][this['HEADERTOKENNAME']] = `${this['Bearer']?'Bearer':''}`;
|
||
config['data'] = config?.parameters ? config['parameters'] : {};
|
||
let Requestconfig = this.Requestintercept(config);
|
||
|
||
// 设置响应数据类型
|
||
config['responseType'] = config?.responseType || 'text';
|
||
this['responseType'] = config['responseType'];
|
||
console.log(Requestconfig, 'Requestconfig');
|
||
// uni.showModal({
|
||
// title: '提示',
|
||
// content: JSON.stringify(Requestconfig),
|
||
// success: function (res) {
|
||
// if (res.confirm) {
|
||
// console.log('用户点击确定');
|
||
// } else if (res.cancel) {
|
||
// console.log('用户点击取消');
|
||
// }
|
||
// }
|
||
// });
|
||
uni.request(Requestconfig);
|
||
})
|
||
}
|
||
// 请求拦截
|
||
Requestintercept(config) {
|
||
// 请求时需要携带用定位
|
||
let cityInfor = state['getters']['GetcityInfor'];
|
||
let token = state['getters']['GetcityInfor'];
|
||
|
||
// 固定写入
|
||
config['header']['authori-zation'] = state['getters']['GetToken'];
|
||
|
||
wx.miniProgram.getEnv((wx) => {
|
||
if (wx.miniprogram) {
|
||
// 小程序环境
|
||
config['header']['form-type'] = 'routine';
|
||
}else {
|
||
//非小程序环境
|
||
config['header']['form-type'] = 'gzh';
|
||
}
|
||
})
|
||
|
||
console.log(cityInfor, '定位信息头')
|
||
if (cityInfor?.adcode) {
|
||
// config['header']['city'] = encodeURIComponent(cityInfor['city']);
|
||
config['header']['city'] = encodeURIComponent(cityInfor['province']);
|
||
config['header']['address'] = encodeURIComponent(cityInfor['province']);
|
||
config['header']['province'] = encodeURIComponent(cityInfor['province']);
|
||
config['header']['lat'] = cityInfor['lat'];
|
||
// config['header']['lng'] = cityInfor['lon'];
|
||
config['header']['lng'] = cityInfor['lng'];
|
||
config['header']['adcode'] = cityInfor['adcode'];
|
||
} else {
|
||
// 后台判定该参数为空所以在没有的情况下需要传空值
|
||
config['header']['city'] = '';
|
||
config['header']['lat'] = '';
|
||
config['header']['adcode'] = '';
|
||
};
|
||
|
||
// config['header']['token'] ='123456'
|
||
//chwl 123456
|
||
//card 654321
|
||
//
|
||
// 微信跳转H5后的逻辑为必须携带token
|
||
const isWechat = getApp()['globalData']['parameters']?.isWechat;
|
||
if (isWechat) {
|
||
config['header']['authori-zation'] = state['getters']['GetToken'];
|
||
}
|
||
// 测试标识
|
||
// 测试时不需要显示在线上的打开
|
||
// config['header']['test1'] = 1;
|
||
// 话费快充测试
|
||
// console.log(process['env']['NODE_ENV'],'NODE_ENV');
|
||
// config['header']['is_test'] = 'YSE';
|
||
// 城市经理添加BD测试
|
||
config['header']['manager'] = 1;
|
||
config['header']['env'] = 1;
|
||
// config['header']['bm_id'] = bm_id;
|
||
// console.log(loginAPI,'loginAPI');
|
||
return config
|
||
}
|
||
|
||
// 响应拦截
|
||
responseintercept(infor) {
|
||
// const isWechat = getApp()['globalData']['parameters']?.isWech'at;
|
||
// console.log(isWechat,process.env.NODE_ENV,'isWechat1');
|
||
// IOS端不经行任何处理
|
||
let isSafari = (/Safari/.test(navigator.userAgent) && !/Chrome/.test(navigator.userAgent));
|
||
if (isSafari && process.env.NODE_ENV !== 'development') return;
|
||
// 响应拦截逻辑开始
|
||
// if (infor['statusCode'] != 200) {
|
||
if (infor.data['status'] != 200) {
|
||
// HTPP网络请求状态错误直接提示
|
||
uni.showToast({
|
||
title: `${infor['data']['msg']}---${infor['url']}`,
|
||
icon: 'none'
|
||
})
|
||
} else {
|
||
if (infor['data']['code'] != 200) {
|
||
if (infor['data']['code'] == 7777) {
|
||
// 微信中进入H5后会携带token,但是如果微信进入H5后token过期则提示用户返回小程序重新登录
|
||
const isWechat = getApp()['globalData']['parameters']?.isWechat;
|
||
if (isWechat) {
|
||
uni.showToast({
|
||
title: '登录过期请刷新微信小程序',
|
||
icon: 'none',
|
||
duration: 5000
|
||
});
|
||
return
|
||
} else {
|
||
if (process.env.NODE_ENV == 'development') {
|
||
loginAPI({
|
||
// 剑南白马ID
|
||
// bm_id: 17693,
|
||
// 张奇镇白马ID
|
||
// bm_id: 4,
|
||
// 柯南白马ID
|
||
// bm_id: 620439,
|
||
// 李美娜白马ID
|
||
// bm_id: 637853,
|
||
// 闫康
|
||
// bm_id:647070
|
||
// bm_id:259
|
||
bm_id:120811
|
||
}).then(res => {
|
||
console.log(res, '登录----')
|
||
location.reload()
|
||
});
|
||
return
|
||
}
|
||
if (login) return;
|
||
login = true;
|
||
uni.showLoading({
|
||
title: '正在登录...'
|
||
});
|
||
getToken().then((res) => {
|
||
if (res) {
|
||
getH5UserInfo().then((res) => {
|
||
uni.hideLoading();
|
||
let userInfo = JSON.parse(res)
|
||
uni.setStorageSync('userInfo', res);
|
||
loginAPI({
|
||
bm_id: userInfo.id
|
||
// userInfo.id
|
||
}).then(res => {
|
||
console.log(res)
|
||
location.reload()
|
||
})
|
||
// showToast(`当前用户为:${JSON.parse(res)}`)
|
||
})
|
||
} else {
|
||
openLoginPage().then((res) => {
|
||
if (res) {
|
||
getToken().then((res) => {
|
||
if (res) {
|
||
getH5UserInfo().then((res) => {
|
||
let userInfo = JSON.parse(res);
|
||
uni.hideLoading();
|
||
loginAPI({
|
||
bm_id: userInfo.id
|
||
// userInfo.id
|
||
}).then(res => {
|
||
console.log(res)
|
||
|
||
location.reload()
|
||
})
|
||
// showToast(`当前用户为:${JSON.parse(res)}`)
|
||
})
|
||
} else {
|
||
showToast('未登录')
|
||
}
|
||
})
|
||
}
|
||
})
|
||
}
|
||
})
|
||
}
|
||
}else if(infor['data']['code'] == 1300){
|
||
window.location.replace('https://jc.agrimedia.cn/maintenance')
|
||
}
|
||
} else {
|
||
// uni.showToast({
|
||
// title: `${infor['data']['msg']}***${infor['url']}`,
|
||
// icon: 'none'
|
||
// })
|
||
}
|
||
}
|
||
}
|
||
|
||
// 请求类型过滤
|
||
method(method) {
|
||
switch (method) {
|
||
case 'GET':
|
||
return 'application/x-www-form-urlencoded';
|
||
case 'POST':
|
||
return 'application/json';
|
||
}
|
||
}
|
||
}
|
||
|
||
let BaseUR = new $URL(window.location.href).origin();
|
||
if(~new $URL(window.location.href).origin().indexOf('bmtest') || ~new $URL(window.location.href).origin().indexOf('tshop')){
|
||
BaseUR = `${new $URL(window.location.href).origin()}/jc_service`;
|
||
};
|
||
console.log(new $URL(window.location.href).host(), '配置路径')
|
||
const request = new Request({ BaseUR });
|
||
|
||
export default request;
|