User.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <?php
  2. namespace app\admin\controller\user;
  3. use think\Db;
  4. use Exception;
  5. use app\common\model\Moneylog;
  6. use think\exception\DbException;
  7. use think\exception\PDOException;
  8. use app\common\controller\Backend;
  9. use think\exception\ValidateException;
  10. /**
  11. * 会员管理
  12. *
  13. * @icon fa fa-user
  14. */
  15. class User extends Backend
  16. {
  17. protected $relationSearch = true;
  18. protected $searchFields = 'id,mobile';
  19. /**
  20. * @var \app\admin\model\User
  21. */
  22. protected $model = null;
  23. public function _initialize()
  24. {
  25. parent::_initialize();
  26. $this->model = model('User');
  27. $this->assign('statusList', $this->model->getStatusList());
  28. $this->assign('typeList', $this->model->getTypeList());
  29. }
  30. /**
  31. * 查看
  32. */
  33. public function index()
  34. {
  35. //设置过滤方法
  36. $this->request->filter(['strip_tags', 'trim']);
  37. if ($this->request->isAjax()) {
  38. //如果发送的来源是Selectpage,则转发到Selectpage
  39. if ($this->request->request('keyField')) {
  40. return $this->selectpage();
  41. }
  42. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  43. $list = $this->model
  44. //->with('group')
  45. ->where($where)
  46. ->order($sort, $order)
  47. ->paginate($limit);
  48. // foreach ($list as $k => $v) {
  49. // $v->avatar = $v->avatar ? cdnurl($v->avatar, true) : letter_avatar($v->nickname);
  50. // $v->hidden(['password', 'salt']);
  51. // }
  52. $result = array("total" => $list->total(), "rows" => $list->items());
  53. return json($result);
  54. }
  55. return $this->view->fetch();
  56. }
  57. /**
  58. * 余额 public function balance($ids = null)
  59. * @param $ids
  60. * @return string
  61. * @throws DbException
  62. * @throws \think\Exception
  63. */
  64. public function balance($ids = null)
  65. {
  66. $row = $this->model->get($ids);
  67. if (!$row) {
  68. $this->error(__('No Results were found'));
  69. }
  70. $adminIds = $this->getDataLimitAdminIds();
  71. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  72. $this->error(__('You have no permission'));
  73. }
  74. if (false === $this->request->isPost()) {
  75. $this->view->assign('row', $row);
  76. return $this->view->fetch();
  77. }
  78. $params = $this->request->post('row/a');
  79. if (empty($params)) {
  80. $this->error(__('Parameter %s can not be empty', ''));
  81. }
  82. $params = $this->preExcludeFields($params);
  83. $result = false;
  84. Db::startTrans();
  85. try {
  86. $balance = build_amount_compute($params['type'], $params['amount'], $row->balance);
  87. //操作记录
  88. Moneylog::create(['user_id'=> $params['id'],
  89. 'from_id'=> $this->auth->id,
  90. 'amount'=> $params['amount'],
  91. 'balance'=> $balance
  92. ]);
  93. $result = $row->allowField(true)->save(['balance'=>$balance]);
  94. Db::commit();
  95. } catch (ValidateException|PDOException|Exception $e) {
  96. Db::rollback();
  97. $this->error($e->getMessage());
  98. }
  99. if (false === $result) {
  100. $this->error(__('No rows were updated'));
  101. }
  102. $this->success();
  103. }
  104. /**
  105. * 卡单
  106. * @param $ids
  107. * @return string
  108. * @throws DbException
  109. * @throws \think\Exception
  110. */
  111. public function cardslip($ids = null)
  112. {
  113. $row = $this->model->get($ids);
  114. if (!$row) {
  115. $this->error(__('No Results were found'));
  116. }
  117. $adminIds = $this->getDataLimitAdminIds();
  118. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  119. $this->error(__('You have no permission'));
  120. }
  121. if (false === $this->request->isPost()) {
  122. $task = json_decode($row->linit_task, true);
  123. $row->which_start = $task['which_start']??'';
  124. $row->min_amount = $task['min_amount']??'';
  125. $row->max_amount = $task['max_amount']??'';
  126. $row->income_multiple= $task['income_multiple']??'';
  127. $this->view->assign('row', $row);
  128. return $this->view->fetch();
  129. }
  130. $params = $this->request->post('row/a');
  131. if (empty($params)) {
  132. $this->error(__('Parameter %s can not be empty', ''));
  133. }
  134. $params = $this->preExcludeFields($params);
  135. $result = false;
  136. Db::startTrans();
  137. try {
  138. unset($params['id']);
  139. $result = $row->allowField(true)->save(['linit_task'=>json_encode($params)]);
  140. Db::commit();
  141. } catch (ValidateException|PDOException|Exception $e) {
  142. Db::rollback();
  143. $this->error($e->getMessage());
  144. }
  145. if (false === $result) {
  146. $this->error(__('No rows were updated'));
  147. }
  148. $this->success();
  149. }
  150. /**
  151. * 收款
  152. * @param $ids
  153. * @return string
  154. * @throws DbException
  155. * @throws \think\Exception
  156. */
  157. public function collection($ids = null)
  158. {
  159. $row = $this->model->get($ids);
  160. if (!$row) {
  161. $this->error(__('No Results were found'));
  162. }
  163. $adminIds = $this->getDataLimitAdminIds();
  164. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  165. $this->error(__('You have no permission'));
  166. }
  167. if (false === $this->request->isPost()) {
  168. $bank = json_decode($row->bank_info, true);
  169. $row->real_name = $bank['real_name']??'';
  170. $row->bank_name = $bank['bank_name']??'';
  171. $row->bank_card = $bank['bank_card']??'';
  172. $this->view->assign('row', $row);
  173. return $this->view->fetch();
  174. }
  175. $params = $this->request->post('row/a');
  176. if (empty($params)) {
  177. $this->error(__('Parameter %s can not be empty', ''));
  178. }
  179. $params = $this->preExcludeFields($params);
  180. $result = false;
  181. Db::startTrans();
  182. try {
  183. $addr = $params['usdt_address'];
  184. unset($params['id'],$params['usdt_address']);
  185. $result = $row->allowField(true)->save(['usdt_address'=> $addr, 'bank_info'=>json_encode($params)]);
  186. Db::commit();
  187. } catch (ValidateException|PDOException|Exception $e) {
  188. Db::rollback();
  189. $this->error($e->getMessage());
  190. }
  191. if (false === $result) {
  192. $this->error(__('No rows were updated'));
  193. }
  194. $this->success();
  195. }
  196. /**
  197. * 清0
  198. * @param $ids
  199. * @return string
  200. * @throws DbException
  201. * @throws \think\Exception
  202. */
  203. public function clear($ids = null)
  204. {
  205. $row = $this->model->get($ids);
  206. if (!$row) {
  207. $this->error(__('No Results were found'));
  208. }
  209. $adminIds = $this->getDataLimitAdminIds();
  210. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  211. $this->error(__('You have no permission'));
  212. }
  213. $result = false;
  214. try {
  215. $result = $row->allowField(true)->save(['task_num'=>0]);
  216. } catch (ValidateException|PDOException|Exception $e) {
  217. $this->error($e->getMessage());
  218. }
  219. if (false === $result) {
  220. $this->error(__('No rows were updated'));
  221. }
  222. $this->success();
  223. }
  224. /**
  225. * 编辑
  226. *
  227. * @param $ids
  228. * @return string
  229. * @throws DbException
  230. * @throws \think\Exception
  231. */
  232. public function edit($ids = null)
  233. {
  234. $row = $this->model->get($ids);
  235. if (!$row) {
  236. $this->error(__('No Results were found'));
  237. }
  238. $adminIds = $this->getDataLimitAdminIds();
  239. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  240. $this->error(__('You have no permission'));
  241. }
  242. if (false === $this->request->isPost()) {
  243. $this->view->assign('row', $row);
  244. return $this->view->fetch();
  245. }
  246. $params = $this->request->post('row/a');
  247. if (empty($params)) {
  248. $this->error(__('Parameter %s can not be empty', ''));
  249. }
  250. $params = $this->preExcludeFields($params);
  251. $result = false;
  252. Db::startTrans();
  253. try {
  254. //是否采用模型验证
  255. if ($this->modelValidate) {
  256. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  257. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  258. $row->validateFailException()->validate($validate);
  259. }
  260. $result = $row->allowField(true)->save($params);
  261. Db::commit();
  262. } catch (ValidateException|PDOException|Exception $e) {
  263. Db::rollback();
  264. $this->error($e->getMessage());
  265. }
  266. if (false === $result) {
  267. $this->error(__('No rows were updated'));
  268. }
  269. $this->success();
  270. }
  271. }