User.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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\TeamLevelModel;
  6. use app\common\model\UserModel;
  7. use Exception;
  8. use fast\GoogleAuthenticator;
  9. use fast\Asset;
  10. use fast\Common;
  11. use fast\MembershipLevel;
  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. list(, $sort, $order, $offset, $limit) = $this->buildparams();
  57. //搜索条件
  58. $where = [];
  59. $filter = json_decode(urldecode(input('filter')), TRUE);
  60. list($where, $pid, $algebra) = $this->_where($filter);
  61. $list = $this->model
  62. ->with('ledgerWallet')
  63. ->alias('a');
  64. if ($pid > 0) {//上级大于0的时候,默认搜索伞下
  65. if($algebra > 0){
  66. $list = $list->where('a.id', 'in', function ($query) use($pid, $algebra) {
  67. $query->table('user_path')->where('parent_id', $pid)->where('distance', $algebra)->field('user_id');
  68. });
  69. }else{
  70. $list = $list->where('a.id', 'in', function ($query) use($pid, $algebra) {
  71. $query->table('user_path')->where('parent_id', $pid)->field('user_id');
  72. });
  73. }
  74. }
  75. $list = $list->where($where)->order($sort, $order)->paginate($limit);
  76. $result = ['total' => $list->total(), 'rows' => $list->items()];
  77. return json($result);
  78. }
  79. /**
  80. * 茶宝调整
  81. * @param $ids
  82. * @return string
  83. * @throws DbException
  84. * @throws \think\Exception
  85. */
  86. public function approve($ids = null)
  87. {
  88. $ids = intval($ids);
  89. $row = $this->model->get($ids);
  90. if (!$row) {
  91. $this->error(__('No Results were found'));
  92. }
  93. $adminIds = $this->getDataLimitAdminIds();
  94. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  95. $this->error(__('You have no permission'));
  96. }
  97. if (false === $this->request->isPost()) {
  98. $wallet = (new LedgerWalletModel())->get($ids);
  99. $row['token'] = $wallet['token'] ?? "-";
  100. //$google=new GoogleAuthenticator();
  101. //生成验证秘钥
  102. //$secret=$google->createSecret();
  103. //$qrCodeUrl = $google->getQRCodeGoogleUrl('RWACHA', config('google_secret'));
  104. $this->view->assign('row', $row);
  105. return $this->view->fetch();
  106. }
  107. $params = $this->request->post('row/a');
  108. if (empty($params)) {
  109. $this->error(__('Parameter %s can not be empty', ''));
  110. }
  111. $params = $this->preExcludeFields($params);
  112. $google=new GoogleAuthenticator();
  113. $checkResult = $google->verifyCode(config('google_secret'), $params['code'], 6);
  114. if (!$checkResult) $this->error('谷歌验证码错误');
  115. //茶宝
  116. $newPower = bcadd($params['new_power'], 0, 6);
  117. // 启动事务
  118. Db::startTrans();
  119. try {
  120. // 更新茶宝
  121. if (bccomp($newPower, 0, 6) !== 0) {
  122. (new LedgerWalletModel)->changeWalletAccount($ids, Asset::TOKEN, $newPower, LedgerWalletModel::System);
  123. }
  124. // 提交事务
  125. Db::commit();
  126. } catch (Exception $e) {
  127. // 回滚事务
  128. Db::rollback();
  129. $this->error('调整失败:' . $e->getMessage());
  130. }
  131. $this->success('调整成功');
  132. }
  133. //搜索条件
  134. private function _where(array $filter): array
  135. {
  136. $map = [];
  137. $pid = 0;
  138. $algebra = 0;
  139. if (isset($filter['id'])) $map['a.id'] = ['=', $filter['id']];
  140. if (isset($filter['address'])) $map['a.address'] = ['like', $filter['address']];
  141. //if (isset($filter['parent_id']) && !isset($filter['algebra'])) $map['a.parent_id'] = ['=', $filter['parent_id']];
  142. if (isset($filter['create_time'])) {
  143. $arr = explode(' - ', $filter['create_time']);
  144. $map['a.create_time']= ['between time',[$arr[0], $arr[1]]];
  145. }
  146. //团队等级
  147. if (isset($filter['team_level_id'])) $map['a.team_level_id'] = ['=', $filter['team_level_id']];
  148. //人数
  149. if (isset($filter['team_num'])) $map['a.team_num'] = ['=', $filter['team_num']];
  150. //直推
  151. if (isset($filter['direct_num'])) $map['a.direct_num'] = ['=', $filter['direct_num']];
  152. //代数
  153. if(isset($filter['parent_id'])) {
  154. if(isset($filter['algebra']) && $filter['algebra'] > 0){
  155. $pid = $filter['parent_id'];
  156. $algebra = $filter['algebra'];
  157. }else{
  158. $pid = $filter['parent_id'];
  159. $algebra = -1;
  160. }
  161. }
  162. return [$map, $pid, $algebra];
  163. }
  164. }