User.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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
  46. ->where($where)
  47. ->order($sort, $order)
  48. ->paginate($limit);
  49. $in = model('MoneyIn');
  50. $out = model('MoneyOut');
  51. $path= model('UsersPath');
  52. foreach ($list as &$item) {
  53. $item->parent_name = $this->model::where('id', $item->parent_id)->value('mobile');
  54. $item->agent = $this->model
  55. ->where('id', '=',
  56. $path->where('user_id', $item->id)->where('is_agent', 1)->order('distance', 'desc')->value('parent_id')
  57. )
  58. ->value('mobile');
  59. $item->recharge = $in::where('status', 200)->where('user_id', $item->id)->sum('amount');
  60. $item->withdraw = $out::where('status', 200)->where('user_id', $item->id)->sum('amount');
  61. }
  62. $result = array("total" => $list->total(), "rows" => $list->items());
  63. return json($result);
  64. }
  65. return $this->view->fetch();
  66. }
  67. /**
  68. * 余额
  69. * @param $ids
  70. * @return string
  71. * @throws DbException
  72. * @throws \think\Exception
  73. */
  74. public function balance($ids = null)
  75. {
  76. $row = $this->model->get($ids);
  77. if (!$row) {
  78. $this->error(__('No Results were found'));
  79. }
  80. $adminIds = $this->getDataLimitAdminIds();
  81. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  82. $this->error(__('You have no permission'));
  83. }
  84. if (false === $this->request->isPost()) {
  85. $this->view->assign('row', $row);
  86. return $this->view->fetch();
  87. }
  88. $params = $this->request->post('row/a');
  89. if (empty($params)) {
  90. $this->error(__('Parameter %s can not be empty', ''));
  91. }
  92. $params = $this->preExcludeFields($params);
  93. $result = false;
  94. Db::startTrans();
  95. try {
  96. $balance = build_amount_compute($params['type'], $params['amount'], $row->balance);
  97. //操作记录
  98. MoneyLog::create(['user_id'=> $params['id'],
  99. 'from_id'=> $this->auth->id,
  100. 'amount'=> $params['amount'],
  101. 'balance'=> $balance
  102. ]);
  103. $result = $row->allowField(true)->save(['balance'=>$balance]);
  104. Db::commit();
  105. } catch (ValidateException|PDOException|Exception $e) {
  106. Db::rollback();
  107. $this->error($e->getMessage());
  108. }
  109. if (false === $result) {
  110. $this->error(__('No rows were updated'));
  111. }
  112. $this->success();
  113. }
  114. /**
  115. * 卡单
  116. * @param $ids
  117. * @return string
  118. * @throws DbException
  119. * @throws \think\Exception
  120. */
  121. public function cardslip($ids = null)
  122. {
  123. $row = $this->model->get($ids);
  124. if (!$row) {
  125. $this->error(__('No Results were found'));
  126. }
  127. $adminIds = $this->getDataLimitAdminIds();
  128. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  129. $this->error(__('You have no permission'));
  130. }
  131. if (false === $this->request->isPost()) {
  132. $task = json_decode($row->limit_task, true);
  133. $row->which_start = $task['which_start']??'';
  134. $row->min_amount = $task['min_amount']??'';
  135. $row->max_amount = $task['max_amount']??'';
  136. $row->income_multiple= $task['income_multiple']??'';
  137. $this->view->assign('row', $row);
  138. return $this->view->fetch();
  139. }
  140. $params = $this->request->post('row/a');
  141. if (empty($params)) {
  142. $this->error(__('Parameter %s can not be empty', ''));
  143. }
  144. $params = $this->preExcludeFields($params);
  145. $result = false;
  146. Db::startTrans();
  147. try {
  148. unset($params['id']);
  149. $result = $row->allowField(true)->save(['limit_task'=>json_encode($params)]);
  150. Db::commit();
  151. } catch (ValidateException|PDOException|Exception $e) {
  152. Db::rollback();
  153. $this->error($e->getMessage());
  154. }
  155. if (false === $result) {
  156. $this->error(__('No rows were updated'));
  157. }
  158. $this->success();
  159. }
  160. /**
  161. * 收款
  162. * @param $ids
  163. * @return string
  164. * @throws DbException
  165. * @throws \think\Exception
  166. */
  167. public function collection($ids = null)
  168. {
  169. $row = $this->model->get($ids);
  170. if (!$row) {
  171. $this->error(__('No Results were found'));
  172. }
  173. $adminIds = $this->getDataLimitAdminIds();
  174. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  175. $this->error(__('You have no permission'));
  176. }
  177. if (false === $this->request->isPost()) {
  178. $bank = json_decode($row->bank_info, true);
  179. $row->real_name = $bank['real_name']??'';
  180. $row->bank_name = $bank['bank_name']??'';
  181. $row->bank_card = $bank['bank_card']??'';
  182. $this->view->assign('row', $row);
  183. return $this->view->fetch();
  184. }
  185. $params = $this->request->post('row/a');
  186. if (empty($params)) {
  187. $this->error(__('Parameter %s can not be empty', ''));
  188. }
  189. $params = $this->preExcludeFields($params);
  190. $result = false;
  191. Db::startTrans();
  192. try {
  193. $addr = $params['usdt_address'];
  194. unset($params['id'],$params['usdt_address']);
  195. $result = $row->allowField(true)->save(['usdt_address'=> $addr, 'bank_info'=>json_encode($params)]);
  196. Db::commit();
  197. } catch (ValidateException|PDOException|Exception $e) {
  198. Db::rollback();
  199. $this->error($e->getMessage());
  200. }
  201. if (false === $result) {
  202. $this->error(__('No rows were updated'));
  203. }
  204. $this->success();
  205. }
  206. /**
  207. * 清零
  208. * @param $ids
  209. * @return string
  210. * @throws DbException
  211. * @throws \think\Exception
  212. */
  213. public function clear($ids = null)
  214. {
  215. $row = $this->model->get($ids);
  216. if (!$row) {
  217. $this->error(__('No Results were found'));
  218. }
  219. $adminIds = $this->getDataLimitAdminIds();
  220. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  221. $this->error(__('You have no permission'));
  222. }
  223. $result = false;
  224. try {
  225. $result = $row->allowField(true)->save(['task_num'=>0]);
  226. } catch (ValidateException|PDOException|Exception $e) {
  227. $this->error($e->getMessage());
  228. }
  229. if (false === $result) {
  230. $this->error(__('No rows were updated'));
  231. }
  232. $this->success();
  233. }
  234. /**
  235. * 编辑
  236. *
  237. * @param $ids
  238. * @return string
  239. * @throws DbException
  240. * @throws \think\Exception
  241. */
  242. public function edit($ids = null)
  243. {
  244. $row = $this->model->get($ids);
  245. if (!$row) {
  246. $this->error(__('No Results were found'));
  247. }
  248. $adminIds = $this->getDataLimitAdminIds();
  249. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  250. $this->error(__('You have no permission'));
  251. }
  252. if (false === $this->request->isPost()) {
  253. $this->view->assign('row', $row);
  254. return $this->view->fetch();
  255. }
  256. $params = $this->request->post('row/a');
  257. if (empty($params)) {
  258. $this->error(__('Parameter %s can not be empty', ''));
  259. }
  260. $params = $this->preExcludeFields($params);
  261. $result = false;
  262. Db::startTrans();
  263. try {
  264. //是否采用模型验证
  265. if ($this->modelValidate) {
  266. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  267. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  268. $row->validateFailException()->validate($validate);
  269. }
  270. if(!empty($params['login_pwd'])){
  271. $params['salt'] = Random::alnum();
  272. $params['fund_pwd'] = $this->auth->getEncryptPassword($params['fund_pwd'], $params['salt']);
  273. }else{
  274. unset($params['login_pwd']);
  275. }
  276. if(!empty($params['fund_pwd'])){
  277. $params['fund_pwd'] = md5($params['fund_pwd']);
  278. }else{
  279. unset($params['fund_pwd']);
  280. }
  281. $result = $row->allowField(true)->save($params);
  282. Db::commit();
  283. } catch (ValidateException|PDOException|Exception $e) {
  284. Db::rollback();
  285. $this->error($e->getMessage());
  286. }
  287. if (false === $result) {
  288. $this->error(__('No rows were updated'));
  289. }
  290. $this->success();
  291. }
  292. /**
  293. * 删除
  294. *
  295. * @param $ids
  296. * @return void
  297. * @throws DbException
  298. * @throws DataNotFoundException
  299. * @throws ModelNotFoundException
  300. */
  301. public function del($ids = null)
  302. {
  303. $row = $this->model->get($ids);
  304. if (!$row) {
  305. $this->error(__('No Results were found'));
  306. }
  307. $adminIds = $this->getDataLimitAdminIds();
  308. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  309. $this->error(__('You have no permission'));
  310. }
  311. $result = false;
  312. try {
  313. $result = $row->allowField(true)->save(['is_delete'=>1]);
  314. } catch (ValidateException|PDOException|Exception $e) {
  315. $this->error($e->getMessage());
  316. }
  317. if (false === $result) {
  318. $this->error(__('No rows were updated'));
  319. }
  320. $this->success();
  321. }
  322. }