123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405 |
- <?php
- namespace app\common\model;
- use app\hander\HelpHander;
- use think\Db;
- use think\Exception;
- use think\Model;
- class Email extends Model
- {
- // 添加邮箱
- public function add(){
- $data = [
- 'email' => strtolower(input('email','','trim')), // 大写转小写
- 'password' => input('password','','trim'),
- 'uname' => '',
- 'position' => '',
- 'tel' => '',
- 'branch_id' => 0,
- 'active' => input('active/d',1), // 启用
- 'is_imap' => 1,
- 'limits' => 0,
- 'limitg' => 0,
- 'is_convert' => 0,
- 'login_password' => 0,
- 'disabled_password' => 1,
- 'my_spam_mode' => 2,
- ];
- if(!$data['password']){
- }
- $info = Db::name('user_email')->where('email',$data['email'])->find();
- if($info){
- HelpHander::error('该邮箱已被占用');
- }
- $config = config('app.email');
- $param = [
- '_key' => $config['key'],
- '_lang' => 'zh-cn',
- ];
- $userId = input('uid/d',0);
- $name = Db::name('user_info')->where('user_id',$userId)->value('name');
- $data['uname'] = $name;
- //获取用户最优先的部门
- $dep = Db::name('user_job')
- ->alias('uj')
- ->join('job j','j.id = uj.job_id')
- ->join('dep d','d.id = j.dep_id')
- ->where('uj.user_id',$userId)
- ->where('j.del',0)
- ->where('j.enable',1)
- ->where('d.del',0)
- ->where('d.enable',1)
- ->order('d.sorts asc,d.id asc')
- ->value('d.email_branch');
- $data['branch_id'] = $dep?$dep:0;
- $param = array_merge($param,$data);
- $url = $config['host'].'User/add';
- Db::startTrans();
- try{
- $sdata = [
- 'email' => $data['email'],
- 'password' => think_encrypt($data['password']),
- 'user_id' => $userId,
- 'create_time' => date('Y-m-d H:i:s'),
- 'active' => $data['active']
- ];
- $res = Db::name('user_email')->insert($sdata);
- if(!$res){
- \exception('操作失败');
- }
- $ret = $this->http_post($url,$param);
- if(!$ret){
- \exception($this->error);
- }
- Db::name('user_email')->where('user_id',$sdata['user_id'])->setField('eid',$ret['data']['id']);
- Db::commit();
- }catch (Exception $e){
- Db::rollback();
- HelpHander::error($e->getMessage());
- }
- return $ret['data']['id'];
- }
- // 编辑邮箱
- public function edit(){
- $password = input('password','','trim');
- $data = [
- 'id' => input('id/d',0),
- 'password' => '',
- 'is_password' => 0,
- 'uname' => '',
- 'position' => '',
- 'tel' => '',
- 'branch_id' => 0,
- 'active' => input('active/d',1), // 启用
- 'is_imap' => 1,
- 'limits' => 0,
- 'limitg' => 0,
- 'is_convert' => 0,
- 'login_password' => 0,
- 'disabled_password' => 1,
- 'my_spam_mode' => 2,
- ];
- $userId = input('uid/d',0);
- if(!empty($data['password'])){
- $data['password'] = $password;
- $data['is_password'] = 1;
- }
- //获取用户最优先的部门
- $dep = Db::name('user_job')
- ->alias('uj')
- ->join('job j','j.id = uj.job_id')
- ->join('dep d','d.id = j.dep_id')
- ->where('uj.user_id',$userId)
- ->where('j.del',0)
- ->where('j.enable',1)
- ->where('d.del',0)
- ->where('d.enable',1)
- ->order('d.sorts asc,d.id asc')
- ->value('d.email_branch');
- $data['branch_id'] = $dep?$dep:0;
- $config = config('app.email');
- $param = [
- '_key' => $config['key'],
- '_lang' => 'zh-cn',
- ];
- $name = Db::name('user_info')->where('user_id',$userId)->value('name');
- $data['uname'] = $name;
- $param = array_merge($param,$data);
- $url = $config['host'].'User/update';
- Db::startTrans();
- try{
- $sdata = [
- 'eid' => $data['id'],
- 'update_time' => date('Y-m-d H:i:s'),
- 'active' => $data['active']
- ];
- if(!empty($data['password'])){
- $sdata['password'] = think_encrypt($data['password']);
- }
- $res = Db::name('user_email')->where('user_id',$userId)->update($sdata);
- if(!$res){
- \exception('操作失败');
- }
- $ret = $this->http_post($url,$param);
- if(!$ret){
- \exception($this->error);
- }
- Db::commit();
- }catch (Exception $e){
- Db::rollback();
- HelpHander::error($e->getMessage());
- }
- return true;
- }
- public function del($id){
- $info = Db::name('user_email')->where('user_id',$id)->find();
- if(!$info){
- $this->error = "记录不存在";
- return false;
- }
- if($info['active'] == 1){
- $this->error = '只有禁用的账号可以删除';
- return false;
- }
- $config = config('app.email');
- $param = [
- '_key' => $config['key'],
- '_lang' => 'zh-cn',
- 'id' => $info['eid']
- ];
- $url = $config['host'].'User/delete';
- Db::startTrans();
- try{
- $res = Db::name('user_email')->delete($info['id']);
- if(!$res){
- \exception('操作失败');
- }
- $logdata = json_encode($info);
- model('ActionLog')->add(20,'删除邮件',0,$logdata);
- $ret = $this->http_post($url,$param);
- if(!$ret){
- \exception('删除失败');
- }
- Db::commit();
- }catch (Exception $e){
- Db::rollback();
- HelpHander::error($e->getMessage());
- }
- return true;
- }
- public function batchDel(){
- $lists = Db::name('user_email')->where('active',0)->select();
- $lists = $lists?$lists:[];
- dump(count($lists));
- $config = config('app.email');
- $param = [
- '_key' => $config['key'],
- '_lang' => 'zh-cn',
- ];
- $url = $config['host'].'User/delete';
- $i = 0;
- foreach ($lists as $k=>$v){
- Db::startTrans();
- try{
- $res = Db::name('user_email')->delete($v['id']);
- if(!$res){
- \exception('操作失败'.json_encode($v));
- }
- $param['id'] = $v['eid'];
- $ret = $this->http_post($url,$param);
- if(!$ret){
- \exception('删除失败'.json_encode($v));
- }
- Db::commit();
- $i++;
- }catch (Exception $e){
- Db::rollback();
- dump($e->getMessage());
- }
- }
- halt('操作完成'.$i);
- }
- public function lists($page,$size,$name,$status,$orgId){
- $map[] = ['u.del','=',0];
- $uids = model('User')->getAdminUsers();
- if($uids){
- $map[] = ['u.id','not in',$uids];
- }
- if($name){
- $map[] = ['ui.name','like','%'.$name.'%'];
- }
- if($status == 1){
- $map[] = ['ue.eid','>',0];
- // $map[] = ['ue.active','=',1];
- }else{
- $map[] = ['ue.eid','=',null];
- }
- $map[] = ['r.type','=',3];
- $map[] = ['r.org_id','=',$orgId];
- $lists = Db::name('user')
- ->alias('u')
- ->join('user_info ui','ui.user_id = u.id')
- ->join('user_email ue','ue.user_id = u.id','left')
- ->join('user_roles ur','ur.user_id = u.id')
- ->join('roles r','ur.roles_id = r.id')
- ->where($map)
- ->field('u.id,u.account,u.phone,u.code,ui.name,ue.email,ue.eid,ue.create_time,ui.company_id,ue.active')
- ->page($page,$size)
- ->order('ui.sorts2 asc,u.id desc')
- ->select();
- $total = Db::name('user')
- ->alias('u')
- ->join('user_info ui','ui.user_id = u.id')
- ->join('user_email ue','ue.user_id = u.id','left')
- ->join('user_roles ur','ur.user_id = u.id')
- ->join('roles r','ur.roles_id = r.id')
- ->where($map)
- ->count();
- $data = [
- 'total' => $total,
- 'list' => $lists?$lists:[]
- ];
- return $data;
- }
- public function login($email){
- $config = config('app.email');
- $param = [
- '_key' => $config['key'],
- '_lang' => 'zh-cn',
- 'email' => $email
- ];
- $url = $config['host'].'User/login_no_password';
- $ret = $this->http_post($url,$param);
- if(!$ret){
- HelpHander::error($this->error);
- }
- return $ret['data']['jump_url'];
- }
- public function newMessage($email){
- $config = config('app.email');
- $param = [
- '_key' => $config['key'],
- '_lang' => 'zh-cn',
- 'email' => $email
- ];
- $url = $config['host'].'MailImap/newMessage';
- $ret = $this->http_post($url,$param);
- if(!$ret){ // 检查新消息不返错误
- HelpHander::success([
- "count" => 0,
- "unseenCount" => 0,
- "uidNext" => 0,
- "newMessage" => []
- ]);
- }
- return $ret['data'];
- }
- private function http_post($url,$param){
- try{
- $ret = curl_post($url,$param);
- trace($url);
- trace($ret);
- $data = json_decode($ret,true);
- if($data['status'] == 1){
- return $data;
- }else{
- \exception($data['msg']);
- }
- }catch (Exception $e){
- $this->error = $e->getMessage();
- return false;
- }
- }
- // 批量更新企业通讯录
- public function batchTel(){
- $config = config('app.email');
- $param = [
- '_key' => $config['key'],
- '_lang' => 'zh-cn',
- ];
- $url = $config['host'].'User/update';
- $users = Db::name('user_email')->select();
- $users = $users?$users:[];
- foreach ($users as $k=>$v){
- $data = [
- 'id' => $v['eid'],
- 'password' => '',
- 'is_password' => 0,
- 'uname' => '',
- 'position' => '',
- 'tel' => '',
- 'branch_id' => 0,
- 'active' => $v['active'],
- 'is_imap' => 1,
- 'limits' => 0,
- 'limitg' => 0,
- 'is_convert' => 0,
- 'login_password' => 0,
- 'disabled_password' => 1,
- 'my_spam_mode' => 2,
- ];
- //获取用户最优先的部门
- $dep = Db::name('user_job')
- ->alias('uj')
- ->join('job j','j.id = uj.job_id')
- ->join('dep d','d.id = j.dep_id')
- ->where('uj.user_id',$v['user_id'])
- ->where('j.del',0)
- ->where('j.enable',1)
- ->where('d.del',0)
- ->where('d.enable',1)
- ->order('d.sorts asc,d.id asc')
- ->value('d.email_branch');
- $data['branch_id'] = $dep?$dep:0;
- $name = Db::name('user_info')->where('user_id',$v['user_id'])->value('name');
- $data['uname'] = $name;
- $params = array_merge($param,$data);
- $this->http_post($url,$params);
- }
- return true;
- }
- }
|