User.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. (new MoneyLog())->change($params['id'], $params['amount'], MoneyLog::SystemChange, $this->auth->id, '后台操作');
  90. Db::commit();
  91. } catch (ValidateException|PDOException|Exception $e) {
  92. Db::rollback();
  93. $this->error($e->getMessage());
  94. }
  95. $this->success();
  96. }
  97. /**
  98. * 卡单
  99. * @param $ids
  100. * @return string
  101. * @throws DbException
  102. * @throws \think\Exception
  103. */
  104. public function cardslip($ids = null)
  105. {
  106. $row = $this->model->get($ids);
  107. if (!$row) {
  108. $this->error(__('No Results were found'));
  109. }
  110. $adminIds = $this->getDataLimitAdminIds();
  111. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  112. $this->error(__('You have no permission'));
  113. }
  114. if (false === $this->request->isPost()) {
  115. $task = json_decode($row->limit_task, true);
  116. $row->which_start = $task['which_start']??'';
  117. $row->min_amount = $task['min_amount']??'';
  118. $row->max_amount = $task['max_amount']??'';
  119. $row->income_multiple= $task['income_multiple']??'';
  120. $this->view->assign('row', $row);
  121. return $this->view->fetch();
  122. }
  123. $params = $this->request->post('row/a');
  124. if (empty($params)) {
  125. $this->error(__('Parameter %s can not be empty', ''));
  126. }
  127. $params = $this->preExcludeFields($params);
  128. $result = false;
  129. Db::startTrans();
  130. try {
  131. unset($params['id']);
  132. $result = $row->allowField(true)->save(['limit_task'=>json_encode($params)]);
  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->real_name = $bank['real_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. $result = $row->allowField(true)->save($params);
  265. Db::commit();
  266. } catch (ValidateException|PDOException|Exception $e) {
  267. Db::rollback();
  268. $this->error($e->getMessage());
  269. }
  270. if (false === $result) {
  271. $this->error(__('No rows were updated'));
  272. }
  273. $this->success();
  274. }
  275. /**
  276. * 删除
  277. *
  278. * @param $ids
  279. * @return void
  280. * @throws DbException
  281. * @throws DataNotFoundException
  282. * @throws ModelNotFoundException
  283. */
  284. public function del($ids = null)
  285. {
  286. $row = $this->model->get($ids);
  287. if (!$row) {
  288. $this->error(__('No Results were found'));
  289. }
  290. $adminIds = $this->getDataLimitAdminIds();
  291. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  292. $this->error(__('You have no permission'));
  293. }
  294. $result = false;
  295. try {
  296. $result = $row->allowField(true)->save(['is_delete'=>1]);
  297. } catch (ValidateException|PDOException|Exception $e) {
  298. $this->error($e->getMessage());
  299. }
  300. if (false === $result) {
  301. $this->error(__('No rows were updated'));
  302. }
  303. $this->success();
  304. }
  305. /**
  306. * 批量更新
  307. * @param $ids
  308. * @return void
  309. */
  310. public function multi($ids = null)
  311. {
  312. if (false === $this->request->isPost()) {
  313. $this->error(__('Invalid parameters'));
  314. }
  315. $ids = $ids ?: $this->request->post('ids');
  316. if (empty($ids)) {
  317. $this->error(__('Parameter %s can not be empty', 'ids'));
  318. }
  319. if (false === $this->request->has('params')) {
  320. $this->error(__('No rows were updated'));
  321. }
  322. parse_str($this->request->post('params'), $values);
  323. //$values = $this->auth->isSuperAdmin() ? $values : array_intersect_key($values, array_flip(is_array($this->multiFields) ? $this->multiFields : explode(',', $this->multiFields)));
  324. if (empty($values)) {
  325. $this->error(__('You have no permission'));
  326. }
  327. $adminIds = $this->getDataLimitAdminIds();
  328. if (is_array($adminIds)) {
  329. $this->model->where($this->dataLimitField, 'in', $adminIds);
  330. }
  331. $count = 0;
  332. Db::startTrans();
  333. try {
  334. $row = $this->model->where($this->model->getPk(), '=', $ids)->find();
  335. $key = key($values); //修改key
  336. if($key == 'is_agent'){
  337. //默认开启:设置伞下当前会员所属代理id
  338. $map['agent_id'] = $values[$key] == 0? $row->agent_id: $ids;
  339. $this->model->where('id','IN', UsersPath::where('parent_id', $ids)->column('user_id'))->update($map);
  340. }
  341. $count = $row->allowField(true)->isUpdate(true)->save($values);
  342. Db::commit();
  343. } catch (PDOException|Exception $e) {
  344. Db::rollback();
  345. $this->error($e->getMessage());
  346. }
  347. if ($count) {
  348. $this->success();
  349. }
  350. $this->error(__('No rows were updated'));
  351. }
  352. }