User.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 app\common\model\CodeUserModel;
  14. use think\Db;
  15. use think\exception\DbException;
  16. /**
  17. * 用户管理
  18. *
  19. * @icon fa fa-user
  20. */
  21. class User extends Backend
  22. {
  23. /**
  24. * User模型对象
  25. * @var \app\admin\model\User
  26. */
  27. protected $model = null;
  28. protected $multiFields = ['is_login','is_withdraw'];
  29. public function _initialize()
  30. {
  31. parent::_initialize();
  32. $this->model = new \app\admin\model\User;
  33. $this->view->assign("statusList", $this->model->getStatusList());
  34. }
  35. /**
  36. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  37. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  38. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  39. */
  40. /**
  41. * 查看
  42. *
  43. * @return string|Json
  44. * @throws \think\Exception
  45. * @throws DbException
  46. */
  47. public function index()
  48. {
  49. //设置过滤方法
  50. $this->request->filter(['strip_tags', 'trim']);
  51. if (false === $this->request->isAjax()) {
  52. return $this->view->fetch();
  53. }
  54. //如果发送的来源是 Selectpage,则转发到 Selectpage
  55. if ($this->request->request('keyField')) {
  56. return $this->selectpage();
  57. }
  58. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  59. $list = $this->model->with('ledgerWallet')->with('productorder')->where($where)->order($sort, $order)->paginate($limit);
  60. foreach ($list as &$item) {
  61. $item['hold_num'] = $item->productorder? count($item->productorder): 0;
  62. //::where('user_id', $item->id)->where('status', 'in',[$productOrder::Paid, $productOrder::Transferred, $productOrder::Freeze])->count();//持有数量
  63. }
  64. $result = ['total' => $list->total(), 'rows' => $list->items()];
  65. return json($result);
  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'])->where('id', '<>', $ids)->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. /**
  177. * 激活码礼包
  178. * @param $ids
  179. * @return string
  180. * @throws DbException
  181. * @throws \think\Exception
  182. */
  183. public function codeno($ids = null)
  184. {
  185. $ids = intval($ids);
  186. $row = CodeUserModel::where('user_id',$ids)->find();
  187. if (false === $this->request->isPost()) {
  188. $this->view->assign('row', $row);
  189. return $this->view->fetch();
  190. }
  191. $params = $this->request->post('row/a');
  192. if (empty($params)) {
  193. $this->error(__('Parameter %s can not be empty', ''));
  194. }
  195. $params = $this->preExcludeFields($params);
  196. // 启动事务
  197. Db::startTrans();
  198. try {
  199. $result = CodeUserModel::updateCodeUser($ids, $params);
  200. // 提交事务
  201. Db::commit();
  202. } catch (Exception $e) {
  203. // 回滚事务
  204. Db::rollback();
  205. $this->error('调整失败:' . $e->getMessage());
  206. }
  207. if (false === $result) {
  208. $this->error(__('No rows were updated'));
  209. }
  210. $this->success();
  211. }
  212. }