Files
peko-h5/view/peko/modules/h5Income/js/login.js
2023-08-07 16:04:25 +08:00

258 lines
7.6 KiB
JavaScript

let urlPrefix = getUrlPrefix()
let browser = checkVersion()
let env = EnvCheck();
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'
})
}
var idOrPhone = true;//判斷id登入或手機號登入
var times = 0;//倒計時計時器
var isClick = true;//判斷是否再次獲取驗證碼
var token;
$(function () {
setTimeout(function () {
getInfoFromClient()
setTimeout(function () {
getAreaInfo();
}, 100)
})
})
// 獲取驗證碼接口
function getCode () {
var mobileDes = encryptDes(`${$('.box .phone .phoneNumberBox .prefix b').text()}${$('.box .phone .phoneNumberBox .phoneNumber').val()}`, '1ea53d260ecf11e7b56e00163e046a26');
showLoading()
networkRequest({
type: 'POST',
url: urlPrefix + '/sms/getCode',
headers: { "pub_uid": "0" },
data: { mobile: mobileDes, phoneAreaCode: $('.box .phone .phoneNumberBox .prefix b').text(), type: 2 },
success (res) {
if (res.code === 200) {
toastMsg(res.message);
$('.box .phone .codeBox .codeBut').addClass('codeButTime')
timerFun()
} else {
toastMsg(res.message)
}
hideLoading(layerIndex)
},
error (err) {
hideLoading(layerIndex)
toastMsg('網絡錯誤,請退出重進')
}
})
}
// ticket接口
function ticket () {
showLoading()
networkRequest({
type: 'POST',
url: urlPrefix + '/oauth/ticket',
data: { issue_type: "multi", access_token: token },
success (res) {
if (res.code === 200) {
sessionStorage.setItem("uid", res.data.uid);
sessionStorage.setItem("ticket", res.data.tickets[0].ticket);
window.location.href = './index.html'
} else {
toastMsg(res.message)
}
hideLoading(layerIndex)
},
error (err) {
hideLoading(layerIndex)
toastMsg('網絡錯誤,請退出重進')
}
})
}
// 登入接口
function oauthToken () {
if (idOrPhone) {
var phone = encryptDes($('.box .idLogin .id').val(), '1ea53d260ecf11e7b56e00163e046a26');
var password = encryptDes($('.box .idLogin .password').val(), '1ea53d260ecf11e7b56e00163e046a26');
} else {
var phone = $('.box .phone .phoneNumberBox .phoneNumber').val();
var code = $('.box .phone .codeBox .code').val();
var phoneAreaCode = $('.box .phone .phoneNumberBox .prefix b').text();
}
showLoading()
networkRequest({
type: 'POST',
url: urlPrefix + '/oauth/h5/token',
data: idOrPhone ? {
"grant_type": "password", phone, password,
} :
{ grant_type: 'verify_code', phoneAreaCode, phone:`${phoneAreaCode}${phone}`, code,},
success (res) {
if (res.code === 200) {
token = res.data.access_token;
// ticket();
sessionStorage.setItem("uid", res.data.uid);
sessionStorage.setItem("ticket", token);
window.location.href = './index.html'
} else {
toastMsg(res.message)
}
hideLoading(layerIndex)
},
error (err) {
hideLoading(layerIndex)
toastMsg('網絡錯誤,請退出重進')
}
})
}
// 區號接口
function getAreaInfo () {
showLoading()
networkRequest({
type: 'GEt',
url: urlPrefix + '/areaInfo/list',
success (res) {
if (res.code === 200) {
var str = '';
res.data.forEach((res, i) => {
str += `
<span>${res.name} +${res.phoneAreaCode}</span>
`
});
$('.area .box .list').append(str);
$('.area .box .list span').click(function () {
$('.box .phone .phoneNumberBox .prefix b').text($(this).text().replace(/[^0-9]/gi, ''));
$(this).addClass('active').siblings().removeClass('active');
})
} else {
toastMsg(res.message)
}
hideLoading(layerIndex)
},
error (err) {
hideLoading(layerIndex)
toastMsg('網絡錯誤,請退出重進')
}
})
}
// 點擊獲取驗證碼
$('.box .phone .codeBox .codeBut').click(function () {
var val = $('.box .phone .phoneNumberBox .phoneNumber').val();
if (val == '') {
toastMsg('請輸入手機號')
return
}
if (isClick) {
getCode()
}
})
// 倒計時
function timerFun () {
isClick = false;
times = 59;
// 開啟定時器
var timer = setInterval(function () {
// 判斷剩余秒數
if (times == 0) {
// 清除定時器和復原按鈕
clearInterval(timer);
$('.box .phone .codeBox .codeBut').text('獲取驗證碼');
$('.box .phone .codeBox .codeBut').removeClass('codeButTime')
isClick = true;
} else {
$('.box .phone .codeBox .codeBut').text(`${times}`);
times--;
}
}, 1000);
}
// 切換登入方式按鈕
$('.box .hone').click(function () {
idOrPhone = !idOrPhone;
$('.box .phone .phoneNumberBox .phoneNumber,.box .phone .codeBox .code,.box .idLogin .id,.box .idLogin .password').val('');
$('.box .but').removeClass('butok');
if (idOrPhone) {
$('.idLogin').show();
$('.phone').hide();
$('.box .hone').text('piko手機號登錄');
} else {
$('.phone').show();
$('.idLogin').hide();
$('.box .hone').text('piko賬號登錄');
}
})
// 打開選擇區號
$('.box .phone .phoneNumberBox .prefix').click(function () {
$('.area').show();
return false;
})
// 關閉選擇區號
$('.area').click(function () {
$('.area').hide();
return false;
})
//監聽是否可以登入函數
function phoneFun () {
var num1 = $(".box .idLogin .id").val();
var num2 = $(".box .idLogin .password").val();
var num3 = $(".box .phone .phoneNumberBox .phoneNumber").val();
var num4 = $(".box .phone .codeBox .code").val();
if (num1 != '' && num2 != '' || num3 != '' && num4 != '') {
$('.box .but').addClass('butok');
return true;
} else {
$('.box .but').removeClass('butok');
return false;
}
}
// 監聽賬號輸入
$(".box .idLogin .id").on("keyup", function () {
phoneFun()
});
// 監聽密碼輸入
$(".box .idLogin .password").on("keyup", function () {
phoneFun()
});
// 監聽手機號輸入
$(".box .phone .phoneNumberBox .phoneNumber").on("keyup", function () {
phoneFun()
});
// 監聽驗證碼輸入
$(".box .phone .codeBox .code").on("keyup", function () {
phoneFun()
});
// 登入按鈕
$('.box .but').click(function () {
if (phoneFun()) {
oauthToken();
}
})
//DES加密
const encryptDes = (message, key) => {
var keyHex = CryptoJS.enc.Utf8.parse(key);
var encrypted = CryptoJS.DES.encrypt(message, keyHex, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
});
return encrypted.toString();
}