| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- // uni-app请求封装
- import {
- TOKEN_KEYAuto
- } from '@/utils/courseTool.js'
- import store from '@/store/index.js'
- export default class Request {
- http(router, data = {}, method, contentType) {
- let that = this;
- //let path = 'http://localhost:7014';
- // let path = 'https://test.userapp.store.cdwjyyh.com';
- // let path = 'https://user.test.ylrztop.com/api';
- let path = 'https://userapp.klbycp.com/store'; //百域承品
- let path2 = 'https://userapp.klbycp.com'; //百域承品
- // let path = 'http://sf39aceb.natappfree.cc/store'; //百域承品 夏伟
- // let path2 = 'http://sf39aceb.natappfree.cc'; //百域承品 夏伟
- // let path = 'http://qeeaf625.natappfree.cc/store'; //本地
- // let path2 = 'http://qeeaf625.natappfree.cc'; //本地
- let type = 0
- uni.setStorageSync('requestPath', path2)
- // uni.showLoading({
- // title: '加载中'
- // });
- if (router.indexOf("/live/liveData/like") != -1) {
- path = 'https://im.fhhx.runtzh.com';
- }
- if (router.indexOf("/app/order/getMyMergedOrderList") != -1) {
- path = 'https://userapp.klbycp.com';
- }
- if (router.indexOf("/live") !== -1 || router.indexOf("/liveAPP") !== -1) {
- // path = 'https://userapp.klbycp.com';
- path = path2
- router = router.replace('/liveAPP', '')
- type = 4
- token = uni.getStorageSync('AppToken');
- }
- if (router.indexOf("/patient/getPatientList") !== -1) {
- // path = 'https://userapp.klbycp.com';
- path = path2
- }
- if (router.indexOf("/getGotoWxAppLiveLink") !== -1) {
- // path = 'https://userapp.klbycp.com/companyapp';
- path = `${path2}/companyapp`;
- }
- let token = uni.getStorageSync('AppToken');
- if (router.indexOf("/companyapp") != -1) {
- // router =router.replace('/companyapp','')
- // path = 'https://userapp.klbycp.com' //百域承品
- path = path2 //本地
- type = 1
- token = uni.getStorageSync('ManageToken');
- }
- //积分商城
- if (router.indexOf("/integral") !== -1) {
- path = path2
- }
- // 企微看课
- if (router.indexOf("/course_auto") != -1) {
- type = 2
- router = router.replace('/course_auto', '')
- // path = 'https://userapp.klbycp.com' //百域承品
- path = path2 //本地
- token = uni.getStorageSync(TOKEN_KEYAuto)
- console.log("TOKEN_KEYAuto===", TOKEN_KEYAuto, token)
- }
- if (router.indexOf("/course_uniapp") != -1) {
- type = 3
- router = router.replace('/course_uniapp', '')
- // path = 'https://userapp.klbycp.com' //百域承品
- path = path2
- token = uni.getStorageSync('TOKEN_WEXIN')
- }
- return new Promise((resolve, reject) => {
- // token = uni.getStorageSync('AppToken');
- var httpContentType = 'application/x-www-form-urlencoded';
- if (contentType != undefined) {
- //application/json;charset=UTF-8
- httpContentType = contentType;
- }
- var routers = router;
- // 请求
- uni.request({
- header: {
- // 'Content-Type': 'application/x-www-form-urlencoded',
- 'Content-Type': httpContentType,
- 'appid': wx.getAccountInfoSync().miniProgram.appId,
- 'AppToken': token
- },
- url: `${path}${router}`,
- data: data,
- method: method,
- success: (res) => {
- if (type !== 0 && (res.data.code == 401 || res.data.code == 4001 || res.data
- .code == 4004)) {
- store.commit('setCoureLogin', 2);
- uni.removeStorageSync("userinfos")
- // uni.removeStorageSync('userInfo');
- uni.removeStorageSync('TOKEN_WEXIN');
- if (type == 2) {
- uni.removeStorageSync(TOKEN_KEYAuto)
- }
- if (type == 1) {
- uni.removeStorageSync('ManageToken');
- uni.navigateTo({
- url: '/pages_manage/login'
- })
- }
- resolve({
- code: 401,
- data: null
- });
- return
- }
- //收到开发者服务器成功返回的回调函数
- if (res.data.code == 401) { //没有权限直接退出到登录界面
- let pages = getCurrentPages();
- pages.forEach(function(element) {
- if (element != undefined && element.route ==
- "pages/auth/login") {
- resolve(res.data)
- return;
- }
- });
- // let url = pages[ pages.length - 1]; //当前页页面实例
- // //如果登录界面已打开,自动关闭
- // if(url!=undefined&&url.route=="pages/auth/numberlogin"){
- // resolve(res.data)
- // return;
- // }
- uni.navigateTo({
- url: '/pages/auth/login',
- success: () => {
- uni.hideLoading();
- },
- fail: () => {
- uni.hideLoading();
- }
- })
- return;
- }
- if (res.data.token && type == 0) {
- uni.setStorageSync('AppToken', res.data.token)
- }
- resolve(res.data)
- },
- fail: (res) => {
- //接口调用失败的回调函数
- },
- complete: (res) => {
- //接口调用结束的回调函数(调用成功、失败都会执行)
- if (res.data.code == 401) {
- return false
- }
- uni.hideLoading();
- }
- })
- })
- }
- }
|