12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115 |
- <?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');
- // }
- $mm = new \app\common\model\User();
- $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');
- $lists[$k]['sex_name'] = isset($mm->sex[$v['sex']])?$mm->sex[$v['sex']]:"";
- }
- //数据返回
- $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 = [];
- if($lists){
- $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' => 'sex_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('Aa123456', 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','sex_name','mobile','depId','rolesId']
- );
- if(empty($excelArray)){
- exit('文件内容为空');
- }
- $succ = [];
- $m = new \app\common\model\User();
- 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;
- }
- }
- $s = array_flip($m->sex);
- $sex = isset($s[$v['sex_name']])?$s[$v['sex_name']]:0;
- $rData = [
- 'account'=>$v['account'],
- 'sex'=>$sex,
- 'real_name'=>$v['real_name'],
- 'mobile'=>$v['mobile'],
- 'ip'=>request()->ip(),
- 'password'=>password_hash('Aa123456', 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;
- }
- // if($v['dep'] == '保洁部' && !in_array($v['roles'],$zh)){
- // $rPid = 8;
- // $v['roles'] = '保洁';
- // }
- // if($v['dep'] == '运送部' && !in_array($v['roles'],$ys)){
- // $rPid = 6;
- // $v['roles'] = '运送';
- // }
- //
- // if($v['dep'] == '导梯部' && !in_array($v['roles'],$zh)){
- // $rPid = 4;
- // $v['roles'] = '综合';
- // }
- $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,
- ]);
- }
- $mobile = strlen($v['account']) == 11 ?$v['account']:'';
- $rData = [
- 'account'=>$v['account'],
- 'real_name'=>$v['real_name'],
- 'mobile'=>$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($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('请上传文件');
- }
- }
- //464
- public function delDepUser(){
- $userIds = Db::name('user')
- ->alias('u')
- ->join('user_dep ud','ud.user_id=u.id')
- ->where('ud.dep_id',464)
- ->where('u.del',0)
- ->where('u.enable',1)
- ->column('u.id');
- $ret = Db::name('user')->where('id','in',$userIds)->setField('del',1);
- echo $ret;
- }
- public function delOrgUser(){
- $userIds = Db::name('user')
- ->alias('u')
- ->join('user_org uo','uo.user_id=u.id')
- ->where('uo.org_id',85)
- ->where('u.del',0)
- ->column('u.id');
- $ret = Db::name('user')->where('id','in',$userIds)->setField('del',1);
- echo $ret;
- }
- public function saveUserPassWord(){
- $p = 'Md123456';
- $pw = password_hash($p, PASSWORD_DEFAULT);
- // $user = Db::name('user')
- // ->where('del',0)
- // ->update(['password'=>$pw]);
- //
- // echo $user;
- }
- }
|