app = $this->getApp(); } /** * 获取应用实例 * * @return \EasyWeChat\OfficialAccount\Application * */ public function getApp() { // 获取配置 $this->app = Factory::officialAccount(config('wechat.official', [])); // 返回结果 return $this->app; } /** * 获取user * @param string $code 通过 前端授权code获取用户openid * * */ public function userFromCode($code) { // 获取手机号 $result = $this->app->oauth->userFromCode($code); // 判断结果 if (!empty($result['errcode'])) return ['error' => $result['errcode'] . '=>' . $result['errmsg']]; // 获取不包含区号的手机号(因为绑定手机号字段会有国际区号) return $result; } /** * 获取JSSDK的配置数组 * @return array */ public function getJssdkConfig($url = '') { // 如果指定路径的话 if ($url) $this->app->jssdk->setUrl($url); // 获取JSSDK的配置对象 $config = $this->app->jssdk->buildConfig(['updateAppMessageShareData', 'updateTimelineShareData'], false, false, false); // 获取JSSDK的配置数组 return $config; } /** * 发送一次性订阅消息 * @param array $params * */ public function sendSubscription($params) { try { $result = $this->app->template_message->sendSubscription([ 'touser' => $params['openid'], 'template_id' => 'template-id', 'miniprogram' => $params['miniprogram'], 'scene' => 1000, 'data' => $params['data'], ]); // 判断结果 if (!empty($result['errcode'])) return ['error' => $result['errcode'] . '=>' . $result['errmsg']]; return $result; } catch (\Exception $e) { Log::info('wechat_subscribe_error', '订阅消息通知推送队列失败', ['data' => $params, 'error' => $e->getMessage()]); } } /** * 发送模板消息 * @param array $params * */ public function send($params) { $result = $this->app->template_message->send([ 'touser' => $params['openid'], 'template_id' => 'template-id', 'url' => $params['url'], 'scene' => 1000, 'data' => $params['data'], ]); // 判断结果 if (!empty($result['errcode'])) return ['error' => $result['errcode'] . '=>' . $result['errmsg']]; return $result; } }