Email.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. <?php
  2. namespace app\common\model;
  3. use app\hander\HelpHander;
  4. use think\Db;
  5. use think\Exception;
  6. use think\Model;
  7. class Email extends Model
  8. {
  9. // 添加邮箱
  10. public function add(){
  11. $data = [
  12. 'email' => strtolower(input('email','','trim')), // 大写转小写
  13. 'password' => input('password','','trim'),
  14. 'uname' => '',
  15. 'position' => '',
  16. 'tel' => '',
  17. 'branch_id' => 0,
  18. 'active' => input('active/d',1), // 启用
  19. 'is_imap' => 1,
  20. 'limits' => 0,
  21. 'limitg' => 0,
  22. 'is_convert' => 0,
  23. 'login_password' => 0,
  24. 'disabled_password' => 1,
  25. 'my_spam_mode' => 2,
  26. ];
  27. if(!$data['password']){
  28. }
  29. $info = Db::name('user_email')->where('email',$data['email'])->find();
  30. if($info){
  31. HelpHander::error('该邮箱已被占用');
  32. }
  33. $config = config('app.email');
  34. $param = [
  35. '_key' => $config['key'],
  36. '_lang' => 'zh-cn',
  37. ];
  38. $userId = input('uid/d',0);
  39. $name = Db::name('user_info')->where('user_id',$userId)->value('name');
  40. $data['uname'] = $name;
  41. //获取用户最优先的部门
  42. $dep = Db::name('user_job')
  43. ->alias('uj')
  44. ->join('job j','j.id = uj.job_id')
  45. ->join('dep d','d.id = j.dep_id')
  46. ->where('uj.user_id',$userId)
  47. ->where('j.del',0)
  48. ->where('j.enable',1)
  49. ->where('d.del',0)
  50. ->where('d.enable',1)
  51. ->order('d.sorts asc,d.id asc')
  52. ->value('d.email_branch');
  53. $data['branch_id'] = $dep?$dep:0;
  54. $param = array_merge($param,$data);
  55. $url = $config['host'].'User/add';
  56. Db::startTrans();
  57. try{
  58. $sdata = [
  59. 'email' => $data['email'],
  60. 'password' => think_encrypt($data['password']),
  61. 'user_id' => $userId,
  62. 'create_time' => date('Y-m-d H:i:s'),
  63. 'active' => $data['active']
  64. ];
  65. $res = Db::name('user_email')->insert($sdata);
  66. if(!$res){
  67. \exception('操作失败');
  68. }
  69. $ret = $this->http_post($url,$param);
  70. if(!$ret){
  71. \exception($this->error);
  72. }
  73. Db::name('user_email')->where('user_id',$sdata['user_id'])->setField('eid',$ret['data']['id']);
  74. Db::commit();
  75. }catch (Exception $e){
  76. Db::rollback();
  77. HelpHander::error($e->getMessage());
  78. }
  79. return $ret['data']['id'];
  80. }
  81. // 编辑邮箱
  82. public function edit(){
  83. $password = input('password','','trim');
  84. $data = [
  85. 'id' => input('id/d',0),
  86. 'password' => '',
  87. 'is_password' => 0,
  88. 'uname' => '',
  89. 'position' => '',
  90. 'tel' => '',
  91. 'branch_id' => 0,
  92. 'active' => input('active/d',1), // 启用
  93. 'is_imap' => 1,
  94. 'limits' => 0,
  95. 'limitg' => 0,
  96. 'is_convert' => 0,
  97. 'login_password' => 0,
  98. 'disabled_password' => 1,
  99. 'my_spam_mode' => 2,
  100. ];
  101. $userId = input('uid/d',0);
  102. if(!empty($data['password'])){
  103. $data['password'] = $password;
  104. $data['is_password'] = 1;
  105. }
  106. //获取用户最优先的部门
  107. $dep = Db::name('user_job')
  108. ->alias('uj')
  109. ->join('job j','j.id = uj.job_id')
  110. ->join('dep d','d.id = j.dep_id')
  111. ->where('uj.user_id',$userId)
  112. ->where('j.del',0)
  113. ->where('j.enable',1)
  114. ->where('d.del',0)
  115. ->where('d.enable',1)
  116. ->order('d.sorts asc,d.id asc')
  117. ->value('d.email_branch');
  118. $data['branch_id'] = $dep?$dep:0;
  119. $config = config('app.email');
  120. $param = [
  121. '_key' => $config['key'],
  122. '_lang' => 'zh-cn',
  123. ];
  124. $name = Db::name('user_info')->where('user_id',$userId)->value('name');
  125. $data['uname'] = $name;
  126. $param = array_merge($param,$data);
  127. $url = $config['host'].'User/update';
  128. Db::startTrans();
  129. try{
  130. $sdata = [
  131. 'eid' => $data['id'],
  132. 'update_time' => date('Y-m-d H:i:s'),
  133. 'active' => $data['active']
  134. ];
  135. if(!empty($data['password'])){
  136. $sdata['password'] = think_encrypt($data['password']);
  137. }
  138. $res = Db::name('user_email')->where('user_id',$userId)->update($sdata);
  139. if(!$res){
  140. \exception('操作失败');
  141. }
  142. $ret = $this->http_post($url,$param);
  143. if(!$ret){
  144. \exception($this->error);
  145. }
  146. Db::commit();
  147. }catch (Exception $e){
  148. Db::rollback();
  149. HelpHander::error($e->getMessage());
  150. }
  151. return true;
  152. }
  153. public function del($id){
  154. $info = Db::name('user_email')->where('user_id',$id)->find();
  155. if(!$info){
  156. $this->error = "记录不存在";
  157. return false;
  158. }
  159. if($info['active'] == 1){
  160. $this->error = '只有禁用的账号可以删除';
  161. return false;
  162. }
  163. $config = config('app.email');
  164. $param = [
  165. '_key' => $config['key'],
  166. '_lang' => 'zh-cn',
  167. 'id' => $info['eid']
  168. ];
  169. $url = $config['host'].'User/delete';
  170. Db::startTrans();
  171. try{
  172. $res = Db::name('user_email')->delete($info['id']);
  173. if(!$res){
  174. \exception('操作失败');
  175. }
  176. $logdata = json_encode($info);
  177. model('ActionLog')->add(20,'删除邮件',0,$logdata);
  178. $ret = $this->http_post($url,$param);
  179. if(!$ret){
  180. \exception('删除失败');
  181. }
  182. Db::commit();
  183. }catch (Exception $e){
  184. Db::rollback();
  185. HelpHander::error($e->getMessage());
  186. }
  187. return true;
  188. }
  189. public function batchDel(){
  190. $lists = Db::name('user_email')->where('active',0)->select();
  191. $lists = $lists?$lists:[];
  192. dump(count($lists));
  193. $config = config('app.email');
  194. $param = [
  195. '_key' => $config['key'],
  196. '_lang' => 'zh-cn',
  197. ];
  198. $url = $config['host'].'User/delete';
  199. $i = 0;
  200. foreach ($lists as $k=>$v){
  201. Db::startTrans();
  202. try{
  203. $res = Db::name('user_email')->delete($v['id']);
  204. if(!$res){
  205. \exception('操作失败'.json_encode($v));
  206. }
  207. $param['id'] = $v['eid'];
  208. $ret = $this->http_post($url,$param);
  209. if(!$ret){
  210. \exception('删除失败'.json_encode($v));
  211. }
  212. Db::commit();
  213. $i++;
  214. }catch (Exception $e){
  215. Db::rollback();
  216. dump($e->getMessage());
  217. }
  218. }
  219. halt('操作完成'.$i);
  220. }
  221. public function lists($page,$size,$name,$status,$orgId){
  222. $map[] = ['u.del','=',0];
  223. $uids = model('User')->getAdminUsers();
  224. if($uids){
  225. $map[] = ['u.id','not in',$uids];
  226. }
  227. if($name){
  228. $map[] = ['ui.name','like','%'.$name.'%'];
  229. }
  230. if($status == 1){
  231. $map[] = ['ue.eid','>',0];
  232. // $map[] = ['ue.active','=',1];
  233. }else{
  234. $map[] = ['ue.eid','=',null];
  235. }
  236. $map[] = ['r.type','=',3];
  237. $map[] = ['r.org_id','=',$orgId];
  238. $lists = Db::name('user')
  239. ->alias('u')
  240. ->join('user_info ui','ui.user_id = u.id')
  241. ->join('user_email ue','ue.user_id = u.id','left')
  242. ->join('user_roles ur','ur.user_id = u.id')
  243. ->join('roles r','ur.roles_id = r.id')
  244. ->where($map)
  245. ->field('u.id,u.account,u.phone,u.code,ui.name,ue.email,ue.eid,ue.create_time,ui.company_id,ue.active')
  246. ->page($page,$size)
  247. ->order('ui.sorts2 asc,u.id desc')
  248. ->select();
  249. $total = Db::name('user')
  250. ->alias('u')
  251. ->join('user_info ui','ui.user_id = u.id')
  252. ->join('user_email ue','ue.user_id = u.id','left')
  253. ->join('user_roles ur','ur.user_id = u.id')
  254. ->join('roles r','ur.roles_id = r.id')
  255. ->where($map)
  256. ->count();
  257. $data = [
  258. 'total' => $total,
  259. 'list' => $lists?$lists:[]
  260. ];
  261. return $data;
  262. }
  263. public function login($email){
  264. $config = config('app.email');
  265. $param = [
  266. '_key' => $config['key'],
  267. '_lang' => 'zh-cn',
  268. 'email' => $email
  269. ];
  270. $url = $config['host'].'User/login_no_password';
  271. $ret = $this->http_post($url,$param);
  272. if(!$ret){
  273. HelpHander::error($this->error);
  274. }
  275. return $ret['data']['jump_url'];
  276. }
  277. public function newMessage($email){
  278. $config = config('app.email');
  279. $param = [
  280. '_key' => $config['key'],
  281. '_lang' => 'zh-cn',
  282. 'email' => $email
  283. ];
  284. $url = $config['host'].'MailImap/newMessage';
  285. $ret = $this->http_post($url,$param);
  286. if(!$ret){ // 检查新消息不返错误
  287. HelpHander::success([
  288. "count" => 0,
  289. "unseenCount" => 0,
  290. "uidNext" => 0,
  291. "newMessage" => []
  292. ]);
  293. }
  294. return $ret['data'];
  295. }
  296. private function http_post($url,$param){
  297. try{
  298. $ret = curl_post($url,$param);
  299. trace($url);
  300. trace($ret);
  301. $data = json_decode($ret,true);
  302. if($data['status'] == 1){
  303. return $data;
  304. }else{
  305. \exception($data['msg']);
  306. }
  307. }catch (Exception $e){
  308. $this->error = $e->getMessage();
  309. return false;
  310. }
  311. }
  312. // 批量更新企业通讯录
  313. public function batchTel(){
  314. $config = config('app.email');
  315. $param = [
  316. '_key' => $config['key'],
  317. '_lang' => 'zh-cn',
  318. ];
  319. $url = $config['host'].'User/update';
  320. $users = Db::name('user_email')->select();
  321. $users = $users?$users:[];
  322. foreach ($users as $k=>$v){
  323. $data = [
  324. 'id' => $v['eid'],
  325. 'password' => '',
  326. 'is_password' => 0,
  327. 'uname' => '',
  328. 'position' => '',
  329. 'tel' => '',
  330. 'branch_id' => 0,
  331. 'active' => $v['active'],
  332. 'is_imap' => 1,
  333. 'limits' => 0,
  334. 'limitg' => 0,
  335. 'is_convert' => 0,
  336. 'login_password' => 0,
  337. 'disabled_password' => 1,
  338. 'my_spam_mode' => 2,
  339. ];
  340. //获取用户最优先的部门
  341. $dep = Db::name('user_job')
  342. ->alias('uj')
  343. ->join('job j','j.id = uj.job_id')
  344. ->join('dep d','d.id = j.dep_id')
  345. ->where('uj.user_id',$v['user_id'])
  346. ->where('j.del',0)
  347. ->where('j.enable',1)
  348. ->where('d.del',0)
  349. ->where('d.enable',1)
  350. ->order('d.sorts asc,d.id asc')
  351. ->value('d.email_branch');
  352. $data['branch_id'] = $dep?$dep:0;
  353. $name = Db::name('user_info')->where('user_id',$v['user_id'])->value('name');
  354. $data['uname'] = $name;
  355. $params = array_merge($param,$data);
  356. $this->http_post($url,$params);
  357. }
  358. return true;
  359. }
  360. }