Files
peko-h5/view/peko/modules/game/js/bindAccount.js
2022-10-21 10:32:31 +08:00

380 lines
12 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

let urlPrefix = getUrlPrefix()
let browser = checkVersion()
let env = EnvCheck();
let locateObj = getQueryString();
if (env == 'test') {
new VConsole();
}
// 封装layer消息提醒框
let layerIndex
const showLoading = (content = '加載中...') => {
layer.open({
type: 2,
shadeClose: false,
content,
success(e) {
layerIndex = $(e).attr('index')
}
})
}
const hideLoading = (index) => {
layer.close(index)
}
const toastMsg = (content = '操作完成', time = 2) => {
layer.open({
content,
time,
skin: 'msg'
})
}
if(!browser.app && env !== 'test'){
toastMsg('请在app内打开');
}else{
$('.wrap').removeClass('no-in-app')
}
const GAME_PLATFORM_TYPE_DIC = {
1: 'QQ',
2: '微信'
}
const AME_PLATFORM_TYPE_QQ = 1;
const AME_PLATFORM_TYPE_WECHAT = 2;
$(function(){
let nowTime;
let accountInfo;
getInfoFromClient()
setTimeout(function(){
getUserGameAccountInfo()
}, 50)
let needBindAccountCount=0;
let bindFailCount = 0;
$('.form-item').on('focus', 'input', function(){
if($(this).val()){
$(this).siblings('.delete').show()
}else{
$(this).siblings('.delete').hide()
}
hasDeleteIcon()
})
$('.form-item').on('blur', 'input', function(){
$(this).siblings('.delete').hide()
})
const hasDeleteIcon = () => {
$('.form-item').on('input', 'input', function(){
if(!$(this).val()){
$(this).siblings('.delete').hide()
}else{
$(this).siblings('.delete').show()
}
})
}
$('.form-item').on('mousedown', '.delete', function(){
$(this).siblings('.form-input-item').val('')
$(this).hide()
setTimeout(()=>{
$(this).siblings('.form-input-item').focus()
}, 50)
})
$('.js-btn').on('click', function () {
fn()
});
const fn = () => {
let bindAccountCount = 0;
let qqAccount;
const tmpQqAccount = getAccountInput('js-qq-form-group');
if ((tmpQqAccount.nick && !tmpQqAccount.accountId) || (!tmpQqAccount.nick && tmpQqAccount.accountId)) {
toastMsg('请填写游戏昵称和游戏编號');
return ;
} else if (tmpQqAccount.nick && tmpQqAccount.accountId) {
tmpQqAccount.platformType = AME_PLATFORM_TYPE_QQ;
bindAccountCount++;
qqAccount = [tmpQqAccount];
}
let wechatAccount;
const tmpWechatAccount = getAccountInput('js-wechat-form-group');
if ((tmpWechatAccount.nick && !tmpWechatAccount.accountId) || (!tmpWechatAccount.nick && tmpWechatAccount.accountId)) {
toastMsg('请填写游戏昵称和游戏编號');
return ;
} else if (tmpWechatAccount.nick && tmpWechatAccount.accountId) {
tmpWechatAccount.platformType = AME_PLATFORM_TYPE_WECHAT;
bindAccountCount++;
wechatAccount = [tmpWechatAccount];
}
if (!tmpQqAccount.nick && !tmpQqAccount.accountId && !tmpWechatAccount.nick && !tmpWechatAccount.accountId) {
toastMsg('请填写游戏昵称和游戏编號');
return ;
}
needBindAccountCount = bindAccountCount;
bindFailCount = 0;
if (needBindAccountCount === 1) {
confirm(`是否确认绑定`,
'取消', '确认', function () {
if (qqAccount) {
if(accountInfo.qqAccount){
if(accountInfo.qqAccount.nick === qqAccount[0].nick && accountInfo.qqAccount.accountId === qqAccount[0].accountId && !accountInfo.wechatAccount){
toastMsg('关联账號未更改')
needBindAccountCount--
}else{
if(tmpWechatAccount.nick || !accountInfo.wechatAccount){
doBindAccount(qqAccount)
.then((res) => {
bindAccountSuccess(res)
})
.catch((error) => {
toastMsg('账號已被其他用户绑定')
bindAccountFail('js-qq-form-group', accountInfo.qqAccount)
})
}else{
toastMsg('请填写游戏昵称和游戏编號');
return
}
}
}else{
doBindAccount(qqAccount)
.then((res) => {
bindAccountSuccess(res)
})
.catch((error) => {
toastMsg('账號已被其他用户绑定')
bindAccountFail('js-qq-form-group')
})
}
}
if (wechatAccount) {
if(accountInfo.wechatAccount){
if(accountInfo.wechatAccount.nick === wechatAccount[0].nick && accountInfo.wechatAccount.accountId === wechatAccount[0].accountId && !accountInfo.qqAccount){
toastMsg('关联账號未更改')
needBindAccountCount--
}else{
if(tmpQqAccount.nick || !accountInfo.qqAccount){
doBindAccount(wechatAccount)
.then((res) => {
bindAccountSuccess(res)
})
.catch((error) => {
toastMsg('账號已被其他用户绑定')
bindAccountFail('js-wechat-form-group', accountInfo.wechatAccount)
})
}else{
toastMsg('请填写游戏昵称和游戏编號');
return
}
}
}else{
doBindAccount(wechatAccount)
.then((res) => {
bindAccountSuccess(res)
})
.catch((error) => {
toastMsg('账號已被其他用户绑定')
bindAccountFail('js-wechat-form-group')
})
}
}
});
}else if (needBindAccountCount === 2) {
confirm(`是否确认绑定`,
'取消', '确认', function () {
if(qqAccount && wechatAccount){
if(accountInfo.qqAccount && accountInfo.wechatAccount){
if(accountInfo.qqAccount.nick === qqAccount[0].nick && accountInfo.qqAccount.accountId === qqAccount[0].accountId && accountInfo.wechatAccount.nick === wechatAccount[0].nick && accountInfo.wechatAccount.accountId === wechatAccount[0].accountId){
toastMsg('关联账號未更改');
needBindAccountCount = 0
}else{
Promise.all([doBindAccount(qqAccount), doBindAccount(wechatAccount)])
.then((result) => {
bindAccountSuccess(result[0])
bindAccountSuccess(result[1])
})
.catch((error) => {
toastMsg('账號已被其他用户绑定')
bindAccountFail('js-qq-form-group', accountInfo.qqAccount)
bindAccountFail('js-wechat-form-group', accountInfo.wechatAccount)
})
}
}else{
Promise.all([doBindAccount(qqAccount), doBindAccount(wechatAccount)])
.then((result) => {
bindAccountSuccess(result[0])
bindAccountSuccess(result[1])
})
.catch((error) => {
toastMsg('账號已被其他用户绑定')
if(accountInfo.qqAccount){
bindAccountFail('js-qq-form-group', accountInfo.qqAccount)
bindAccountFail('js-wechat-form-group')
}else if(accountInfo.wechatAccount){
bindAccountFail('js-qq-form-group')
bindAccountFail('js-wechat-form-group', accountInfo.wechatAccount)
}else {
bindAccountFail('js-qq-form-group')
bindAccountFail('js-wechat-form-group')
}
})
}
}
});
}
}
function confirm(tip, cancelText, okText, okCallback) {
$('.shade').show()
$('.shade').fadeIn(300)
$('.confirm-dialog .tip').html(tip)
$('.confirm-dialog .cancel').html(cancelText)
$('.confirm-dialog .ok').html(okText)
$('.confirm-dialog').show()
$('.confirm-dialog').fadeIn()
$('.confirm-dialog .cancel').off("click").click(function () {
$('.shade').hide()
$('.confirm-dialog').hide()
})
$('.confirm-dialog .ok').off("click").click(function () {
$('.shade').hide()
$('.confirm-dialog').hide()
okCallback()
})
}
const doBindAccount = (account) => {
return new Promise((resolve, reject) => {
showLoading();
networkRequest({
type: 'POST',
url: urlPrefix + '/gameManage/user/bindOrChangeGameAccounts',
data: {
gameAccounts: JSON.stringify(account)
},
success(res){
if (res.code === 200) {
resolve(account[0])
} else {
reject(res.message)
}
},
error(err){
hideLoading(layerIndex)
toastMsg('網絡錯誤')
reject(err)
}
})
})
}
function bindAccountSuccess(account) {
needBindAccountCount--;
// 渲染相应的帐號信息
// renderInfoText(account.platformType == AME_PLATFORM_TYPE_QQ ? 'js-qq-form-group' : 'js-wechat-form-group', account);
if (needBindAccountCount <= 0 && bindFailCount <=0) {
hideLoading(layerIndex);
toastMsg('账號关联成功')
setTimeout(() => {
window.location.reload()
}, 500)
}
}
function bindAccountFail(groupClassName, account) {
renderFormInput(groupClassName, account);
needBindAccountCount--;
if (needBindAccountCount <= 0) {
hideLoading(layerIndex);
}
}
function getAccountInput(groupClassName) {
return {
nick : $(`.${groupClassName} .js-nick-item .form-input-item`).val(),
accountId : $(`.${groupClassName} .js-account-id-item .form-input-item`).val(),
}
}
function getUserGameAccountInfo() {
showLoading();
networkRequest({
type: 'GET',
url: urlPrefix + '/gameManage/user/getBindGameAccounts',
success(res) {
hideLoading(layerIndex)
if (res.code === 200) {
accountInfo = res.data;
renderForm(accountInfo);
} else {
toastMsg(res.message)
}
},
error(err) {
hideLoading(layerIndex)
toastMsg('網絡錯誤')
}
})
}
function renderForm(accountInfo) {
// renderInfoText('js-qq-form-group', accountInfo.qqAccount);
// renderInfoText('js-wechat-form-group', accountInfo.wechatAccount);
renderFormInput('js-qq-form-group', accountInfo.qqAccount);
renderFormInput('js-wechat-form-group', accountInfo.wechatAccount);
}
function renderInfoText(groupClassName, account) {
$(`.${groupClassName} .js-nick-item`).html(
// `<div class="form-text-item">游戏昵称:${account.nick}</div>`
`<div class="form-text-item">游戏昵称:${account ? account.nick : "未关联"}</div>`
)
$(`.${groupClassName} .js-account-id-item`).html(
// `<div class="form-text-item">游戏编號:${account.accountId}</div>`
`<div class="form-text-item">游戏编號:${account ? account.accountId : "未关联"}</div>`
)
}
function renderFormInput(groupClassName, account) {
let typeName = 'QQ';
if (groupClassName == 'js-wechat-form-group') {
typeName = '微信'
}
$(`.${groupClassName} .js-nick-item`).html(
`${
account ?
`<input class="form-input-item" value="${account.nick}"/><img src="./images/delete.png" alt="" class="delete">` :
`<input class="form-input-item" placeholder="请输入您的和平精英${typeName}区游戏昵称" /><img src="./images/delete.png" alt="" class="delete">`
}`
)
$(`.${groupClassName} .js-account-id-item`).html(
`${
account ?
`<input class="form-input-item" value="${account.accountId}" /><img src="./images/delete.png" alt="" class="delete">` :
`<input class="form-input-item" placeholder="请输入您的和平精英${typeName}区游戏编號" /><img src="./images/delete.png" alt="" class="delete">`
}`
)
}
})