You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 lines
4.5 KiB

import {accountManager} from "../../../js/utils";
const app = getApp();
Page({
data: {
eduImgDir: global.config.eduImgDir
},
onLoad: function (options) {
},
onShow(){
this.refresh();
},
navBack(){
wx.navigateBack({
delta:1
});
},
refresh(){
this.setData({loading:1});
app.syncUser()
.then(res=>{
if (this.oldNum!=null && accountManager.getAccounts().length>this.oldNum && !this.data.currentAccountSaved && this.data.currentAccount) {
// 增加了新账号,将未保存的老账号信息保存
console.error("add current")
console.log(this.data.currentAccount);
accountManager.addAccount(this.data.currentAccount);
this.setData({ currentAccountSaved: 1 });
}
// 更新当前账号信息, 如果更改了信息,可以显示最新的信息 @todo 完善
let {user_id, image_url, show_name} = res.user;
if(show_name)
accountManager.updateAccount({user_id, image_url, name:show_name});
else
accountManager.updateAccount({ user_id, image_url });
let addedAccounts = accountManager.getAccounts();
var currentAccount = accountManager.getCurrentAccount();
var currentAccountSaved = 0;
if(res.user.user_id&&res.user.user_id!=2){
if(!currentAccount){
var currentAccount = { login: res.user.phone || res.user.email, password: "", save_password: 0, image_url: res.user.image_url, name: res.user.real_name||res.user.name, user_id: res.user.user_id};
console.log("currentAccount", currentAccount);
accountManager.setCurrentAccount(currentAccount);
}
for (var account of addedAccounts){
if(account.user_id==currentAccount.user_id)
currentAccountSaved = 1;
}
if(!currentAccountSaved)
var accounts = [...addedAccounts, currentAccount];
else
var accounts = addedAccounts;
}else{
var accounts = addedAccounts;
}
this.setData({ accounts, user: res.user, currentAccountSaved, currentAccount,loading:0});
})
},
removeAccount(e){
console.log(e);
let { currentTarget: { dataset: { id: user_id } } } = e;
var info = this.data.user.user_id == user_id?'退出登录并移除':"移除此账号";
wx.showActionSheet({
itemList: [info],
success:res=>{
if(res.tapIndex==0){
console.log(user_id);
accountManager.removeAccount({user_id});
if(this.data.user.user_id==user_id){
accountManager.clearCurrentAccount();
app.client.session.cookies = "";
app.client.synch = 0;
}
this.refresh();
}
}
})
},
switchAccount(e){
console.log(e);
let { currentTarget: {dataset: {id: user_id}}} = e;
if (user_id==this.data.user.user_id) return;
if (accountManager.getAccounts().length<5&&!this.data.currentAccountSaved&&this.data.currentAccount){
accountManager.addAccount(this.data.currentAccount);
this.setData({currentAccountSaved:1});
}
let account = this.data.accounts.filter(i=>i.user_id == user_id) [0];
if(!account.save_password){
wx.navigateTo({ url: "../account/account?nostorage=1&error=账号过期,需重新登录; 密码将加密保存至本地&save_password=1&addaccount=1&login_disabled=1&login="+account.login});
}else{
app.api("accounts.login")(account)
.then(res=>{
res.login = account.login; // 使用登录的手机号或邮箱而不是login字符
accountManager.updateAccount(res);
this.showMsg({message:"切换成功"});
accountManager.setCurrentAccount(account);
this.refresh();
}).catch(e=>{
wx.navigateTo({ url: "../account/account?nostorage=1&error=账号过期,需重新登录;密码将加密保存至本地&save_password=1&addaccount=1&login_disabled=1&login=" + account.login });
})
}
},
addAccount(){
this.oldNum = accountManager.getAccounts().length
if(this.oldNum>=5)
return wx.showToast({
title: '已达数量上限,无法添加',icon:"none"
})
wx.navigateTo({ url:"../account/account?nostorage=1&error=密码将加密保存至本地&save_password=1&addaccount=1"});
},
showMsg({message,duration=800}){
console.log(message);
if(this.timeoutId)
clearTimeout(this.timeoutId);
this.setData({ message, showMessage:1});
this.timeoutId = setTimeout(()=>{
this.hideMsg()
},duration);
},
hideMsg(){
this.setData({showMessage:0});
this.timeoutId = 0;
}
})