| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313 |
- import Vue from 'vue'
- import __config from 'config/env';
- import util from 'utils/util';
- import {
- validatenull
- } from './validate';
- const request = (url, method, data, showLoading) => {
- let _url = url;
- //#ifndef H5
- _url = __config.basePath + url
- //#endif
- _url = __config.basePath + url
- let tokenName = uni.getStorageSync(__config.tokenNameKey)
- let tokenValue = uni.getStorageSync(__config.tokenValueKey)
- let shopId = uni.getStorageSync(__config.shopIdSessionKey)
- let headerTokenObj = {}
- if (tokenName && tokenValue) {
- headerTokenObj[tokenName ? tokenName : 'rs-auth-token'] = tokenValue ? tokenValue : ''
- }
- return new Promise((resolve, reject) => {
- if (showLoading) {
- uni.showLoading({
- title: '加载中'
- });
- }
- uni.request({
- url: _url,
- method: method,
- data: data,
- withCredentials: true,
- header: Object.assign({
- 'tenant-id': __config.tenantId,
- //#ifdef H5
- // 'client-type': util.isWeiXinBrowser() ? 'H5-WX' : 'H5', //客户端类型普通H5或微信H5
- 'app-id': getApp().globalData.appId ? getApp().globalData.appId :
- '', //微信h5有appId
- 'client-type': validatenull(shopId) ? 'LSD-HALL' :
- 'LSD-SHOP', //点餐机类型 大厅的/门店内
- // 'device-id': getApp().globalData.deviceId, //点餐机-唯一设备编号
- 'device-id': "1f82c7ab30405ebf", //点餐机-唯一设备编号
- //#endif
- //#ifdef APP-PLUS
- /* 点餐机模式 */
- 'client-type': validatenull(shopId) ? 'LSD-HALL' :
- 'LSD-SHOP', //点餐机类型 大厅的/门店内
- 'device-id': getApp().globalData.deviceId, //点餐机-唯一设备编号
- 'switch-token': null, //临时用户身份凭证
- 'shop-id': shopId, //店铺ID
- //#endif
- }, headerTokenObj),
- success(res) {
- if (res.statusCode == 200) {
- if (res.data.code != 200) {
- if (res.data.code == 60001 || res.data.code == 406) {
- if (!getApp().globalData.logining) {
- getApp().globalData.logining = true //防止同时多个接口触发登录
- if (util.isMiniPg() || (getApp().globalData.appId && util
- .isWeiXinBrowser())) {
- //小程序或公众号H5,删除wx_token_name|wx_token_value重新登录
- uni.removeStorageSync(__config.tokenNameKey);
- uni.removeStorageSync(__config.tokenValueKey);
- getApp().doLogin().then(res => {
- var pages = getCurrentPages(); // 获取页面栈
- var currPage = pages[pages.length - 1]; // 当前页面
- currPage.onLoad(currPage.options)
- currPage.onShow()
- });
- } else {
- util.backLoginPage()
- }
- setTimeout(function() {
- getApp().globalData.logining = false;
- }, 2000);
- }
- reject("session过期重新登录");
- } else if (res.data.code == 60003) {
- if (!getApp().globalData.logining2) {
- getApp().globalData.logining2 = true //防止同时多个接口触发登录
- util.backLoginPage()
- setTimeout(function() {
- getApp().globalData.logining2 = false;
- }, 2000);
- }
- reject("请先登录系统");
- } else {
- // uni.showModal({
- // title: '提示',
- // content: res.data.msg + '',
- // success() {},
- // complete() {
- // }
- // });
- // reject(res.data.msg);
- }
- }
- resolve(res.data);
- } else if (res.statusCode == 404) {
- uni.showModal({
- title: '提示',
- content: '接口请求出错,请检查手机网络',
- success(res) {}
- });
- reject();
- } else if (res.statusCode == 502) {
- console.log(502)
- uni.showModal({
- title: '提示',
- content: '服务器维护中,请稍后再来',
- success(res) {}
- });
- reject();
- } else if (res.statusCode == 503) {
- console.log(503)
- uni.showModal({
- title: '提示',
- content: '503错误,服务未启动',
- success(res) {}
- });
- reject();
- } else {
- console.log(res);
- uni.showModal({
- title: '提示',
- content: '错误:' + res.data.msg,
- success(res) {}
- });
- reject();
- }
- },
- fail(error) {
- console.log(error);
- uni.showModal({
- title: '提示',
- content: '接口请求出错:' + error.errMsg,
- success(res) {}
- });
- reject(error);
- },
- complete(res) {
- uni.hideLoading();
- }
- });
- });
- };
- // 登录成功后需要进行的统一操作
- const loginSuccess = (request) => {
- return new Promise((resolve, reject) => {
- request.then((res) => {
- if (res && res.data.id) { // 登录成功
- console.log("************登录成功************");
- let userInfo = res.data;
- uni.setStorageSync('third_session', userInfo.thirdSession);
- uni.setStorageSync('user_info', userInfo);
- getApp().userOnlineHeartbeat()
- }
- resolve(res)
- }).catch((err) => {
- reject(err)
- })
- })
- }
- // 退出登录后需要进行的统一操作
- const logoutSuccess = (request) => {
- return new Promise((resolve, reject) => {
- request.then((res) => {
- //退出登录成功
- console.log("************退出登录成功************");
- getApp().userOnlineHeartbeat(true)
- resolve(res)
- }).catch((err) => {
- reject(err)
- })
- })
- }
- module.exports = {
- request,
- doGet: function(url, params, isShowLoading = true) {
- return request(url, 'get', params, isShowLoading);
- },
- doPost: function(url, params, isShowLoading = true) {
- return request(url, 'post', params, isShowLoading);
- },
- doPut: function(url, params, isShowLoading = true) {
- return request(url, 'put', params, isShowLoading);
- },
- doDelete: function(url, params, isShowLoading = true) {
- return request(url, 'delete', params, isShowLoading);
- },
- deviceReport: (data) => {
- // 点餐机设备信息上报
- return new Promise((resolve, reject) => {
- request('/mallapi/deviceReport/add', 'post', data, false).then(res => {
- resolve(res)
- }).catch(error => {
- reject(error)
- })
- })
- },
- userInfoGet: () => {
- //查询点餐平台用户信息
- //小程序获取当前登录用户信息
- return new Promise((resolve, reject) => {
- request('/openapi/ma/common/getCurrentUser', 'get', null, false).then(res => {
- /**
- * 缓存用户信息
- */
- let wxMemberInfo = util.parseUser(res.data)
- uni.setStorageSync(__config.loginWxUserInfoKey, wxMemberInfo)
- res.data = wxMemberInfo
- resolve(res)
- }).catch(error => {
- console.error('小程序获取当前登录用户信息出错: ' + error)
- reject(error)
- })
- })
- },
- getCurrentUserSync: (data) => {
- //小程序获取当前登录用户信息
- return new Promise((resolve, reject) => {
- request('/openapi/ma/common/getCurrentUser', 'get', data, false).then(res => {
- /**
- * 缓存用户信息
- */
- let wxMemberInfo = util.parseUser(res.data)
- uni.setStorageSync(__config.loginWxUserInfoKey, wxMemberInfo)
- res.data = wxMemberInfo
- resolve(res)
- }).catch(error => {
- console.error('小程序获取当前登录用户信息出错: ' + error)
- reject(error)
- })
- })
- },
- /* 小程序文件上传方法 */
- uploadFile: (params, filePath) => {
- uni.showLoading({
- title: '文件上传中...'
- });
- let tokenName = uni.getStorageSync(__config.tokenNameKey)
- let tokenValue = uni.getStorageSync(__config.tokenValueKey)
- let headerTokenObj = {}
- if (tokenName && tokenValue) {
- headerTokenObj[tokenName ? tokenName : 'rs-auth-token'] = tokenValue ? tokenValue : ''
- }
- headerTokenObj = Object.assign({
- "Content-Type": "multipart/form-data"
- }, headerTokenObj)
- if (!params) {
- params = {
- dir: '/uploads/',
- }
- }
- //小程序上传文件
- return new Promise((resolve, reject) => {
- uni.uploadFile({
- url: __config.basePath + '/openapi/storage/upload?dir=' + params.dir,
- filePath: filePath,
- name: 'file',
- header: headerTokenObj,
- //formData: params,
- success: (res) => {
- resolve(JSON.parse(res.data))
- },
- fail: (err) => {
- reject(err)
- },
- complete(res) {
- uni.hideLoading();
- }
- })
- })
- },
- getDictByKey: data => {
- //获取字典信息(根据字典编码)
- return request('/openapi/common/getDictByKey/' + data, 'get', null, false);
- },
- getConfigByKey: data => {
- //获取字典信息(根据字典编码)
- return request('/openapi/common/getConfigByKey/' + data, 'get', null, false);
- },
- deviceReport: data => {
- //上报设备(大屏)信息接口
- return new Promise((resolve, reject) => {
- request('/mallapi/deviceReport/addScreenDevice', 'post', data, false).then(res => {
- resolve(res)
- })
- })
- },
- };
|