Skip to content

Commit

Permalink
feat 添加后台接口
Browse files Browse the repository at this point in the history
  • Loading branch information
tumobi committed Sep 15, 2017
1 parent 9b83144 commit 51b3cec
Show file tree
Hide file tree
Showing 39 changed files with 771 additions and 18 deletions.
71 changes: 53 additions & 18 deletions nideshop.sql

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
"lint-fix": "eslint --fix src/"
},
"dependencies": {
"gm": "^1.23.0",
"jsonwebtoken": "^8.0.0",
"kcors": "^2.2.1",
"lodash": "^4.17.4",
"moment": "^2.18.1",
"request": "^2.81.0",
Expand Down
4 changes: 4 additions & 0 deletions src/admin/config/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// default config
module.exports = {

};
41 changes: 41 additions & 0 deletions src/admin/controller/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const Base = require('./base.js');

module.exports = class extends Base {
async loginAction() {
const username = this.post('username');
const password = this.post('password');

const admin = await this.model('admin').where({ username: username }).find();
if (think.isEmpty(admin)) {
return this.fail(401, '用户名或密码不正确1');
}

if (think.md5(password + '' + admin.password_salt) !== admin.password) {
return this.fail(400, '用户名或密码不正确2');
}

// 更新登录信息
await this.model('admin').where({ id: admin.id }).update({
last_login_time: parseInt(Date.now() / 1000),
last_login_ip: this.ctx.ip
});

const TokenSerivce = this.service('token', 'admin');
const sessionKey = await TokenSerivce.create({
user_id: admin.id
});

if (think.isEmpty(sessionKey)) {
return this.fail('登录失败');
}

const userInfo = {
id: admin.id,
username: admin.username,
avatar: admin.avatar,
admin_role_id: admin.admin_role_id
};

return this.success({ token: sessionKey, userInfo: userInfo });
}
};
15 changes: 15 additions & 0 deletions src/admin/controller/base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports = class extends think.Controller {
async __before() {
// 根据token值获取用户id
think.token = this.ctx.header['x-nideshop-token'] || '';
const tokenSerivce = think.service('token', 'admin');
think.userId = await tokenSerivce.getUserId();

// 只允许登录操作
if (this.ctx.controller !== 'auth') {
if (think.userId <= 0) {
return this.fail(401, '请先登录');
}
}
}
};
54 changes: 54 additions & 0 deletions src/admin/controller/brand.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const Base = require('./base.js');

module.exports = class extends Base {
/**
* index action
* @return {Promise} []
*/
async indexAction() {
const page = this.get('page') || 1;
const size = this.get('size') || 10;
const name = this.get('name') || '';

const model = this.model('brand');
const data = await model.field(['id', 'name', 'floor_price', 'app_list_pic_url', 'is_new', 'sort_order', 'is_show']).where({name: ['like', `%${name}%`]}).order(['id DESC']).page(page, size).countSelect();

return this.success(data);
}

async infoAction() {
const id = this.get('id');
const model = this.model('brand');
const data = await model.where({id: id}).find();

return this.success(data);
}

async storeAction() {
if (!this.isPost) {
return false;
}

const values = this.post();
const id = this.post('id');

const model = this.model('brand');
values.is_show = values.is_show ? 1 : 0;
values.is_new = values.is_new ? 1 : 0;
if (id > 0) {
await model.where({id: id}).update(values);
} else {
delete values.id;
await model.add(values);
}
return this.success(values);
}

async destoryAction() {
const id = this.post('id');
await this.model('brand').where({id: id}).limit(1).delete();
// TODO 删除图片

return this.success();
}
};
70 changes: 70 additions & 0 deletions src/admin/controller/category.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
const Base = require('./base.js');
const _ = require('lodash');

module.exports = class extends Base {
/**
* index action
* @return {Promise} []
*/
async indexAction() {
const model = this.model('category');
const data = await model.where({is_show: 1}).order(['sort_order ASC']).select();
const topCategory = data.filter((item) => {
return item.parent_id === 0;
});
const categoryList = [];
topCategory.map((item) => {
item.level = 1;
categoryList.push(item);
data.map((child) => {
if (child.parent_id === item.id) {
child.level = 2;
categoryList.push(child);
}
});
});
return this.success(categoryList);
}

async topCategoryAction() {
const model = this.model('category');
const data = await model.where({parent_id: 0}).order(['id ASC']).select();

return this.success(data);
}

async infoAction() {
const id = this.get('id');
const model = this.model('category');
const data = await model.where({id: id}).find();

return this.success(data);
}

async storeAction() {
if (!this.isPost) {
return false;
}

const values = this.post();
const id = this.post('id');

const model = this.model('category');
values.is_show = values.is_show ? 1 : 0;
if (id > 0) {
await model.where({id: id}).update(values);
} else {
delete values.id;
await model.add(values);
}
return this.success(values);
}

async destoryAction() {
const id = this.post('id');
await this.model('category').where({id: id}).limit(1).delete();
// TODO 删除图片

return this.success();
}
};
55 changes: 55 additions & 0 deletions src/admin/controller/goods.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const Base = require('./base.js');

module.exports = class extends Base {
/**
* index action
* @return {Promise} []
*/
async indexAction() {
const page = this.get('page') || 1;
const size = this.get('size') || 10;
const name = this.get('name') || '';

const model = this.model('goods');
const data = await model.where({name: ['like', `%${name}%`]}).order(['id DESC']).page(page, size).countSelect();

return this.success(data);
}

async infoAction() {
const id = this.get('id');
const model = this.model('goods');
const data = await model.where({id: id}).find();

return this.success(data);
}

async storeAction() {
if (!this.isPost) {
return false;
}

const values = this.post();
const id = this.post('id');

const model = this.model('goods');
values.is_on_sale = values.is_on_sale ? 1 : 0;
values.is_new = values.is_new ? 1 : 0;
values.is_hot = values.is_hot ? 1 : 0;
if (id > 0) {
await model.where({id: id}).update(values);
} else {
delete values.id;
await model.add(values);
}
return this.success(values);
}

async destoryAction() {
const id = this.post('id');
await this.model('goods').where({id: id}).limit(1).delete();
// TODO 删除图片

return this.success();
}
};
7 changes: 7 additions & 0 deletions src/admin/controller/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const Base = require('./base.js');

module.exports = class extends Base {
indexAction() {
return this.display();
}
};
64 changes: 64 additions & 0 deletions src/admin/controller/order.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
const Base = require('./base.js');

module.exports = class extends Base {
/**
* index action
* @return {Promise} []
*/
async indexAction() {
const page = this.get('page') || 1;
const size = this.get('size') || 10;
const orderSn = this.get('orderSn') || '';
const consignee = this.get('consignee') || '';

const model = this.model('order');
const data = await model.where({order_sn: ['like', `%${orderSn}%`], consignee: ['like', `%${consignee}%`]}).order(['id DESC']).page(page, size).countSelect();
const newList = [];
for (const item of data.data) {
item.order_status_text = await this.model('order').getOrderStatusText(item.id);
newList.push(item);
}
data.data = newList;
return this.success(data);
}

async infoAction() {
const id = this.get('id');
const model = this.model('order');
const data = await model.where({id: id}).find();

return this.success(data);
}

async storeAction() {
if (!this.isPost) {
return false;
}

const values = this.post();
const id = this.post('id');

const model = this.model('order');
values.is_show = values.is_show ? 1 : 0;
values.is_new = values.is_new ? 1 : 0;
if (id > 0) {
await model.where({id: id}).update(values);
} else {
delete values.id;
await model.add(values);
}
return this.success(values);
}

async destoryAction() {
const id = this.post('id');
await this.model('order').where({id: id}).limit(1).delete();

// 删除订单商品
await this.model('order_goods').where({order_id: id}).delete();

// TODO 事务,验证订单是否可删除(只有失效的订单才可以删除)

return this.success();
}
};
54 changes: 54 additions & 0 deletions src/admin/controller/topic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const Base = require('./base.js');

module.exports = class extends Base {
/**
* index action
* @return {Promise} []
*/
async indexAction() {
const page = this.get('page') || 1;
const size = this.get('size') || 10;
const name = this.get('name') || '';

const model = this.model('topic');
const data = await model.where({title: ['like', `%${name}%`]}).order(['id DESC']).page(page, size).countSelect();

return this.success(data);
}

async infoAction() {
const id = this.get('id');
const model = this.model('topic');
const data = await model.where({id: id}).find();

return this.success(data);
}

async storeAction() {
if (!this.isPost) {
return false;
}

const values = this.post();
const id = this.post('id');

const model = this.model('topic');
values.is_show = values.is_show ? 1 : 0;
values.is_new = values.is_new ? 1 : 0;
if (id > 0) {
await model.where({id: id}).update(values);
} else {
delete values.id;
await model.add(values);
}
return this.success(values);
}

async destoryAction() {
const id = this.post('id');
await this.model('topic').where({id: id}).limit(1).delete();
// TODO 删除图片

return this.success();
}
};
Loading

0 comments on commit 51b3cec

Please sign in to comment.