diff --git a/common/js/request.js b/common/js/request.js index a330c34..8363d66 100644 --- a/common/js/request.js +++ b/common/js/request.js @@ -136,6 +136,28 @@ export default { uni.setStorageSync('userProfile', userInfo); return true; }, + async getWxUserInfo() { + // 获取微信登录凭证 + const wxLoginRes = await wx.login(); + // 获取openid + const wxAuthRes = await uni.request({ + url: '/wx/auth', + header: { + code: wxLoginRes.code + } + }) + const openId = wxAuthRes[1].data.data.openid; + // 从服务端获取用户信息 + let wxGetUserRes = await this.qryUserInfo(openId); + let userInfo = null; + if (wxGetUserRes.data) { + userInfo = { + ...wxGetUserRes.data, + wxLoginCode: wxLoginRes.code + } + } + return userInfo; + }, async storageExistUser() { // 获取微信登录凭证 const wxLoginRes = await wx.login(); @@ -191,12 +213,13 @@ export default { }) return res[1].data; }, - async qryUserInfo(openId) { + async qryUserInfo(openId, phone) { let res = await uni.request({ url: '/wx/getWorkerInfo', method: 'POST', data: { - openId: openId + openId: openId, + phone: phone } }) return res[1].data; diff --git a/pages/area-proxy/my-team.vue b/pages/area-proxy/my-team.vue index 662a270..e2d4bb1 100644 --- a/pages/area-proxy/my-team.vue +++ b/pages/area-proxy/my-team.vue @@ -121,7 +121,7 @@ }, async login() { // 更新缓存中的userInfo - await this.$request.storageExistUser(); + // await this.$request.storageExistUser(); // 返回缓存中的userInfo return this.$request.getCurUserInfo(); }, diff --git a/pages/index/index-home.vue b/pages/index/index-home.vue index 9c73afb..27317ca 100644 --- a/pages/index/index-home.vue +++ b/pages/index/index-home.vue @@ -169,20 +169,27 @@ methods: { async authLogin() { // 更新缓存中的userInfo - let res = await this.$request.storageExistUser(); + // let res = await this.$request.storageExistUser(); // 获取缓存中的userInfo let curUserInfo = this.$request.getCurUserInfo(); + console.log(curUserInfo) // 校验提示登录 - if (!curUserInfo || !curUserInfo.openId) { - this.$refs.vertifyLogin.showModal(); + // if (!curUserInfo || !curUserInfo.openId) { + // this.$refs.vertifyLogin.showModal(); + // return false; + // } else { + // this.$refs.vertifyLogin.hideModal(); + // } + if (!curUserInfo) { + uni.reLaunch({ + url: '/pages/login/login' + }) return false; - } else { - this.$refs.vertifyLogin.hideModal(); } // 校验是否注册过 if (!curUserInfo.phone) { uni.reLaunch({ - url: '/pages/login/login?operType=1' + url: '/pages/login/login' }) return false; } diff --git a/pages/index/index.vue b/pages/index/index.vue index 2ae675b..98c4c7b 100644 --- a/pages/index/index.vue +++ b/pages/index/index.vue @@ -79,20 +79,27 @@ }, async authLogin() { // 更新缓存中的userInfo - let res = await this.$request.storageExistUser(); + // let res = await this.$request.storageExistUser(); // 获取缓存中的userInfo let curUserInfo = this.$request.getCurUserInfo(); + console.log(curUserInfo) // 校验提示登录 - if (!curUserInfo || !curUserInfo.openId) { - this.$refs.vertifyLogin.showModal(); + // if (!curUserInfo || !curUserInfo.openId) { + // this.$refs.vertifyLogin.showModal(); + // return false; + // } else { + // this.$refs.vertifyLogin.hideModal(); + // } + if (!curUserInfo) { + uni.reLaunch({ + url: '/pages/login/login' + }) return false; - } else { - this.$refs.vertifyLogin.hideModal(); } // 校验是否注册过 if (!curUserInfo.phone) { uni.reLaunch({ - url: '/pages/login/login?operType=1' + url: '/pages/login/login' }) return false; } diff --git a/pages/login/login.vue b/pages/login/login.vue index 690b1dc..23c9749 100644 --- a/pages/login/login.vue +++ b/pages/login/login.vue @@ -1,75 +1,84 @@ @@ -78,193 +87,201 @@ export default { data() { return { - pageContentTop: this.CustomBar, + pageContentTop: this.CustomBar, operType: 0, // 0为登录,1为注册 - agreeContract: false, + countDownNum: 0, + agreeContract: false, formData: {} } - }, - onLoad(options) { - if (options && options.operType) { - this.operType = Number(options.operType) - } - }, - methods: { - changeAgreeContract() { - this.agreeContract = !this.agreeContract; - }, - changeOperType() { - this.reset(); - this.operType = this.operType === 0 ? 1 : 0; - if (this.operType === 1) { - this.authLogin(); - } - }, - reset() { - this.formData = {}; - this.agreeContract = false; - }, - async authLogin() { - // 更新缓存中的userInfo - let res = await this.$request.storageExistUser(); - // 获取缓存中的userInfo - let curUserInfo = this.$request.getCurUserInfo(); - // 校验提示登录 - if (!curUserInfo || !curUserInfo.openId) { - this.$refs.vertifyLogin.showModal(); - return false; - } else { - this.$refs.vertifyLogin.hideModal(); - } - return true; - }, - validLoginData(type) { - let errMsg = null; - if (type === 'pwd') { - if (!this.formData.phone) { - errMsg = '请输入帐号'; - } else if (!this.formData.password) { - errMsg = '请输入密码'; - } - } - if (errMsg) { - uni.showToast({ - icon: 'none', - title: errMsg - }) - return false; - } - return true; - }, - async loginByPwd() { - if (!this.validLoginData('pwd')) { - return; - } - - let res = await this.$request.appLogin(this.formData); + }, + onLoad(options) { + if (options && options.operType) { + this.operType = Number(options.operType) + } + }, + methods: { + changeAgreeContract() { + this.agreeContract = !this.agreeContract; + }, + changeOperType() { + this.reset(); + this.operType = this.operType === 0 ? 1 : 0; + if (this.operType === 1) { + this.authLogin(); + } + }, + reset() { + this.formData = {}; + this.agreeContract = false; + }, + async authLogin() { + // 获取微信用户信息 + let curUserInfo = await this.$request.getWxUserInfo(); + // 校验提示登录 + if (!curUserInfo || !curUserInfo.openId) { + this.$refs.vertifyLogin.showModal(); + return false; + } else { + this.$refs.vertifyLogin.hideModal(); + } + return true; + }, + validLoginData(type) { + let errMsg = null; + if (type === 'pwd') { + if (!this.formData.phone) { + errMsg = '请输入帐号'; + } else if (!this.formData.password) { + errMsg = '请输入密码'; + } + } + if (errMsg) { + uni.showToast({ + icon: 'none', + title: errMsg + }) + return false; + } + return true; + }, + async loginByPwd() { + if (!this.validLoginData('pwd')) { + return; + } + + let res = await this.$request.appLogin(this.formData); if (res && res.code === 0) { - uni.showToast({ - icon: 'none', - title: '登录成功' - }) - uni.reLaunch({ - url: '/pages/index/index' - }) - } else if (res && res.msg) { - uni.showToast({ - icon: 'none', - title: res.msg - }) - } else { - uni.showToast({ - icon: 'none', - title: '登录失败' - }) - } + let cacheRes = await this.$request.qryUserInfo(null, this.formData.phone); + uni.setStorageSync('userProfile', cacheRes.data); + uni.showToast({ + icon: 'none', + title: '登录成功' + }) + uni.reLaunch({ + url: '/pages/index/index' + }) + } else if (res && res.msg) { + uni.showToast({ + icon: 'none', + title: res.msg + }) + } else { + uni.showToast({ + icon: 'none', + title: '登录失败' + }) + } }, - async sendVertifyCode() { - if (this.$validate.validContactNum(this.formData.phone)) { - let res = await this.$request.sendVertifyCode({ - to: this.formData.phone - }); - if (res && res.code === 0) { - uni.showToast({ - icon: 'none', - title: '验证码已发送' - }) - } else if (res && res.msg) { - uni.showToast({ - icon: 'none', - title: res.msg - }) - } else { - uni.showToast({ - icon: 'none', - title: '验证码发送失败' - }) + startCountDownNum() { + this.countDownNum = 180; + let t = setInterval(() => { + this.countDownNum -= 1; + if (this.countDownNum === 0) { + clearInterval(t); } - } else { - uni.showToast({ - icon: 'none', - title: '请正确输入手机号码' - }) - } - }, - validRegisterData() { - let errMsg = null; - if (!this.formData.name) { - errMsg = '请输入用户昵称'; - } else if (!this.$validate.validContactNum(this.formData.phone)) { - errMsg = '请正确输入手机号码'; - } else if (!this.formData.password || this.formData.password.length < 6) { - errMsg = '密码至少为6位数'; - } else if (!this.formData.passwordAgain || this.formData.password !== this.formData.passwordAgain) { - errMsg = '两次输入密码不一致'; - } else if (!this.formData.registerCode) { - errMsg = '请输入验证码' - } else if (!this.agreeContract) { - errMsg = '请先阅读并同意用户注册协议' - } - if (errMsg) { - uni.showToast({ - icon: 'none', - title: errMsg - }) - return false; - } - return true; - }, - async registerByPhone() { - let curUserInfo = this.$request.getCurUserInfo(); - if (!curUserInfo || !curUserInfo.workerId) { - uni.showToast({ - icon: 'none', - title: '注册失败' - }) - return; - } - - if (!this.validRegisterData()) { - return; - } - - let res = await this.$request.realRegisterUser({ - ...this.formData, - workerId: curUserInfo.workerId - }); - if (res && res.code === 0) { - uni.showToast({ - icon: 'none', - title: '注册成功' - }) - this.changeOperType(); - } else if (res && res.msg) { - uni.showToast({ - icon: 'none', - title: res.msg - }) - } else { - uni.showToast({ - icon: 'none', - title: '注册失败' - }) - } - } + }, 1000); + }, + async sendVertifyCode() { + if (this.$validate.validContactNum(this.formData.phone)) { + let res = await this.$request.sendVertifyCode({ + to: this.formData.phone + }); + if (res && res.code === 0) { + uni.showToast({ + icon: 'none', + title: '验证码已发送' + }) + this.startCountDownNum(); + } else if (res && res.msg) { + uni.showToast({ + icon: 'none', + title: res.msg + }) + } else { + uni.showToast({ + icon: 'none', + title: '验证码发送失败' + }) + } + } else { + uni.showToast({ + icon: 'none', + title: '请正确输入手机号码' + }) + } + }, + validRegisterData() { + let errMsg = null; + if (!this.formData.name) { + errMsg = '请输入用户昵称'; + } else if (!this.$validate.validContactNum(this.formData.phone)) { + errMsg = '请正确输入手机号码'; + } else if (!this.formData.password || this.formData.password.length < 6) { + errMsg = '密码至少为6位数'; + } else if (!this.formData.passwordAgain || this.formData.password !== this.formData.passwordAgain) { + errMsg = '两次输入密码不一致'; + } else if (!this.formData.registerCode) { + errMsg = '请输入验证码' + } else if (!this.agreeContract) { + errMsg = '请先阅读并同意用户注册协议' + } + if (errMsg) { + uni.showToast({ + icon: 'none', + title: errMsg + }) + return false; + } + return true; + }, + async registerByPhone() { + let curUserInfo = this.$request.getCurUserInfo(); + if (!curUserInfo || !curUserInfo.workerId) { + uni.showToast({ + icon: 'none', + title: '注册失败' + }) + return; + } + + if (!this.validRegisterData()) { + return; + } + + let res = await this.$request.realRegisterUser({ + ...this.formData, + workerId: curUserInfo.workerId + }); + if (res && res.code === 0) { + uni.showToast({ + icon: 'none', + title: '注册成功' + }) + this.changeOperType(); + } else if (res && res.msg) { + uni.showToast({ + icon: 'none', + title: res.msg + }) + } else { + uni.showToast({ + icon: 'none', + title: '注册失败' + }) + } + } }, }