## 合成分享碼
由于公眾號二維碼有次數跟時間的限制,素材上傳也有次數限制,這里對素材ID做了一個redis緩存,緩存時間就是最大的30天。
>[danger] 小插曲:由于這個方法是在守護進程里面實現的,當時還遇到一個小問題,就是 PHP CLI模式下無法使用 `file_putcontents` 這個方法,由于此時我已經成功的對接了對話聊天功能,我就通過聊天窗口向ChatGPT詢問解決方案,結果也讓我很滿意,對話內容貼在下方,當前分享碼也只需要給公眾號發送關鍵字 **推廣碼** 就能看到推文。
>[info] 代碼邏輯,有圖片素材ID的緩存就直接拿這個緩存去發消息給關鍵詞 **推廣碼** ,沒有緩存就直接調用公眾號的生成帶參二維碼的接口,生成一個有效期為30天的臨時二維碼,只要素材ID的緩存時間小于二維碼有效期,就不會擔心分享出去的二維碼失效。 二維碼下載保存本地以后做了圖片壓縮以及圖片合成,才達到了最終的效果: [項目演示](項目演示.md)
### 聊天內容:(<span style="color:red;font-weight:800">相關核心代碼在圖片下方</span>)
 
```
/**
* @param $openId
* @return void
* 推廣碼
*/
public function CreateAndSends($openId)
{
if ($openId) {
$redis = new Redis();
$opmedia_id = $openId . 'md';
if ($redis->has($opmedia_id)) {
$media_id = $redis->get($opmedia_id);
if ($media_id) {
$image = new Image($media_id);
$this->app->customer_service->message($image)->to($openId)->send();
/*補充一句說明*/
$memo = unicode2utf8("?") . '溫馨提示:凡事通過您二維碼關注的用戶,對方可以獲得' . config("site.times") . '次機會,您自己會獲得' . config("site.give_up_times") . '次機會,邀請無上限' . unicode2utf8("??") . unicode2utf8("??") . unicode2utf8("??");
$message = new Text($memo);
$this->app->customer_service->message($message)->to($openId)->send();
} else {
self::GetCode($openId);
}
} else {
self::GetCode($openId);
}
}
}
/**
* @param $openId
* @return void
* 生成二維碼
*/
public function GetCode($openId)
{
/*計算請求次數START*/
$hx = new Redis();
if ($hx->has("hexin")) {
$hxnum = $hx->get("hexin");
$newhxnum = bcadd($hxnum, 1);
$hx->set("hexin", $newhxnum);
} else {
$hxnum = 1;
$hx->set("hexin", $hxnum);
}
/*計算請求次數END*/
$redis = new Redis();
$opmedia_id = $openId . 'md';
$result = $this->app->qrcode->temporary($openId, 2592000); /*合成二維碼*/
$ticket = '';
if (isset($result['ticket'])) {
$ticket = $result['ticket'];
} else if (isset($result['errcode'])) {
$message = new Text(unicode2utf8("?") . '公眾號人數壓力過大,請明日來獲取推廣碼,今日已達上限');
$this->app->customer_service->message($message)->to($openId)->send();
return;
}
if (!$ticket) {
$message = new Text(unicode2utf8("?") . '公眾號人數壓力過大,請明日來獲取推廣碼,今日已達上限');
$this->app->customer_service->message($message)->to($openId)->send();
return;
}
$redis->set($result['ticket'], $openId); /*對應二維碼的ticket跟Openid進行綁定*/
$url = $this->app->qrcode->url($ticket);
$path = __DIR__ . '/../../../public/images/' . $openId . '.jpg';
/*PHPCLI 代碼改寫START*/
// $url = '<圖片 URL>';
// $destination = '<圖片保存路徑>';
$ch = curl_init($url);
$fp = fopen($path, "wb");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
/*PHPCLI 代碼改寫END*/
/*壓縮圖片*/
$imageYs = \think\Image::open($path);
$imageYs->thumb(150, 150)->save($path);
$backpath = __DIR__ . '/../../../public/images/' . 'back.jpg'; /*背景圖*/
$path2 = __DIR__ . '/../../../public/images/' . 'newimg/' . $openId . '.jpg'; /*保存路徑*/
$image = \think\Image::open($backpath);
// 給原圖左上角添加水印并保存water_image.png[390, 1030]
$image->water($path, [390, 1030])->save($path2);
$result2 = $this->app->material->uploadImage($path2);
if ($result2) {
if (isset($result2['media_id'])) {
$media_id = $result2['media_id'];
$redis->set($opmedia_id, $media_id, 864000);
$image = new Image($media_id);
$this->app->customer_service->message($image)->to($openId)->send();
/*補充一句說明*/
$memo = unicode2utf8("?") . '溫馨提示:凡事通過您二維碼關注的用戶,對方可以獲得' . config("site.times") . '次機會,您自己會獲得' . config("site.give_up_times") . '次機會,邀請無上限' . unicode2utf8("??") . unicode2utf8("??") . unicode2utf8("??");
$message = new Text($memo);
$this->app->customer_service->message($message)->to($openId)->send();
}
}
}
```
*****