|
|
@@ -3,6 +3,82 @@ import BaseTool from '@/utils/tool';
|
|
|
import { loginQuickly } from '@/api/upms/user';
|
|
|
// 根据开发环境或生产环境破设置服务器根域名
|
|
|
let ajaxTimes = 0;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 记录登录前的页面路径,供登录成功后回跳
|
|
|
+ * 扫码场景:免登录查看设备详情 → 点操作弹登录 → 登录后应回到原页面继续操作,而不是重新扫码
|
|
|
+ */
|
|
|
+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('&');
|
|
|
+ uni.setStorageSync('login_back_url', '/' + cur.route + (query ? '?' + query : ''));
|
|
|
+ } 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) => {
|
|
|
@@ -28,26 +104,7 @@ export default ({ url, method, data }) => {
|
|
|
return reject(res.data)
|
|
|
}
|
|
|
// #endif
|
|
|
- uni.showModal({
|
|
|
- content: res.data.message,
|
|
|
- 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' })
|
|
|
- }
|
|
|
- }
|
|
|
- })
|
|
|
+ goLogin(res.data.message);
|
|
|
return reject(res.data)
|
|
|
}
|
|
|
if (res.data.code === 403) {
|