Weixin.php 9.3 KB

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