From 86f9a86aeb6a29d4b19874659706aa036826a47f Mon Sep 17 00:00:00 2001 From: vapao Date: Thu, 16 Jan 2020 18:36:56 +0800 Subject: [PATCH] =?UTF-8?q?A=20=E6=B7=BB=E5=8A=A0=E4=B8=AA=E4=BA=BA?= =?UTF-8?q?=E4=B8=AD=E5=BF=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spug_api/apps/account/urls.py | 1 + spug_api/apps/account/views.py | 23 ++++++++ spug_web/src/layout/Header.js | 7 ++- spug_web/src/pages/login/index.js | 4 +- .../src/pages/welcome/{ => index}/index.js | 0 spug_web/src/pages/welcome/info/Basic.js | 39 +++++++++++++ spug_web/src/pages/welcome/info/Reset.js | 56 +++++++++++++++++++ spug_web/src/pages/welcome/info/index.js | 43 ++++++++++++++ .../src/pages/welcome/info/index.module.css | 25 +++++++++ spug_web/src/pages/welcome/info/store.js | 12 ++++ spug_web/src/pages/welcome/routes.js | 4 +- 11 files changed, 209 insertions(+), 5 deletions(-) rename spug_web/src/pages/welcome/{ => index}/index.js (100%) create mode 100644 spug_web/src/pages/welcome/info/Basic.js create mode 100644 spug_web/src/pages/welcome/info/Reset.js create mode 100644 spug_web/src/pages/welcome/info/index.js create mode 100644 spug_web/src/pages/welcome/info/index.module.css create mode 100644 spug_web/src/pages/welcome/info/store.js diff --git a/spug_api/apps/account/urls.py b/spug_api/apps/account/urls.py index d13ef8e..9e59e40 100644 --- a/spug_api/apps/account/urls.py +++ b/spug_api/apps/account/urls.py @@ -10,4 +10,5 @@ urlpatterns = [ url(r'^logout/', logout), url(r'^user/$', UserView.as_view()), url(r'^role/$', RoleView.as_view()), + url(r'^self/$', SelfView.as_view()), ] diff --git a/spug_api/apps/account/views.py b/spug_api/apps/account/views.py index 0962cf8..007a71b 100644 --- a/spug_api/apps/account/views.py +++ b/spug_api/apps/account/views.py @@ -108,6 +108,29 @@ class RoleView(View): return json_response(error=error) +class SelfView(View): + def patch(self, request): + form, error = JsonParser( + Argument('old_password', required=False), + Argument('new_password', required=False), + Argument('nickname', required=False), + ).parse(request.body, True) + if error is None: + if form.get('old_password') and form.get('new_password'): + if len(form.new_password) < 6: + return json_response(error='请设置至少6位的新密码') + if request.user.verify_password(form.old_password): + request.user.password_hash = User.make_password(form.new_password) + request.user.token_expired = 0 + request.user.save() + else: + return json_response(error='原密码错误,请重新输入') + if form.get('nickname'): + request.user.nickname = form.nickname + request.user.save() + return json_response(error=error) + + def login(request): form, error = JsonParser( Argument('username', help='请输入用户名'), diff --git a/spug_web/src/layout/Header.js b/spug_web/src/layout/Header.js index 312534f..7869348 100644 --- a/spug_web/src/layout/Header.js +++ b/spug_web/src/layout/Header.js @@ -4,6 +4,7 @@ * Released under the MIT License. */ import React from 'react'; +import { Link } from 'react-router-dom'; import { Layout, Dropdown, Menu, List, Icon, Badge, Avatar } from 'antd'; import styles from './layout.module.css'; import http from '../libs/http'; @@ -57,8 +58,10 @@ export default class extends React.Component { }; menu = ( - - 个人中心 + + + 个人中心 + diff --git a/spug_web/src/pages/login/index.js b/spug_web/src/pages/login/index.js index 3712408..0cd937f 100644 --- a/spug_web/src/pages/login/index.js +++ b/spug_web/src/pages/login/index.js @@ -52,7 +52,7 @@ class LoginIndex extends React.Component { if (history.location.state && history.location.state['from']) { history.push(history.location.state['from']) } else { - history.push('/welcome') + history.push('/welcome/index') } }; @@ -67,7 +67,7 @@ class LoginIndex extends React.Component {
this.setState({loginType: e})}> - +
diff --git a/spug_web/src/pages/welcome/index.js b/spug_web/src/pages/welcome/index/index.js similarity index 100% rename from spug_web/src/pages/welcome/index.js rename to spug_web/src/pages/welcome/index/index.js diff --git a/spug_web/src/pages/welcome/info/Basic.js b/spug_web/src/pages/welcome/info/Basic.js new file mode 100644 index 0000000..bfc506f --- /dev/null +++ b/spug_web/src/pages/welcome/info/Basic.js @@ -0,0 +1,39 @@ +/** + * Copyright (c) OpenSpug Organization. https://github.com/openspug/spug + * Copyright (c) + * Released under the MIT License. + */ +import React, { useState } from 'react'; +import { Button, Form, Input, message } from 'antd'; +import styles from './index.module.css'; +import { http } from 'libs'; +import store from './store'; + + +export default function Basic(props) { + const [nickname, setNickname] = useState(localStorage.getItem('nickname')); + + function handleSubmit() { + store.loading = true; + http.patch('/api/account/self/', {nickname}) + .then(() => { + message.success('设置成功,重新登录或刷新页面后生效'); + localStorage.setItem('nickname', nickname) + }) + .finally(() => store.loading = false) + } + + return ( + +
基本设置
+ + + setNickname(e.target.value)}/> + + + + + +
+ ) +} diff --git a/spug_web/src/pages/welcome/info/Reset.js b/spug_web/src/pages/welcome/info/Reset.js new file mode 100644 index 0000000..55d7242 --- /dev/null +++ b/spug_web/src/pages/welcome/info/Reset.js @@ -0,0 +1,56 @@ +/** + * Copyright (c) OpenSpug Organization. https://github.com/openspug/spug + * Copyright (c) + * Released under the MIT License. + */ +import React, { useState } from 'react'; +import { Button, Form, Input, message } from 'antd'; +import styles from './index.module.css'; +import { http } from 'libs'; +import store from './store'; +import history from 'libs/history'; + + +export default function Reset(props) { + const [old_password, setOldPassword] = useState(); + const [new_password, setNewPassword] = useState(); + const [new2_password, setNew2Password] = useState(); + + function handleSubmit() { + if (!old_password) { + return message.error('请输入原密码') + } else if (!new_password) { + return message.error('请输入新密码') + } else if (new_password !== new2_password) { + return message.error('两次输入密码不一致') + } + store.loading = true; + http.patch('/api/account/self/', {old_password, new_password}) + .then(() => { + message.success('密码修改成功'); + history.push('/'); + http.get('/api/account/logout/') + }) + .finally(() => store.loading = false) + } + + return ( + +
修改密码
+
+ + setOldPassword(e.target.value)}/> + + + setNewPassword(e.target.value)}/> + + + setNew2Password(e.target.value)}/> + + + + +
+
+ ) +} diff --git a/spug_web/src/pages/welcome/info/index.js b/spug_web/src/pages/welcome/info/index.js new file mode 100644 index 0000000..1f2b56c --- /dev/null +++ b/spug_web/src/pages/welcome/info/index.js @@ -0,0 +1,43 @@ +/** + * Copyright (c) OpenSpug Organization. https://github.com/openspug/spug + * Copyright (c) + * Released under the MIT License. + */ +import React from 'react'; +import { Menu } from 'antd'; +import Basic from './Basic'; +import Reset from './Reset'; +import styles from './index.module.css'; + +class Index extends React.Component { + constructor(props) { + super(props); + this.state = { + selectedKeys: ['basic'] + } + } + + render() { + const {selectedKeys} = this.state; + return ( +
+
+ this.setState({selectedKeys})}> + 基本设置 + 修改密码 + +
+
+ {selectedKeys[0] === 'basic' && } + {selectedKeys[0] === 'reset' && } +
+
+ ) + } +} + +export default Index diff --git a/spug_web/src/pages/welcome/info/index.module.css b/spug_web/src/pages/welcome/info/index.module.css new file mode 100644 index 0000000..0f0a127 --- /dev/null +++ b/spug_web/src/pages/welcome/info/index.module.css @@ -0,0 +1,25 @@ +.container { + display: flex; + background-color: #fff; + padding: 16px 0; +} +.left { + flex: 2; + border-right: 1px solid #e8e8e8; +} +.right { + flex: 7; + padding: 8px 40px; +} + +.title { + margin-bottom: 12px; + color: rgba(0, 0, 0, .85); + font-weight: 500; + font-size: 20px; + line-height: 28px; +} + +.form { + max-width: 320px; +} \ No newline at end of file diff --git a/spug_web/src/pages/welcome/info/store.js b/spug_web/src/pages/welcome/info/store.js new file mode 100644 index 0000000..eae54d5 --- /dev/null +++ b/spug_web/src/pages/welcome/info/store.js @@ -0,0 +1,12 @@ +/** + * Copyright (c) OpenSpug Organization. https://github.com/openspug/spug + * Copyright (c) + * Released under the MIT License. + */ +import { observable } from "mobx"; + +class Store { + @observable loading = false; +} + +export default new Store() diff --git a/spug_web/src/pages/welcome/routes.js b/spug_web/src/pages/welcome/routes.js index 07df5c5..b707ea7 100644 --- a/spug_web/src/pages/welcome/routes.js +++ b/spug_web/src/pages/welcome/routes.js @@ -5,7 +5,9 @@ */ import { makeRoute } from 'libs/router'; import Index from './index'; +import Info from './info'; export default [ - makeRoute('', Index), + makeRoute('/index', Index), + makeRoute('/info', Info), ]