User.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. namespace app\admin\controller\user;
  3. use app\common\controller\Backend;
  4. use app\common\model\LedgerWalletModel;
  5. use app\common\model\LedgerTokenChangeModel;
  6. use app\common\model\LedgerFrozenChangeModel;
  7. use Exception;
  8. use fast\GoogleAuthenticator;
  9. use fast\Asset;
  10. use app\common\model\ProductOrder;
  11. use app\common\model\LedgerTeacChangeModel;
  12. use think\Db;
  13. use think\exception\DbException;
  14. /**
  15. * 用户管理
  16. *
  17. * @icon fa fa-user
  18. */
  19. class User extends Backend
  20. {
  21. /**
  22. * User模型对象
  23. * @var \app\admin\model\User
  24. */
  25. protected $model = null;
  26. protected $multiFields = ['is_login','is_withdraw'];
  27. public function _initialize()
  28. {
  29. parent::_initialize();
  30. $this->model = new \app\admin\model\User;
  31. $this->view->assign("statusList", $this->model->getStatusList());
  32. }
  33. /**
  34. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  35. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  36. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  37. */
  38. /**
  39. * 查看
  40. *
  41. * @return string|Json
  42. * @throws \think\Exception
  43. * @throws DbException
  44. */
  45. public function index()
  46. {
  47. //设置过滤方法
  48. $this->request->filter(['strip_tags', 'trim']);
  49. if (false === $this->request->isAjax()) {
  50. return $this->view->fetch();
  51. }
  52. //如果发送的来源是 Selectpage,则转发到 Selectpage
  53. if ($this->request->request('keyField')) {
  54. return $this->selectpage();
  55. }
  56. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  57. $list = $this->model->with('ledgerWallet')->with('productorder')->where($where)->order($sort, $order)->paginate($limit);
  58. foreach ($list as &$item) {
  59. $item['hold_num'] = $item->productorder? count($item->productorder): 0;
  60. //::where('user_id', $item->id)->where('status', 'in',[$productOrder::Paid, $productOrder::Transferred, $productOrder::Freeze])->count();//持有数量
  61. }
  62. $result = ['total' => $list->total(), 'rows' => $list->items()];
  63. return json($result);
  64. }
  65. /**
  66. * 茶宝调整
  67. * @param $ids
  68. * @return string
  69. * @throws DbException
  70. * @throws \think\Exception
  71. */
  72. public function approve($ids = null)
  73. {
  74. $ids = intval($ids);
  75. $row = $this->model->get($ids);
  76. if (!$row) {
  77. $this->error(__('No Results were found'));
  78. }
  79. $adminIds = $this->getDataLimitAdminIds();
  80. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  81. $this->error(__('You have no permission'));
  82. }
  83. if (false === $this->request->isPost()) {
  84. $wallet = (new LedgerWalletModel())->get($ids);
  85. $row['token'] = $wallet['token'] ?? "-";
  86. $row['frozen'] = $wallet['frozen'] ?? "-";
  87. $row['teac'] = $wallet['teac'] ?? "-";
  88. //$google=new GoogleAuthenticator();
  89. //生成验证秘钥
  90. //$secret=$google->createSecret();
  91. //$qrCodeUrl = $google->getQRCodeGoogleUrl('RWACHA', config('google_secret'));
  92. $this->view->assign('row', $row);
  93. return $this->view->fetch();
  94. }
  95. $params = $this->request->post('row/a');
  96. if (empty($params)) {
  97. $this->error(__('Parameter %s can not be empty', ''));
  98. }
  99. $params = $this->preExcludeFields($params);
  100. $google=new GoogleAuthenticator();
  101. $checkResult = $google->verifyCode(config('google_secret'), $params['code'], 6);
  102. if (!$checkResult) $this->error('谷歌验证码错误');
  103. //资金
  104. $newPower = bcadd($params['new_power'], 0, 6);
  105. // 启动事务
  106. Db::startTrans();
  107. try {
  108. // 更新资金
  109. if (bccomp($newPower, 0, 6) !== 0) {
  110. if (empty($params['type_id'])) {
  111. (new LedgerWalletModel)->changeWalletAccount($ids, Asset::TOKEN, $newPower, LedgerTokenChangeModel::System);
  112. }elseif($params['type_id'] == 1){
  113. (new LedgerWalletModel)->changeWalletAccount($ids, Asset::FROZEN, $newPower, LedgerFrozenChangeModel::System);
  114. }else{
  115. (new LedgerWalletModel)->changeWalletAccount($ids, Asset::TEAC, $newPower, LedgerTeacChangeModel::System);
  116. }
  117. }
  118. // 提交事务
  119. Db::commit();
  120. } catch (Exception $e) {
  121. // 回滚事务
  122. Db::rollback();
  123. $this->error('调整失败:' . $e->getMessage());
  124. }
  125. $this->success('调整成功');
  126. }
  127. //搜索条件
  128. private function _where(array $filter): array
  129. {
  130. $map = [];
  131. $pid = 0;
  132. $algebra = 0;
  133. if (isset($filter['id'])) $map['a.id'] = ['=', $filter['id']];
  134. if (isset($filter['address'])) $map['a.address'] = ['like', $filter['address']];
  135. if (isset($filter['parent_id'])) $map['parent_id'] = ['=', $filter['parent_id']];
  136. if (isset($filter['create_time'])) {
  137. $arr = explode(' - ', $filter['create_time']);
  138. $map['a.create_time']= ['between time',[$arr[0], $arr[1]]];
  139. }
  140. //团队等级
  141. if (isset($filter['team_level_id'])) $map['a.team_level_id'] = ['=', $filter['team_level_id']];
  142. //人数
  143. if (isset($filter['team_num'])) $map['a.team_num'] = ['=', $filter['team_num']];
  144. //直推
  145. if (isset($filter['direct_num'])) $map['a.direct_num'] = ['=', $filter['direct_num']];
  146. //代数
  147. if(isset($filter['parent_id'])) {
  148. if(isset($filter['algebra']) && $filter['algebra'] > 0){
  149. $pid = $filter['parent_id'];
  150. $algebra = $filter['algebra'];
  151. }else{
  152. $pid = $filter['parent_id'];
  153. $algebra = -1;
  154. }
  155. }
  156. return [$map, $pid, $algebra];
  157. }
  158. }