UserBalance.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace app\data\controller;
  3. use app\data\service\UserService;
  4. use think\admin\Controller;
  5. use think\admin\extend\CodeExtend;
  6. use think\admin\service\AdminService;
  7. /**
  8. * 余额充值管理
  9. * Class UserBalance
  10. * @package app\data\controller
  11. */
  12. class UserBalance extends Controller
  13. {
  14. /**
  15. * 绑定数据表
  16. * @var string
  17. */
  18. private $table = 'DataUserBalance';
  19. /**
  20. * 余额充值管理
  21. * @auth true
  22. * @menu true
  23. * @throws \think\db\exception\DataNotFoundException
  24. * @throws \think\db\exception\DbException
  25. * @throws \think\db\exception\ModelNotFoundException
  26. */
  27. public function index()
  28. {
  29. $this->title = '余额充值管理';
  30. $query = $this->_query($this->table);
  31. // 用户搜索查询
  32. $db = $this->_query('DataUser')->like('phone#user_phone,nickname#user_nickname')->db();
  33. if ($db->getOptions('where')) $query->whereRaw("uid in {$db->field('id')->buildSql()}");
  34. // 数据查询分页
  35. $query->where(['deleted' => 0])->like('code,name')->dateBetween('create_at')->order('id desc')->page();
  36. }
  37. /**
  38. * 数据列表处理
  39. * @param array $data
  40. */
  41. protected function _index_page_filter(array &$data)
  42. {
  43. UserService::instance()->buildByUid($data);
  44. $uids = array_unique(array_column($data, 'create_by'));
  45. $users = $this->app->db->name('SystemUser')->whereIn('id', $uids)->column('username', 'id');
  46. foreach ($data as &$vo) $vo['create_byname'] = $users[$vo['create_by']] ?? $vo['create_by'];
  47. }
  48. /**
  49. * 添加余额充值
  50. * @auth true
  51. * @throws \think\db\exception\DataNotFoundException
  52. * @throws \think\db\exception\DbException
  53. * @throws \think\db\exception\ModelNotFoundException
  54. */
  55. public function add()
  56. {
  57. $data = $this->_vali(['uid.require' => '用户UID不能为空!']);
  58. $this->user = $this->app->db->name('DataUser')->where(['id' => $data['uid']])->find();
  59. if (empty($this->user)) $this->error('待充值的用户不存在!');
  60. $this->_form($this->table, 'form');
  61. }
  62. /**
  63. * 表单数据处理
  64. * @param array $data
  65. */
  66. protected function _form_filter(array &$data)
  67. {
  68. if (empty($data['code'])) {
  69. $data['code'] = CodeExtend::uniqidDate('16', 'B');
  70. }
  71. if ($this->request->isPost()) {
  72. if ($data['amount'] <= 0) {
  73. $this->error('充值金额不能少于零');
  74. }
  75. $data['create_by'] = AdminService::instance()->getUserId();
  76. }
  77. }
  78. /**
  79. * 表单结果处理
  80. * @param bool $state
  81. * @param array $data
  82. * @throws \think\db\exception\DbException
  83. */
  84. protected function _form_result(bool $state, array $data)
  85. {
  86. if ($state && isset($data['uid'])) {
  87. UserService::instance()->balance($data['uid']);
  88. }
  89. }
  90. /**
  91. * 删除充值记录
  92. * @auth true
  93. * @throws \think\db\exception\DbException
  94. */
  95. public function remove()
  96. {
  97. $this->_delete($this->table);
  98. }
  99. /**
  100. * 删除结果处理
  101. * @param bool $state
  102. * @throws \think\db\exception\DbException
  103. */
  104. protected function _delete_result(bool $state)
  105. {
  106. if ($state) {
  107. $ids = str2arr(input('id', ''));
  108. $query = $this->app->db->name($this->table);
  109. foreach ($query->whereIn('id', $ids)->cursor() as $vo) {
  110. UserService::instance()->balance($vo['uid']);
  111. }
  112. }
  113. }
  114. }