User.php 11 KB

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