Weixin.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. namespace app\admin\controller\qingdong\weixin;
  3. use app\common\controller\Backend;
  4. use addons\qingdong\model\StaffDepartment;
  5. use addons\qingdong\model\DingStaff;
  6. use addons\qingdong\model\DingCustomer;
  7. use addons\qingdong\model\DingContacts;
  8. use addons\qingdong\model\AdminConfig;
  9. use addons\qingdong\model\Staff;
  10. use think\Session;
  11. /**
  12. * 微信同步
  13. */
  14. class Weixin extends Backend
  15. {
  16. public function __construct(){
  17. $this->corpid =AdminConfig::where(array('field'=>'corpid'))->value('value');
  18. $corpsecret=AdminConfig::where(array('field'=>'corpsecret'))->value('value');
  19. $url ='https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid='.$this->corpid.'&corpsecret='.$corpsecret;
  20. $token = $this->http_get($url);
  21. $token = json_decode($token,true);
  22. if($token['errcode'] != 0){
  23. $this->error($token['errmsg']);
  24. }
  25. $this->access_token = $token['access_token'];
  26. }
  27. /**
  28. * 获取部门
  29. */
  30. public function index(){
  31. //获取部门
  32. $url = 'https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token='.$this->access_token;
  33. $department = $this->http_get($url);
  34. $department = json_decode($department,true);
  35. if($department['errcode'] != 0){
  36. $this->error($department['errmsg']);
  37. }
  38. foreach($department['department'] as $k=>$v){
  39. $departmentData = StaffDepartment::where(array('number'=>$this->corpid.'_'.$v['id']))->find();
  40. if($departmentData){
  41. continue;
  42. }
  43. $pid = 0;
  44. if($v['parentid'] !=0){
  45. $pid = StaffDepartment::where(array('number'=>$this->corpid.'_'.$v['parentid']))->value('id');
  46. if(!$pid){
  47. $pid = 0;
  48. }
  49. }
  50. $deRes = array(
  51. 'type'=>1,
  52. 'name'=>$v['name'],
  53. 'pid'=>$pid,
  54. 'number'=>$this->corpid.'_'.$v['id'],
  55. 'parentid'=>$v['parentid']
  56. );
  57. $departmentInfo = StaffDepartment::create($deRes);
  58. if(!$departmentInfo){
  59. $this->error('同步失败');
  60. }
  61. $ids = StaffDepartment::getLastInsID();
  62. $parentid = StaffDepartment::where(array('parentid'=>$v['id']))->select();
  63. if($parentid){
  64. foreach($parentid as $ks=>$vs){
  65. if($vs['pid'] !=$ids){
  66. StaffDepartment::where(array('id'=>$vs['id']))->update(['pid'=>$ids]);
  67. }
  68. }
  69. }
  70. }
  71. $this->success('同步成功');
  72. }
  73. /**
  74. * 获取员工
  75. */
  76. public function staffinfo(){
  77. $department = StaffDepartment::where(array('type'=>1))->select();
  78. if(!$department){
  79. $this->error('部门为空,请先同步部门');
  80. }
  81. foreach($department as $k=>$v){
  82. $deptId = explode('_',$v['number']);
  83. $url ='https://qyapi.weixin.qq.com/cgi-bin/user/list?access_token='.$this->access_token.'&department_id='.$deptId[1];
  84. $staff =$this->http_get($url);
  85. $staff = json_decode($staff,true);
  86. if($staff['errcode'] != 0 || !$staff['userlist']){
  87. continue;
  88. }
  89. foreach($staff['userlist'] as $ks=>$vs){
  90. //重复员工记录不存储
  91. $staffFind = DingStaff::where(array('user_id' =>$vs['userid'],'type'=>1))->find();
  92. if($staffFind){
  93. continue;
  94. }
  95. $department = StaffDepartment::where(array('number'=>$deptId[0].'_'.$vs['department'][0]))->value('id');
  96. $staffUser = array(
  97. 'type' => 1,
  98. 'dept_id' =>$department,
  99. 'user_id' =>$vs['userid'],
  100. 'name' =>filter_Emoji($vs['name']),
  101. 'mobile' => isset($vs['mobile']) ? $vs['mobile'] : $vs['telephone'],
  102. 'status' =>0,
  103. );
  104. DingStaff::create($staffUser);
  105. }
  106. }
  107. $this->success('同步成功');
  108. }
  109. /**
  110. * 批量获取客户
  111. */
  112. public function customer(){
  113. $cursor = '';
  114. if(Session('weixinkey')){
  115. $cursor = Session('weixinkey');
  116. }
  117. $DingStaff = DingStaff::where(array('type'=>1))->limit(100)->select();
  118. if(!$DingStaff){
  119. $this->error('员工为空,请先同步员工为空');
  120. }
  121. $staffUser = array();
  122. foreach($DingStaff as $k=>$v){
  123. $staffUser[] = $v['user_id'];
  124. }
  125. $user = array(
  126. 'userid_list'=>$staffUser,
  127. 'cursor'=>$cursor,
  128. 'limit'=>100
  129. );
  130. $url ='https://qyapi.weixin.qq.com/cgi-bin/externalcontact/batch/get_by_user?access_token='.$this->access_token;
  131. $customer = $this->http_post($url,json_encode($user));
  132. $customer = json_decode($customer,true);
  133. if($customer['errcode'] != 0){
  134. $this->error($customer['errmsg']);
  135. }
  136. if(isset($customer['next_cursor']) && $customer['next_cursor']){
  137. Session('weixinkey',$customer['next_cursor']);
  138. }else{
  139. Session::delete("weixinkey");
  140. }
  141. foreach($customer['external_contact_list'] as $ks=>$vs){
  142. $customerFind = DingCustomer::where(array('instance_id'=>$vs['external_contact']['external_userid']))->find();
  143. if($customerFind){
  144. continue;
  145. }
  146. $data = $vs;
  147. $instance_id = isset($data['external_contact']['external_userid']) ? $data['external_contact']['external_userid'] : '';
  148. $customerName = isset($data['external_contact']['name']) ? addslashes(filter_Emoji($data['external_contact']['name'])) : '';
  149. $owner_staff_id=isset($data['follow_info']['userid']) ? $data['follow_info']['userid'] : '';
  150. $create_staff_id=isset($data['follow_info']['userid']) ? $data['follow_info']['userid'] : '';
  151. $createtime = isset($data['follow_info']['createtime']) ? $data['follow_info']['createtime'] : '';
  152. $updatetime = isset($data['follow_info']['createtime']) ? $data['follow_info']['createtime'] : '';
  153. $customerData = array(
  154. 'type' => 1,
  155. 'instance_id'=>$instance_id,
  156. 'user_id'=>$create_staff_id,
  157. 'name'=>$customerName,
  158. 'owner_staff_id'=>$owner_staff_id,
  159. 'create_staff_id'=>$create_staff_id,
  160. 'createtime'=>$createtime,
  161. 'updatetime'=>$updatetime,
  162. );
  163. $customers = DingCustomer::create($customerData);
  164. $customerId = DingCustomer::getLastInsID();
  165. //同步联系人
  166. $contactFind = DingContacts::where(array('instance_id'=>$instance_id))->find();
  167. if($contactFind){
  168. DingContacts::where(array('id'=>$contactFind['id']))->update(array('customer_id' =>$customerId));
  169. continue;
  170. }
  171. $contactData = array(
  172. 'type' =>1,
  173. 'instance_id' =>$instance_id,
  174. 'customer_id' =>$customerId,
  175. 'name' =>$customerName,
  176. 'mobile' =>isset($data['follow_info']['remark_mobiles'][0]) ? $data['follow_info']['remark_mobiles'][0] : '',
  177. 'create_staff_id' =>$owner_staff_id,
  178. 'owner_staff_id' =>$create_staff_id,
  179. );
  180. DingContacts::create($contactData);
  181. }
  182. $this->success('同步成功');
  183. }
  184. /**
  185. * curl请求
  186. */
  187. private function http_get($url){
  188. $oCurl = curl_init();
  189. if(stripos($url,"https://")!==FALSE){
  190. curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
  191. curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
  192. curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
  193. }
  194. curl_setopt($oCurl, CURLOPT_URL, $url);
  195. curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );
  196. $sContent = curl_exec($oCurl);
  197. $aStatus = curl_getinfo($oCurl);
  198. curl_close($oCurl);
  199. if(intval($aStatus["http_code"])==200){
  200. return $sContent;
  201. }else{
  202. return false;
  203. }
  204. }
  205. /**
  206. * curl请求
  207. */
  208. private function http_post($url,$param,$post_file=false){
  209. $oCurl = curl_init();
  210. if(stripos($url,"https://")!==FALSE){
  211. curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
  212. curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
  213. curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
  214. }
  215. if (is_string($param) || $post_file) {
  216. $strPOST = $param;
  217. } else {
  218. $aPOST = array();
  219. foreach($param as $key=>$val){
  220. $aPOST[] = $key."=".urlencode($val);
  221. }
  222. $strPOST = join("&", $aPOST);
  223. }
  224. curl_setopt($oCurl, CURLOPT_URL, $url);
  225. curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );
  226. curl_setopt($oCurl, CURLOPT_POST,true);
  227. curl_setopt($oCurl, CURLOPT_POSTFIELDS,$strPOST);
  228. $sContent = curl_exec($oCurl);
  229. $aStatus = curl_getinfo($oCurl);
  230. curl_close($oCurl);
  231. if(intval($aStatus["http_code"])==200){
  232. return $sContent;
  233. }else{
  234. return false;
  235. }
  236. }
  237. public function mobile(){
  238. $mobile = input('mobile');
  239. if(!$mobile){
  240. $this->error('请绑定员工手机号');
  241. }
  242. $user = array(
  243. 'mobile'=>$mobile,
  244. );
  245. $url ='https://qyapi.weixin.qq.com/cgi-bin/user/getuserid?access_token='.$this->access_token;
  246. $customer = $this->http_post($url,json_encode($user));
  247. $result = json_decode($customer,true);
  248. if($result['errcode'] ==0 ){
  249. Staff::where(['mobile'=>$mobile])->update(['wx_userid'=>$result['userid']]);
  250. }
  251. dump($result);exit;
  252. }
  253. }