config = [ 'app_id' => config('wechat.official_account.app_id'), 'secret' => config('wechat.official_account.secret'), 'token' => config('wechat.official_account.token'), 'aes_key' => config('wechat.official_account.aes_key'), 'oauth' => [ // 'scopes' => ['snsapi_base'], // 'redirect_url' => '/examples/oauth_callback.php', ], /** * 接口请求相关配置,超时时间等,具体可用参数请参考: * https://github.com/symfony/symfony/blob/5.3/src/Symfony/Contracts/HttpClient/HttpClientInterface.php */ 'http' => [ 'timeout' => 5.0, 'retry' => true, // 使用默认重试配置 ], ]; $this->wechatApp = new Application($this->config); } /** * @param $code * @return \Overtrue\Socialite\User * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException */ public function oauthSnsapiBaseUser($code) { $user = $this->wechatApp->getOAuth()->userFromCode($code); return $user; } public function sendTemplateMessage(Order $order) { $shopId = $order->shop_id; if ($order->cooperate_id > 0 && $order->cooperate_show == BoolEnum::YES) { $shopId = $order->cooperate_id; } Log::info('sendTemplateMessage will send message', [ 'order_sn' => $order->order_sn, 'shop_id' => $order->shop_id, 'cooperate_id' => $order->cooperate_id, 'cooperate_show' => $order->cooperate_show, ]); $sellers = Seller::with('wechatUser') ->where('shop_id', $shopId) ->where('wechat_user_id', '>', 0) ->where('notice_order_pay_success', BoolEnum::YES) ->get(); foreach ($sellers as $seller) { if (!$seller->wechatUser) { Log::info('sendTemplateMessage not wechatUser', [ 'seller_id' => $seller->id ]); continue; } $message = [ 'touser' => $seller->wechatUser->open_id, 'template_id' => env('WECHAT_TEMPLATE_ID'), 'topcolor' => "#FF0000", 'data' => [ 'character_string19' => [ 'value' => $order->order_sn, 'color' => '#173177', ], 'amount13' => [ 'value' => Helps::floatFormat($order->money), 'color' => '#173177', ], 'time4' => [ 'value' => $order->created_at->format('Y-m-d H:i:s'), 'color' => '#173177', ], 'thing3' => [ 'value' => '您有新的订单', 'color' => '#173177', ] ] ]; $url = 'cgi-bin/message/template/send?access_token=' . $this->config['token']; try { $response = $this->wechatApp->getClient()->post($url, [ 'json' => $message ]); Log::info('sendTemplateMessage post message', [ 'url' => $url, 'message' => $message, 'response' => $response, ]); } catch (\Exception $exception) { Log::info('sendTemplateMessage error', [ 'error' => $exception->getMessage(), 'message' => $message ]); } } } }