Commit 9bd4c00ed681c3334d79814dba334bff51bccbbd

Authored by Aaron.Liu
1 parent 1f83f272
Exists in master

增加用户管理,合同管理,客户管理,数据字典接口

app/controller/contract.js 0 → 100644
... ... @@ -0,0 +1,75 @@
  1 +'use strict';
  2 +
  3 +const qs = require('qs');
  4 +const Controller = require('egg').Controller;
  5 +const uuid = require('node-uuid');
  6 +
  7 +class ContractController extends Controller {
  8 + async info() {
  9 + const ctx = this.ctx;
  10 + const id = ctx.params.id;
  11 + try {
  12 + const result = await ctx.service.contract.find(id);
  13 + this.ctx.body = {
  14 + result,
  15 + success: true,
  16 + };
  17 + } catch (error) {
  18 + this.ctx.body = {
  19 + success: false,
  20 + error,
  21 + };
  22 + }
  23 + }
  24 + async list() {
  25 + const param = qs.parse(this.ctx.query);
  26 + const ctx = this.ctx;
  27 + try {
  28 + const result = await ctx.service.contract.list(param);
  29 + const total = await ctx.service.contract.count();
  30 + this.ctx.body = {
  31 + result,
  32 + total: total[0].total,
  33 + success: true,
  34 + };
  35 + } catch (error) {
  36 + this.ctx.body = {
  37 + success: false,
  38 + error,
  39 + };
  40 + }
  41 + }
  42 + async add() {
  43 + const ctx = this.ctx;
  44 + const id = uuid.v4();
  45 + const data = ctx.request.body;
  46 + data.id = id;
  47 + try {
  48 + await ctx.service.contract.add(data);
  49 + this.ctx.body = { success: true };
  50 + } catch (error) {
  51 + this.ctx.body = { success: false, error };
  52 + }
  53 + }
  54 + async update() {
  55 + const ctx = this.ctx;
  56 + const data = ctx.request.body;
  57 + try {
  58 + await ctx.service.contract.update(data);
  59 + this.ctx.body = { success: true };
  60 + } catch (error) {
  61 + this.ctx.body = { success: false, error };
  62 + }
  63 + }
  64 + async remove() {
  65 + const ctx = this.ctx;
  66 + try {
  67 + await ctx.service.contract.remove(ctx.request.body);
  68 + this.ctx.body = { success: true };
  69 + } catch (error) {
  70 + this.ctx.body = { success: false, error };
  71 + }
  72 + }
  73 +}
  74 +
  75 +module.exports = ContractController;
... ...
app/controller/customer.js 0 → 100644
... ... @@ -0,0 +1,75 @@
  1 +'use strict';
  2 +
  3 +const qs = require('qs');
  4 +const Controller = require('egg').Controller;
  5 +const uuid = require('node-uuid');
  6 +
  7 +class CustomerController extends Controller {
  8 + async info() {
  9 + const ctx = this.ctx;
  10 + const id = ctx.params.id;
  11 + try {
  12 + const result = await ctx.service.customer.find(id);
  13 + this.ctx.body = {
  14 + result,
  15 + success: true,
  16 + };
  17 + } catch (error) {
  18 + this.ctx.body = {
  19 + success: false,
  20 + error,
  21 + };
  22 + }
  23 + }
  24 + async list() {
  25 + const param = qs.parse(this.ctx.query);
  26 + const ctx = this.ctx;
  27 + try {
  28 + const result = await ctx.service.customer.list(param);
  29 + const total = await ctx.service.customer.count();
  30 + this.ctx.body = {
  31 + result,
  32 + total: total[0].total,
  33 + success: true,
  34 + };
  35 + } catch (error) {
  36 + this.ctx.body = {
  37 + success: false,
  38 + error,
  39 + };
  40 + }
  41 + }
  42 + async add() {
  43 + const ctx = this.ctx;
  44 + const id = uuid.v4();
  45 + const data = ctx.request.body;
  46 + data.id = id;
  47 + try {
  48 + await ctx.service.customer.add(data);
  49 + this.ctx.body = { success: true };
  50 + } catch (error) {
  51 + this.ctx.body = { success: false, error };
  52 + }
  53 + }
  54 + async update() {
  55 + const ctx = this.ctx;
  56 + const data = ctx.request.body;
  57 + try {
  58 + await ctx.service.customer.update(data);
  59 + this.ctx.body = { success: true };
  60 + } catch (error) {
  61 + this.ctx.body = { success: false, error };
  62 + }
  63 + }
  64 + async remove() {
  65 + const ctx = this.ctx;
  66 + try {
  67 + await ctx.service.customer.remove(ctx.request.body);
  68 + this.ctx.body = { success: true };
  69 + } catch (error) {
  70 + this.ctx.body = { success: false, error };
  71 + }
  72 + }
  73 +}
  74 +
  75 +module.exports = CustomerController;
