123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472 |
- <?php
- namespace app\admin\controller;
- /**
- * 管理用户
- */
- class User extends Admin{
- protected $User = null;
- protected function _initialize(){
- parent::_initialize();
- $this->User = model('User');
- }
- public function index(){
- $this->assign("meta_title","用户列表");
- return $this->fetch();
- }
- public function load(){
- $where = [];
- $rpid = input('param.rpid');
- if (!empty($rpid)) {
- $where['r.pid'] = $rpid;
- }else{
- $where['r.name'] = ['eq','user'];
- }
- $user = input('param.user');
- if (!empty($user)) {
- $where['u.id|u.name'] = $user;
- }
- $page = input('get.page');
- $limit = input('get.limit');
- $list = $this->User->field('u.*')->alias('u')
- ->join('Role r','r.id = u.role_id')
- ->where($where)->order('u.id desc')->paginate($limit,false,['page'=>$page]);
- $data = [];
- foreach ($list as $key => $value) {
- $data[$key]['id'] = $value['id'];
- $data[$key]['name'] = $value['name'];
- $data[$key]['state'] = $value['state'];
- }
- $this->output(0,'加载成功',$data,$list->total());
- }
- public function add(){
- if($this->request->isPost()){
- $name = input('post.name');
- $user = $this->User->where(['name'=>$name])->find();
- if ($user) {
- return json(['data'=>null,'code'=>1,'msg'=>'用户名已经存在']);
- }
- $role_id = input('post.role_id');
- $where['id'] = $role_id;
- $Role = model('Role');
- $role = $Role->where($where)->find();
- if (!$role) {
- return json(['data'=>null,'code'=>1,'msg'=>'请选择角色']);
- }
- $password = input('post.password');
- if (empty($password)) {
- return json(['data'=>null,'code'=>1,'msg'=>'密码不能为空']);
- }
-
- $this->User->name= $name;
- $salt = getNonceStr(4);
- $this->User->password = md5(md5($password).$salt);
- $this->User->salt = $salt;
- $this->User->role_id = $role_id;
- $result = $this->User->save();
- if (!$result) {
- output(1,'添加失败');
- }
- output(0,'添加成功');
- }else{
- $rpid = input('get.rpid');
- if (empty($rpid)) {
- $where['name'] = 'user';
- }else{
- $where['pid'] = $rpid;
- }
- $Role = model('Role');
- $roles = $Role->where($where)->select();
- $this->assign('roles',$roles);
-
- $this->assign('meta_title','添加用户');
- return $this->fetch();
- }
- }
- public function edit(){
- if ($this->request->isPost()) {
- $id = input('post.id');
- $name = input('post.name');
- if (!empty($name)) {
- $user = $this->User->where(['name'=>$name])->find();
- if ($user && $user['id'] != $id) {
- return json(['data'=>null,'code'=>1,'msg'=>'用户名已经存在']);
- }
- }
- $user = $this->User->where(['id'=>$id])->find();
- if (!$user) {
- return json(['data'=>null,'code'=>1,'msg'=>'参数错误']);
- }
- $role_id = input('post.role_id');
- $where['id'] = $role_id;
- $Role = model('Role');
- $role = $Role->where($where)->find();
- if (!$role) {
- return json(['data'=>null,'code'=>1,'msg'=>'请选择角色']);
- }
- $password = input('post.password');
- if (!empty($password)) {
- $salt = $user['salt'];
- $user->password = md5(md5($password).$salt);
- }
- $oppwd = input('post.oppwd');
- if (!empty($oppwd)) {
- $user->oppwd = $oppwd;
- }
- $user->name= $name;
- $user->role_id = $role_id;
- $result = $user->save();
- if ($result) {
- return json(['data'=>null,'code'=>0,'msg'=>'编辑成功']);
- }
- return json(['data'=>null,'code'=>1,'msg'=>'编辑成功']);
- }else{
- $id = input('get.id');
- $user = $this->User->where(['id'=>$id])->find();
- $this->assign('user',$user);
- $role = $user['role'];
- if ($role['name'] == 'user') {
- $where['name'] = ['eq','user'];
- }else{
- $where['name'] = ['not in',['user','admin']];
- }
- $Role = model('Role');
- $roles = $Role->where($where)->select();
- $this->assign('roles',$roles);
- $id = input('get.id');
- $user = $this->User->where(['id'=>$id])->find();
- $this->assign('user',$user);
- $this->assign('meta_title','编辑用户');
- return $this->fetch();
- }
- }
- public function center(){
- if ($this->request->isPost()) {
- $id = input('post.id');
- $user = $this->User->where(['id'=>$id])->find();
- if (!$user) {
- $this->output(1,'参数错误');
- }
- $center = input('post.center');
- $user->center = $center == 'true'?1:0;
- $result = $user->save();
- if ($result) {
- $this->output(0,'编辑成功');
- }
- $this->output(1,'编辑失败');
- }
- }
- public function active(){
- if ($this->request->isPost()) {
- $id = input('post.id');
- $user = $this->User->where(['id'=>$id])->find();
- if (!$user) {
- $this->output(1,'参数错误');
- }
- $active = input('post.active');
- $user->active = $active == 'true'?1:0;
- $result = $user->save();
- if ($result) {
- $this->output(0,'编辑成功');
- }
- $this->output(1,'编辑失败');
- }
- }
- public function state(){
- if ($this->request->isPost()) {
- $id = input('post.id');
- $user = $this->User->where(['id'=>$id])->find();
- if (!$user) {
- $this->output(1,'参数错误');
- }
- $state = input('post.state');
- $user->state = $state == 'true'?1:0;
- $result = $user->save();
- if ($result) {
- $this->output(0,'编辑成功');
- }
- $this->output(1,'编辑失败');
- }
- }
- public function level(){
- if ($this->request->isPost()) {
- $id = input('post.id');
- $user = $this->User->where(['id'=>$id])->find();
- if (!$user) {
- output(1,'参数错误');
- }
- $UserLevel = model('user.Level');
- $level = input('post.level');
- $data = [];
- for ($i=0; $i <= $level; $i++) {
- $userlevel = $UserLevel->where(['user_id'=>$user['id'],'level'=>$i])->find();
- if (!$userlevel) {
- $data[$i]['user_id'] = $user['id'];
- $data[$i]['level'] = $i;
- }
- }
- $UserLevel->where(['user_id'=>$user['id'],'level'=>['gt',$level]])->delete();
- if (count($data) > 0) {
- $UserLevel->saveAll($data);
- }
- $user->level = $level;
- $result = $user->save();
- if ($result) {
- output(0,'编辑成功',['level_text'=>$user['level_text']]);
- }
- output(1,'编辑失败');
- }
- }
- public function balance(){
- if ($this->request->isPost()) {
- $id = input('post.id');
- $user = $this->User->where(['id'=>$id])->find();
- if (!$user) {
- output(1,'参数错误');
- }
- $balance = input('post.balance');
- $balance = floatval($balance);
- $this->User->startTrans();
- $user['balance'] += $balance;
- $result = $user->save();
- if (!$result) {
- $this->User->rollback();
- $this->output(1,'编辑失败');
- }
- $billdata = [];
- $i = 0;
- $billdata[$i]['user_id'] = $user['id'];
- $billdata[$i]['amount'] = $user['balance'];
- $billdata[$i]['value'] = $balance;
- $billdata[$i]['fee'] = 0;
- $billdata[$i]['type'] = 1;
- $billdata[$i]['account'] = 'balance';
- $Bill = model('Bill');
- $result = $Bill->saveAll($billdata);
- if (!$result) {
- $this->User->rollback();
- $this->output(1,'账单保存失败');
- }
- $this->User->commit();
- $this->output(0,'编辑成功',['balance'=>$user['balance']]);
-
- }
- }
- public function coin(){
- if ($this->request->isPost()) {
- $id = input('post.id');
- $user = $this->User->where(['id'=>$id])->find();
- if (!$user) {
- output(1,'参数错误');
- }
- $coin = input('post.coin');
- $coin = floatval($coin);
- $user['coin'] += $coin;
- $result = $user->save();
- if ($result) {
- output(0,'编辑成功',['coin'=>$user['coin']]);
- }
- output(1,'编辑失败');
- }
- }
- public function points(){
- if ($this->request->isPost()) {
- $id = input('post.id');
- $user = $this->User->where(['id'=>$id])->find();
- if (!$user) {
- $this->output(1,'参数错误');
- }
- $points = input('post.points');
- $points = floatval($points);
- $user['points'] += $points;
- $result = $user->save();
- if (!$result) {
- $this->User->rollback();
- $this->output(1,'编辑失败');
- }
- $billdata = [];
- $i = 0;
- $billdata[$i]['user_id'] = $user['id'];
- $billdata[$i]['amount'] = $user['points'];
- $billdata[$i]['value'] = $points;
- $billdata[$i]['fee'] = 0;
- $billdata[$i]['type'] = 1;
- $billdata[$i]['account'] = 'points';
- $Bill = model('Bill');
- $result = $Bill->saveAll($billdata);
- if (!$result) {
- $this->User->rollback();
- $this->output(1,'账单保存失败');
- }
- $this->User->commit();
- $this->output(0,'编辑成功',['points'=>$user['points']]);
- output(1,'编辑失败');
- }
- }
- public function remark(){
- if ($this->request->isPost()) {
- $id = input('post.id');
- $user = $this->User->where(['id'=>$id])->find();
- if (!$user) {
- output(1,'参数错误');
- }
- $value = input('post.value');
- $user->remark = $value;
- $result = $user->save();
- if ($result) {
- output(0,'编辑成功');
- }
- output(1,'编辑失败');
- }
- }
- public function forbid(){
- if ($this->request->isPost()) {
- $id = input('post.id');
- $user = $this->User->where(['id'=>$id])->find();
- if (!$user) {
- $this->output(1,'参数错误');
- }
- $options = input('options/a','');
- $user->forbid = $options;
- $result = $user->save();
- if (!$result) {
- $this->output(1,'保存失败');
- }
- $this->output(0,'保存成功');
- }else{
- $id = input('get.id');
- $user = $this->User->where(['id'=>$id])->find();
- if (!$user) {
- $this->error('参数错误');
- }
- $this->assign('user',$user);
- $this->assign('meta_title','禁止用户');
- return $this->fetch();
- }
- }
- public function delete(){
- if ($this->request->isPost()) {
- $id = input('post.id');
- $user = $this->User->where(['id'=>$id])->find();
- if (!$user) {
- $this->output(1,'参数错误');
- }
- $Tree = model('Tree');
- $tree = $Tree->where(['user_id'=>$user['id'],'layer'=>1])->find();
- if ($tree) {
- $Tree->where(['node_id'=>$user['id']])->update(['node_id'=>$tree['node_id']]);
- }
- $result = $user->delete();
- if (!$result) {
- $this->output(1,'删除失败');
- }
- $this->output(0,'删除成功');
- }
- }
- public function team(){
- $id = input('get.id');
- $user = $this->User->where(['id'=>$id])->find();
- if (!$user) {
- $this->error('参数错误');
- }
- $this->assign('user',$user);
- $this->assign('meta_title','团队');
- return $this->fetch();
- }
- public function loadusers(){;
- $pid = input('get.pid',0);
- $level = input('get.level',9999);
- $list = $this->User->tree($pid,$level);
- $this->output(0,'加载成功',$list);
- }
- public function fans(){
- $id = input('get.id');
- $user = $this->User->where(['id'=>$id])->find();
- if (!$user) {
- $this->error('参数错误');
- }
- $this->assign('user',$user);
- $this->assign('meta_title','粉丝团');
- return $this->fetch();
- }
- public function loadfans(){;
- $pid = input('get.pid',0);
- $level = input('get.level',9999);
- $list = $this->User->fans($pid,$level);
- $this->output(0,'加载成功',$list);
- }
- public function getinfobyname(){
- $name = input('post.name');
- $user = $this->User->where(['name'=>$name,'state'=>1])->find();
- if ($user) {
- return json(['data'=>$user,'code'=>0,'msg'=>'获取成功']);
- }
- return json(['data'=>null,'code'=>1,'msg'=>'获取失败']);
- }
- public function getinfobyno(){
- $no = input('post.no');
- $user = $this->User->where(['no'=>$no,'state'=>1])->find();
- if ($user) {
- $data['id'] = $user['id'];
- $data['no'] = $user['no'];
- $role = $user['role'];
- $data['role_name'] = $role['name'];
- $company = $user['company'];
- $data['company_cname'] = $company['cname'];
- return json(['data'=>$data,'code'=>0,'msg'=>'获取成功']);
- }
- return json(['data'=>null,'code'=>1,'msg'=>'获取失败']);
- }
- public function getinfobyid(){
- $id = input('post.id');
- $user = $this->User->where(['id'=>$id,'state'=>1])->find();
- if ($user) {
- return json(['data'=>$user,'code'=>0,'msg'=>'获取成功']);
- }
- return json(['data'=>null,'code'=>1,'msg'=>'获取失败']);
- }
- }
|