User.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. <?php
  2. namespace app\admin\controller\user;
  3. use think\Db;
  4. use Exception;
  5. use fast\Random;
  6. use app\admin\model\UsersPath;
  7. use app\common\model\MoneyLog;
  8. use think\exception\DbException;
  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. * @return string
  64. * @throws DbException
  65. * @throws \think\Exception
  66. */
  67. public function balance($ids = null)
  68. {
  69. $row = $this->model->get($ids);
  70. if (!$row) {
  71. $this->error(__('No Results were found'));
  72. }
  73. $adminIds = $this->getDataLimitAdminIds();
  74. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  75. $this->error(__('You have no permission'));
  76. }
  77. if (false === $this->request->isPost()) {
  78. $this->view->assign('row', $row);
  79. return $this->view->fetch();
  80. }
  81. $params = $this->request->post('row/a');
  82. if (empty($params)) {
  83. $this->error(__('Parameter %s can not be empty', ''));
  84. }
  85. $params = $this->preExcludeFields($params);
  86. $result = false;
  87. Db::startTrans();
  88. try {
  89. $amount = $params['type'] == 1 ?-$params['amount']:$params['amount'];
  90. (new MoneyLog())->change($params['id'], $amount, MoneyLog::SystemChange, $this->auth->id, '后台操作');
  91. Db::commit();
  92. } catch (ValidateException|PDOException|Exception $e) {
  93. Db::rollback();
  94. $this->error($e->getMessage());
  95. }
  96. $this->success();
  97. }
  98. /**
  99. * 卡单
  100. * @param $ids
  101. * @return string
  102. * @throws DbException
  103. * @throws \think\Exception
  104. */
  105. public function cardslip($ids = null)
  106. {
  107. $row = $this->model->get($ids);
  108. if (!$row) {
  109. $this->error(__('No Results were found'));
  110. }
  111. $adminIds = $this->getDataLimitAdminIds();
  112. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  113. $this->error(__('You have no permission'));
  114. }
  115. if (false === $this->request->isPost()) {
  116. // $task = json_decode($row->limit_task, true);
  117. // $row->which_start = $task['which_start']??'';
  118. // $row->min_amount = $task['min_amount']??'';
  119. // $row->max_amount = $task['max_amount']??'';
  120. // $row->income_multiple= $task['income_multiple']??'';
  121. $this->view->assign('row', $row);
  122. return $this->view->fetch();
  123. }
  124. $params = $this->request->post('row/a');
  125. if (empty($params)) {
  126. $this->error(__('Parameter %s can not be empty', ''));
  127. }
  128. $params = $this->preExcludeFields($params);
  129. $result = false;
  130. Db::startTrans();
  131. try {
  132. $result = $row->allowField(true)->save(['limit_task'=>$params['limit_task']]);
  133. Db::commit();
  134. } catch (ValidateException|PDOException|Exception $e) {
  135. Db::rollback();
  136. $this->error($e->getMessage());
  137. }
  138. if (false === $result) {
  139. $this->error(__('No rows were updated'));
  140. }
  141. $this->success();
  142. }
  143. /**
  144. * 收款
  145. * @param $ids
  146. * @return string
  147. * @throws DbException
  148. * @throws \think\Exception
  149. */
  150. public function collection($ids = null)
  151. {
  152. $row = $this->model->get($ids);
  153. if (!$row) {
  154. $this->error(__('No Results were found'));
  155. }
  156. $adminIds = $this->getDataLimitAdminIds();
  157. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  158. $this->error(__('You have no permission'));
  159. }
  160. if (false === $this->request->isPost()) {
  161. $bank = json_decode($row->bank_info, true);
  162. $row->account_name = $bank['account_name']??'';
  163. $row->bank_name = $bank['bank_name']??'';
  164. $row->bank_card = $bank['bank_card']??'';
  165. $this->view->assign('row', $row);
  166. return $this->view->fetch();
  167. }
  168. $params = $this->request->post('row/a');
  169. if (empty($params)) {
  170. $this->error(__('Parameter %s can not be empty', ''));
  171. }
  172. $params = $this->preExcludeFields($params);
  173. $result = false;
  174. Db::startTrans();
  175. try {
  176. $addr = $params['usdt_address'];
  177. unset($params['id'],$params['usdt_address']);
  178. $result = $row->allowField(true)->save(['usdt_address'=> $addr, 'bank_info'=>json_encode($params)]);
  179. Db::commit();
  180. } catch (ValidateException|PDOException|Exception $e) {
  181. Db::rollback();
  182. $this->error($e->getMessage());
  183. }
  184. if (false === $result) {
  185. $this->error(__('No rows were updated'));
  186. }
  187. $this->success();
  188. }
  189. /**
  190. * 清零
  191. * @param $ids
  192. * @return string
  193. * @throws DbException
  194. * @throws \think\Exception
  195. */
  196. public function clear($ids = null)
  197. {
  198. $row = $this->model->get($ids);
  199. if (!$row) {
  200. $this->error(__('No Results were found'));
  201. }
  202. $adminIds = $this->getDataLimitAdminIds();
  203. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  204. $this->error(__('You have no permission'));
  205. }
  206. $result = false;
  207. try {
  208. $result = $row->allowField(true)->save(['task_num'=>0]);
  209. } catch (ValidateException|PDOException|Exception $e) {
  210. $this->error($e->getMessage());
  211. }
  212. if (false === $result) {
  213. $this->error(__('No rows were updated'));
  214. }
  215. $this->success();
  216. }
  217. /**
  218. * 编辑
  219. *
  220. * @param $ids
  221. * @return string
  222. * @throws DbException
  223. * @throws \think\Exception
  224. */
  225. public function edit($ids = null)
  226. {
  227. $row = $this->model->get($ids);
  228. if (!$row) {
  229. $this->error(__('No Results were found'));
  230. }
  231. $adminIds = $this->getDataLimitAdminIds();
  232. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  233. $this->error(__('You have no permission'));
  234. }
  235. if (false === $this->request->isPost()) {
  236. $this->view->assign('row', $row);
  237. return $this->view->fetch();
  238. }
  239. $params = $this->request->post('row/a');
  240. if (empty($params)) {
  241. $this->error(__('Parameter %s can not be empty', ''));
  242. }
  243. $params = $this->preExcludeFields($params);
  244. $result = false;
  245. Db::startTrans();
  246. try {
  247. //是否采用模型验证
  248. if ($this->modelValidate) {
  249. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  250. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  251. $row->validateFailException()->validate($validate);
  252. }
  253. if(!empty($params['login_pwd'])){
  254. $params['salt'] = Random::alnum();
  255. $params['login_pwd'] = $this->auth->getEncryptPassword($params['login_pwd'], $params['salt']);
  256. }else{
  257. unset($params['login_pwd']);
  258. }
  259. if(!empty($params['fund_pwd'])){
  260. $params['fund_pwd'] = md5($params['fund_pwd']);
  261. }else{
  262. unset($params['fund_pwd']);
  263. }
  264. //修改代理
  265. if($row->agent_id !=$params['agent_id'] && empty($row->is_agent)) {
  266. //伞下不是代理/代理是他的
  267. $this->model->where('id','IN', UsersPath::where('parent_id', $ids)->column('user_id'))
  268. ->where('is_agent', 0)
  269. ->update(['agent_id'=>$params['agent_id']]);
  270. }
  271. $result = $row->allowField(true)->save($params);
  272. Db::commit();
  273. } catch (ValidateException|PDOException|Exception $e) {
  274. Db::rollback();
  275. $this->error($e->getMessage());
  276. }
  277. if (false === $result) {
  278. $this->error(__('No rows were updated'));
  279. }
  280. $this->success();
  281. }
  282. /**
  283. * 删除
  284. *
  285. * @param $ids
  286. * @return void
  287. * @throws DbException
  288. * @throws DataNotFoundException
  289. * @throws ModelNotFoundException
  290. */
  291. public function del($ids = null)
  292. {
  293. $row = $this->model->get($ids);
  294. if (!$row) {
  295. $this->error(__('No Results were found'));
  296. }
  297. $adminIds = $this->getDataLimitAdminIds();
  298. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  299. $this->error(__('You have no permission'));
  300. }
  301. $result = false;
  302. try {
  303. $result = $row->allowField(true)->save(['is_delete'=>1]);
  304. } catch (ValidateException|PDOException|Exception $e) {
  305. $this->error($e->getMessage());
  306. }
  307. if (false === $result) {
  308. $this->error(__('No rows were updated'));
  309. }
  310. $this->success();
  311. }
  312. /**
  313. * 批量更新
  314. * @param $ids
  315. * @return void
  316. */
  317. public function multi($ids = null)
  318. {
  319. if (false === $this->request->isPost()) {
  320. $this->error(__('Invalid parameters'));
  321. }
  322. $ids = $ids ?: $this->request->post('ids');
  323. if (empty($ids)) {
  324. $this->error(__('Parameter %s can not be empty', 'ids'));
  325. }
  326. if (false === $this->request->has('params')) {
  327. $this->error(__('No rows were updated'));
  328. }
  329. parse_str($this->request->post('params'), $values);
  330. //$values = $this->auth->isSuperAdmin() ? $values : array_intersect_key($values, array_flip(is_array($this->multiFields) ? $this->multiFields : explode(',', $this->multiFields)));
  331. if (empty($values)) {
  332. $this->error(__('You have no permission'));
  333. }
  334. $adminIds = $this->getDataLimitAdminIds();
  335. if (is_array($adminIds)) {
  336. $this->model->where($this->dataLimitField, 'in', $adminIds);
  337. }
  338. $count = 0;
  339. Db::startTrans();
  340. try {
  341. $row = $this->model->where($this->model->getPk(), '=', $ids)->find();
  342. $key = key($values); //修改key
  343. if($key == 'is_agent'){
  344. //默认开启:设置伞下当前会员所属代理id
  345. $map['agent_id'] = $values[$key] == 0? $row->agent_id: $ids;
  346. //伞下/代理是自己, 是agent_id = 自己user_id吗
  347. $this->model->where('id','IN', UsersPath::where('parent_id', $ids)->column('user_id'))
  348. ->where('is_agent', 0)
  349. ->update($map);
  350. }
  351. $count = $row->allowField(true)->isUpdate(true)->save($values);
  352. Db::commit();
  353. } catch (PDOException|Exception $e) {
  354. Db::rollback();
  355. $this->error($e->getMessage());
  356. }
  357. if ($count) {
  358. $this->success();
  359. }
  360. $this->error(__('No rows were updated'));
  361. }
  362. }