import msg from './message'; import BaseTool from '@/utils/tool'; import { loginQuickly } from '@/api/upms/user'; // 根据开发环境或生产环境破设置服务器根域名 let ajaxTimes = 0; /** * 记录登录前的页面路径,供登录成功后回跳 * 扫码场景:免登录查看设备详情 → 点操作弹登录 → 登录后应回到原页面继续操作,而不是重新扫码 * 同时记录是否为 tabBar 页面,以便回跳时选择正确的导航方法 */ const TAB_PAGES = ['/pages/home/home', '/pages/operate/operate', '/pages/middle/middle', '/pages/tidings/tidings', '/pages/my/my']; const recordBackUrl = () => { try { const pages = getCurrentPages(); if (!pages || pages.length === 0) { return; } const cur = pages[pages.length - 1]; const opts = cur.options || (cur.$page && cur.$page.options) || {}; const query = Object.keys(opts) .map(k => k + '=' + encodeURIComponent(opts[k])) .join('&'); const route = '/' + cur.route; uni.setStorageSync('login_back_url', route + (query ? '?' + query : '')); uni.setStorageSync('login_back_is_tab', TAB_PAGES.indexOf(route) !== -1); } catch (e) { // 取不到当前页面时忽略,登录后走默认首页 } }; /** * 取出并清除登录回跳地址(由登录成功逻辑调用) */ export const takeBackUrl = () => { const url = uni.getStorageSync('login_back_url'); uni.removeStorageSync('login_back_url'); return url; }; /** * 登录方式选择弹框:账号登录 / 微信快捷登录 * 供两处复用:1) 接口返回 401 登录过期;2) 未登录点击操作类按钮时的前置拦截 */ export const goLogin = (content) => { recordBackUrl(); uni.showModal({ content: content || '该操作需要登录后进行,请先登录', showCancel: true, cancelText: '账号登录', confirmText: '快捷登录', success: res => { if (res.confirm) { loginQuickly().then(res => { const url = res.data if (url !== '' && url !== null && url != undefined) { // 请求用户授权 window.location.href = url } }) } else if (res.cancel) { uni.reLaunch({ url: '/pages/login/login' }) } } }) }; /** * 操作类按钮统一登录校验(扫码查看设备信息无需登录,但提交类操作必须先登录) * @param {Function} next 已登录时执行的实际操作 * @param {String} content 未登录时的提示文案 * @return {Boolean} 是否已登录 */ export const requireLogin = (next, content) => { if (BaseTool.UserUtil.isLogin()) { if (typeof next === 'function') { next(); } return true; } goLogin(content); return false; }; export default ({ url, method, data }) => { let closeLoading = true; return new Promise((resolve, reject) => { ajaxTimes++; msg.loadingMustClose(); uni.request({ url: BaseTool.UserUtil.getBaseUrl() + url, header: { 'Content-Type': 'application/json;charset=UTF-8', Authorization: BaseTool.UserUtil.getToken(url) }, data: data, method: method || 'GET', success (res) { // todo future 登录过期,需要单独处理 if (res.data.code === 401) { // msg.hideLoading(); // closeLoading = false; uni.clearStorageSync(); // #ifdef MP-WEIXIN if (url.indexOf('/workplace/backlogs/user/page') >= 0) { return reject(res.data) } // #endif goLogin(res.data.message); return reject(res.data) } if (res.data.code === 403) { // uni.showModal({ content: res.data.message, showCancel: false }) // msg.hideLoading(); // closeLoading = false; uni.showToast({ icon: 'none', title: res.data.message }); return reject(res); } // 重定向的专门处理 if (res.data.code === 301) { // uni.showModal({ content: res.data.message, showCancel: false }) // msg.hideLoading(); // closeLoading = false; window.location.href = res.data } if (res.data.code !== 0) { // uni.showModal({ content: res.data.message, showCancel: false }) // msg.hideLoading(); // closeLoading = false; setTimeout(() => { uni.showToast({ icon: 'none', title: res.data.message }); }, 50) return reject(res.data); } resolve(res.data); }, fail (err) { // msg.hideLoading(); // closeLoading = false; uni.showToast({ icon: 'none', title: '系统异常,请联系客服!' }); reject(err); }, complete: () => { ajaxTimes--; if (ajaxTimes === 0) { // 关闭正在等待的图标 closeLoading = false; msg.hideLoading(); } } }); }).finally(() => { // if (closeLoading) { // msg.hideLoading(); // } }); };