| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- <?php
- namespace app\admin\controller\user;
- use app\common\controller\Backend;
- use app\common\model\LedgerWalletModel;
- use app\common\model\TeamLevelModel;
- use app\common\model\UserModel;
- use Exception;
- use fast\GoogleAuthenticator;
- use fast\Asset;
- use fast\Common;
- use fast\MembershipLevel;
- use think\Db;
- use think\exception\DbException;
- /**
- * 用户管理
- *
- * @icon fa fa-user
- */
- class User extends Backend
- {
- /**
- * User模型对象
- * @var \app\admin\model\User
- */
- protected $model = null;
- protected $multiFields = ['is_login','is_withdraw'];
- public function _initialize()
- {
- parent::_initialize();
- $this->model = new \app\admin\model\User;
- $this->view->assign("statusList", $this->model->getStatusList());
- }
- /**
- * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
- * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
- * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
- */
-
- /**
- * 查看
- *
- * @return string|Json
- * @throws \think\Exception
- * @throws DbException
- */
- public function index()
- {
- //设置过滤方法
- $this->request->filter(['strip_tags', 'trim']);
- if (false === $this->request->isAjax()) {
- return $this->view->fetch();
- }
- //如果发送的来源是 Selectpage,则转发到 Selectpage
- if ($this->request->request('keyField')) {
- return $this->selectpage();
- }
- list(, $sort, $order, $offset, $limit) = $this->buildparams();
-
- //搜索条件
- $where = [];
- $filter = json_decode(urldecode(input('filter')), TRUE);
- list($where, $pid, $algebra) = $this->_where($filter);
-
- $list = $this->model
- ->with('ledgerWallet')
- ->alias('a');
- if ($pid > 0) {//上级大于0的时候,默认搜索伞下
- if($algebra > 0){
- $list = $list->where('a.id', 'in', function ($query) use($pid, $algebra) {
- $query->table('user_path')->where('parent_id', $pid)->where('distance', $algebra)->field('user_id');
- });
- }else{
- $list = $list->where('a.id', 'in', function ($query) use($pid, $algebra) {
- $query->table('user_path')->where('parent_id', $pid)->field('user_id');
- });
- }
- }
-
- $list = $list->where($where)->order($sort, $order)->paginate($limit);
- $result = ['total' => $list->total(), 'rows' => $list->items()];
- return json($result);
- }
- /**
- * 编辑
- *
- * @param $ids
- * @return string
- * @throws DbException
- * @throws \think\Exception
- */
- public function edit($ids = null)
- {
- $google=new GoogleAuthenticator();
- //生成验证秘钥
- $secret=$google->createSecret();
- //生成验证二维码 $username 需要绑定的用户名
- echo $secret;
- // $qrCodeUrl = $ga->getQRCodeGoogleUrl('localhost', $secret);
- $qrCodeUrl = $google->getQRCodeGoogleUrl('localhost', $secret);
-
- dump($qrCodeUrl);die;
- $ids = intval($ids);
- $row = $this->model->get($ids);
- if (!$row) {
- $this->error(__('No Results were found'));
- }
- $adminIds = $this->getDataLimitAdminIds();
- if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
- $this->error(__('You have no permission'));
- }
- if (false === $this->request->isPost()) {
- $wallet = (new LedgerWalletModel())->get($ids);
- $row['power'] = $wallet['power'] ?? "-";
- $this->view->assign('row', $row);
- return $this->view->fetch();
- }
- $params = $this->request->post('row/a');
- if (empty($params)) {
- $this->error(__('Parameter %s can not be empty', ''));
- }
- $params = $this->preExcludeFields($params);
- $newLevelId = $params['team_level_id'];
- if($newLevelId > 0 && empty((new TeamLevelModel())->getTeamLevelName($newLevelId))){
- $this->error('会员等级错误');
- }
- //茶宝
- $newPower = bcadd($params['new_power'], 0, 6);
- // 启动事务
- Db::startTrans();
- try {
- // 更新会员等级
- if ($newLevelId != $row['team_level_id']) {
- $updated = (new UserModel())->save(['team_level_id' => $newLevelId], ['id' => $row['id']]);
- if (empty($updated)) {
- throw new Exception('更新会员等级影响行数为0');
- }
- }
- // 更新茶宝
- if (bccomp($newPower, 0, 6) !== 0) {
- (new LedgerWalletModel)->changeWalletAccount($ids, Asset::TOKEN, $newPower, LedgerWalletModel::System);
- }
- // 提交事务
- Db::commit();
- } catch (Exception $e) {
- // 回滚事务
- Db::rollback();
- $this->error('调整失败:' . $e->getMessage());
- }
- $this->success('调整成功');
- }
- //搜索条件
- private function _where(array $filter): array
- {
- $map = [];
- $pid = 0;
- $algebra = 0;
- if (isset($filter['id'])) $map['a.id'] = ['=', $filter['id']];
- if (isset($filter['address'])) $map['a.address'] = ['like', $filter['address']];
- //if (isset($filter['parent_id']) && !isset($filter['algebra'])) $map['a.parent_id'] = ['=', $filter['parent_id']];
- if (isset($filter['create_time'])) {
- $arr = explode(' - ', $filter['create_time']);
- $map['a.create_time']= ['between time',[$arr[0], $arr[1]]];
- }
- //团队等级
- if (isset($filter['team_level_id'])) $map['a.team_level_id'] = ['=', $filter['team_level_id']];
- //人数
- if (isset($filter['team_num'])) $map['a.team_num'] = ['=', $filter['team_num']];
- //直推
- if (isset($filter['direct_num'])) $map['a.direct_num'] = ['=', $filter['direct_num']];
-
-
- //代数
- if(isset($filter['parent_id'])) {
- if(isset($filter['algebra']) && $filter['algebra'] > 0){
- $pid = $filter['parent_id'];
- $algebra = $filter['algebra'];
- }else{
- $pid = $filter['parent_id'];
- $algebra = -1;
- }
- }
- return [$map, $pid, $algebra];
- }
-
- }
|