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.

106 lines
3.7 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 });
}
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=>{
wx.showToast({
title: '切换成功'
});
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"});
}
})