users.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. 'use strict';
  2. const ManagerController = require('../manager.js');
  3. /**
  4. * [exports 用户控制器]
  5. * @type {[type]}
  6. */
  7. module.exports = class UsersController extends ManagerController {
  8. /**
  9. * [useModel 使用模型]
  10. * @return {[type]} [description]
  11. */
  12. get useModel() {
  13. const that = this;
  14. return that.app.model.Users;
  15. }
  16. /**
  17. * [selectValidate 查询用户列表]
  18. * @return {[type]} [description]
  19. */
  20. get selectValidate() {
  21. const that = this;
  22. return {
  23. page: that.ctx.rules.default(1)
  24. .number(),
  25. limit: that.ctx.rules.default(20)
  26. .number(),
  27. uname: that.ctx.rules.default('')
  28. .required(),
  29. };
  30. }
  31. /**
  32. * [updateValidate 用户充值验证器]
  33. * @return {[type]} [description]
  34. */
  35. get updateValidate() {
  36. const that = this;
  37. return {
  38. user_id: that.ctx.rules.name('用户ID')
  39. .required()
  40. .notEmpty()
  41. .number(),
  42. money: that.ctx.rules.name('充值金额')
  43. .required()
  44. .notEmpty()
  45. .number(),
  46. admin_id: that.ctx.rules.default(that.service.manager.ActionAdminUserId())
  47. .number(),
  48. };
  49. }
  50. /**
  51. * [moneyLogsValidate description]
  52. * @return {[type]} [description]
  53. */
  54. get moneyLogsValidate() {
  55. const that = this;
  56. return {
  57. user_id: that.ctx.rules.name('用户ID')
  58. .required()
  59. .notEmpty()
  60. .number(),
  61. page: that.ctx.rules.default(1)
  62. .number(),
  63. limit: that.ctx.rules.default(20)
  64. .number(),
  65. };
  66. }
  67. /**
  68. * [selectOptions description]
  69. * @param {Object} data [description]
  70. * @return {[type]} [description]
  71. */
  72. async selectOptions(data = {}) {
  73. const that = this;
  74. const seq = that.app.Sequelize;
  75. const options = {
  76. offset: (data.page - 1) * data.limit, limit: data.limit, where: {},
  77. attributes: { exclude: [ 'password' ] }, order: [[ 'user_id', 'desc' ]],
  78. };
  79. if (data.uname) {
  80. options.where = {
  81. [seq.Op.or]: [
  82. { nickname: { [seq.Op.regexp]: data.uname } },
  83. { account_name: { [seq.Op.regexp]: data.uname } },
  84. ],
  85. };
  86. }
  87. return options;
  88. }
  89. /**
  90. * [create 给用户充值]
  91. * @return {[type]} [description]
  92. */
  93. async update() {
  94. const that = this;
  95. let transaction;
  96. try {
  97. const data = await that.ctx.validate(that.updateValidate, await that.ctx.anyParse());
  98. // console.log(data);// { money: 168, user_id: 26, admin_id: 19 }
  99. transaction = await that.app.model.transaction();
  100. const result = await that.service.shop.userMoneyAdd(data.user_id, data.money, transaction, '管理员给用户充值', data.admin_id);
  101. await transaction.commit();
  102. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
  103. } catch (err) {
  104. if (transaction) transaction.rollback();
  105. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  106. }
  107. }
  108. /**
  109. * [moneyLogs 获取用户资金明细记录]
  110. * @return {[type]} [description]
  111. */
  112. async moneyLogs() {
  113. const that = this;
  114. try {
  115. const data = await that.ctx.validate(that.moneyLogsValidate, await that.ctx.getParse());
  116. const seq = that.app.Sequelize;
  117. const options = {
  118. offset: (data.page - 1) * data.limit, limit: data.limit, where: {
  119. user_id: data.user_id,
  120. }, include: [
  121. { model: that.app.model.AdminUser, as: 'admin_user', attributes: [], required: false },
  122. ], attributes: {
  123. include: [[ seq.col('admin_user.username'), 'username' ]],
  124. },
  125. };
  126. const selectBean = await that.app.comoBean.instance(data, options);
  127. const result = await that.service.base.select(selectBean, that.app.model.UsersMoneyLogs, '查询资金明细失败,请重试', true, true);
  128. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
  129. } catch (err) {
  130. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  131. }
  132. }
  133. };