Skip to content

Commit 6faf6d4

Browse files
committed
saucxs
1 parent b6ea1e3 commit 6faf6d4

6 files changed

Lines changed: 363 additions & 0 deletions

File tree

src/personal/config/config.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
/**
3+
* config
4+
*/
5+
export default {
6+
//key: value
7+
};

src/personal/controller/base.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
'use strict';
2+
3+
export default class extends think.controller.base {
4+
/**
5+
* some base method in here
6+
*/
7+
async __before() {
8+
9+
// assign后台设置
10+
let _web = await this.getConfig();
11+
this.assign('_web', _web);
12+
13+
//设置CSRF值
14+
let csrf = await this.session("__CSRF__");
15+
this.assign("csrf", csrf);
16+
17+
// 是否登录
18+
let uinfo = await this.session('uInfo');
19+
let islogin = (!think.isEmpty(uinfo)) ? 1 : 0;
20+
this.assign("islogin", islogin);
21+
if (!think.isEmpty(uinfo)) {
22+
let logininfo = await this.model('personal').findAll('user', { name: uinfo.name });
23+
//自己的个人中心基本信息
24+
this.assign("logininfo", logininfo[0]);
25+
}
26+
//是否登录
27+
28+
//获取导航链接
29+
let navList = await this.model('personal').getOrderList('menu', { appear: 1 });
30+
this.assign("navList", navList);
31+
32+
// 设置主题地址
33+
this.THEME_VIEW_PATH = `${think.THEME_PATH}${think.sep}${_web.theme}${think.sep}${think.Modules_Src}${think.sep}${this.http.module}${think.sep}`;
34+
this.assign("theme_url", 'static/theme/' + _web.theme + '/res');
35+
36+
}
37+
async getConfig() {
38+
let sysdata = await this.model('personal').findOne('system', { id: 1 });
39+
this.assign('_web', sysdata);
40+
return sysdata;
41+
}
42+
43+
// 渲染主题view层
44+
async displayView(name) {
45+
return this.display(this.THEME_VIEW_PATH + name + '.html');
46+
}
47+
}

src/personal/controller/index.js

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
'use strict';
2+
3+
import Base from './base.js';
4+
export default class extends Base {
5+
/**
6+
* index action
7+
* @return {Promise} []
8+
*/
9+
async indexAction() {
10+
this.assign("title", "个人中心");
11+
let uinfo = await this.session('uInfo');
12+
let uname = await this.get('uname');
13+
let isself, isselftag;
14+
if (!think.isEmpty(uname)) {
15+
// 是否登录
16+
let islogin = (!think.isEmpty(uinfo)) ? 1 : 0;
17+
//是否本人
18+
if (!think.isEmpty(uinfo)) {
19+
//登录
20+
if (uinfo.name === uname) {
21+
isselftag = "我的";
22+
isself = 1;
23+
} else {
24+
isselftag = "Ta的";
25+
isself = 0;
26+
}
27+
} else {
28+
// 未登录
29+
isselftag = "Ta的";
30+
isself = 0;
31+
}
32+
this.assign("islogin", islogin);
33+
this.assign("isself", isself);
34+
this.assign("isselftag", isselftag);
35+
// 排行
36+
let pointList = await think.cache("pointList", () => {
37+
return this.model("personal").getPointList();
38+
});
39+
this.assign("pointList", pointList);
40+
41+
//我的话题
42+
let topicList = await this.model('personal').getMytopic({ author: uname });
43+
this.assign("topicList", topicList);
44+
45+
//我的回复
46+
let replyList = await this.model('personal').findAll('topic_comment', { author: uname });
47+
this.assign("replyList", replyList);
48+
49+
//我的收藏
50+
let collectList = await this.model('personal').getMycollect({ author: uname });
51+
this.assign("collectList", collectList);
52+
53+
let userinfo = await this.model('personal').findAll('user', { name: uname });
54+
if (!think.isEmpty(userinfo)) {
55+
// 个人排名
56+
let ranking = await think.cache("ranking_" + userinfo[0].id);
57+
if (ranking === undefined) {
58+
let allPoints = await this.model("personal").getAllPoint();
59+
for (var i = 0; i < allPoints.length; i++) {
60+
if (allPoints[i].id == userinfo[0].id) {
61+
ranking = i+1;
62+
think.cache("ranking_" + userinfo[0].id, ranking);
63+
break;
64+
}
65+
}
66+
}
67+
68+
this.assign("ranking", ranking);
69+
70+
//他人的个人中心基本信息
71+
this.assign("userinfo", userinfo[0]);
72+
return this.displayView("index_index");
73+
74+
} else {
75+
return this.displayView("../common/error_404");
76+
}
77+
} else {
78+
return this.displayView("../common/error_404");
79+
}
80+
81+
}
82+
async settingAction() {
83+
return this.displayView("index_setting");
84+
}
85+
86+
//收藏/取消收藏接口
87+
async savecollectAction() {
88+
let data = await this.post();
89+
let mycreatetime = think.datetime(this.post("createtime"));
90+
if (!think.isEmpty(this.post("cid"))) {
91+
//已收藏,这里取消收藏。
92+
let rs = await this.model("personal").deleteRecord("user_collect", { id: data.cid });
93+
let points = await this.model("personal").decrepoint({ name: data.author }, this.config('point.addcollect'));
94+
if (rs) return this.success({ status: 0, msg: "取消收藏成功!", cid: data.cid });
95+
} else {
96+
//未收藏,这里收藏
97+
data.createtime = mycreatetime;
98+
let rs = await this.model("personal").addRecord("user_collect", data);
99+
let points = await this.model("personal").increpoint({ name: data.author }, this.config('point.addcollect'));
100+
if (rs) return this.success({ status: 1, msg: "收藏成功!", cid: rs });
101+
}
102+
}
103+
}

