有赞标签批量增加

日期:2019-03-31 浏览:188次

公司需要与有赞标签同步,其给的接口函数为单一:

youzan.users.weixin.follower.tags.add

根据微信粉丝用户的 weixin_openid 或 fans_id 绑定对应的标签

链接:https://open.youzan.com/v3/apicenter/doc-api-main/1/2/tags/youzan.users.weixin.follower.tags.add

最初依照接口做的单一粉丝修改,后面需求为批量打标签。在同事的提醒下发现其底层代码为curl

为此在本网站批量打标签的基础上,进行同步到有赞。相关代码如下:

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
public function set_tags()
{
    //设计公司信息,省略中。。。
    //$weixin_openid = [];为批量数据openid数组</pre>
<pre></pre>
<pre>//同步有赞标签
$appid = USER_APPID;
$tag_str = '';
$tags_id = join(',',$tags_ids_arr);//tags_ids_arr 标签id,用于id名称查询
$res_tag = Db::name('tags')-&gt;where('id','in',$tags_id)-&gt;where('appid',$appid)-&gt;field('tags_name')-&gt;select();
foreach ($res_tag as $val){
$tag_str .= $val['tags_name'].',';
}
$tag_str = rtrim($tag_str,',');
//数据库源数据
$map = [];
$map['appid'] = $appid;
$weixin_openids = implode(',', $weixin_openid);
$map['openid'] = ['in', $weixin_openids];
$old_yz_tags = Db::name('yz_tags')-&gt;field('openid, tags')-&gt;where($map)-&gt;select();
$yztags_old_arr = [];
$result_yz_insert = [];
$result_updata_op = [];
$result_yz_updata = '';
if($old_yz_tags) {
foreach($old_yz_tags as $v) {
$yztags_old_arr[$v['openid']] = $v['tags'];
}
}
//调用yz
$is_yz = Db::name('store')-&gt;where('appid', $appid)-&gt;value('client_id');
if(!$is_yz) {
return false;
}
$youzan = new Youzan();
$token = $youzan-&gt;get_access_token('', $appid);
$client = new YZTokenClient($token);
//要调用的api版本号
$api_version = '3.0.0';
//要调用的api名称
$method = 'youzan.users.weixin.follower.tags.add';
$my_params = [
'access_token' =&gt; $token,
'method' =&gt; $api_version,
'tags' =&gt; $tag_str,
];
$my_files = [];
//拼接url
$url = $client-&gt;curl_post($method, $api_version);

$yz_count = count($weixin_openid);
$yz_one = 200;
$yz_for = (int)ceil($yz_count/$yz_one);
$weixin_openid = array_chunk($weixin_openid, $yz_one);

$num = 0;
$mh = curl_multi_init();
for($j = 0; $j &lt; $yz_for; $j++) {
foreach ($weixin_openid[$j] as $key=&gt;$val){
$my_params['weixin_openid'] = $val;
$postfields = http_build_query($my_params);

$ch[$num] = curl_init();
curl_setopt($ch[$num], CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
curl_setopt($ch[$num], CURLOPT_USERAGENT, 'KdtApiSdk Client v0.1');
curl_setopt($ch[$num], CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch[$num], CURLOPT_TIMEOUT, 30);
curl_setopt($ch[$num], CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch[$num], CURLOPT_ENCODING, "");
curl_setopt($ch[$num], CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch[$num], CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch[$num], CURLOPT_HEADER, FALSE);
curl_setopt($ch[$num], CURLOPT_POST, TRUE);
curl_setopt($ch[$num], CURLOPT_POSTFIELDS, $postfields);
curl_setopt($ch[$num], CURLOPT_URL, $url);
curl_setopt($ch[$num], CURLOPT_HTTPHEADER, $my_files);
curl_setopt($ch[$num], CURLINFO_HEADER_OUT, TRUE);
curl_multi_add_handle($mh, $ch[$num]);

$num ++;
}
unset($weixin_openid[$j]);
//执行批处理句柄
$active = null;
do {
while (($mrc = curl_multi_exec($mh, $active)) == CURLM_CALL_MULTI_PERFORM) ;
if ($mrc != CURLM_OK) {
break;
}
while ($done = curl_multi_info_read($mh)) {
$result = json_decode(curl_multi_getcontent($done['handle']),true);
$result['response']['user']['weixin_openid'];
$tags_name = '';
if (count($result['response']['user']['tags']&gt;1)){
foreach ($result['response']['user']['tags'] as $value){
$tags_name .= $value['name'].',';
}
$tags_name = rtrim($tags_name,',');
}else{
$tags_name = $result['response']['user']['tags'][0]['name'];
}

$tmp_openid = $result['response']['user']['weixin_openid'];
$tags_key = array_key_exists($tmp_openid, $yztags_old_arr); // 听说array_key_exists比array_search、in_array速度快几十倍上百倍?
if($tags_key) {
if ($yztags_old_arr[$tmp_openid] != $tags_name){
//$result_yz_updata .= ' WHEN '.$tmp_openid.' THEN \''.$tags_name.'\'';
$result_updata_op[] = '("' . $appid . '","' . $tmp_openid . '","' . $tags_name . '")';
}
}else{
$result_yz_insert[] = '("' . $appid . '","' . $tmp_openid . '","' . $tags_name . '")';
}
curl_multi_remove_handle($mh, $done['handle']);
curl_close($done['handle']);
usleep(10);
}
if ($active &gt; 0) {
curl_multi_select($mh);
}

} while ($active);


}
curl_multi_close($mh);

//$result_updata_op = rtrim($result_updata_op,',');
if (!empty($result_updata_op)){
$sql = "replace into ". Config::get('database.prefix') ."yz_tags (appid,openid,tags) value ". join(',', $result_updata_op);;
//'replace into test_tbl (id,dr) values (1,\'2\'),(2,\'3\'),...(x,\'y\');'
//Db::name('')-&gt;where('appid',$appid)-&gt;where('openid','in',$result_updata_op)
//$sql = "UPDATE ". Config::get('database.prefix') ."yz_tags SET tags = CASE openid ".$result_yz_updata." END WHERE openid IN (".join(',', $result_updata_op).")";
//halt($sql);
Db::execute($sql);
}
if (!empty($result_yz_insert)){
$sql = 'insert into ' . Config::get('database.prefix') . 'yz_tags' . ' (`appid`, `openid`, `tags`) VALUES ' . join(',', $result_yz_insert);
Db::execute($sql);
}

unset($sql);
unset($result_updata_op);
unset($result_yz_insert);

}

eg:
自定义了有赞一个curl_post()函数,位于:$client->post($method, $api_version, $my_params, $my_files),post函数同级

public function curl_post($method, $methodVersion){
    return $this->url($method,$methodVersion);

}
批量更新最初采用:
UPDATE yoiurtable
    SET dingdan = CASE id 
        WHEN 1 THEN 3 
        WHEN 2 THEN 4 
        WHEN 3 THEN 5 
    END
WHERE id IN (1,2,3)
应无法获取到主键id,后改为:
replace into test_tbl (id,dr) values (1,'2'),(2,'3'),...(x,'y');

其他几种批量更新方法(https://www.cnblogs.com/duhuo/p/4239436.html):

insert into ...on duplicate key update批量更新

insert into test_tbl (id,dr) values (1,'2'),(2,'3'),...(x,'y') on duplicate key update dr=values(dr);

创建临时表,先更新临时表,然后从临时表中update

1、create temporary table tmp(id int(4) primary key,dr varchar(50));
2、insert into tmp values  (0,'gone'), (1,'xx'),...(m,'yy');
3、update test_tbl, tmp set test_tbl.dr=tmp.dr where test_tbl.id=tmp.id;


 

1 评论

  • liusongs 2019-04-28在11:02

    if (!empty($result_yz_insert)){ 不能使用isset,因为$result_yz_insert定义了穿在,会导致报错:MYSQL SERVER VERSION FOR THE RIGHT SYNTAX TO USE NEAR ” AT LINE 1,因为当为空,mysql语句无法执行

发表评论