From 35187f62bcc6e02b35f1758a8a44582ed2677d01 Mon Sep 17 00:00:00 2001 From: q326066766 <326066766@qq.com> Date: Thu, 6 Feb 2020 00:28:46 +0800 Subject: [PATCH 1/7] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BA=86=E5=AD=A6?= =?UTF-8?q?=E5=8F=B7=E9=94=99=E8=AF=AF=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cloudfunctions-aliyun/signIn/index.js | 102 +++--- pages.json | 122 +++---- pages/index/index.vue | 262 +++++++-------- pages/login/login.vue | 458 +++++++++++++------------- pages/student/student_day.vue | 43 +-- pages/student_bind/student_bind.vue | 90 ++--- pages/teacher_bind/teacher_bind.vue | 3 +- 7 files changed, 529 insertions(+), 551 deletions(-) diff --git a/cloudfunctions-aliyun/signIn/index.js b/cloudfunctions-aliyun/signIn/index.js index d4c9241..959a19c 100644 --- a/cloudfunctions-aliyun/signIn/index.js +++ b/cloudfunctions-aliyun/signIn/index.js @@ -223,19 +223,19 @@ function base64urlEscape(str) { var jwtSimple = jwt_1; -const passSecret = ''; //用于用户数据库密码加密的密钥,使用一个比较长的随机字符串即可 - -//上面三个字段非常重要!!! +const passSecret = ''; //用于用户数据库密码加密的密钥,使用一个比较长的随机字符串即可 + +//上面三个字段非常重要!!! const tokenExp = 7200000; -function encryptPassword(password) { - const hmac = crypto.createHmac('sha1', passSecret.toString('ascii')); - hmac.update(password); - return hmac.digest('hex'); +function encryptPassword(password) { + const hmac = crypto.createHmac('sha1', passSecret.toString('ascii')); + hmac.update(password); + return hmac.digest('hex'); } -function userTypeConfig(userType) { +function userTypeConfig(userType) { // 根据tab类型匹配对应用户表 // 0:老师 // 1:学生 @@ -250,59 +250,59 @@ function userTypeConfig(userType) { return userOptionDB[userType]; } -const db = uniCloud.database(); - -async function signUp(event) { - const { +const db = uniCloud.database(); + +async function signUp(event) { + const { username, password, - userType, - } = event; - - let userInfo = { - username + userType, + } = event; + + let userInfo = { + username }; // 根据用户类型匹配表名称 - const userDBkye = userTypeConfig(userType); - const userInDB = await db.collection(userDBkye).where({ + const userDBkye = userTypeConfig(userType); + const userInDB = await db.collection(userDBkye).where({ username, - //管理员不加密 使用明文 - password: userDBkye === 'admin_users' ? password : encryptPassword(password), + //管理员不加密 使用明文 + password: userDBkye === 'admin_users' ? password : encryptPassword(password), }).get(); - // 增加用户类型返回给前端 + // 增加用户类型返回给前端 userInfo.userType = userType; - - let tokenSecret = crypto.randomBytes(16).toString('hex'), - token = jwtSimple.encode(userInfo, tokenSecret); - let userUpdateResult; - if (userInDB.data && userInDB.data.length === 0) { - return { - status: -1, - msg: '用户名或密码不正确' - } - } else { - userUpdateResult = await db.collection(userDBkye).doc(userInDB.data[0]._id).update({ - tokenSecret, - exp: Date.now() + tokenExp - }); - } - - if (userUpdateResult.id || userUpdateResult.affectedDocs === 1) { - return { - status: 0, - token, + + let tokenSecret = crypto.randomBytes(16).toString('hex'), + token = jwtSimple.encode(userInfo, tokenSecret); + let userUpdateResult; + if (userInDB.data && userInDB.data.length === 0) { + return { + status: -1, + msg: '用户名或密码不正确' + } + } else { + userUpdateResult = await db.collection(userDBkye).doc(userInDB.data[0]._id).update({ + tokenSecret, + exp: Date.now() + tokenExp + }); + } + + if (userUpdateResult.id || userUpdateResult.affectedDocs === 1) { + return { + status: 0, + token, msg: '登录成功', uid:userInDB.data[0]._id, class_id:userType==0?userInDB.data[0].class_info:userInDB.data[0].class_id, - stu_no:(userType==1 || userType==2)?userInDB.data[0].stu_no:'', - stu_name:(userType==1 || userType==2)?userInDB.data[0].stu_name:'' - } - } - - return { - status: -1, - msg: '登录失败' - } + stu_num:(userType==1 || userType==2)?userInDB.data[0].stu_num:'', + stu_name:(userType==1 || userType==2)?userInDB.data[0].stu_name:'' + } + } + + return { + status: -1, + msg: '登录失败' + } } exports.main = signUp; diff --git a/pages.json b/pages.json index dd33329..9532e63 100644 --- a/pages.json +++ b/pages.json @@ -1,66 +1,66 @@ { - "pages": [ //pages数组中第一项表示应用启动页,参考:https://uniapp.dcloud.io/collocation/pages - { - "path": "pages/index/index", - "style": { - "navigationBarTitleText": "首页" - } + "pages": [ //pages数组中第一项表示应用启动页,参考:https://uniapp.dcloud.io/collocation/pages + { + "path": "pages/index/index", + "style": { + "navigationBarTitleText": "首页" + } }, - { - "path" : "pages/register/register", - "style" : { - "navigationBarTitleText": "注册" - } - }, - { - "path" : "pages/login/login", - "style" : { - "navigationBarTitleText": "登录" - } - }, - { - "path" : "pages/weixinRegister/weixinRegister", - "style" : { - "navigationBarTitleText": "微信注册" - } - }, - { - "path":"pages/student/student_day", - "style":{ - "navigationBarTitleText":"健康报备" - } - }, - { - "path":"pages/administrator/admin_grade", - "style":{ - "navigationBarTitleText":"年级管理" - } - }, - { - "path":"pages/administrator/admin_class", - "style":{ - "navigationBarTitleText":"班级管理" - } - }, - { - "path": "pages/list/list", - "style": { - "navigationBarTitleText": "健康统计汇总" - } - }, - { - "path": "pages/teacher_bind/teacher_bind", - "style": { - "navigationBarTitleText": "绑定班级" - } - }, - { - "path": "pages/student_bind/student_bind", - "style": { - "navigationBarTitleText": "绑定学生信息" - } - } - ], + { + "path": "pages/register/register", + "style": { + "navigationBarTitleText": "注册" + } + }, + { + "path": "pages/login/login", + "style": { + "navigationBarTitleText": "登录" + } + }, + { + "path": "pages/weixinRegister/weixinRegister", + "style": { + "navigationBarTitleText": "微信注册" + } + }, + { + "path": "pages/student/student_day", + "style": { + "navigationBarTitleText": "健康报备" + } + }, + { + "path": "pages/administrator/admin_grade", + "style": { + "navigationBarTitleText": "年级管理" + } + }, + { + "path": "pages/administrator/admin_class", + "style": { + "navigationBarTitleText": "班级管理" + } + }, + { + "path": "pages/list/list", + "style": { + "navigationBarTitleText": "健康统计汇总" + } + }, + { + "path": "pages/teacher_bind/teacher_bind", + "style": { + "navigationBarTitleText": "绑定班级" + } + }, + { + "path": "pages/student_bind/student_bind", + "style": { + "navigationBarTitleText": "绑定学生信息" + } + } + ], "globalStyle": { "navigationBarTextStyle": "black", "navigationBarTitleText": "uni-app", diff --git a/pages/index/index.vue b/pages/index/index.vue index 84d21c5..d30c3f1 100644 --- a/pages/index/index.vue +++ b/pages/index/index.vue @@ -1,44 +1,44 @@ @@ -47,74 +47,74 @@ export default { data() { return { - school: '', - userType:4, + school: '', + userType:4, userText:'', } }, onShow() { this.getUserInfo() }, - methods: { - logout(){ - //用户退出,清楚缓存信息 - uni.removeStorageSync('token') - uni.removeStorageSync("userType") - uni.removeStorageSync("uid") - uni.removeStorageSync("class_id") - uni.removeStorageSync("stu_no") - uni.removeStorageSync("stu_name") - - uni.navigateTo({ - url: '/pages/login/login' - }); - }, - setUserText(userType){ - const userOptionDB = { - 0:'老师', - 1:'学生', - 2:'家长', - 3:'管理员', - } - return userOptionDB[userType]; + methods: { + logout(){ + //用户退出,清楚缓存信息 + uni.removeStorageSync('token') + uni.removeStorageSync("userType") + uni.removeStorageSync("uid") + uni.removeStorageSync("class_id") + uni.removeStorageSync("stu_num") + uni.removeStorageSync("stu_name") + + uni.navigateTo({ + url: '/pages/login/login' + }); }, - getUserInfo(){ - console.log("getUserInfo begin") - uni.showLoading({ - title: '加载中...' - }); - - let token = uni.getStorageSync('token') - - if(token){ - uniCloud.callFunction({ - name: 'validateToken', - data: { - token: uni.getStorageSync('token') - } - }).then((res) => { - console.log("validateToken",res.result.data) - try{ - const { userType,school } = res.result.data; - this.school = school; - this.userType = userType; - this.userText = this.setUserText(userType) - }catch(e){ - throw new Error("参数错误请重新登录"); - } - uni.hideLoading() - }).catch((err) => { - uni.hideLoading() - uni.showModal({ - content: '请求云函数发生错误,' + err.message, - showCancel: false - }) - }) - }else{ - uni.navigateTo({ - url:"../login/login" - }) - } + setUserText(userType){ + const userOptionDB = { + 0:'老师', + 1:'学生', + 2:'家长', + 3:'管理员', + } + return userOptionDB[userType]; + }, + getUserInfo(){ + console.log("getUserInfo begin") + uni.showLoading({ + title: '加载中...' + }); + + let token = uni.getStorageSync('token') + + if(token){ + uniCloud.callFunction({ + name: 'validateToken', + data: { + token: uni.getStorageSync('token') + } + }).then((res) => { + console.log("validateToken",res.result.data) + try{ + const { userType,school } = res.result.data; + this.school = school; + this.userType = userType; + this.userText = this.setUserText(userType) + }catch(e){ + throw new Error("参数错误请重新登录"); + } + uni.hideLoading() + }).catch((err) => { + uni.hideLoading() + uni.showModal({ + content: '请求云函数发生错误,' + err.message, + showCancel: false + }) + }) + }else{ + uni.navigateTo({ + url:"../login/login" + }) + } } } } @@ -137,42 +137,42 @@ margin-bottom: 50rpx; } - .text-area { + .text-area { text-align: center; - } - .tipText{ - text-align: left; - margin: 40upx 0; + } + .tipText{ + text-align: left; + margin: 40upx 0; } .title { - font-size: 20px; + font-size: 20px; text-align: center; - font-weight: bold; - margin-bottom: 30upx; - padding: 20px 0px; + font-weight: bold; + margin-bottom: 30upx; + padding: 20px 0px; - } - - .buttonGroup{ width: 100%; margin-bottom:40upx ;} - .buttonGroup .linkBtn{ - font-size: 30upx; - display: block; - line-height: 80upx; - margin: 0 auto; - width:90%; - text-align: center; - margin-bottom: 20upx; - border-radius: 40upx; - box-shadow: 0 3px 5px rgba(5,26,180,0.35); - font-weight: bold; - background: linear-gradient(135deg, #576FEC 0%, #576FEC 76%, #536DEC 76%); - color: #fff; - } - .logOutBtn{ flex: 1; width: 90%;margin-top: 20upx;} - - .welcome{ - padding-left: 22px; - margin-bottom: 35px; + } + + .buttonGroup{ width: 100%; margin-bottom:40upx ;} + .buttonGroup .linkBtn{ + font-size: 30upx; + display: block; + line-height: 80upx; + margin: 0 auto; + width:90%; + text-align: center; + margin-bottom: 20upx; + border-radius: 40upx; + box-shadow: 0 3px 5px rgba(5,26,180,0.35); + font-weight: bold; + background: linear-gradient(135deg, #576FEC 0%, #576FEC 76%, #536DEC 76%); + color: #fff; + } + .logOutBtn{ flex: 1; width: 90%;margin-top: 20upx;} + + .welcome{ + padding-left: 22px; + margin-bottom: 35px; } - + diff --git a/pages/login/login.vue b/pages/login/login.vue index b9f4198..6a72d79 100644 --- a/pages/login/login.vue +++ b/pages/login/login.vue @@ -1,41 +1,41 @@ - @@ -48,73 +48,85 @@ data() { return { school: '', - userType:4, - userText:'', + userType: 4, + userText: '', } }, + onLoad:function(){ + this.school = uni.getStorageSync('school') + }, onShow() { - this.getUserInfo() + if (uni.getStorageSync('token')) { + this.getUserInfo() + } else { + uni.navigateTo({ + url: '../login/login' + }) + } }, methods: { - logout(){ - //用户退出,清楚缓存信息 + logout() { + //用户退出,清楚缓存信息 uni.removeStorageSync('token') - uni.removeStorageSync("userType") - uni.removeStorageSync("uid") - uni.removeStorageSync("class_id") - uni.removeStorageSync("stu_num") - uni.removeStorageSync("stu_name") - + uni.removeStorageSync("userType") + uni.removeStorageSync("uid") + uni.removeStorageSync("class_id") + uni.removeStorageSync("stu_num") + uni.removeStorageSync("stu_name") + uni.navigateTo({ - url: '/pages/login/login' + url: '/pages/login/login' }); }, - setUserText(userType){ + setUserText(userType) { const userOptionDB = { - 0:'老师', - 1:'学生', - 2:'家长', - 3:'管理员', + 0: '老师', + 1: '学生', + 2: '家长', + 3: '管理员', } return userOptionDB[userType]; }, - getUserInfo(){ - console.log("getUserInfo begin") + getUserInfo() { + console.log("getUserInfo begin") uni.showLoading({ title: '加载中...' }); - - let token = uni.getStorageSync('token') - - if(token){ - uniCloud.callFunction({ - name: 'validateToken', - data: { - token: uni.getStorageSync('token') - } - }).then((res) => { - console.log("validateToken",res.result.data) - try{ - const { userType,school } = res.result.data; - this.school = school; - this.userType = userType; - this.userText = this.setUserText(userType) - }catch(e){ - throw new Error("参数错误请重新登录"); - } - uni.hideLoading() - }).catch((err) => { - uni.hideLoading() - uni.showModal({ - content: '请求云函数发生错误,' + err.message, - showCancel: false - }) - }) - }else{ - uni.navigateTo({ - url:"../login/login" - }) - } + + let token = uni.getStorageSync('token') + + if (token) { + uniCloud.callFunction({ + name: 'validateToken', + data: { + token: uni.getStorageSync('token') + } + }).then((res) => { + console.log("validateToken", res.result.data) + try { + const { + userType, + school + } = res.result.data; + this.school = school; + this.userType = userType; + this.userText = this.setUserText(userType) + } catch (e) { + throw new Error("参数错误请重新登录"); + } + uni.hideLoading() + }).catch((err) => { + uni.hideLoading() + uni.showModal({ + content: '请求云函数发生错误,' + err.message, + showCancel: false + }) + }) + } else { + uni.navigateTo({ + url: "../login/login" + }) + } } } } @@ -140,7 +152,8 @@ .text-area { text-align: center; } - .tipText{ + + .tipText { text-align: left; margin: 40upx 0; } @@ -150,29 +163,32 @@ text-align: center; font-weight: bold; margin-bottom: 30upx; - padding: 20px 0px; + padding: 20px 0px; + + } + .buttonGroup { + width: 100%; + margin-bottom: 40upx; } - - .buttonGroup{ width: 100%; margin-bottom:40upx ;} - .buttonGroup .linkBtn{ + + .buttonGroup .linkBtn { font-size: 30upx; display: block; line-height: 80upx; margin: 0 auto; - width:90%; + width: 90%; text-align: center; - margin-bottom: 20upx; - border-radius: 40upx; - box-shadow: 0 3px 5px rgba(5,26,180,0.35); + margin-bottom: 20upx; + border-radius: 40upx; + box-shadow: 0 3px 5px rgba(5, 26, 180, 0.35); font-weight: bold; background: linear-gradient(135deg, #576FEC 0%, #576FEC 76%, #536DEC 76%); color: #fff; } - .logOutBtn{ flex: 1; width: 90%;margin-top: 20upx;} - - .welcome{ - padding-left: 22px; - margin-bottom: 35px; - } - + + .welcome { + padding-left: 22px; + margin-bottom: 35px; + } + diff --git a/pages/list/list.vue b/pages/list/list.vue index 20096b9..2d08dee 100644 --- a/pages/list/list.vue +++ b/pages/list/list.vue @@ -17,7 +17,7 @@ class="list" :class="{ listBgc: index % 2 == 0, listOne: index == 0, listFinally: index == stat - 1 }" v-for="(item, index) in arr" - :key="item" + :key="index" > 学号:{{ item.stu_num }} @@ -60,9 +60,15 @@ export default { uniCalendar }, onLoad(data) { - this.class_id = uni.getStorageSync("class_id") - this.time = myDate.getFullYear() + '/' + (myDate.getMonth() + 1) + '/' + myDate.getDate(); - this.http(); + if (uni.getStorageSync('token')) { + this.class_id = uni.getStorageSync("class_id") + this.time = myDate.getFullYear() + '/' + (myDate.getMonth() + 1) + '/' + myDate.getDate(); + this.http(); + }else{ + uni.navigateTo({ + url:'../login/login' + }) + } }, methods: { change(e) { diff --git a/pages/login/login.vue b/pages/login/login.vue index 6a72d79..36f0d43 100644 --- a/pages/login/login.vue +++ b/pages/login/login.vue @@ -1,20 +1,13 @@ @@ -87,13 +72,14 @@ { name: '家长', icon: 'cuIcon-wifi' + }, + { + name: '管理员', + icon: 'cuIcon-wifi' } - // { - // name: '管理员', - // icon: 'cuIcon-wifi' - // } ], userType: 0, + school:'' }; }, components: { @@ -102,6 +88,7 @@ }, computed: {}, onLoad() { + this.school = uni.getStorageSync('school') }, methods: { tabChange(index) { @@ -113,22 +100,6 @@ password, userType, } = this - - if (username.length < 3) { - uni.showModal({ - content: '用户名长度均不能小于3', - showCancel: false - }) - return - } - - if (password.length < 6) { - uni.showModal({ - content: '密码长度均不能小于6', - showCancel: false - }) - return - } uni.showLoading({ title: '登录中...' }) @@ -140,44 +111,57 @@ userType, }, }).then((res) => { - console.log('返回的',res); + console.log('返回的', res); uni.hideLoading() if (res.result.status !== 0) { return Promise.reject(new Error(res.result.msg)) } uni.setStorageSync('token', res.result.token) - uni.setStorageSync('uid', res.result.uid) - uni.setStorageSync("userType",this.userType) + uni.setStorageSync('uid', res.result.uid) + uni.setStorageSync("userType", this.userType) uni.showModal({ content: '登录成功', showCancel: false }) - - if(!res.result.class_id){ - if(this.userType==0){ - uni.navigateTo({ - url: '/pages/teacher_bind/teacher_bind' - }); - }else if(this.userType==1 || this.userType==2){ - uni.navigateTo({ - url:'/pages/student_bind/student_bind' - }) - } - }else{ - uni.setStorageSync("class_id",res.result.class_id) - - if(this.userType==1 || this.userType==2){ - uni.setStorageSync("stu_num",res.result.stu_num) - uni.setStorageSync("stu_name",res.result.stu_name) - } - - uni.navigateTo({ - url: '/pages/index/index' - }); - } - - + + if (!res.result.class_id) { + switch (this.userType){ + case 0: + uni.navigateTo({ + url: '/pages/teacher_bind/teacher_bind' + }); + break; + case 1: + uni.navigateTo({ + url: '/pages/student_bind/student_bind' + }) + break; + case 2: + uni.navigateTo({ + url: '/pages/student_bind/student_bind' + }) + break; + case 3: + uni.navigateTo({ + url: '/pages/administrator/admin_grade' + }) + break; + } + } else { + uni.setStorageSync("class_id", res.result.class_id) + + if (this.userType == 1 || this.userType == 2) { + uni.setStorageSync("stu_num", res.result.stu_num) + uni.setStorageSync("stu_name", res.result.stu_name) + } + + uni.navigateTo({ + url: '/pages/index/index' + }); + } + + }).catch((err) => { console.log(err); uni.hideLoading() @@ -191,10 +175,12 @@ uni.showLoading({ title: '登录中...' }) - + this.getCode().then((code) => { console.log('code', code); - const { userType } = this; + const { + userType + } = this; return uniCloud.callFunction({ name: 'login', data: { @@ -269,13 +255,42 @@ diff --git a/pages/register/register.vue b/pages/register/register.vue index e1eaf7d..d298ad9 100644 --- a/pages/register/register.vue +++ b/pages/register/register.vue @@ -1,16 +1,9 @@ diff --git a/pages/teacher_bind/teacher_bind.vue b/pages/teacher_bind/teacher_bind.vue index 9004748..1a73752 100644 --- a/pages/teacher_bind/teacher_bind.vue +++ b/pages/teacher_bind/teacher_bind.vue @@ -25,8 +25,9 @@ - - + + + @@ -45,21 +46,31 @@ className:"" }; }, - onLoad(data) { - this.teacher_id = uni.getStorageSync("uid") - uniCloud.callFunction({ - name: 'getClassList', - }) - .then(res => { - console.log(res); - this.classList = res.result - this.gradeArr = res.result.gradeList - }) - .catch(err => { - uni.hideLoading(); - console.error(err); - }); - + onLoad(data) { + this.teacher_id = uni.getStorageSync("uid") + uniCloud.callFunction({ + name: 'getClassList', + }) + .then(res => { + console.log(res); + this.classList = res.result + this.gradeArr = res.result.gradeList + if (uni.getStorageSync('class_id')) { + uni.showModal({ + title:'提示', + content:'您已经设置过班级信息了,真的要重新绑定吗?', + success: (res) => { + if (res.cancel) { + this.go_back() + } + } + }) + } + }) + .catch(err => { + uni.hideLoading(); + console.error(err); + }); }, methods: { @@ -102,7 +113,16 @@ console.error(err); }); - } + }, + go_back: function() { + if (getCurrentPages().length > 1) { + uni.navigateBack(); + } else { + uni.switchTab({ + url: '../index/index' + }) + } + }, } }; diff --git a/pages/weixinRegister/weixinRegister.vue b/pages/weixinRegister/weixinRegister.vue index ee709d4..f353e7a 100644 --- a/pages/weixinRegister/weixinRegister.vue +++ b/pages/weixinRegister/weixinRegister.vue @@ -1,14 +1,6 @@