User.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. *
  82. * @param $ids
  83. * @return string
  84. * @throws DbException
  85. * @throws \think\Exception
  86. */
  87. public function edit($ids = null)
  88. {
  89. $google=new GoogleAuthenticator();
  90. //生成验证秘钥
  91. $secret=$google->createSecret();
  92. //生成验证二维码 $username 需要绑定的用户名
  93. echo $secret;
  94. // $qrCodeUrl = $ga->getQRCodeGoogleUrl('localhost', $secret);
  95. $qrCodeUrl = $google->getQRCodeGoogleUrl('localhost', $secret);
  96. dump($qrCodeUrl);die;
  97. $ids = intval($ids);
  98. $row = $this->model->get($ids);
  99. if (!$row) {
  100. $this->error(__('No Results were found'));
  101. }
  102. $adminIds = $this->getDataLimitAdminIds();
  103. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  104. $this->error(__('You have no permission'));
  105. }
  106. if (false === $this->request->isPost()) {
  107. $wallet = (new LedgerWalletModel())->get($ids);
  108. $row['power'] = $wallet['power'] ?? "-";
  109. $this->view->assign('row', $row);
  110. return $this->view->fetch();
  111. }
  112. $params = $this->request->post('row/a');
  113. if (empty($params)) {
  114. $this->error(__('Parameter %s can not be empty', ''));
  115. }
  116. $params = $this->preExcludeFields($params);
  117. $newLevelId = $params['team_level_id'];
  118. if($newLevelId > 0 && empty((new TeamLevelModel())->getTeamLevelName($newLevelId))){
  119. $this->error('会员等级错误');
  120. }
  121. //茶宝
  122. $newPower = bcadd($params['new_power'], 0, 6);
  123. // 启动事务
  124. Db::startTrans();
  125. try {
  126. // 更新会员等级
  127. if ($newLevelId != $row['team_level_id']) {
  128. $updated = (new UserModel())->save(['team_level_id' => $newLevelId], ['id' => $row['id']]);
  129. if (empty($updated)) {
  130. throw new Exception('更新会员等级影响行数为0');
  131. }
  132. }
  133. // 更新茶宝
  134. if (bccomp($newPower, 0, 6) !== 0) {
  135. (new LedgerWalletModel)->changeWalletAccount($ids, Asset::TOKEN, $newPower, LedgerWalletModel::System);
  136. }
  137. // 提交事务
  138. Db::commit();
  139. } catch (Exception $e) {
  140. // 回滚事务
  141. Db::rollback();
  142. $this->error('调整失败:' . $e->getMessage());
  143. }
  144. $this->success('调整成功');
  145. }
  146. //搜索条件
  147. private function _where(array $filter): array
  148. {
  149. $map = [];
  150. $pid = 0;
  151. $algebra = 0;
  152. if (isset($filter['id'])) $map['a.id'] = ['=', $filter['id']];
  153. if (isset($filter['address'])) $map['a.address'] = ['like', $filter['address']];
  154. //if (isset($filter['parent_id']) && !isset($filter['algebra'])) $map['a.parent_id'] = ['=', $filter['parent_id']];
  155. if (isset($filter['create_time'])) {
  156. $arr = explode(' - ', $filter['create_time']);
  157. $map['a.create_time']= ['between time',[$arr[0], $arr[1]]];
  158. }
  159. //团队等级
  160. if (isset($filter['team_level_id'])) $map['a.team_level_id'] = ['=', $filter['team_level_id']];
  161. //人数
  162. if (isset($filter['team_num'])) $map['a.team_num'] = ['=', $filter['team_num']];
  163. //直推
  164. if (isset($filter['direct_num'])) $map['a.direct_num'] = ['=', $filter['direct_num']];
  165. //代数
  166. if(isset($filter['parent_id'])) {
  167. if(isset($filter['algebra']) && $filter['algebra'] > 0){
  168. $pid = $filter['parent_id'];
  169. $algebra = $filter['algebra'];
  170. }else{
  171. $pid = $filter['parent_id'];
  172. $algebra = -1;
  173. }
  174. }
  175. return [$map, $pid, $algebra];
  176. }
  177. }