... ...
app/controller/dict.js 0 → 100644
... ... @@ -0,0 +1,75 @@
  1 +'use strict';
  2 +
  3 +const qs = require('qs');
  4 +const Controller = require('egg').Controller;
  5 +const uuid = require('node-uuid');
  6 +
  7 +class DictController extends Controller {
  8 + async info() {
  9 + const ctx = this.ctx;
  10 + const id = ctx.params.id;
  11 + try {
  12 + const result = await ctx.service.dict.find(id);
  13 + this.ctx.body = {
  14 + result,
  15 + success: true,
  16 + };
  17 + } catch (error) {
  18 + this.ctx.body = {
  19 + success: false,
  20 + error,
  21 + };
  22 + }
  23 + }
  24 + async list() {
  25 + const param = qs.parse(this.ctx.query);
  26 + const ctx = this.ctx;
  27 + try {
  28 + const result = await ctx.service.dict.list(param);
  29 + const total = await ctx.service.dict.count();
  30 + this.ctx.body = {
  31 + result,
  32 + total: total[0].total,
  33 + success: true,
  34 + };
  35 + } catch (error) {
  36 + this.ctx.body = {
  37 + success: false,
  38 + error,
  39 + };
  40 + }
  41 + }
  42 + async add() {
  43 + const ctx = this.ctx;
  44 + const id = uuid.v4();
  45 + const data = ctx.request.body;
  46 + data.id = id;
  47 + try {
  48 + await ctx.service.dict.add(data);
  49 + this.ctx.body = { success: true };
  50 + } catch (error) {
  51 + this.ctx.body = { success: false, error };
  52 + }
  53 + }
  54 + async update() {
  55 + const ctx = this.ctx;
  56 + const data = ctx.request.body;
  57 + try {
  58 + await ctx.service.dict.update(data);
  59 + this.ctx.body = { success: true };
  60 + } catch (error) {
  61 + this.ctx.body = { success: false, error };
  62 + }
  63 + }
  64 + async remove() {
  65 + const ctx = this.ctx;
  66 + try {
  67 + await ctx.service.dict.remove(ctx.request.body);
  68 + this.ctx.body = { success: true };
  69 + } catch (error) {
  70 + this.ctx.body = { success: false, error };
  71 + }
  72 + }
  73 +}
  74 +
  75 +module.exports = DictController;
