进一步整合js工具

This commit is contained in:
aixianling
2022-03-03 16:18:03 +08:00
parent 23db081c99
commit c521c38af3

View File

@@ -306,6 +306,51 @@ const $calcAge = (code) => {
} }
return Math.ceil($dayjs.duration($dayjs().unix() - $dayjs(birthday).unix(), 's').asYears()) return Math.ceil($dayjs.duration($dayjs().unix() - $dayjs(birthday).unix(), 's').asYears())
} }
/**
* 获取code
* @returns {Promise<unknown>}
*/
const $getLoginCode = () => {
return new Promise(function (resolve, reject) {
uni.login({
success: function (res) {
if (res.code) {
resolve(res);
} else {
reject(res);
}
},
fail: function () {
reject(false);
}
});
});
};
const $autoLogin = (params, store) => {
params = params ? params : {};
return new Promise(function (resolve, reject) {
$getLoginCode().then((res) => {
let body = {...params, code: res.code};
store.commit('getToken', {
...body,
then: (v1) => {
if (v1) {
store.commit('getUserInfo', (v2) => {
if (v2) {
resolve(v2);
} else {
reject(v2);
}
});
} else {
reject(v1);
}
}
});
});
});
};
export default { export default {
$toast, $toast,
$loading, $loading,
@@ -318,5 +363,7 @@ export default {
$idCardNoUtil: idCardNoUtil, $idCardNoUtil: idCardNoUtil,
$calcAge, $calcAge,
$dayjs, $dayjs,
$dict $dict,
$getLoginCode,
$autoLogin
}; };