小程序-訂閱消息推送


/**
* 獲取用戶的openid
*
* @param int $uid 用戶id
*
* @return mixed|string
*
* @throws ServiceException
*/
public static function getOpenId(int $uid)
{
if (!$user = User::query()->find($uid, ['token_id'])) {
throw new ServiceException('用戶不存在或,openId 為空');
}
if (!$openid = UserToken::query()->find($user->token_id, ['miniprogram_token'])->miniprogram_token ?? '') {
throw new ServiceException('用戶不存在或,openId 為空');
}
return $openid;
}


/**
* 采納新品需求消息
* $message = [
* 'uid' => 用戶id
* 'title' => 商品名稱
* 'state' => 采納結果
* 'remark' => 備注
* ]
* @param array $message 發送消息
*
* @throws GuzzleException
*/
public function goodsDemandStatus(array $message)
{
try {
$postData = [
'touser' => self::getOpenId($message['uid']), // 小程序openid
'template_id' => '16545', // 訂閱消息id
"page" => "pages/allGreens/allGreens", // 小程序跳轉界面,訂閱消息跳轉界面
'data' => [
'thing1' => [
'value' => $message['title'],
],
'phrase2' => [
'value' => $message['state'],
],
'thing3' => [
'value' => $message['remark'],
],
],
];
$response = $this->send($postData);
$re = json_decode($response->getBody()->getContents(), 1);
if (!empty($re['errcode'])) {
Log::channel('message')->info("消息發送失敗:" . $re['errmsg']);
}
} catch (\Exception $exception) {
Log::channel('message')->info("消息發送失敗:" . $exception->getMessage());
}
}

/**
* 發送訂閱消息
*
* @param $data
*
* @return ResponseInterface
*
* @throws GuzzleException
*/
public function send($data)
{
return $this->httpPostJson('cgi-bin/message/subscribe/send', $data);
}

/**
* post 請求
*
* @param string $url 請求uri
* @param array $data 請求參數
*
* @return ResponseInterface
*
* @throws GuzzleException
*/
public function httpPostJson(string $url, array $data = [])
{
$tokenQuery = http_build_query([
'access_token' => $this->accessToken
]);

return $this->httpPostJsons($url . "?" . $tokenQuery, $data);
}

/**
* 獲取小程序token
*
* @return string
*
* @throws RequestException
* @throws \App\Lib\Vendor\Miniprogram\RequestException
* @throws GuzzleException
*/
public function getToken()
{
// 如果有做緩存,則讀取緩存內容
// if ($cache = Cache::get('cdy_wx:access_token')) {
// return $cache;
//}

$re = $this->httpGet("token", [
'grant_type' => 'client_credential',
'appid' => $this->appId,
'secret' => $this->appSecret,
])->getBody()->getContents();
$response = json_decode($re, true);

if (!empty($response['errcode'])) {
Log::channel('store')->error([
'getAccessTokenError' => $response
]);
throw new \App\Lib\Vendor\Miniprogram\RequestException("獲取token失敗:" . $response["errmsg"]);
}

Cache::put(
'cdy_wx:access_token',
$response['access_token'],
now()->addSeconds($response['expires_in'] - 500)
);
return $response['access_token'];
}

/**
* post json 請求
*
* @param string $url 請求uri
* @param array $data 請求參數
*
* @return ResponseInterface
*
* @throws GuzzleException
*/
public function httpPostJsons(string $url, array $data = [])
{
return $this->request($url, 'POST', ['json' => $data, 'verify' => false]);
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM