| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- <?php
- namespace api\services;
- use api\models\user\UserInfo;
- use EasyWeChat\Factory;
- use Yii;
- /**
- * 微信会员信息
- * @package api\services
- */
- class WechatUserService extends BaseService
- {
- /**
- * @var \EasyWeChat\OfficialAccount\Application
- */
- protected $app;
- public function __construct($config = [])
- {
- parent::__construct($config);
- if (empty($this->mall_id)) return $this->error('未设置Mall-Id');
- // 获取微信配置
- $wechat = Yii::$app->services->setting->mall->get($this->mall_id, 'wechat');
- if (!($wechat['app_id'] && $wechat['secret'])) return $this->error('未配置微信公众号');
- $options = [
- 'app_id' => $wechat['app_id'],
- 'secret' => $wechat['secret'],
- 'log' => [
- 'level' => 'debug',
- 'file' => '/tmp/easywechat.log',
- ],
- ];
- $this->app = Factory::officialAccount($options);
- }
- /**
- * 用户是否订阅
- *
- * @param integer $user_id
- * @return boolean
- */
- public function isSubscribe(int $user_id)
- {
- // 查询用户openid
- $openid = UserInfo::getWechatOpenid($this->mall_id, $user_id);
- if (empty($openid)) return $this->error('未找到用户公众号信息');
- // 查询微信信息
- $wechat_info = $this->info($openid);
- if (empty($wechat_info)) return $this->error($this->getError());
- return boolval($wechat_info['subscribe']);
- }
- /**
- * 会员详情
- *
- * @param string $openid
- * @return array|false
- */
- public function info(string $openid)
- {
- $info = $this->app->user->get($openid);
- if (isset($info['errcode'])) return $this->error($info['errmsg']);
- return $info;
- }
- }
|