const app = getApp(); const key = { save_password: "we,nd;ke;hcy", login: "login", password: "Fie[R‘xu[Eu?ua;c" } Page({ data: { imgDir: global.config.imgDir, action: "login", code_button_text: "获取", action_text: { login: "登录", register: "注册", reset: "找回密码" }, pos: { login: 1, register: 2, reset: 3 }, signUp: 1, logIn:0 }, log_In: function (event) { this.setData({ signUp: 0, logIn: 1 }) }, sign_Up: function (event) { this.setData({ signUp: 1, logIn: 0 }) }, login_test() { var data = { login: "educoder_weapp@126.com", password: "abcdefgh" }; this.setData(data); this.login(data); }, login({ login, password, showToast = 1 }) { app.api("accounts.login")({ login, password }) .then(res => { res.message = "登录成功"; if (showToast) app.showMsg(res); this.navBack(); }) .catch(e => { if (showToast) app.showError(e); }) }, register({ login, password, code }) { app.api("accounts.register")({ login, password, code }) .then(res => { app.showMsg(res); this.navBack(); }).catch(e => { app.showError(e); }) }, reset({ login, password: new_password, password_confirmation: new_password_confirmation, code, no_login }) { app.api("accounts.reset_password")({ login, new_password, new_password_confirmation, code }) .then(res => { res.message = "重置成功"; app.showMsg(res); if (!no_login) this.login({ login, password: new_password, showToast: 0 }); }) .catch(e => { app.showError(e); }) }, getCode({ login }) { if (this.data.action == "register") var type = 1; else if (this.data.action == "reset") var type = 2; else return; this.setData({ code_status: 2, code_button_text: "发送中" }); app.api("accounts.get_verification_code")({ login, type }) .then(res => { res.message = "发送成功"; this.countDown(); app.showMsg(res); }).catch(e => { app.showError(e); this.setData({ code_status: 0, code_button_text: "获取验证码" }) }) }, clearCount(id) { console.log("clearCount", id); clearInterval(id); this.setData({ code_status: 0, code_button_text: "获取验证码" }); }, countDown() { var count = 60; this.setData({ code_status: 1, code_button_text: count-- + "秒后重试" }); var id = setInterval(() => { if (count <= 0) return this.clearCount(id); this.setData({ code_button_text: count-- + "秒后重试" }) }, 1000); console.log("id", id); return id; }, onSubmit({ detail: { value, target } }) { console.log("onSubmit", value, target, this.action); console.log(this.checkInput({ value, action: target.id })); if (!this.checkInput({ value, action: target.id })) return; console.log("target.id") if (target.id == "code") return this.getCode(value); console.log("setAction", this.data); if (target.id != this.data.action) return this.setAction(target.id); console.log("setStorage") this.setStorage(value); console.log("callapi", value); this[target.id](value); }, checkInput({ value, action }) { if (action != this.data.action && action != "code") return true; if (!value.login) return wx.showToast({ title: '请输入邮箱或手机号', icon: "none" }); if (action == "code") return true; if (!value.password) return wx.showToast({ title: '请输入密码', icon: "none" }); if (action == "login") return true; if (!value.code) return wx.showToast({ title: '请输入验证码', icon: "none" }); if (action == "register") return true; if (!value.password_confirmation) return wx.showToast({ title: '请再次输入密码', icon: "none" }); return true; }, setAction(action) { let { pos } = this.data; if (!(action in pos)) return; let tmp = pos[action]; pos[action] = pos[this.data.action]; pos[this.data.action] = tmp; this.setData({ pos, action }); }, onLoad: function (options) { let { action = "login" } = options; this.setAction({ action }); this.getStorage(); }, setStorage({ login, password, save_password }) { wx.setStorageSync(key.login, login); wx.setStorageSync("_password", password); wx.setStorageSync(key.save_password, save_password); if (save_password) wx.setStorageSync(key.password, password); else wx.clearStorageSync(key.password); }, getStorage() { let save_password = wx.getStorageSync(key.save_password); if (save_password !== 0) save_password = 1; let login = wx.getStorageSync(key.login); if (save_password) var password = wx.getStorageSync(key.password); else var password = ""; this.setData({ save_password, login, password }); }, navBack() { setTimeout(() => { wx.navigateBack({ delta: 1 }); }, 500); } })