User.php 11 KB

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