diff --git a/spug_api/apps/account/user.py b/spug_api/apps/account/user.py index 5241c5c..48cbcff 100644 --- a/spug_api/apps/account/user.py +++ b/spug_api/apps/account/user.py @@ -79,25 +79,41 @@ def put(u_id): return json_response(message=error) -@blueprint.route('/modifypwd', methods=['POST']) -def modify_pwd(): - form, error = JsonParser('password', 'newpassword').parse() +@blueprint.route('/setting/password', methods=['POST']) +def setting_password(): + form, error = JsonParser( + Argument('password', help='请输入原密码'), + Argument('newpassword', help='请输入心密码') + ).parse() if error is None: if g.user.verify_password(form.password): g.user.password = form.newpassword g.user.save() - return json_response() else: return json_response(message='原密码错误') return json_response(message=error) -@blueprint.route('/', methods=['GET']) -def get_person(u_id): - if u_id: - u_info = User.query.get_or_404(u_id) - return json_response(u_info) - return json_response(message='user_id不能为空') +@blueprint.route('/setting/info', methods=['POST']) +def setting_info(): + form, error = JsonParser( + Argument('nickname', help='请输入昵称'), + Argument('mobile', help='请输入手机号码'), + Argument('email', help='请输入电子邮件地址'), + ).parse() + if error is None: + g.user.update(**form) + return json_response(message=error) + + +@blueprint.route('/self', methods=['GET']) +def get_self(): + return json_response({ + 'username': g.user.username, + 'nickname': g.user.nickname, + 'mobile': g.user.mobile, + 'email': g.user.email, + }) @blueprint.route('/login/', methods=['POST']) @@ -112,9 +128,12 @@ def login(): user.access_token = token user.token_expired = time.time() + 8 * 60 * 60 user.save() - user_data = user.to_json() - user_data.update({'token': token, 'permissions': list(user.permissions)}) - return json_response(user_data) + return json_response({ + 'token': token, + 'is_supper': user.is_supper, + 'nickname': user.nickname, + 'permissions': list(user.permissions) + }) else: login_limit[form.username] += 1 if login_limit[form.username] >= 3: diff --git a/spug_web/src/components/Login.vue b/spug_web/src/components/Login.vue index 5719a88..e806251 100644 --- a/spug_web/src/components/Login.vue +++ b/spug_web/src/components/Login.vue @@ -87,7 +87,6 @@ localStorage.setItem('token', res.result['token']); localStorage.setItem('is_supper', res.result['is_supper']); localStorage.setItem('permissions', res.result['permissions']); - localStorage.setItem('user_id', res.result['id']); localStorage.setItem('nickname', res.result['nickname']); this.$router.push('/welcome'); }, response => { diff --git a/spug_web/src/components/account/Person.vue b/spug_web/src/components/account/Person.vue index 5e97dad..b921269 100644 --- a/spug_web/src/components/account/Person.vue +++ b/spug_web/src/components/account/Person.vue @@ -40,7 +40,7 @@ methods: { //获取用户信息 getPersonInfo(user_id) { - this.$http.get(`/api/account/users/${user_id}`).then((response) => { + this.$http.get(`/api/account/users/self`).then((response) => { this.personInfo = response.result; }, (response) => this.$layer_message(response.result)) }, diff --git a/spug_web/src/components/account/PersonSet.vue b/spug_web/src/components/account/PersonSet.vue index 2b21dd1..6e886fa 100644 --- a/spug_web/src/components/account/PersonSet.vue +++ b/spug_web/src/components/account/PersonSet.vue @@ -133,8 +133,8 @@ }, methods: { //获取用户信息 - getPersonInfo(user_id) { - this.$http.get(`/api/account/users/${user_id}`).then((response) => { + getPersonInfo() { + this.$http.get(`/api/account/users/self`).then((response) => { this.personInfo = response.result; }, (response) => this.$layer_message(response.result)) }, @@ -147,24 +147,30 @@ }, modifyPwd: function () { - this.$http.post(`/api/account/users/modifypwd`, this.pwdInfo).then(response => { + if (this.pwdInfo.newpassword !== this.pwdInfo.surepassword) { + this.$layer_message('两次输入密码不一致'); + return + } + this.$http.post(`/api/account/users/setting/password`, this.pwdInfo).then(() => { this.$layer_message('修改成功', 'success'); + this.logout(); }, response => this.$layer_message(response.result)); }, modifyPerson: function () { - console.log(this.personInfo); - this.$http.put(`/api/account/users/${this.personInfo.id}`, this.personInfo).then(response => { + this.$http.post(`/api/account/users/setting/info`, this.personInfo).then(() => { this.$layer_message('保存成功', 'success'); - localStorage.setItem('nickname', response.result['nickname']); + localStorage.setItem('nickname', this.personInfo['nickname']); }, response => this.$layer_message(response.result)); }, - getPerson(){ - let user_id = localStorage.getItem('user_id'); - this.getPersonInfo(user_id); + logout() { + this.$http.get('/api/account/users/logout/').finally(() => { + localStorage.removeItem('token'); + this.$router.push({name: 'login'}) + }) } }, mounted() { - this.getPerson(); + this.getPersonInfo(); } } \ No newline at end of file