src/personal/controller/setting.js

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
'use strict';
2+
3+
import Base from './base.js';
4+
export default class extends Base {
5+
/**
6+
* index action
7+
* @return {Promise} []
8+
*/
9+
//页面初始化数据
10+
async initPage(page){
11+
let uinfo=await this.session('uInfo');
12+
if(!think.isEmpty(uinfo)){
13+
if(uinfo.name){
14+
let userinfo=await this.model('personal').findUser({name:uinfo.name});
15+
this.assign("userinfo",userinfo[0]);
16+
return this.displayView("setting_"+page);
17+
}else{
18+
return this.displayView("../common/error_404");
19+
}
20+
}else {
21+
return this.redirect('/login.html');
22+
}
23+
}
24+
//检查是否存在
25+
async checkIsExist(where){
26+
let rs= await this.model('personal').findUser(where);
27+
let s=(rs.length>0)?0:1;
28+
return s;
29+
}
30+
// 基础信息设置
31+
async indexAction(){
32+
this.assign("title","基本信息设置");
33+
this.initPage("index");
34+
}
35+
// 保存基础信息
36+
async savebaseAction(){
37+
let uinfo=await this.session('uInfo');
38+
let newData=this.post();
39+
let name=newData.name;
40+
let nickname=newData.nickname;
41+
let email=newData.email;
42+
let sign=newData.sign;
43+
let userinfo=await this.model("personal").findOne('user',{name:uinfo.name});
44+
console.log(userinfo);
45+
46+
if(name!==''&&nickname!==''&&email!==''&&sign!==''){
47+
if(userinfo.email===email&&userinfo.nickname===nickname&&userinfo.sign===sign){
48+
//邮箱和昵称都未改变
49+
return this.json({status:0,errno:1,errmsg:'请填写要提交的修改信息!'});
50+
}else{
51+
let userFlag=await this.checkIsExist({name:name});
52+
let emailFlag=await this.checkIsExist({email:email});
53+
if(userFlag===0&&uinfo.name!==name){
54+
return this.json({status:0,errno:1,errmsg:'该用户名已存在!'});
55+
}else if(emailFlag===0&&userinfo.email!==email){
56+
return this.json({status:0,errno:1,errmsg:'该邮箱已存在!'});
57+
}else {
58+
let rs=await this.model('personal').saveUserInfo(newData,{name:newData.name});
59+
if(rs) return this.success();
60+
}
61+
}
62+
}
63+
}
64+
// 重置密码
65+
async resetpwdAction(){
66+
let uinfo=await this.session('uInfo');
67+
let newData=this.post();
68+
newData.password=think.md5(newData.password);
69+
newData.way='site';
70+
let oldpassword=think.md5(newData.oldpassword);
71+
let password=newData.password;
72+
73+
if(oldpassword!==''){
74+
//验证原始密码是否正确
75+
let userinfo=await this.model('personal').findUser({name:uinfo.name});
76+
if(oldpassword!==userinfo[0].password){
77+
return this.json({status:0,errno:1,errmsg:'原始密码不正确!'});
78+
}
79+
if(password!==''){
80+
let rs=await this.model('personal').saveUserInfo(newData,{name:uinfo.name});
81+
if(rs) return this.success();
82+
}
83+
}
84+
}
85+
// 重置头像
86+
async resetpicAction(){
87+
let uinfo=await this.session('uInfo');
88+
let newData=this.post();
89+
let pic=newData.pic;
90+
91+
if(pic!==''){
92+
let rs=await this.model('personal').saveUserInfo(newData,{name:uinfo.name});
93+
if(rs) return this.success();
94+
}
95+
}
96+
//校验邮箱是否存在
97+
async checkemailAction(){
98+
let email=await this.post('email');
99+
let rs= await this.model('personal').findAll('user',{'email':email});
100+
if(rs.length>0){
101+
return this.json({status:0,errno:1,errmsg:'该邮箱已存在!'});
102+
}else{
103+
return this.json({status:1,errno:0,errmsg:'该邮箱可用!'});
104+
}
105+
}
106+
async passwordAction(){
107+
this.assign("title","修改密码");
108+
this.initPage("password");
109+
}
110+
async picAction(){
111+
this.assign("title","头像修改");
112+
this.initPage("pic");
113+
}
114+
}

