User.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. $result = $row->allowField(true)->save(['limit_task'=>$params['limit_task']]);
  132. Db::commit();
  133. } catch (ValidateException|PDOException|Exception $e) {
  134. Db::rollback();
  135. $this->error($e->getMessage());
  136. }
  137. if (false === $result) {
  138. $this->error(__('No rows were updated'));
  139. }
  140. $this->success();
  141. }
  142. /**
  143. * 收款
  144. * @param $ids
  145. * @return string
  146. * @throws DbException
  147. * @throws \think\Exception
  148. */
  149. public function collection($ids = null)
  150. {
  151. $row = $this->model->get($ids);
  152. if (!$row) {
  153. $this->error(__('No Results were found'));
  154. }
  155. $adminIds = $this->getDataLimitAdminIds();
  156. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  157. $this->error(__('You have no permission'));
  158. }
  159. if (false === $this->request->isPost()) {
  160. $bank = json_decode($row->bank_info, true);
  161. $row->real_name = $bank['real_name']??'';
  162. $row->bank_name = $bank['bank_name']??'';
  163. $row->bank_card = $bank['bank_card']??'';
  164. $this->view->assign('row', $row);
  165. return $this->view->fetch();
  166. }
  167. $params = $this->request->post('row/a');
  168. if (empty($params)) {
  169. $this->error(__('Parameter %s can not be empty', ''));
  170. }
  171. $params = $this->preExcludeFields($params);
  172. $result = false;
  173. Db::startTrans();
  174. try {
  175. $addr = $params['usdt_address'];
  176. unset($params['id'],$params['usdt_address']);
  177. $result = $row->allowField(true)->save(['usdt_address'=> $addr, 'bank_info'=>json_encode($params)]);
  178. Db::commit();
  179. } catch (ValidateException|PDOException|Exception $e) {
  180. Db::rollback();
  181. $this->error($e->getMessage());
  182. }
  183. if (false === $result) {
  184. $this->error(__('No rows were updated'));
  185. }
  186. $this->success();
  187. }
  188. /**
  189. * 清零
  190. * @param $ids
  191. * @return string
  192. * @throws DbException
  193. * @throws \think\Exception
  194. */
  195. public function clear($ids = null)
  196. {
  197. $row = $this->model->get($ids);
  198. if (!$row) {
  199. $this->error(__('No Results were found'));
  200. }
  201. $adminIds = $this->getDataLimitAdminIds();
  202. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  203. $this->error(__('You have no permission'));
  204. }
  205. $result = false;
  206. try {
  207. $result = $row->allowField(true)->save(['task_num'=>0]);
  208. } catch (ValidateException|PDOException|Exception $e) {
  209. $this->error($e->getMessage());
  210. }
  211. if (false === $result) {
  212. $this->error(__('No rows were updated'));
  213. }
  214. $this->success();
  215. }
  216. /**
  217. * 编辑
  218. *
  219. * @param $ids
  220. * @return string
  221. * @throws DbException
  222. * @throws \think\Exception
  223. */
  224. public function edit($ids = null)
  225. {
  226. $row = $this->model->get($ids);
  227. if (!$row) {
  228. $this->error(__('No Results were found'));
  229. }
  230. $adminIds = $this->getDataLimitAdminIds();
  231. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  232. $this->error(__('You have no permission'));
  233. }
  234. if (false === $this->request->isPost()) {
  235. $this->view->assign('row', $row);
  236. return $this->view->fetch();
  237. }
  238. $params = $this->request->post('row/a');
  239. if (empty($params)) {
  240. $this->error(__('Parameter %s can not be empty', ''));
  241. }
  242. $params = $this->preExcludeFields($params);
  243. $result = false;
  244. Db::startTrans();
  245. try {
  246. //是否采用模型验证
  247. if ($this->modelValidate) {
  248. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  249. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  250. $row->validateFailException()->validate($validate);
  251. }
  252. if(!empty($params['login_pwd'])){
  253. $params['salt'] = Random::alnum();
  254. $params['login_pwd'] = $this->auth->getEncryptPassword($params['login_pwd'], $params['salt']);
  255. }else{
  256. unset($params['login_pwd']);
  257. }
  258. if(!empty($params['fund_pwd'])){
  259. $params['fund_pwd'] = md5($params['fund_pwd']);
  260. }else{
  261. unset($params['fund_pwd']);
  262. }
  263. $result = $row->allowField(true)->save($params);
  264. Db::commit();
  265. } catch (ValidateException|PDOException|Exception $e) {
  266. Db::rollback();
  267. $this->error($e->getMessage());
  268. }
  269. if (false === $result) {
  270. $this->error(__('No rows were updated'));
  271. }
  272. $this->success();
  273. }
  274. /**
  275. * 删除
  276. *
  277. * @param $ids
  278. * @return void
  279. * @throws DbException
  280. * @throws DataNotFoundException
  281. * @throws ModelNotFoundException
  282. */
  283. public function del($ids = null)
  284. {
  285. $row = $this->model->get($ids);
  286. if (!$row) {
  287. $this->error(__('No Results were found'));
  288. }
  289. $adminIds = $this->getDataLimitAdminIds();
  290. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  291. $this->error(__('You have no permission'));
  292. }
  293. $result = false;
  294. try {
  295. $result = $row->allowField(true)->save(['is_delete'=>1]);
  296. } catch (ValidateException|PDOException|Exception $e) {
  297. $this->error($e->getMessage());
  298. }
  299. if (false === $result) {
  300. $this->error(__('No rows were updated'));
  301. }
  302. $this->success();
  303. }
  304. /**
  305. * 批量更新
  306. * @param $ids
  307. * @return void
  308. */
  309. public function multi($ids = null)
  310. {
  311. if (false === $this->request->isPost()) {
  312. $this->error(__('Invalid parameters'));
  313. }
  314. $ids = $ids ?: $this->request->post('ids');
  315. if (empty($ids)) {
  316. $this->error(__('Parameter %s can not be empty', 'ids'));
  317. }
  318. if (false === $this->request->has('params')) {
  319. $this->error(__('No rows were updated'));
  320. }
  321. parse_str($this->request->post('params'), $values);
  322. //$values = $this->auth->isSuperAdmin() ? $values : array_intersect_key($values, array_flip(is_array($this->multiFields) ? $this->multiFields : explode(',', $this->multiFields)));
  323. if (empty($values)) {
  324. $this->error(__('You have no permission'));
  325. }
  326. $adminIds = $this->getDataLimitAdminIds();
  327. if (is_array($adminIds)) {
  328. $this->model->where($this->dataLimitField, 'in', $adminIds);
  329. }
  330. $count = 0;
  331. Db::startTrans();
  332. try {
  333. $row = $this->model->where($this->model->getPk(), '=', $ids)->find();
  334. $key = key($values); //修改key
  335. if($key == 'is_agent'){
  336. //默认开启:设置伞下当前会员所属代理id
  337. $map['agent_id'] = $values[$key] == 0? $row->agent_id: $ids;
  338. $this->model->where('id','IN', UsersPath::where('parent_id', $ids)->column('user_id'))->update($map);
  339. }
  340. $count = $row->allowField(true)->isUpdate(true)->save($values);
  341. Db::commit();
  342. } catch (PDOException|Exception $e) {
  343. Db::rollback();
  344. $this->error($e->getMessage());
  345. }
  346. if ($count) {
  347. $this->success();
  348. }
  349. $this->error(__('No rows were updated'));
  350. }
  351. }