User.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. <?php
  2. namespace app\admin\controller\user;
  3. use think\Db;
  4. use Exception;
  5. use fast\Random;
  6. use think\Hook;
  7. use app\common\model\UsersPath;
  8. use app\common\model\MoneyLog;
  9. use think\exception\PDOException;
  10. use app\common\controller\Backend;
  11. use think\exception\ValidateException;
  12. /**
  13. * 会员管理
  14. *
  15. * @icon fa fa-user
  16. */
  17. class User extends Backend
  18. {
  19. protected $relationSearch = true;
  20. protected $searchFields = 'id,mobile';
  21. /**
  22. * @var \app\admin\model\Users
  23. */
  24. protected $model = null;
  25. public function _initialize()
  26. {
  27. parent::_initialize();
  28. $this->model = model('Users');
  29. $this->assign('statusList', $this->model->getStatusList());
  30. $this->assign('typeList', $this->model->getTypeList());
  31. }
  32. /**
  33. * 查看
  34. */
  35. public function index()
  36. {
  37. //设置过滤方法
  38. $this->request->filter(['strip_tags', 'trim']);
  39. if ($this->request->isAjax()) {
  40. //如果发送的来源是Selectpage,则转发到Selectpage
  41. if ($this->request->request('keyField')) {
  42. return $this->selectpage();
  43. }
  44. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  45. $list = $this->model->with('parent,agent')
  46. ->where($where)->where('users.is_delete', 0)
  47. ->order($sort, $order)
  48. ->paginate($limit);
  49. $in = model('MoneyIn');
  50. $out = model('MoneyOut');
  51. foreach ($list as &$item) {
  52. $item->recharge = $in::where('status', 200)->where('user_id', $item->id)->sum('amount');
  53. $item->withdraw = $out::where('status', 200)->where('user_id', $item->id)->sum('amount');
  54. }
  55. $result = array("total" => $list->total(), "rows" => $list->items());
  56. return json($result);
  57. }
  58. return $this->view->fetch();
  59. }
  60. /**
  61. * 余额
  62. * @param $ids
  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. $amount = $params['type'] == 1 ?-$params['amount']:$params['amount'];
  87. (new MoneyLog())->change($params['id'], $amount, MoneyLog::SystemChange, $this->auth->id, '后台操作');
  88. Db::commit();
  89. } catch (ValidateException|PDOException|Exception $e) {
  90. Db::rollback();
  91. $this->error($e->getMessage());
  92. }
  93. $this->success();
  94. }
  95. /**
  96. * 卡单
  97. */
  98. public function cardslip($ids = null)
  99. {
  100. $row = $this->model->get($ids);
  101. if (!$row) {
  102. $this->error(__('No Results were found'));
  103. }
  104. $adminIds = $this->getDataLimitAdminIds();
  105. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  106. $this->error(__('You have no permission'));
  107. }
  108. if (false === $this->request->isPost()) {
  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. $result = false;
  118. Db::startTrans();
  119. try {
  120. $result = $row->allowField(true)->save(['limit_task'=>$params['limit_task']]);
  121. Db::commit();
  122. } catch (ValidateException|PDOException|Exception $e) {
  123. Db::rollback();
  124. $this->error($e->getMessage());
  125. }
  126. if (false === $result) {
  127. $this->error(__('No rows were updated'));
  128. }
  129. $this->success();
  130. }
  131. /**
  132. * 收款
  133. * @param $ids
  134. */
  135. public function collection($ids = null)
  136. {
  137. $row = $this->model->get($ids);
  138. if (!$row) {
  139. $this->error(__('No Results were found'));
  140. }
  141. $adminIds = $this->getDataLimitAdminIds();
  142. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  143. $this->error(__('You have no permission'));
  144. }
  145. if (false === $this->request->isPost()) {
  146. $bank = json_decode($row->bank_info, true);
  147. $row->account_name = $bank['account_name']??'';
  148. $row->bank_name = $bank['bank_name']??'';
  149. $row->bank_card = $bank['bank_card']??'';
  150. $this->view->assign('row', $row);
  151. return $this->view->fetch();
  152. }
  153. $params = $this->request->post('row/a');
  154. if (empty($params)) {
  155. $this->error(__('Parameter %s can not be empty', ''));
  156. }
  157. $params = $this->preExcludeFields($params);
  158. $result = false;
  159. Db::startTrans();
  160. try {
  161. $addr = $params['usdt_address'];
  162. unset($params['id'],$params['usdt_address']);
  163. $result = $row->allowField(true)->save(['usdt_address'=> $addr, 'bank_info'=>json_encode($params)]);
  164. Db::commit();
  165. } catch (ValidateException|PDOException|Exception $e) {
  166. Db::rollback();
  167. $this->error($e->getMessage());
  168. }
  169. if (false === $result) {
  170. $this->error(__('No rows were updated'));
  171. }
  172. $this->success();
  173. }
  174. /**
  175. * 清零
  176. */
  177. public function clear($ids = null)
  178. {
  179. $row = $this->model->get($ids);
  180. if (!$row) {
  181. $this->error(__('No Results were found'));
  182. }
  183. $adminIds = $this->getDataLimitAdminIds();
  184. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  185. $this->error(__('You have no permission'));
  186. }
  187. $result = false;
  188. try {
  189. $result = $row->allowField(true)->save(['task_num'=>0]);
  190. } catch (ValidateException|PDOException|Exception $e) {
  191. $this->error($e->getMessage());
  192. }
  193. if (false === $result) {
  194. $this->error(__('No rows were updated'));
  195. }
  196. $this->success();
  197. }
  198. /**
  199. * 添加
  200. */
  201. public function add()
  202. {
  203. if (false === $this->request->isPost()) {
  204. return $this->view->fetch();
  205. }
  206. $params = $this->request->post('row/a');
  207. if (empty($params)) {
  208. $this->error(__('Parameter %s can not be empty', ''));
  209. }
  210. $params = $this->preExcludeFields($params);
  211. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  212. $params[$this->dataLimitField] = $this->auth->id;
  213. }
  214. if ($params['mobile'] && \app\common\model\Users::getByCodeAndMobile($params['code'], $params['mobile'])) {
  215. $this->error(__('Mobile already exist', ''));
  216. }
  217. $parent_info = $this->model::getByParentInfo($params['parent_id']);
  218. if(empty($parent_info)) $this->error('不存在上级ID');
  219. $ip = request()->ip();
  220. $time = time();
  221. $user_data = [
  222. 'mobile' => $params['mobile'],
  223. 'code' => $params['code'],
  224. 'login_pwd'=> $params['login_pwd'],
  225. 'parent_id'=> $params['parent_id'],
  226. 'agent_id' => $parent_info['agent_id'],
  227. 'user_type'=> 1,//真人
  228. ];
  229. if($parent_info['is_agent']) $user_data['agent_id'] = $parent_info['id'];
  230. $user_data = array_merge($user_data, [
  231. 'nickname' => preg_match("/^1[3-9]{1}\d{9}$/", $params['mobile']) ? substr_replace($params['mobile'], '****', 3, 4) : $params['mobile'],
  232. 'salt' => Random::alnum(),
  233. 'join_time' => $time,
  234. 'login_ip' => $ip,
  235. 'login_time'=> $time,
  236. ]);
  237. $user_data['login_pwd'] = $this->auth->getEncryptPassword($params['login_pwd'], $user_data['salt']);
  238. $result = false;
  239. Db::startTrans();
  240. try {
  241. $result = $user = $this->model::create($user_data, true);
  242. $_user = $this->model::get($user->id);
  243. // 创建网体
  244. (new UsersPath())->createPath($user->id, $parent_info['id']);
  245. //上级人数+1
  246. $this->model->where('id', $parent_info['id'])->setInc('team_num');
  247. //设置Token
  248. //注册成功的事件
  249. Hook::listen("user_register_successed", $_user, $user_data);
  250. Db::commit();
  251. } catch (ValidateException|PDOException|Exception $e) {
  252. Db::rollback();
  253. $this->error($e->getMessage());
  254. }
  255. if ($result === false) {
  256. $this->error(__('No rows were inserted'));
  257. }
  258. $this->success();
  259. }
  260. /**
  261. * 编辑
  262. */
  263. public function edit($ids = null)
  264. {
  265. $row = $this->model->get($ids);
  266. if (!$row) {
  267. $this->error(__('No Results were found'));
  268. }
  269. $adminIds = $this->getDataLimitAdminIds();
  270. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  271. $this->error(__('You have no permission'));
  272. }
  273. if (false === $this->request->isPost()) {
  274. $this->view->assign('row', $row);
  275. return $this->view->fetch();
  276. }
  277. $params = $this->request->post('row/a');
  278. if (empty($params)) {
  279. $this->error(__('Parameter %s can not be empty', ''));
  280. }
  281. $params = $this->preExcludeFields($params);
  282. $result = false;
  283. Db::startTrans();
  284. try {
  285. //是否采用模型验证
  286. if ($this->modelValidate) {
  287. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  288. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  289. $row->validateFailException()->validate($validate);
  290. }
  291. if(!empty($params['login_pwd'])){
  292. $params['salt'] = Random::alnum();
  293. $params['login_pwd'] = $this->auth->getEncryptPassword($params['login_pwd'], $params['salt']);
  294. }else{
  295. unset($params['login_pwd']);
  296. }
  297. if(!empty($params['fund_pwd'])){
  298. $params['fund_pwd'] = md5($params['fund_pwd']);
  299. }else{
  300. unset($params['fund_pwd']);
  301. }
  302. //修改代理
  303. if($row->agent_id !=$params['agent_id'] && empty($row->is_agent)) {
  304. //伞下不是代理/代理是他的
  305. $this->model->where('id','IN', UsersPath::where('parent_id', $ids)->column('user_id'))
  306. ->where('is_agent', 0)
  307. ->update(['agent_id'=>$params['agent_id']]);
  308. }
  309. $result = $row->allowField(true)->save($params);
  310. Db::commit();
  311. } catch (ValidateException|PDOException|Exception $e) {
  312. Db::rollback();
  313. $this->error($e->getMessage());
  314. }
  315. if (false === $result) {
  316. $this->error(__('No rows were updated'));
  317. }
  318. $this->success();
  319. }
  320. /**
  321. * 删除
  322. */
  323. public function del($ids = null)
  324. {
  325. $row = $this->model->get($ids);
  326. if (!$row) {
  327. $this->error(__('No Results were found'));
  328. }
  329. $adminIds = $this->getDataLimitAdminIds();
  330. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  331. $this->error(__('You have no permission'));
  332. }
  333. $result = false;
  334. try {
  335. $result = $row->allowField(true)->save(['is_delete'=>1]);
  336. } catch (ValidateException|PDOException|Exception $e) {
  337. $this->error($e->getMessage());
  338. }
  339. if (false === $result) {
  340. $this->error(__('No rows were updated'));
  341. }
  342. $this->success();
  343. }
  344. /**
  345. * 批量更新
  346. * @param $ids
  347. * @return void
  348. */
  349. public function multi($ids = null)
  350. {
  351. if (false === $this->request->isPost()) {
  352. $this->error(__('Invalid parameters'));
  353. }
  354. $ids = $ids ?: $this->request->post('ids');
  355. if (empty($ids)) {
  356. $this->error(__('Parameter %s can not be empty', 'ids'));
  357. }
  358. if (false === $this->request->has('params')) {
  359. $this->error(__('No rows were updated'));
  360. }
  361. parse_str($this->request->post('params'), $values);
  362. //$values = $this->auth->isSuperAdmin() ? $values : array_intersect_key($values, array_flip(is_array($this->multiFields) ? $this->multiFields : explode(',', $this->multiFields)));
  363. if (empty($values)) {
  364. $this->error(__('You have no permission'));
  365. }
  366. $adminIds = $this->getDataLimitAdminIds();
  367. if (is_array($adminIds)) {
  368. $this->model->where($this->dataLimitField, 'in', $adminIds);
  369. }
  370. $count = 0;
  371. Db::startTrans();
  372. try {
  373. $row = $this->model->where($this->model->getPk(), '=', $ids)->find();
  374. $key = key($values); //修改key
  375. if($key == 'is_agent'){
  376. //默认开启:设置伞下当前会员所属代理id
  377. $map['agent_id'] = $values[$key] == 0? $row->agent_id: $ids;
  378. //伞下/代理是自己, 是agent_id = 自己user_id吗
  379. $this->model->where('id','IN', UsersPath::where('parent_id', $ids)->column('user_id'))
  380. ->where('is_agent', 0)
  381. ->update($map);
  382. }
  383. $count = $row->allowField(true)->isUpdate(true)->save($values);
  384. Db::commit();
  385. } catch (PDOException|Exception $e) {
  386. Db::rollback();
  387. $this->error($e->getMessage());
  388. }
  389. if ($count) {
  390. $this->success();
  391. }
  392. $this->error(__('No rows were updated'));
  393. }
  394. }