src/personal/logic/index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
/**
3+
* logic
4+
* @param {} []
5+
* @return {} []
6+
*/
7+
export default class extends think.logic.base {
8+
/**
9+
* index action logic
10+
* @return {} []
11+
*/
12+
indexAction(){
13+
14+
}
15+
16+
}

src/personal/model/personal.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
'use strict';
2+
/**
3+
* model
4+
*/
5+
export default class extends think.model.base {
6+
7+
//保存用户信息
8+
saveUserInfo(data, where) {
9+
return this.model('user').where(where).update(data);
10+
}
11+
12+
//查找用户
13+
findUser(where) {
14+
return this.model('user').where(where).select();
15+
}
16+
17+
//返回全部列表
18+
findAll(db, where) {
19+
return this.model(db).where(where).select();
20+
}
21+
22+
//返回特定记录
23+
findOne(db, where) {
24+
return this.model(db).where(where).find();
25+
}
26+
27+
//更新数据
28+
updateRecord(db, where, data) {
29+
return this.model(db).where(where).update(data);
30+
}
31+
32+
//添加数据
33+
addRecord(db, data) {
34+
return this.model(db).add(data);
35+
}
36+
37+
//添加数据
38+
deleteRecord(db, where) {
39+
return this.model(db).where(where).delete();
40+
}
41+
42+
decrepoint(where, count) {
43+
return this.model("user").where(where).decrement('point', count);
44+
}
45+
46+
increpoint(where, count) {
47+
return this.model("user").where(where).increment('point', count);
48+
}
49+
50+
getMytopic(where) {
51+
return this.model('topic').field("*,li_topic.id as tid,li_topic.createtime as mytime").join({
52+
user: {on: "author,name"}
53+
}).where(where).select();
54+
}
55+
56+
getMycollect(where) {
57+
return this.model('user_collect').field("*,li_user_collect.createtime as mytime").join({
58+
user: {on: "author,name"}
59+
}).where(where).select();
60+
}
61+
62+
// 积分排行
63+
getPointList() {
64+
return this.model('user').field("id,name,point").order("point desc").limit(10).select();
65+
}
66+
67+
// 所有积分排行
68+
getAllPoint() {
69+
return this.model('user').field("id,point").order("point desc").select();
70+
}
71+
72+
//获取排序后的列表
73+
getOrderList(db, where) {
74+
return this.model(db).where(where).order("orders").select();
75+
}
76+
}

0 commit comments

Comments
 (0)