Skip to content

Commit 39c0d92

Browse files
committed
增加修改密码,修复图片上传
1 parent 890a8a9 commit 39c0d92

File tree

90 files changed

+350
-144
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+350
-144
lines changed

admin/src/api/image.js

+3
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ import Vue from 'vue'
22
export default {
33

44
upload (data) {
5+
console.log(data)
56
return new Promise((resolve, reject) => {
67
Vue.axios.post('/image', data, {
78
headers: { 'Content-Type': 'multipart/form-data' }
89
}).then(response => {
910
resolve(response.data)
11+
}).catch(error => {
12+
reject(error)
1013
})
1114
})
1215
}

admin/src/axios/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ axios.interceptors.request.use(config => {
1515
config.headers.Authorization = localStorage.getItem('token')
1616
}
1717
// 防止缓存
18-
if (config.method === 'post') {
18+
if (config.method === 'post'&& config.headers['Content-Type']!=='multipart/form-data') {
1919
config.data = {
2020
...config.data,
2121
_t: Date.parse(new Date()) / 1000

admin/src/router/router.js

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export const adminRouter = [{
4141
{ path: 'tag/save', name: 'TagSave', meta: { title: '标签编辑', requiresAuth: true }, component: resolve => { require(['@/view/tag/save.vue'], resolve) } },
4242
{ path: 'tag/list', name: 'TagList', meta: { title: '标签列表', requiresAuth: true }, component: resolve => { require(['@/view/tag/list.vue'], resolve) } },
4343
{ path: 'user/info', name: 'UserInfo', meta: { title: '个人资料', requiresAuth: true }, component: resolve => { require(['@/view/user/info.vue'], resolve) } },
44+
{ path: 'user/password', name: 'UserPassword', meta: { title: '修改密码', requiresAuth: true }, component: resolve => { require(['@/view/user/password.vue'], resolve) } },
4445
{ path: 'config', name: 'System', meta: { title: '系统设置', requiresAuth: true }, component: resolve => { require(['@/view/config/save.vue'], resolve) } }
4546
]
4647
}]

admin/src/view/Main.vue

+9-5
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<MenuItem :name="item.path" v-if="!item.children">
2020
<Icon :type="item.icon" :size="iconSize"></Icon>
2121
<span class="layout-text">{{item.name}}</span>
22-
</MenuItem>
22+
</MenuItem>
2323
</template>
2424
</Menu>
2525
</Col>
@@ -29,7 +29,7 @@
2929
<Col span="12" class="layout-header-left">
3030
<Button type="text" @click="toggleClick">
3131
<Icon type="navicon" size="32"></Icon>
32-
</Button>
32+
</Button>
3333
</Col>
3434
<Col span="12" class="layout-header-right">
3535
<a class="home" target="_blank" href="/"><Icon type="home"></Icon></a>
@@ -40,6 +40,7 @@
4040
</a>
4141
<DropdownMenu slot="list">
4242
<DropdownItem name="userInfo">个人资料</DropdownItem>
43+
<DropdownItem name="changePassword">修改密码</DropdownItem>
4344
<DropdownItem name="loginOut">退出后台</DropdownItem>
4445
</DropdownMenu>
4546
</Dropdown>
@@ -80,7 +81,7 @@ export default {
8081
userInfo: {},
8182
fold: this.$store.getters.getMenu.fold,
8283
//菜单数据
83-
// TODO:需要优化,这样写太丑
84+
// TODO:需要优化,这样写太丑
8485
menu: [
8586
{
8687
name: "控制台",
@@ -173,12 +174,15 @@ export default {
173174
this.$router.push("/login");
174175
},
175176
dropdownClick(name) {
176-
if (name == "loginOut") {
177+
if (name === 'loginOut') {
177178
this.loginOut();
178179
}
179-
if (name == "userInfo") {
180+
if (name === 'userInfo') {
180181
this.$router.push("/user/info");
181182
}
183+
if (name === 'changePassword') {
184+
this.$router.push("/user/password");
185+
}
182186
}
183187
}
184188
};

admin/src/view/config/save.vue

+32-8
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@
2020

2121
<FormItem label="底部说明">
2222
<Input type="textarea" v-model="site.footer"></Input>
23-
</FormItem>
23+
</FormItem>
2424

2525
<FormItem>
2626
<Button type="primary" @click="post('site',site)">保存</Button>
27-
</FormItem>
28-
</Form>
27+
</FormItem>
28+
</Form>
2929
</TabPane>
30-
30+
3131
<TabPane label="邮箱设置">
3232
<Form :model="email" :label-width="80">
3333
<FormItem label="服务器">
@@ -51,12 +51,32 @@
5151

5252
<FormItem label="密码">
5353
<Input v-model="email.pass"></Input>
54-
</FormItem>
54+
</FormItem>
5555

5656
<FormItem>
5757
<Button type="primary" @click="post('email',email)">保存</Button>
58-
</FormItem>
59-
</Form>
58+
</FormItem>
59+
</Form>
60+
</TabPane>
61+
62+
<TabPane label="七牛配置">
63+
<Form :model="qiniu" :label-width="80">
64+
<FormItem label="access_key">
65+
<Input v-model="qiniu.access_key"></Input>
66+
</FormItem>
67+
68+
<FormItem label="secret_key">
69+
<Input v-model="qiniu.secret_key"></Input>
70+
</FormItem>
71+
72+
<FormItem label="bucket">
73+
<Input v-model="qiniu.bucket"></Input>
74+
</FormItem>
75+
76+
<FormItem>
77+
<Button type="primary" @click="post('qiniu',qiniu)">保存</Button>
78+
</FormItem>
79+
</Form>
6080
</TabPane>
6181
</Tabs>
6282
</template>
@@ -72,7 +92,8 @@ export default {
7292
site: {},
7393
email:{
7494
secure:1
75-
}
95+
},
96+
qiniu :{}
7697
};
7798
},
7899
methods: {
@@ -84,6 +105,9 @@ export default {
84105
if(res.data.email){
85106
this.email = res.data.email;
86107
}
108+
if(res.data.qiniu){
109+
this.qiniu = res.data.qiniu;
110+
}
87111
});
88112
},
89113
post(type,data) {

admin/src/view/content/save.vue

+5-5
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
time: [
8686
{ required: true, message: '发布时间必须选择' }
8787
],
88-
},
88+
},
8989
category:[],
9090
tag:[]
9191
}
@@ -138,7 +138,7 @@
138138
getTag(){
139139
tag.getList().then(res=>{
140140
this.tag=res.data;
141-
});
141+
});
142142
},
143143
get(id){
144144
content.getInfo(id).then(res=>{
@@ -153,13 +153,13 @@
153153
});
154154
},
155155
imgAdd(pos, $file){
156-
var formdata = new FormData();
157-
formdata.append('image', $file);
156+
let formdata = new FormData();
157+
formdata.append('image', $file);
158158
image.upload(formdata).then(res=>{
159159
if(res.errno==0&&res.data.url){
160160
this.$refs['md'].$img2Url(pos, res.data.url);
161161
}
162-
});
162+
});
163163
}
164164
},
165165
mounted(){

admin/src/view/user/password.vue

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<template>
2+
<Form :model="formItem" ref="formItem" :rules="ruleInline" :label-width="80">
3+
<FormItem label="旧密码" prop="password">
4+
<Input v-model="formItem.password"></Input>
5+
</FormItem>
6+
<FormItem label="新密码" prop="newPassword">
7+
<Input v-model="formItem.newPassword"></Input>
8+
</FormItem>
9+
<FormItem label="确认密码" prop="confirmPassword">
10+
<Input v-model="formItem.confirmPassword"></Input>
11+
</FormItem>
12+
<FormItem>
13+
<Button type="primary" @click="post">保存</Button>
14+
</FormItem>
15+
</Form>
16+
</template>
17+
<script>
18+
import { user } from "@/api";
19+
import { Form, Button, FormItem, Input } from 'iview';
20+
export default {
21+
components: {
22+
Button,
23+
Form,
24+
FormItem,
25+
Input
26+
},
27+
data() {
28+
const validatePassCheck = (rule, value, callback) => {
29+
if (value === '') {
30+
callback(new Error('确认密码不能为空'));
31+
} else if (value !== this.formItem.newPassword) {
32+
callback(new Error('两次密码不一致'));
33+
} else {
34+
callback();
35+
}
36+
};
37+
return {
38+
formItem: {
39+
password: "",
40+
newPassword: "",
41+
confirmPassword: ""
42+
},
43+
ruleInline: {
44+
password: [
45+
{ required: true, message: '密码不能为空', trigger: 'blur' }
46+
],
47+
newPassword: [
48+
{ required: true, message: '新密码不能为空', trigger: 'blur' }
49+
],
50+
confirmPassword: [
51+
{ required: true, validator: validatePassCheck, trigger: 'blur' }
52+
]
53+
}
54+
};
55+
},
56+
methods: {
57+
post() {
58+
this.$refs['formItem'].validate((valid) => {
59+
if (valid) {
60+
user.update(this.$store.state.admin.user.name, this.formItem).then(res => {
61+
62+
});
63+
} else {
64+
this.$Message.error('请填写必要信息');
65+
}
66+
})
67+
}
68+
},
69+
mounted() {
70+
this.get();
71+
}
72+
};
73+
</script>

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
},
1212
"dependencies": {
1313
"html-minifier": "^3.5.15",
14+
"qiniu": "^7.1.3",
1415
"think-cache": "^1.0.0",
1516
"think-cache-file": "^1.0.8",
1617
"think-email": "^1.1.0",

src/bootstrap/worker.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ think.beforeStartServer(async() => {
2020
hooks[node] = [];
2121
}
2222
if (typeof service[hook] === 'function') {
23-
hooks[node].push(service[hook]);
23+
hooks[node].push({
24+
service:serviceName,
25+
function:hook
26+
});
2427
}
2528
}
2629
}

