| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436 |
- <?php
- namespace app\admin\controller\user;
- use think\Db;
- use Exception;
- use fast\Random;
- use think\Hook;
- use app\common\model\UsersPath;
- use app\common\model\MoneyLog;
- use think\exception\PDOException;
- use app\common\controller\Backend;
- use think\exception\ValidateException;
- /**
- * 会员管理
- *
- * @icon fa fa-user
- */
- class User extends Backend
- {
- protected $relationSearch = true;
- protected $searchFields = 'id,mobile';
- /**
- * @var \app\admin\model\Users
- */
- protected $model = null;
- public function _initialize()
- {
- parent::_initialize();
- $this->model = model('Users');
- $this->assign('statusList', $this->model->getStatusList());
- $this->assign('typeList', $this->model->getTypeList());
-
- }
- /**
- * 查看
- */
- public function index()
- {
- //设置过滤方法
- $this->request->filter(['strip_tags', 'trim']);
- if ($this->request->isAjax()) {
- //如果发送的来源是Selectpage,则转发到Selectpage
- if ($this->request->request('keyField')) {
- return $this->selectpage();
- }
- list($where, $sort, $order, $offset, $limit) = $this->buildparams();
- $list = $this->model->with('parent,agent')
- ->where($where)->where('users.is_delete', 0)
- ->order($sort, $order)
- ->paginate($limit);
- $in = model('MoneyIn');
- $out = model('MoneyOut');
- foreach ($list as &$item) {
- $item->recharge = $in::where('status', 200)->where('user_id', $item->id)->sum('amount');
- $item->withdraw = $out::where('status', 200)->where('user_id', $item->id)->sum('amount');
- }
- $result = array("total" => $list->total(), "rows" => $list->items());
- return json($result);
- }
- return $this->view->fetch();
- }
- /**
- * 余额
- * @param $ids
- */
- public function balance($ids = null)
- {
- $row = $this->model->get($ids);
- if (!$row) {
- $this->error(__('No Results were found'));
- }
- $adminIds = $this->getDataLimitAdminIds();
- if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
- $this->error(__('You have no permission'));
- }
- if (false === $this->request->isPost()) {
- $this->view->assign('row', $row);
- return $this->view->fetch();
- }
- $params = $this->request->post('row/a');
- if (empty($params)) {
- $this->error(__('Parameter %s can not be empty', ''));
- }
- $params = $this->preExcludeFields($params);
- $result = false;
- Db::startTrans();
- try {
-
- $amount = $params['type'] == 1 ?-$params['amount']:$params['amount'];
- (new MoneyLog())->change($params['id'], $amount, MoneyLog::SystemChange, $this->auth->id, '后台操作');
- Db::commit();
- } catch (ValidateException|PDOException|Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- $this->success();
- }
- /**
- * 卡单
- */
- public function cardslip($ids = null)
- {
- $row = $this->model->get($ids);
- if (!$row) {
- $this->error(__('No Results were found'));
- }
- $adminIds = $this->getDataLimitAdminIds();
- if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
- $this->error(__('You have no permission'));
- }
- if (false === $this->request->isPost()) {
- $this->view->assign('row', $row);
- return $this->view->fetch();
- }
- $params = $this->request->post('row/a');
- if (empty($params)) {
- $this->error(__('Parameter %s can not be empty', ''));
- }
- $params = $this->preExcludeFields($params);
- $result = false;
- Db::startTrans();
- try {
-
- $result = $row->allowField(true)->save(['limit_task'=>$params['limit_task']]);
- Db::commit();
- } catch (ValidateException|PDOException|Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- if (false === $result) {
- $this->error(__('No rows were updated'));
- }
- $this->success();
- }
- /**
- * 收款
- * @param $ids
- */
- public function collection($ids = null)
- {
- $row = $this->model->get($ids);
- if (!$row) {
- $this->error(__('No Results were found'));
- }
- $adminIds = $this->getDataLimitAdminIds();
- if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
- $this->error(__('You have no permission'));
- }
- if (false === $this->request->isPost()) {
- $bank = json_decode($row->bank_info, true);
- $row->account_name = $bank['account_name']??'';
- $row->bank_name = $bank['bank_name']??'';
- $row->bank_card = $bank['bank_card']??'';
- $this->view->assign('row', $row);
- return $this->view->fetch();
- }
- $params = $this->request->post('row/a');
- if (empty($params)) {
- $this->error(__('Parameter %s can not be empty', ''));
- }
- $params = $this->preExcludeFields($params);
- $result = false;
- Db::startTrans();
- try {
- $addr = $params['usdt_address'];
- unset($params['id'],$params['usdt_address']);
- $result = $row->allowField(true)->save(['usdt_address'=> $addr, 'bank_info'=>json_encode($params)]);
- Db::commit();
- } catch (ValidateException|PDOException|Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- if (false === $result) {
- $this->error(__('No rows were updated'));
- }
- $this->success();
- }
- /**
- * 清零
- */
- public function clear($ids = null)
- {
- $row = $this->model->get($ids);
- if (!$row) {
- $this->error(__('No Results were found'));
- }
- $adminIds = $this->getDataLimitAdminIds();
- if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
- $this->error(__('You have no permission'));
- }
- $result = false;
- try {
- $result = $row->allowField(true)->save(['task_num'=>0]);
- } catch (ValidateException|PDOException|Exception $e) {
- $this->error($e->getMessage());
- }
- if (false === $result) {
- $this->error(__('No rows were updated'));
- }
- $this->success();
- }
- /**
- * 添加
- */
- public function add()
- {
- if (false === $this->request->isPost()) {
- return $this->view->fetch();
- }
- $params = $this->request->post('row/a');
- if (empty($params)) {
- $this->error(__('Parameter %s can not be empty', ''));
- }
- $params = $this->preExcludeFields($params);
- if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
- $params[$this->dataLimitField] = $this->auth->id;
- }
- if ($params['mobile'] && \app\common\model\Users::getByCodeAndMobile($params['code'], $params['mobile'])) {
- $this->error(__('Mobile already exist', ''));
- }
- $parent_info = $this->model::getByParentInfo($params['parent_id']);
- if(empty($parent_info)) $this->error('不存在上级ID');
-
- $ip = request()->ip();
- $time = time();
- $user_data = [
- 'mobile' => $params['mobile'],
- 'code' => $params['code'],
- 'login_pwd'=> $params['login_pwd'],
- 'parent_id'=> $params['parent_id'],
- 'agent_id' => $parent_info['agent_id'],
- 'user_type'=> 1,//真人
- ];
- if($parent_info['is_agent']) $user_data['agent_id'] = $parent_info['id'];
-
- $user_data = array_merge($user_data, [
- 'nickname' => preg_match("/^1[3-9]{1}\d{9}$/", $params['mobile']) ? substr_replace($params['mobile'], '****', 3, 4) : $params['mobile'],
- 'salt' => Random::alnum(),
- 'join_time' => $time,
- 'login_ip' => $ip,
- 'login_time'=> $time,
- ]);
- $user_data['login_pwd'] = $this->auth->getEncryptPassword($params['login_pwd'], $user_data['salt']);
-
- $result = false;
- Db::startTrans();
- try {
- $result = $user = $this->model::create($user_data, true);
- $_user = $this->model::get($user->id);
- // 创建网体
- (new UsersPath())->createPath($user->id, $parent_info['id']);
- //上级人数+1
- $this->model->where('id', $parent_info['id'])->setInc('team_num');
- //设置Token
- //注册成功的事件
- Hook::listen("user_register_successed", $_user, $user_data);
- Db::commit();
- } catch (ValidateException|PDOException|Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- if ($result === false) {
- $this->error(__('No rows were inserted'));
- }
- $this->success();
- }
- /**
- * 编辑
- */
- public function edit($ids = null)
- {
- $row = $this->model->get($ids);
- if (!$row) {
- $this->error(__('No Results were found'));
- }
- $adminIds = $this->getDataLimitAdminIds();
- if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
- $this->error(__('You have no permission'));
- }
- if (false === $this->request->isPost()) {
- $this->view->assign('row', $row);
- return $this->view->fetch();
- }
- $params = $this->request->post('row/a');
- if (empty($params)) {
- $this->error(__('Parameter %s can not be empty', ''));
- }
- $params = $this->preExcludeFields($params);
- $result = false;
- Db::startTrans();
- try {
- //是否采用模型验证
- if ($this->modelValidate) {
- $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
- $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
- $row->validateFailException()->validate($validate);
- }
- if(!empty($params['login_pwd'])){
- $params['salt'] = Random::alnum();
- $params['login_pwd'] = $this->auth->getEncryptPassword($params['login_pwd'], $params['salt']);
- }else{
- unset($params['login_pwd']);
- }
- if(!empty($params['fund_pwd'])){
- $params['fund_pwd'] = md5($params['fund_pwd']);
- }else{
- unset($params['fund_pwd']);
- }
- //修改代理
- if($row->agent_id !=$params['agent_id'] && empty($row->is_agent)) {
- //伞下不是代理/代理是他的
- $this->model->where('id','IN', UsersPath::where('parent_id', $ids)->column('user_id'))
- ->where('is_agent', 0)
- ->update(['agent_id'=>$params['agent_id']]);
- }
-
- $result = $row->allowField(true)->save($params);
- Db::commit();
- } catch (ValidateException|PDOException|Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- if (false === $result) {
- $this->error(__('No rows were updated'));
- }
- $this->success();
- }
-
- /**
- * 删除
- */
- public function del($ids = null)
- {
- $row = $this->model->get($ids);
- if (!$row) {
- $this->error(__('No Results were found'));
- }
- $adminIds = $this->getDataLimitAdminIds();
- if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
- $this->error(__('You have no permission'));
- }
- $result = false;
- try {
- $result = $row->allowField(true)->save(['is_delete'=>1]);
- } catch (ValidateException|PDOException|Exception $e) {
- $this->error($e->getMessage());
- }
- if (false === $result) {
- $this->error(__('No rows were updated'));
- }
- $this->success();
- }
- /**
- * 批量更新
- * @param $ids
- * @return void
- */
- public function multi($ids = null)
- {
- if (false === $this->request->isPost()) {
- $this->error(__('Invalid parameters'));
- }
- $ids = $ids ?: $this->request->post('ids');
- if (empty($ids)) {
- $this->error(__('Parameter %s can not be empty', 'ids'));
- }
- if (false === $this->request->has('params')) {
- $this->error(__('No rows were updated'));
- }
- parse_str($this->request->post('params'), $values);
- //$values = $this->auth->isSuperAdmin() ? $values : array_intersect_key($values, array_flip(is_array($this->multiFields) ? $this->multiFields : explode(',', $this->multiFields)));
- if (empty($values)) {
- $this->error(__('You have no permission'));
- }
- $adminIds = $this->getDataLimitAdminIds();
- if (is_array($adminIds)) {
- $this->model->where($this->dataLimitField, 'in', $adminIds);
- }
- $count = 0;
- Db::startTrans();
- try {
- $row = $this->model->where($this->model->getPk(), '=', $ids)->find();
- $key = key($values); //修改key
- if($key == 'is_agent'){
- //默认开启:设置伞下当前会员所属代理id
- $map['agent_id'] = $values[$key] == 0? $row->agent_id: $ids;
- //伞下/代理是自己, 是agent_id = 自己user_id吗
- $this->model->where('id','IN', UsersPath::where('parent_id', $ids)->column('user_id'))
- ->where('is_agent', 0)
- ->update($map);
- }
- $count = $row->allowField(true)->isUpdate(true)->save($values);
- Db::commit();
- } catch (PDOException|Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- if ($count) {
- $this->success();
- }
- $this->error(__('No rows were updated'));
- }
- }
|