caixianzhi pc接口修改完成
This commit is contained in:
@@ -254,6 +254,8 @@ const CommonUtil = {
|
||||
loadPageConfig(options = {}) {
|
||||
if (typeof ApiClient === 'undefined') return;
|
||||
this.installUnifiedPagination();
|
||||
this.installWechatLoginEntry();
|
||||
this.installWechatAccountBindEntry();
|
||||
const loadHomeData = options.home !== false && this.isHomePageDom();
|
||||
const request = loadHomeData ? this.loadHomePageData() : this.loadBasePageData(options);
|
||||
this.applyCaiExpertListLabels();
|
||||
@@ -270,6 +272,8 @@ const CommonUtil = {
|
||||
this.processAndRenderAds(data.adList, options.adPositions);
|
||||
}
|
||||
this.applyCaiExpertListLabels();
|
||||
this.installWechatLoginEntry();
|
||||
this.installWechatAccountBindEntry();
|
||||
if (options.links !== false) this.loadLinks();
|
||||
if (typeof options.onSuccess === 'function') options.onSuccess(result);
|
||||
})
|
||||
@@ -278,6 +282,230 @@ const CommonUtil = {
|
||||
});
|
||||
},
|
||||
|
||||
installWechatLoginEntry() {
|
||||
if (typeof window === 'undefined' || window.__WECHAT_LOGIN_ENTRY_INSTALLED__) return;
|
||||
const $entry = this.$('.wxdl');
|
||||
if (!$entry.length || typeof ApiClient === 'undefined') return;
|
||||
|
||||
window.__WECHAT_LOGIN_ENTRY_INSTALLED__ = true;
|
||||
$entry.find('a').attr({
|
||||
href: 'javascript:void(0)',
|
||||
title: '微信登录',
|
||||
});
|
||||
|
||||
$entry.on('click.wechatLogin', 'a, .wx_Box', (event) => {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
this.startWechatLogin();
|
||||
});
|
||||
},
|
||||
|
||||
getWechatLoginDomain() {
|
||||
if (typeof window === 'undefined' || !window.location || window.location.protocol === 'file:') {
|
||||
return window.CURRENT_CONFIG && window.CURRENT_CONFIG.SITE_DOMAIN
|
||||
? window.CURRENT_CONFIG.SITE_DOMAIN
|
||||
: 'http://47.108.24.205:9100';
|
||||
}
|
||||
return window.location.origin;
|
||||
},
|
||||
|
||||
extractWechatAuthUrl(res) {
|
||||
const data = res && res.data;
|
||||
if (typeof data === 'string') return data;
|
||||
if (data && typeof data === 'object') {
|
||||
return data.url || data.authUrl || data.authorizeUrl || data.redirectUrl || data.loginUrl || '';
|
||||
}
|
||||
return '';
|
||||
},
|
||||
|
||||
startWechatLogin() {
|
||||
const $ = this.$;
|
||||
const layer = typeof layui !== 'undefined' && layui.layer;
|
||||
const $agreement = $('#checkbox, #agreementCheckbox').first();
|
||||
|
||||
if ($agreement.length && !$agreement.prop('checked')) {
|
||||
if (layer) layer.msg('请先阅读并同意用户协议和隐私政策', { icon: 2 });
|
||||
return;
|
||||
}
|
||||
|
||||
const $loading = $('#loadingOverlay');
|
||||
const $button = $('.wxdl');
|
||||
$loading.addClass('is-active');
|
||||
$button.addClass('is-loading');
|
||||
|
||||
ApiClient.get(
|
||||
ApiClient.API.authWechatOpen,
|
||||
{ domain: this.getWechatLoginDomain() },
|
||||
{
|
||||
publicRequest: true,
|
||||
headers: { clientid: ApiClient.CLIENT_ID },
|
||||
}
|
||||
)
|
||||
.then((res) => {
|
||||
if (!ApiClient.isSuccess(res)) {
|
||||
throw new Error((res && res.msg) || '微信授权地址获取失败');
|
||||
}
|
||||
const authUrl = this.extractWechatAuthUrl(res);
|
||||
if (!authUrl) throw new Error('后端未返回微信授权地址');
|
||||
window.location.href = authUrl;
|
||||
})
|
||||
.catch((error) => {
|
||||
const message = error.message || '微信登录暂不可用,请稍后再试';
|
||||
if (layer) layer.msg(message, { icon: 2 });
|
||||
else alert(message);
|
||||
})
|
||||
.finally(() => {
|
||||
$loading.removeClass('is-active');
|
||||
$button.removeClass('is-loading');
|
||||
});
|
||||
},
|
||||
|
||||
installWechatAccountBindEntry() {
|
||||
if (typeof window === 'undefined') return;
|
||||
const button = document.getElementById('wechat-bind-button');
|
||||
if (!button || typeof ApiClient === 'undefined') return;
|
||||
|
||||
this.normalizeWechatAccountBindPanel();
|
||||
if (window.__WECHAT_ACCOUNT_BIND_ENTRY_INSTALLED__) return;
|
||||
|
||||
window.__WECHAT_ACCOUNT_BIND_ENTRY_INSTALLED__ = true;
|
||||
button.setAttribute('title', '绑定微信');
|
||||
button.addEventListener(
|
||||
'click',
|
||||
(event) => {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
if (event.stopImmediatePropagation) event.stopImmediatePropagation();
|
||||
this.startWechatAccountBind();
|
||||
},
|
||||
true
|
||||
);
|
||||
this.observeWechatAccountBindPanel();
|
||||
},
|
||||
|
||||
isWechatAccountBindPanelBound() {
|
||||
const $ = this.$;
|
||||
const descText = ($('#wechat-bind-desc').text() || '').trim();
|
||||
const buttonText = ($('#wechat-bind-button').text() || '').trim();
|
||||
return (
|
||||
this.hasWechatAccountBindSuccessMark() ||
|
||||
$('#wechat-bind-card').hasClass('is-bound') ||
|
||||
descText.indexOf('已绑定') > -1 ||
|
||||
buttonText.indexOf('已绑定') > -1
|
||||
);
|
||||
},
|
||||
|
||||
hasWechatAccountBindSuccessMark() {
|
||||
if (typeof window === 'undefined') return false;
|
||||
try {
|
||||
return sessionStorage.getItem('wechatAccountBindSuccess') === '1';
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
},
|
||||
|
||||
applyWechatAccountBindBoundPanel() {
|
||||
const $button = this.$('#wechat-bind-button');
|
||||
if (!$button.length) return;
|
||||
this.$('#wechat-bind-desc').text('微信已绑定');
|
||||
this.$('#wechat-bind-card').removeClass('is-pending is-ready').addClass('is-bound');
|
||||
$button
|
||||
.text('已绑定')
|
||||
.attr('title', '微信已绑定')
|
||||
.removeClass('is-pending')
|
||||
.addClass('is-bound')
|
||||
.prop('disabled', true);
|
||||
},
|
||||
|
||||
normalizeWechatAccountBindPanel() {
|
||||
const $button = this.$('#wechat-bind-button');
|
||||
if (!$button.length) return;
|
||||
|
||||
if (this.hasWechatAccountBindSuccessMark()) {
|
||||
this.applyWechatAccountBindBoundPanel();
|
||||
return;
|
||||
}
|
||||
if (this.isWechatAccountBindPanelBound()) return;
|
||||
|
||||
this.$('#wechat-bind-desc').text('未绑定微信');
|
||||
this.$('#wechat-bind-card').removeClass('is-pending').addClass('is-ready');
|
||||
$button
|
||||
.text('绑定微信')
|
||||
.attr('title', '绑定微信')
|
||||
.removeClass('is-bound is-pending')
|
||||
.prop('disabled', false);
|
||||
},
|
||||
|
||||
observeWechatAccountBindPanel() {
|
||||
if (typeof window === 'undefined' || window.__WECHAT_ACCOUNT_BIND_OBSERVER__) return;
|
||||
const target = document.getElementById('wechat-bind-card') || document.getElementById('wechat-bind-button');
|
||||
if (!target || typeof MutationObserver === 'undefined') return;
|
||||
|
||||
window.__WECHAT_ACCOUNT_BIND_OBSERVER__ = new MutationObserver(() => {
|
||||
window.clearTimeout(window.__WECHAT_ACCOUNT_BIND_NORMALIZE_TIMER__);
|
||||
window.__WECHAT_ACCOUNT_BIND_NORMALIZE_TIMER__ = window.setTimeout(() => {
|
||||
this.normalizeWechatAccountBindPanel();
|
||||
}, 0);
|
||||
});
|
||||
window.__WECHAT_ACCOUNT_BIND_OBSERVER__.observe(target, {
|
||||
attributes: true,
|
||||
childList: true,
|
||||
subtree: true,
|
||||
});
|
||||
},
|
||||
|
||||
rememberWechatAccountBindMode() {
|
||||
if (typeof window === 'undefined') return;
|
||||
const returnUrl =
|
||||
`${window.location.pathname}${window.location.search}${window.location.hash}` || '/html/usercenter.html';
|
||||
const pairs = {
|
||||
wechatSocialMode: 'account_bind',
|
||||
wechatSocialReturnUrl: returnUrl,
|
||||
wechatSocialStartedAt: String(Date.now()),
|
||||
};
|
||||
|
||||
Object.keys(pairs).forEach((key) => {
|
||||
try {
|
||||
sessionStorage.setItem(key, pairs[key]);
|
||||
} catch (e) {}
|
||||
try {
|
||||
localStorage.setItem(key, pairs[key]);
|
||||
} catch (e) {}
|
||||
});
|
||||
},
|
||||
|
||||
startWechatAccountBind() {
|
||||
const layer = typeof layui !== 'undefined' && layui.layer;
|
||||
const token = typeof ApiClient !== 'undefined' && ApiClient.token ? ApiClient.token() : '';
|
||||
|
||||
if (!token) {
|
||||
if (layer) layer.msg('请先登录后再绑定微信', { icon: 2 });
|
||||
return;
|
||||
}
|
||||
|
||||
const $button = this.$('#wechat-bind-button');
|
||||
this.rememberWechatAccountBindMode();
|
||||
$button.addClass('is-loading').prop('disabled', true);
|
||||
|
||||
ApiClient.get(ApiClient.API.authWechatOpen, { domain: this.getWechatLoginDomain() })
|
||||
.then((res) => {
|
||||
if (!ApiClient.isSuccess(res)) {
|
||||
throw new Error((res && res.msg) || '微信授权地址获取失败');
|
||||
}
|
||||
const authUrl = this.extractWechatAuthUrl(res);
|
||||
if (!authUrl) throw new Error('后端未返回微信授权地址');
|
||||
window.location.href = authUrl;
|
||||
})
|
||||
.catch((error) => {
|
||||
const message = error.message || '微信绑定暂不可用,请稍后再试';
|
||||
if (layer) layer.msg(message, { icon: 2 });
|
||||
else alert(message);
|
||||
})
|
||||
.finally(() => {
|
||||
$button.removeClass('is-loading').prop('disabled', false);
|
||||
});
|
||||
},
|
||||
|
||||
installUnifiedPagination() {
|
||||
if (typeof window === 'undefined' || typeof document === 'undefined') return;
|
||||
this.injectPaginationStyles();
|
||||
|
||||
Reference in New Issue
Block a user