import {accountManager} from "../../../js/utils"; const app = getApp(); Page({ data: { eduImgDir: global.config.eduImgDir, buttons:[{text:"删除",type:"warn"}] }, onLoad: function (options) { console.log("Load", Date.now()); this.refresh() .then(res=>{ this.loaded = 1; }) }, onReady(){ app.reportPageHistory(); }, onShow(){ console.log("show", Date.now()); if(this.loaded) this.refresh(); }, navBack(){ wx.navigateBack({ delta:1 }); }, refresh(){ console.log("start", Date.now()); this.setData({loading:1}); return app.syncUser() .then(res=>{ if (this.oldNum!=null && accountManager.getAccounts().length>this.oldNum && !this.data.currentAccountSaved && this.currentAccount) { // 增加了新账号,将未保存的老账号信息保存 accountManager.addAccount(this.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}; 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.accounts = accounts; accounts = accounts.map(i=>{ return {user_id:i.user_id,name: i.name,login: i.login, image_url:i.image_url}; }) this.currentAccount = currentAccount; this.setData({ accounts, user: {user_id }, currentAccountSaved,loading:0},()=>{ console.log("渲染完成", Date.now()); }); console.log("end", Date.now()); }).catch(e=>{ }) }, deleteAccount({user_id}){ accountManager.removeAccount({user_id}); if(this.data.user.user_id==user_id){ accountManager.clearCurrentAccount(); app.client.session.cookies = ""; app.client.synch = 0; } }, removeAccount2(e){ let { currentTarget: { dataset: { id: user_id } } } = e; this.deleteAccount({user_id}); setTimeout(()=>{ this.refresh(); },300); }, removeAccount1(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){ this.deleteAccount({user_id}); this.refresh(); } } }) }, switchAccount(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.currentAccount){ accountManager.addAccount(this.currentAccount); this.setData({currentAccountSaved:1}); } let account = this.accounts.filter(i=>i.user_id == user_id) [0]; if(!account.save_password){ this.navToAccount({login:account.login, message:"账号过期,需重新登录",login_disabled:1, msgType:"error"}); }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=>{ accountManager.forgetPassword({user_id}); this.navToAccount({login:account.login, message:"账号过期,需重新登录",login_disabled:1, msgType:"error"}); }) } }, navToAccount({login="", message="", msgType="info", login_disabled=""}={}){ wx.navigateTo({ url:`../account/account?nostorage=1&showMsg=1&message=${message}&msgType=${msgType}&msgDuration=0&&save_password=1&addaccount=1&login=${login}&login_disabled=${login_disabled}` }); }, addAccount(){ this.oldNum = accountManager.getAccounts().length if(this.oldNum>=5) return wx.showToast({ title: '已达数量上限,无法添加',icon:"none" }) this.navToAccount({message:"密码将在本地加密保存"}); }, showMsg({message,duration=1000}){ 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; } })