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