src/config/adapter.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ exports.session = {
6464
tokenType: 'header', // ['query', 'body', 'header', 'cookie'], 'cookie' is default
6565
tokenName: 'authorization', // if tokenType not 'cookie', this will be token name, 'jwt' is default
6666
sign: {
67-
expiresIn: 60 * 60
67+
expiresIn: 60 * 60 * 12
6868
},
6969
verify: {
7070
// verify options is not required

src/controller/api/image.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
const BaseRest = require('../rest.js');
21
const fs = require('fs');
32
const path = require('path');
43
const rename = think.promisify(fs.rename, fs);
54

6-
module.exports = class extends BaseRest {
5+
module.exports = class extends think.Controller {
6+
77
// 图片上传
88
async postAction() {
99
const file = this.file('image');
@@ -21,6 +21,10 @@ module.exports = class extends BaseRest {
2121
const filepath = path.join(think.ROOT_PATH, 'www' + savepath);
2222
think.mkdir(path.dirname(filepath));
2323
rename(file.path, filepath);
24-
return this.success({ url: savepath }, '上传成功');
24+
25+
let data={ url: savepath, basename:basename, filepath:filepath};
26+
await this.hook('upload',data);
27+
delete data.filepath;
28+
return this.success(data, '上传成功');
2529
}
2630
};

src/controller/api/user.js

+21-6
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,36 @@ module.exports = class extends BaseRest {
1919
}
2020
}
2121

22-
// 更新用户信息
22+
/**
23+
* 更新用户信息
24+
* @return {[type]} [description]
25+
*/
2326
async putAction() {
24-
// 删除缓存
25-
think.cache('user', null);
2627

27-
const userInfo = this.userInfo;
28-
const data = this.post();
28+
const userInfo = await this.modelInstance.where({ username: this.id }).find();
29+
let data = this.post();
2930
if (think.isEmpty(data)) {
3031
return this.fail(1000, '数据不能为空');
3132
}
33+
console.log(data)
34+
if(!think.isEmpty(data.password)){
35+
if(data.newPassword!==data.confirmPassword){
36+
return this.fail(1000, '两次密码不一致');
37+
}
38+
39+
if(!this.modelInstance.verifyPassword(userInfo,data.password)){
40+
return this.fail(1000, '旧密码不正确');
41+
}
42+
43+
data.password=this.modelInstance.sign(userInfo,data.newPassword);
44+
}
3245
const rows = await this.modelInstance.where({ id: userInfo.id }).update(data);
3346
if (rows) {
47+
data.id = userInfo.id;
48+
await this.hook('userUpdate', data);
3449
return this.success({ affectedRows: rows }, '更新成功');
3550
} else {
3651
return this.fail(1000, '更新失败');
3752
}
3853
}
39-
};
54+
};

src/controller/base.js

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ module.exports = class extends think.Controller {
44
const config = await think.model('config').cache('config').getList();
55
this.assign('site', config.site);
66
// 获取用户资料
7-
// TODO:如果要改为多用户系统,此处需要修改
87
const user = await think.model('user').cache('user').find();
98
this.assign('user', user);
109

src/controller/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module.exports = class extends Base {
99
if (this.post('s')) {
1010
return this.redirect('/search/' + this.post('s') + '/');
1111
}
12-
await this.hook('commentCreate', {email: '[email protected]'});
12+
await this.hook('contentUpdate');
1313
return this.action('content', 'list');
1414
}
1515
};

src/extend/controller.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ module.exports = {
1111
try {
1212
if (hooks[node]) {
1313
for (const i in hooks[node]) {
14-
await hooks[node][i](data);
14+
let hook=hooks[node][i];
15+
let service=think.service(hook.service);
16+
await service[hook.function](data);
1517
}
1618
}
1719
} catch (e) {

0 commit comments

Comments
 (0)