|
|
@@ -0,0 +1,98 @@
|
|
|
+<?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',
|
|
|
+ 'url' => $params['url'],
|
|
|
+ '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;
|
|
|
+ }
|
|
|
+}
|