User.php 11 KB

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