123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041 |
- <?php
- namespace app\admin\controller;
- use app\common\util\ExcelUtil;
- use think\Db;
- use think\Exception;
- use tools\Phptree;
- class User extends Auth
- {
- public function index(){
- if(request()->isAjax()){
- //分页参数
- $length = input('rows',10,'intval'); //每页条数
- $page = input('page',1,'intval'); //第几页
- $start = ($page - 1) * $length; //分页开始位置
- //排序
- $sortRow = input('sidx','u.id','trim'); //排序列
- $sort = input('sord','desc','trim'); //排序方式
- $order = $sortRow.' '.$sort;
- $title = input('title','','trim');
- if($title){
- $map[] = ['u.account|u.real_name|u.mobile','like','%'.$title.'%'];
- }
- $enable = input('enable','','trim');
- if($enable != ''){
- $map[] = ['u.enable','=',$enable];
- }
- $depId = input('depId','','trim');
- if($depId != ''){
- // $map[] = ['ud.dep_id','=',$depId];
- $uids = Db::name('user_dep')->where('dep_id',$depId)->column('user_id');
- $map[] = ['u.id','in',$uids];
- }
- $rolesId = input('rolesId','','trim');
- if($rolesId != ''){
- $userRolesId = Db::name('user_roles')
- ->where('roles_id',$rolesId)
- ->column('user_id');
- $map[] = ['u.id','in',$userRolesId];
- }
- $map[] = ['u.del','=',0];
- $map[] = ['u.type','=',0];
- $map[] = ['uo.org_id','=',$this->orgId];
- $map= empty($map) ? true: $map;
- //数据查询
- // $lists = db('user')
- // ->alias('u')
- // ->join('user_dep ud','u.id=ud.user_id')
- // ->join('user_roles ur','u.id=ur.user_id')
- // ->join('user_org uo','u.id=uo.user_id')
- // ->field('u.*,ud.dep_id,ur.roles_id,uo.org_id')
- // ->where($map)
- // ->limit($start,$length)
- // ->order(['u.sort'=>'asc','u.id'=>'desc'])
- // ->select();
- // foreach ($lists as $k=>$v){
- // $lists[$k]['dep_name'] = Db::name('dep')->where('id',$v['dep_id'])->value('title');
- // $lists[$k]['roles_name'] = Db::name('roles')->where('id',$v['roles_id'])->value('name');
- // }
- $lists = db('user')
- ->alias('u')
- ->join('user_org uo','u.id=uo.user_id')
- ->where($map)
- ->field('u.*,uo.org_id')
- ->limit($start,$length)
- ->order(['u.sort'=>'asc','u.id'=>'desc'])
- ->select();
- foreach ($lists as $k=>$v){
- $lists[$k]['dep_name'] = Db::name('user_dep')
- ->alias('ud')
- ->join('dep d','d.id=ud.dep_id')
- ->where('ud.user_id',$v['id'])
- ->where('d.org_id',$v['org_id'])
- ->value('d.title');
- $lists[$k]['roles_name'] = Db::name('user_roles')
- ->alias('ur')
- ->join('roles r','r.id=ur.roles_id')
- ->where('ur.user_id',$v['id'])
- ->where('r.org_id',$v['org_id'])
- ->value('r.name');
- }
- //数据返回
- $totalCount = db('user')
- ->alias('u')
- ->join('user_org uo','u.id=uo.user_id')
- ->where($map)
- ->count();
- $totalPage = ceil($totalCount/$length);
- $result['page'] = $page;
- $result['total'] = $totalPage;
- $result['records'] = $totalCount;
- $result['rows'] = $lists;
- return json($result);
- }else{
- $deps = model('Dep')->getList();
- $this->assign('deps',$deps);
- $roles = model('Roles')->getList();
- $this->assign('roles',$roles);
- return $this->fetch();
- }
- }
- public function wxlist(){
- if(request()->isAjax()){
- //分页参数
- $length = input('rows',10,'intval'); //每页条数
- $page = input('page',1,'intval'); //第几页
- $start = ($page - 1) * $length; //分页开始位置
- //排序
- $sortRow = input('sidx','u.id','trim'); //排序列
- $sort = input('sord','desc','trim'); //排序方式
- $order = $sortRow.' '.$sort;
- $title = input('title','','trim');
- if($title){
- $map[] = ['u.account|u.real_name|u.mobile','like','%'.$title.'%'];
- }
- $enable = input('enable','','trim');
- if($enable != ''){
- $map[] = ['u.enable','=',$enable];
- }
- $depId = input('depId','','trim');
- if($depId != ''){
- $map[] = ['ud.dep_id','=',$depId];
- }
- $rolesId = input('rolesId','','trim');
- if($rolesId != ''){
- $map[] = ['ur.roles_id','=',$rolesId];
- }
- $map[] = ['u.del','=',0];
- $map[] = ['u.type','=',2];
- $map[] = ['uo.org_id','=',$this->orgId];
- $map= empty($map) ? true: $map;
- //数据查询
- $lists = db('user')
- ->alias('u')
- ->join('user_dep ud','u.id=ud.user_id')
- ->join('user_roles ur','u.id=ur.user_id','left')
- ->join('user_org uo','u.id=uo.user_id')
- ->field('u.*,ud.dep_id,ur.roles_id,uo.org_id')
- ->where($map)
- ->limit($start,$length)
- ->order(['u.sort'=>'asc','u.id'=>'desc'])
- ->select();
- foreach ($lists as $k=>$v){
- $lists[$k]['dep_name'] = Db::name('dep')->where('id',$v['dep_id'])->value('title');
- $lists[$k]['roles_name'] = Db::name('roles')->where('id',$v['roles_id'])->value('name');
- }
- //数据返回
- $totalCount = db('user')
- ->alias('u')
- ->join('user_dep ud','ud.user_id = u.id')
- ->join('user_roles ur','ur.user_id = u.id')
- ->join('user_org uo','uo.user_id = u.id')
- ->where($map)->count();
- $totalPage = ceil($totalCount/$length);
- $result['page'] = $page;
- $result['total'] = $totalPage;
- $result['records'] = $totalCount;
- $result['rows'] = $lists;
- return json($result);
- }else{
- $deps = model('Dep')->getList();
- $this->assign('deps',$deps);
- $roles = model('Roles')->getList();
- $this->assign('roles',$roles);
- return $this->fetch();
- }
- }
- /**
- * 新增/编辑
- */
- public function add($id=0){
- if(request()->isPost()){
- $res = model('User')->addUser();
- if($res){
- $this->success('操作成功',url('index'));
- }else{
- $this->error(model('User')->getError());
- }
- }else{
- if($id){
- $info = db('user')
- ->alias('u')
- ->join('user_dep ud','ud.user_id = u.id','left')
- ->join('user_roles ur','ur.user_id = u.id','left')
- ->field('u.*,ud.dep_id,ur.roles_id')
- ->where('u.id',$id)->find();
- $role = Db::name('roles')
- ->where('id',$info['roles_id'])
- ->find();
- $info['is_bx'] = ($role['parent_id']==7 || $info['roles_id']==7)?1:0;
- $this->assign('info',$info);
- }
- $deps = model('Dep')->getList();
- $this->assign('deps',$deps);
- $address = (new \app\common\model\Address())->getListByType(2);
- $roles = model('Roles')->getList();
- $this->assign('roles',$roles);
- $this->assign('address',$address);
- $orderType =model('OrderType')->list();
- $this->assign('orderType',$orderType);
- return $this->fetch();
- }
- }
- /**
- * 新增/编辑
- */
- public function wxadd($id=0){
- if(request()->isPost()){
- $res = model('User')->wxaddUser();
- if($res){
- $this->success('操作成功',url('index'));
- }else{
- $this->error(model('User')->getError());
- }
- }else{
- if($id){
- $info = db('user')
- ->alias('u')
- ->join('user_dep ud','ud.user_id = u.id')
- ->join('user_roles ur','ur.user_id = u.id','left')
- ->field('u.*,ud.dep_id,ur.roles_id')
- ->where('u.id',$id)->find();
- $this->assign('info',$info);
- }
- $deps = model('Dep')->getList();
- $this->assign('deps',$deps);
- // $address = (new \app\common\model\Address())->getListByType(2);
- $roles = model('Roles')->getList();
- $this->assign('roles',$roles);
- // $this->assign('address',$address);
- return $this->fetch();
- }
- }
- /**
- * 新增/编辑
- */
- public function wxzadd($id=0){
- if(request()->isPost()){
- $res = model('User')->addUser();
- if($res){
- $this->success('操作成功',url('index'));
- }else{
- $this->error(model('User')->getError());
- }
- }else{
- if($id){
- $info = db('user')
- ->alias('u')
- ->join('user_dep ud','ud.user_id = u.id')
- ->join('user_roles ur','ur.user_id = u.id','left')
- ->field('u.*,ud.dep_id,ur.roles_id')
- ->where('u.id',$id)->find();
- $this->assign('info',$info);
- }
- $deps = model('Dep')->getList();
- $this->assign('deps',$deps);
- $address = (new \app\common\model\Address())->getListByType(2);
- $roles = model('Roles')->getList();
- $this->assign('roles',$roles);
- $this->assign('address',$address);
- return $this->fetch();
- }
- }
- /**
- * 删除记录
- * @param int $id
- */
- public function del($id=0){
- if(!$id){
- $this->error('参数错误');
- }
- $res = db('user')->where('id',$id)->setField('del',1);
- if($res){
- $this->success('删除成功');
- }else{
- $this->error('删除失败');
- }
- }
- /**
- * 改变字段值
- * @param int $fv
- * @param string $fn
- * @param int $fv
- */
- public function changeField($id=0,$fn='',$fv=0){
- if(!$fn||!$id){
- $this->error('参数错误');
- }
- $res = db('user')->where('id',$id)->setField($fn,$fv);
- if($res){
- $this->success('操作成功');
- }else{
- $this->error('操作失败');
- }
- }
- public function company(){
- if(request()->isAjax()){
- //分页参数
- $length = input('rows',10,'intval'); //每页条数
- $page = input('page',1,'intval'); //第几页
- $start = ($page - 1) * $length; //分页开始位置
- //排序
- $sortRow = input('sidx','u.id','trim'); //排序列
- $sort = input('sord','desc','trim'); //排序方式
- $order = $sortRow.' '.$sort;
- $title = input('title','','trim');
- if($title){
- $map[] = ['u.account|u.real_name|u.mobile','like','%'.$title.'%'];
- }
- $enable = input('enable','','trim');
- if($enable != ''){
- $map[] = ['u.enable','=',$enable];
- }
- $rolesId = input('rolesId','','trim');
- if($rolesId != ''){
- $map[] = ['ur.roles_id','=',$rolesId];
- }
- $map[] = ['u.del','=',0];
- $map[] = ['u.type','=',1];
- $map= empty($map) ? true: $map;
- //数据查询
- $lists = db('user')
- ->alias('u')
- ->join('user_roles ur','ur.user_id = u.id')
- ->field('u.*,ur.roles_id')
- ->where($map)
- ->limit($start,$length)
- ->order($order)->select();
- foreach ($lists as $k=>$v){
- $lists[$k]['roles_name'] = Db::name('roles')->where('id',$v['roles_id'])->value('name');
- }
- //数据返回
- $totalCount = db('user')
- ->alias('u')
- ->join('user_roles ur','ur.user_id = u.id')
- ->where($map)->count();
- $totalPage = ceil($totalCount/$length);
- $result['page'] = $page;
- $result['total'] = $totalPage;
- $result['records'] = $totalCount;
- $result['rows'] = $lists;
- return json($result);
- }else{
- $roles = model('Roles')->getCommonList(2);
- $this->assign('roles',$roles);
- $type = Db::name('user')->where('id',$this->userId)->value('type');
- $this->assign('type',$type);
- return $this->fetch();
- }
- }
- /**
- * 新增/编辑
- */
- public function addcompany($id=0){
- if(request()->isPost()){
- $res = model('User')->addCompany();
- if($res){
- $this->success('操作成功',url('index'));
- }else{
- $this->error(model('User')->getError());
- }
- }else{
- if($id){
- $info = db('user')
- ->alias('u')
- ->join('user_roles ur','ur.user_id = u.id')
- ->field('u.*,ur.roles_id')
- ->where('u.id',$id)->find();
- if($info){
- $orgs = $info['orgs']?explode(',',$info['orgs']):[];
- $info['orgs'] = [];
- if($orgs){
- $ids = [];
- foreach ($orgs as $k=>$v){
- $t = $this->getAllLastId($v); //获取所有上级id 数组是倒序
- array_unshift($t,(int)$v); //把组织插入数组的开头
- $c = array_reverse($t); //数组反转
- $ids[] = $c;
- }
- $info['orgs'] = $ids;
- }
- }
- $this->assign('info',$info);
- }
- $orgs = model('Org')->getAllOrgs();
- $this->assign('orgs',$orgs);
- $tree = $this->orgTree();
- $this->assign('orgs',$tree);
- $roles = model('Roles')->getCommonList(2);
- $this->assign('roles',$roles);
- return $this->fetch();
- }
- }
- //获取所有上级id
- public function getAllLastId($id,$data=[]){
- $pids = DB::name('org')->where('id',$id)->value('parent_id');
- if($pids > 0){
- $data[] = $pids;
- $data = $this->getAllLastId($pids,$data);
- }
- if(count($data)>0){
- return $data;
- }else{
- return [];
- }
- }
- public function orgTree(){
- $lists = Db::name('org')
- ->field('id as value,name as label,parent_id')
- ->where('del',0)
- ->where('enable',1)
- ->order('sort asc,id asc')
- ->select();
- if (empty($lists)) {
- return array();
- }
- $tree = Phptree::makeTree(($lists), array(
- 'primary_key'=>'value',
- 'parent_key'=>'parent_id',
- 'expanded' => true
- ));
- return $tree;
- }
- public function modifyPass(){
- if(request()->isAjax()){
- $data = [
- 'oldpass' => input('oldpass','','trim'),
- 'pass' => input('pass','','trim'),
- 'pass_confirm' => input('pass_confirm','','trim'),
- 'id' => $this->userId
- ];
- $ret = model('User')->modifyPass($data);
- if(!$ret){
- $this->error(model('User')->getError());
- }
- // 清除登录信息,重新登录
- session('user_auth',null);
- session('user_auth_sign',null);
- $this->success('操作成功');
- }else{
- return $this->fetch();
- }
- }
- /*
- * 考勤设置*/
- public function set($id=0){
- if(request()->isPost()){
- $data = request()->post();
- if(!$data['kq_name']){
- $this->error('考勤姓名不能为空');
- }
- if(!isset($data['kq_img']) && empty($data['kq_img'])){
- $this->error('考勤图片不能为空');
- }
- $uid = $data['id'];
- unset($data['id']);
- db('user')->where('id',$uid)->update($data);
- $this->success('操作成功',url('index'));
- }else{
- if($id>0){
- $info = db('user')->where('id',$id)->find();
- $this->assign('info',$info);
- }
- }
- return $this->fetch();
- }
- /*
- * 重置设备编号*/
- public function reset($id=0){
- if($id < 1){
- $this->error('参数错误');
- }
- db('user')->where('id',$id)->update(['device_sn'=>'']);
- $this->success('重置成功',url('index'));
- }
- /**
- * 排序
- * @param int $id
- * @param int $sort
- */
- public function changeSort($id=0,$sort=0){
- if($id<0||$sort<0){
- $this->error('参数错误');
- }
- $res = db('user')->where('id',$id)->setField('sort',$sort);
- if($res){
- $this->success('操作成功');
- }else{
- $this->error('操作失败');
- }
- }
- public function batchSort(){
- $data = input('data','','trim');
- if(!$data){
- $this->error('参数错误');
- }
- $data = json_decode($data,true);
- if(!$data){
- $this->error('参数错误');
- }
- Db::startTrans();
- try{
- foreach ($data as $k=>$v){
- Db::name('user')->where('id',$v['id'])->setField('sort',$v['sort']);
- }
- Db::commit();
- }catch (Exception $e){
- Db::rollback();
- $this->error('操作失败');
- }
- $this->success('操作成功');
- }
- public function copyRoles(){
- if(request()->isPost()){
- $orgId = input('orgId',0);
- if($orgId < 1 ){
- $this->error('请选择复制的项目');
- }
- $rolesList = Db::name('roles')
- ->where('org_id',$orgId)
- ->where('enable',1)
- ->where('del',0)
- ->select();
- foreach ($rolesList as $k=>$v){
- $rolesInfo = Db::name('roles')
- ->where('org_id',$this->orgId)
- ->where('del',0)
- ->where('parent_id',$v['parent_id'])
- ->where('name',$v['name'])
- ->find();
- if(!$rolesInfo){
- $data = [
- 'name'=>$v['name'],
- 'content'=>$v['content'],
- 'org_id'=>$this->orgId,
- 'parent_id'=>$v['parent_id'],
- 'work_type_mode'=>$v['work_type_mode'],
- 'type'=>$v['type'],
- 'auths'=>$v['auths'],
- 'appauths'=>$v['appauths'],
- 'level'=>$v['level'],
- ];
- Db::name('roles')->insert($data);
- }
- }
- $this->success('操作成功',url('index'));
- }else{
- $orgList = Db::name('org')
- ->where('del',0)
- ->where('type',2)
- ->select();
- foreach ($orgList as $k=>$v){
- $orgList[$k]['title'] = $v['name'];
- }
- $this->assign('orgList',$orgList);
- return $this->fetch();
- }
- }
- public function workCard($id){
- $info = Db::name('user')->where('id',$id)->find();
- if (!$info) {
- exit('数据不存在');
- }
- $info['dep'] = Db::name('user_dep')
- ->alias('ud')
- ->join('dep d','d.id = ud.dep_id')
- ->where('ud.user_id',$info['id'])
- ->where('d.del',0)
- ->value('d.title');
- $orgName = Db::name('org')->where('id',$this->orgId)->value('name');
- $code = get_qrcode_str('work_card', $id);
- $this->assign('code',$code);
- $this->assign('info',$info);
- $this->assign('orgName',$orgName);
- return $this->fetch();
- }
- public function import(){
- return $this->fetch();
- }
- /**
- * 下载点模板
- */
- public function downloadtem(){
- set_time_limit(0);
- ini_set("memory_limit","512M");
- $header = [
- ['title' => '账号', 'name' => 'account','width'=>'30'],
- ['title' => '姓名', 'name' => 'real_name','width'=>'30'],
- ['title' => '手机号', 'name' => 'mobile','width'=>'30'],
- ['title' => '部门ID(默认0)', 'name' => 'depId','width'=>'30'],
- ['title' => '角色ID(默认0)', 'name' => 'rolesId','width'=>'30'],
- ];
- $filename = '用户导入';
- ExcelUtil::export($filename,$header,[]);
- }
- /**
- * 导入
- */
- public function importexcel_bak(){
- set_time_limit(0);
- ini_set("memory_limit", -1);
- ob_flush();//清空缓存
- flush();//刷新缓存
- $orgId = $this->orgId;
- if(request()->file()) {
- $file = request()->file('file');
- // 移动到框架应用根目录/uploads/ 目录下
- $info = $file->validate([ 'size'=>50*1024*1024,'ext'=>'xls,xlsx' ])
- ->move(env('root_path') . 'public' . DIRECTORY_SEPARATOR . 'uploads'. DIRECTORY_SEPARATOR . 'temp');
- if(!$info){
- exit('文件上传失败');
- }
- $img = './uploads/temp/' . $info->getSaveName();
- $filePath = str_replace('\\', '/', $img);
- $excelArray = ExcelUtil::read($filePath,
- ['id','real_name','mobile','dep','roles']
- );
- unset($excelArray[0]);
- if(empty($excelArray)){
- exit('文件内容为空');
- }
- $succ = [];
- foreach ($excelArray as $k => $v) {
- $v['account'] = $v['mobile'];
- if(!$v['real_name']){
- echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,姓名为空,未导入</font><br />";
- continue;
- }
- if(!$v['mobile']){
- echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,手机号为空,未导入</font><br />";
- continue;
- }
- $v['id'] = 0;
- $result = validate('User')->check($v, [], 'bachAdd');
- if (true !== $result) {
- echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,".validate('User')->getError()."</font><br />";
- continue;
- }
- $v['depId'] = 130;
- $rolesId = 145;
- // $rolesId = Db::name('roles')
- // ->where('name', $v['roles'])
- // ->where('org_id', $this->orgId)
- // ->value('id');
- // if(empty($rolesId)){
- // echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,角色不存在</font><br />";
- // continue;
- // }
- $rData = [
- 'account'=>$v['mobile'],
- 'real_name'=>$v['real_name'],
- 'mobile'=>$v['mobile'],
- 'ip'=>request()->ip(),
- 'password'=>password_hash('123456', PASSWORD_DEFAULT),
- 'create_time'=>getTime()
- ];
- $v['rolesId'] = $rolesId;
- $ret = Db::name('user')->insertGetId($rData);
- if($ret){
- $succ[] = $ret;
- $ids = $ret;
- Db::name('user_org')->insert(['user_id' => $ids, 'org_id' => $this->orgId]);
- if(!empty($v['depId'])){
- Db::name('user_dep')->insert(['user_id' => $ids, 'dep_id' => $v['depId']]);
- }
- if(!empty($v['rolesId'])){
- Db::name('user_roles')->insert(['user_id' => $ids, 'roles_id' => $v['rolesId']]);
- }
- echo "<font color=\"green\" style='margin-left:20px;font-size: 17px'>第".($k+1)."行,导入成功</font><br />";
- }else{
- echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,导入失败</font><br />";
- }
- }
- }else{
- exit('请上传文件');
- }
- }
- public function importexcel(){
- set_time_limit(0);
- ini_set("memory_limit", -1);
- ob_flush();//清空缓存
- flush();//刷新缓存
- $orgId = $this->orgId;
- if(request()->file()) {
- $file = request()->file('file');
- // 移动到框架应用根目录/uploads/ 目录下
- $info = $file->validate([ 'size'=>50*1024*1024,'ext'=>'xls,xlsx' ])
- ->move(env('root_path') . 'public' . DIRECTORY_SEPARATOR . 'uploads'. DIRECTORY_SEPARATOR . 'temp');
- if(!$info){
- exit('文件上传失败');
- }
- $img = './uploads/temp/' . $info->getSaveName();
- $filePath = str_replace('\\', '/', $img);
- $excelArray = ExcelUtil::read($filePath,
- ['account','real_name','mobile','depId','rolesId']
- );
- if(empty($excelArray)){
- exit('文件内容为空');
- }
- $succ = [];
- foreach ($excelArray as $k => $v) {
- if(!$v['account']){
- echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,账号为空,未导入</font><br />";
- continue;
- }
- if(!$v['real_name']){
- echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,姓名为空,未导入</font><br />";
- continue;
- }
- if(!$v['mobile']){
- echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,手机号为空,未导入</font><br />";
- continue;
- }
- $v['id'] = 0;
- $result = validate('User')->check($v, [], 'bachAdd');
- if (true !== $result) {
- echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,".validate('User')->getError()."</font><br />";
- continue;
- }
- if(!empty($v['depId'])){
- $checkDep = Db::name('dep')
- ->where('org_id',$this->orgId)
- ->where('id',$v['depId'])
- ->where('del',0)
- ->find();
- if(!$checkDep){
- echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,部门不存在</font><br />";
- continue;
- }
- }
- if(!empty($v['rolesId'])){
- $checkRoles = Db::name('roles')
- ->where('org_id',$this->orgId)
- ->where('id',$v['rolesId'])
- ->where('del',0)
- ->where('parent_id','>',0)
- ->find();
- if(!$checkRoles){
- echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,角色不存在</font><br />";
- continue;
- }
- }
- $rData = [
- 'account'=>$v['account'],
- 'real_name'=>$v['real_name'],
- 'mobile'=>$v['mobile'],
- 'ip'=>request()->ip(),
- 'password'=>password_hash('123456', PASSWORD_DEFAULT),
- 'create_time'=>getTime()
- ];
- $ret = Db::name('user')->insertGetId($rData);
- if($ret){
- $succ[] = $ret;
- $ids = $ret;
- Db::name('user_org')->insert(['user_id' => $ids, 'org_id' => $this->orgId]);
- if(!empty($v['depId'])){
- Db::name('user_dep')->insert(['user_id' => $ids, 'dep_id' => $v['depId']]);
- }
- if(!empty($v['rolesId'])){
- Db::name('user_roles')->insert(['user_id' => $ids, 'roles_id' => $v['rolesId']]);
- }
- $rolesParentId = Db::name('roles')
- ->where('id', $v['rolesId'])
- ->value('parent_id');
- if ($rolesParentId == 11) {
- Db::name('worker')
- ->where('user_id',$ids)
- ->delete();
- $a = [
- 'user_id' => $ids
- ];
- Db::name('worker')
- ->insertGetId($a);
- }
- echo "<font color=\"green\" style='margin-left:20px;font-size: 17px'>第".($k+1)."行,导入成功</font><br />";
- }else{
- echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,导入失败</font><br />";
- }
- }
- }else{
- exit('请上传文件');
- }
- }
- public function getRole(){
- $id = input('id','');
- $info = Db::name('roles')
- ->where('id',$id)
- ->find();
- $data = [
- 'show'=>($info['parent_id']==7 || $id==7)?1:0,
- ];
- $this->success('操作成功','',$data);
- }
- public function importexcel1(){
- set_time_limit(0);
- ini_set("memory_limit", -1);
- ob_flush();//清空缓存
- flush();//刷新缓存
- $orgId = $this->orgId;
- if(request()->file()) {
- $file = request()->file('file');
- // 移动到框架应用根目录/uploads/ 目录下
- $info = $file->validate([ 'size'=>50*1024*1024,'ext'=>'xls,xlsx' ])
- ->move(env('root_path') . 'public' . DIRECTORY_SEPARATOR . 'uploads'. DIRECTORY_SEPARATOR . 'temp');
- if(!$info){
- exit('文件上传失败');
- }
- $img = './uploads/temp/' . $info->getSaveName();
- $filePath = str_replace('\\', '/', $img);
- $excelArray = ExcelUtil::read($filePath,
- ['','real_name','account','dep','roles']
- );
- if(empty($excelArray)){
- exit('文件内容为空');
- }
- $succ = [];
- foreach ($excelArray as $k => $v) {
- if($k < 1){
- continue;
- }
- if(!$v['account']){
- echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,账号为空,未导入</font><br />";
- continue;
- }
- if(!$v['real_name']){
- echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,姓名为空,未导入</font><br />";
- continue;
- }
- if(!$v['dep']){
- echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,部门为空,未导入</font><br />";
- continue;
- }
- $depId = Db::name('dep')
- ->where('org_id',$this->orgId)
- ->where('title',$v['dep'])
- ->where('del',0)
- ->value('id');
- if($depId < 1){
- $depId = Db::name('dep')
- ->insertGetId([
- 'title'=>$v['dep'],
- 'org_id'=>$orgId,
- 'del'=>0,
- 'sort'=>50,
- 'create_time'=>date('Y-m-d H:i:s')
- ]);
- }
- if(!$v['roles']){
- echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,角色为空,未导入</font><br />";
- continue;
- }
- $rPid = 0; //父级角色id
- $gl = ['']; //管理
- $zh = ['项目经理','项目副经理','经理','文员','后勤主任','公共事务主管','主任','电梯主管','班长','后勤主任','后勤干事','项目副经理/文员','电梯导乘','送检工','保障处助理','主管','总务科科长','分管物业,工会主席']; //综合 pid=4
- $bj = ['保洁主管','保洁班长','保洁工']; // pid=8
- $wx = ['维修主管','维修工']; // pid=7
- $kh = ['']; // 客户 pid=3
- $ys = ['运送主管','运送班长','垃圾运送']; //运送 pid=6
- if(in_array($v['roles'],$zh)){
- $rPid = 4;
- }
- if(in_array($v['roles'],$bj)){
- $rPid = 8;
- }
- if(in_array($v['roles'],$wx)){
- $rPid = 7;
- }
- if(in_array($v['roles'],$ys)){
- $rPid = 6;
- }
- $rolesId = Db::name('roles')
- ->where('org_id',$this->orgId)
- ->where('name',$v['roles'])
- ->where('del',0)
- ->where('parent_id',$rPid)
- ->value('id');
- if(!$rolesId){
- $rolesId = Db::name('roles')
- ->insertGetId([
- 'name'=>$v['roles'],
- 'org_id'=>$orgId,
- 'parent_id'=>$rPid,
- 'type'=>1,
- 'del'=>0,
- 'sort'=>0,
- ]);
- }
- $rData = [
- 'account'=>$v['account'],
- 'real_name'=>$v['real_name'],
- 'mobile'=>$v['account'],
- 'ip'=>request()->ip(),
- 'password'=>password_hash('123456', PASSWORD_DEFAULT),
- 'create_time'=>getTime()
- ];
- $ret = Db::name('user')->insertGetId($rData);
- if($ret){
- $succ[] = $ret;
- $ids = $ret;
- Db::name('user_org')->insert(['user_id' => $ids, 'org_id' => $this->orgId]);
- if(!empty($depId)){
- Db::name('user_dep')->insert(['user_id' => $ids, 'dep_id' => $depId]);
- }
- if(!empty($rolesId)){
- Db::name('user_roles')->insert(['user_id' => $ids, 'roles_id' => $rolesId]);
- }
- // $rolesParentId = Db::name('roles')
- // ->where('id', $v['rolesId'])
- // ->value('parent_id');
- // if ($rolesParentId == 11) {
- // Db::name('worker')
- // ->where('user_id',$ids)
- // ->delete();
- // $a = [
- // 'user_id' => $ids
- // ];
- // Db::name('worker')
- // ->insertGetId($a);
- // }
- // echo "<font color=\"green\" style='margin-left:20px;font-size: 17px'>第".($k+1)."行,导入成功</font><br />";
- }else{
- echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,导入失败</font><br />";
- }
- }
- }else{
- exit('请上传文件');
- }
- }
- }
|