User.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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. 'invitation_code' => (new \app\common\model\Users)->generateInviteCode(),
  227. 'agent_id' => $parent_info['agent_id'],
  228. 'user_type'=> 1,//真人
  229. ];
  230. if($parent_info['is_agent']) $user_data['agent_id'] = $parent_info['id'];
  231. $user_data = array_merge($user_data, [
  232. 'nickname' => preg_match("/^1[3-9]{1}\d{9}$/", $params['mobile']) ? substr_replace($params['mobile'], '****', 3, 4) : $params['mobile'],
  233. 'salt' => Random::alnum(),
  234. 'join_time' => $time,
  235. 'login_ip' => $ip,
  236. 'login_time'=> $time,
  237. ]);
  238. $user_data['login_pwd'] = $this->auth->getEncryptPassword($params['login_pwd'], $user_data['salt']);
  239. $result = false;
  240. Db::startTrans();
  241. try {
  242. $result = $user = $this->model::create($user_data, true);
  243. $_user = $this->model::get($user->id);
  244. // 创建网体
  245. (new UsersPath())->createPath($user->id, $parent_info['id']);
  246. //上级人数+1
  247. $this->model->where('id', $parent_info['id'])->setInc('team_num');
  248. //设置Token
  249. //注册成功的事件
  250. Hook::listen("user_register_successed", $_user, $user_data);
  251. Db::commit();
  252. } catch (ValidateException|PDOException|Exception $e) {
  253. Db::rollback();
  254. $this->error($e->getMessage());
  255. }
  256. if ($result === false) {
  257. $this->error(__('No rows were inserted'));
  258. }
  259. $this->success();
  260. }
  261. /**
  262. * 编辑
  263. */
  264. public function edit($ids = null)
  265. {
  266. $row = $this->model->get($ids);
  267. if (!$row) {
  268. $this->error(__('No Results were found'));
  269. }
  270. $adminIds = $this->getDataLimitAdminIds();
  271. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  272. $this->error(__('You have no permission'));
  273. }
  274. if (false === $this->request->isPost()) {
  275. $this->view->assign('row', $row);
  276. return $this->view->fetch();
  277. }
  278. $params = $this->request->post('row/a');
  279. if (empty($params)) {
  280. $this->error(__('Parameter %s can not be empty', ''));
  281. }
  282. $params = $this->preExcludeFields($params);
  283. $result = false;
  284. Db::startTrans();
  285. try {
  286. //是否采用模型验证
  287. if ($this->modelValidate) {
  288. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  289. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  290. $row->validateFailException()->validate($validate);
  291. }
  292. if(!empty($params['login_pwd'])){
  293. $params['salt'] = Random::alnum();
  294. $params['login_pwd'] = $this->auth->getEncryptPassword($params['login_pwd'], $params['salt']);
  295. }else{
  296. unset($params['login_pwd']);
  297. }
  298. if(!empty($params['fund_pwd'])){
  299. $params['fund_pwd'] = md5($params['fund_pwd']);
  300. }else{
  301. unset($params['fund_pwd']);
  302. }
  303. //修改代理
  304. if($row->agent_id !=$params['agent_id'] && empty($row->is_agent)) {
  305. //伞下不是代理/代理是他的
  306. $this->model->where('id','IN', UsersPath::where('parent_id', $ids)->column('user_id'))
  307. ->where('is_agent', 0)
  308. ->update(['agent_id'=>$params['agent_id']]);
  309. }
  310. $result = $row->allowField(true)->save($params);
  311. Db::commit();
  312. } catch (ValidateException|PDOException|Exception $e) {
  313. Db::rollback();
  314. $this->error($e->getMessage());
  315. }
  316. if (false === $result) {
  317. $this->error(__('No rows were updated'));
  318. }
  319. $this->success();
  320. }
  321. /**
  322. * 删除
  323. */
  324. public function del($ids = null)
  325. {
  326. $row = $this->model->get($ids);
  327. if (!$row) {
  328. $this->error(__('No Results were found'));
  329. }
  330. $adminIds = $this->getDataLimitAdminIds();
  331. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  332. $this->error(__('You have no permission'));
  333. }
  334. $result = false;
  335. try {
  336. $result = $row->allowField(true)->save(['is_delete'=>1]);
  337. } catch (ValidateException|PDOException|Exception $e) {
  338. $this->error($e->getMessage());
  339. }
  340. if (false === $result) {
  341. $this->error(__('No rows were updated'));
  342. }
  343. $this->success();
  344. }
  345. /**
  346. * 批量更新
  347. * @param $ids
  348. * @return void
  349. */
  350. public function multi($ids = null)
  351. {
  352. if (false === $this->request->isPost()) {
  353. $this->error(__('Invalid parameters'));
  354. }
  355. $ids = $ids ?: $this->request->post('ids');
  356. if (empty($ids)) {
  357. $this->error(__('Parameter %s can not be empty', 'ids'));
  358. }
  359. if (false === $this->request->has('params')) {
  360. $this->error(__('No rows were updated'));
  361. }
  362. parse_str($this->request->post('params'), $values);
  363. //$values = $this->auth->isSuperAdmin() ? $values : array_intersect_key($values, array_flip(is_array($this->multiFields) ? $this->multiFields : explode(',', $this->multiFields)));
  364. if (empty($values)) {
  365. $this->error(__('You have no permission'));
  366. }
  367. $adminIds = $this->getDataLimitAdminIds();
  368. if (is_array($adminIds)) {
  369. $this->model->where($this->dataLimitField, 'in', $adminIds);
  370. }
  371. $count = 0;
  372. Db::startTrans();
  373. try {
  374. $row = $this->model->where($this->model->getPk(), '=', $ids)->find();
  375. $key = key($values); //修改key
  376. if($key == 'is_agent'){
  377. //默认开启:设置伞下当前会员所属代理id
  378. $map['agent_id'] = $values[$key] == 0? $row->agent_id: $ids;
  379. //伞下/代理是自己, 是agent_id = 自己user_id吗
  380. $this->model->where('id','IN', UsersPath::where('parent_id', $ids)->column('user_id'))
  381. ->where('is_agent', 0)
  382. ->update($map);
  383. }
  384. $count = $row->allowField(true)->isUpdate(true)->save($values);
  385. Db::commit();
  386. } catch (PDOException|Exception $e) {
  387. Db::rollback();
  388. $this->error($e->getMessage());
  389. }
  390. if ($count) {
  391. $this->success();
  392. }
  393. $this->error(__('No rows were updated'));
  394. }
  395. }