| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- <?php
- namespace App\Servers\Wechat;
- use EasyWeChat\Factory;
- /**
- * 微信公众号
- *
- */
- class Official
- {
- // 工作列表
- protected \EasyWeChat\OfficialAccount\Application $app;
- // 构造函数
- public function __construct()
- {
- // 应用实例
- $this->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)
- {
- $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;
- }
- /**
- * 发送模板消息
- * @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;
- }
- }
|