User.php 6.7 KB

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