User.php 10 KB

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