... ...
app/controller/user.js
... ... @@ -7,9 +7,9 @@ const uuid = require('node-uuid');
7 7 class UserController extends Controller {
8 8 async info() {
9 9 const ctx = this.ctx;
10   - const userId = ctx.params.id;
  10 + const id = ctx.params.id;
11 11 try {
12   - const result = await ctx.service.user.find(userId);
  12 + const result = await ctx.service.user.find(id);
13 13 this.ctx.body = {
14 14 result,
15 15 success: true,
... ...
app/router.js
... ... @@ -6,9 +6,28 @@
6 6 module.exports = app => {
7 7 const { router, controller } = app;
8 8 router.get('/', controller.home.index);
  9 + // 用户管理
9 10 router.get('/user/list', controller.user.list);
10 11 router.get('/user/:id', controller.user.info);
11 12 router.post('/user', controller.user.add);
12 13 router.put('/user', controller.user.update);
13 14 router.post('/user/delete', controller.user.remove);
  15 + // 数据字典
  16 + router.get('/dict/list', controller.dict.list);
  17 + router.get('/dict/:id', controller.dict.info);
  18 + router.post('/dict', controller.dict.add);
  19 + router.put('/dict', controller.dict.update);
  20 + router.post('/dict/delete', controller.dict.remove);
  21 + // 客户管理
  22 + router.get('/customer/list', controller.customer.list);
  23 + router.get('/customer/:id', controller.customer.info);
  24 + router.post('/customer', controller.customer.add);
  25 + router.put('/customer', controller.customer.update);
  26 + router.post('/customer/delete', controller.customer.remove);
  27 + // 合同管理
  28 + router.get('/contract/list', controller.contract.list);
  29 + router.get('/contract/:id', controller.contract.info);
  30 + router.post('/contract', controller.contract.add);
  31 + router.put('/contract', controller.contract.update);
  32 + router.post('/contract/delete', controller.contract.remove);
14 33 };
... ...
app/service/contract.js 0 → 100644
... ... @@ -0,0 +1,54 @@
  1 +'use strict';
  2 +
  3 +const Service = require('egg').Service;
  4 +const objExclude = require('../util/common').objExclude;
  5 +const objToQuery = require('../util/common').objToQuery;
  6 +
  7 +class ContractService extends Service {
  8 + async count() {
  9 + const result = await this.app.mysql.query('select count(*) as total from contract');
  10 + return result;
  11 + }
  12 + async add(data) {
  13 + const result = await this.app.mysql.insert('contract', data);
  14 + return result;
  15 + }
  16 + async update(data) {
  17 + const result = await this.app.mysql.update('contract', data);
  18 + return result;
  19 + }
  20 + async find(id) {
  21 + // const user = await this.app.mysql.get('contract', { id });
  22 + const user = await this.app.mysql.query(`
  23 + select id, customer_id, value, product, invoice, date_format(invoice_time, '%Y-%m-%d') as invoice_time,
  24 + method, date_format(method_time, '%Y-%m-%d') as method_time, other_expenses, remark, type
  25 + from contract where id = ?
  26 + `, [ id ]);
  27 + const reulst = user && user.length > 0 ? user[0] : {};
  28 + return reulst;
  29 + }
  30 + async list(data) {
  31 + const { current, pageSize } = data;
  32 + const other = objExclude(data, [ 'current', 'pageSize' ]);
  33 + const limit = Number(pageSize);
  34 + const offset = Number((current - 1) * pageSize);
  35 + // const list = await this.app.mysql.select([ 'contract', 'customer' ], { columns: [ 'customer.name as cname', 'contract.type' ], where: other, limit, offset, orders: [[ 'type', 'asc' ]] });
  36 + const where = objToQuery(other, 'contract');
  37 + const sql = `
  38 + select contract.*, customer.name as customer_name
  39 + from contract left join customer on (contract.customer_id = customer.id)
  40 + where ${where}
  41 + order by contract.type asc
  42 + limit ${offset},${limit}
  43 + `;
  44 + const list = await this.app.mysql.query(sql);
  45 + console.log(sql);
  46 + return list;
  47 + }
  48 + async remove(data) {
  49 + const result = await this.app.mysql.delete('contract', data);
  50 + return result;
  51 + }
  52 +}
  53 +
  54 +module.exports = ContractService;
... ...
app/service/customer.js 0 → 100644
... ... @@ -0,0 +1,37 @@
  1 +'use strict';
  2 +
  3 +const Service = require('egg').Service;
  4 +const objExclude = require('../util/common').objExclude;
  5 +
  6 +class CustomerService extends Service {
  7 + async count() {
  8 + const result = await this.app.mysql.query('select count(*) as total from customer');
  9 + return result;
  10 + }
  11 + async add(data) {
  12 + const result = await this.app.mysql.insert('customer', data);
  13 + return result;
  14 + }
  15 + async update(data) {
  16 + const result = await this.app.mysql.update('customer', data);
  17 + return result;
  18 + }
  19 + async find(id) {
  20 + const user = await this.app.mysql.get('customer', { id });
  21 + return user;
  22 + }
  23 + async list(data) {
  24 + const { current, pageSize } = data;
  25 + const other = objExclude(data, [ 'current', 'pageSize' ]);
  26 + const limit = Number(pageSize);
  27 + const offset = Number((current - 1) * pageSize);
  28 + const list = await this.app.mysql.select('customer', { where: other, limit, offset, orders: [[ 'type', 'asc' ]] });
  29 + return list;
  30 + }
  31 + async remove(data) {
  32 + const result = await this.app.mysql.delete('customer', data);
  33 + return result;
  34 + }
  35 +}
  36 +
  37 +module.exports = CustomerService;
... ...
app/service/dict.js 0 → 100644
... ... @@ -0,0 +1,37 @@
  1 +'use strict';
  2 +
  3 +const Service = require('egg').Service;
  4 +const objExclude = require('../util/common').objExclude;
  5 +
  6 +class DictService extends Service {
  7 + async count() {
  8 + const result = await this.app.mysql.query('select count(*) as total from dict');
  9 + return result;
  10 + }
  11 + async add(data) {
  12 + const result = await this.app.mysql.insert('dict', data);
  13 + return result;
  14 + }
  15 + async update(data) {
  16 + const result = await this.app.mysql.update('dict', data);
  17 + return result;
  18 + }
  19 + async find(id) {
  20 + const user = await this.app.mysql.get('dict', { id });
  21 + return user;
  22 + }
  23 + async list(data) {
  24 + const { current, pageSize } = data;
  25 + const other = objExclude(data, [ 'current', 'pageSize' ]);
  26 + const limit = Number(pageSize);
  27 + const offset = Number((current - 1) * pageSize);
  28 + const list = await this.app.mysql.select('dict', { where: other, limit, offset, orders: [[ 'type', 'asc' ], [ 'code', 'asc' ]] });
  29 + return list;
  30 + }
  31 + async remove(data) {
  32 + const result = await this.app.mysql.delete('dict', data);
  33 + return result;
  34 + }
  35 +}
  36 +
  37 +module.exports = DictService;
... ...
app/service/user.js
... ... @@ -25,7 +25,7 @@ class UserService extends Service {
25 25 const other = objExclude(data, [ 'current', 'pageSize' ]);
26 26 const limit = Number(pageSize);
27 27 const offset = Number((current - 1) * pageSize);
28   - const list = await this.app.mysql.select('user', { columns: [ 'id', 'name', 'username', 'role' ], where: other, limit, offset });
  28 + const list = await this.app.mysql.select('user', { columns: [ 'id', 'name', 'username', 'role' ], where: other, limit, offset, orders: [[ 'role', 'asc' ]] });
29 29 return list;
30 30 }
31 31 async remove(data) {
... ...
app/util/common.js
... ... @@ -10,4 +10,11 @@ module.exports = {
10 10 });
11 11 return result;
12 12 },
  13 + objToQuery(obj, prefix) {
  14 + const arr = [];
  15 + Object.keys(obj).forEach(key => {
  16 + arr.push(`${!prefix ? '' : `${prefix}.`}${key} = ${obj[key]}`);
  17 + });
  18 + return arr.join('and');
  19 + },
13 20 };
... ...