| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
- <?php
- namespace addons\Wechat\services;
- use Yii;
- use common\helpers\ArrayHelper;
- use common\components\Service;
- use addons\Wechat\common\models\Fans;
- use addons\Wechat\common\enums\FansFollowEnum;
- /**
- * Class FansService
- * @package addons\Wechat\services
- * @author jianyan74 <751393839@qq.com>
- */
- class FansService extends Service
- {
- /**
- * @param $openid
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
- */
- public function follow($openid)
- {
- // 获取用户信息
- $user = Yii::$app->wechat->app->user->get($openid);
- $user = ArrayHelper::toArray($user);
- $fans = $this->findModel($openid);
- $fans->attributes = $user;
- $fans->group_id = $user['groupid'];
- $fans->head_portrait = $user['headimgurl'];
- $fans->follow_time = $user['subscribe_time'];
- $fans->follow = FansFollowEnum::ON;
- $fans->save();
- Yii::$app->wechatService->fansStat->upFollowNum();
- }
- /**
- * 取消关注
- *
- * @param $openid
- */
- public function unFollow($openid)
- {
- if ($fans = Fans::findOne(['openid' => $openid])) {
- $fans->follow = FansFollowEnum::OFF;
- $fans->unfollow_time = time();
- $fans->save();
- Yii::$app->wechatService->fansStat->upUnFollowNum();
- }
- }
- /**
- * 同步关注的用户信息
- *
- * @param $openid
- * @return false
- * @throws \EasyWeChat\Kernel\Exceptions\HttpException
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
- * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
- * @throws \Psr\SimpleCache\InvalidArgumentException
- * @throws \yii\db\Exception
- * @throws \yii\web\UnprocessableEntityHttpException
- */
- public function syncByOpenid($openid)
- {
- $user = Yii::$app->wechat->app->user->get($openid);
- Yii::$app->services->base->getWechatError($user);
- $fans = $this->findModel($openid);
- if (empty($fans)) {
- return false;
- }
- if ($user['subscribe'] == FansFollowEnum::ON) {
- $fans->attributes = $user;
- $fans->group_id = $user['groupid'];
- $fans->head_portrait = $user['headimgurl'];
- $fans->follow_time = $user['subscribe_time'];
- $fans->follow = FansFollowEnum::ON;
- !$fans->save() && $this->error($fans);
- // 同步标签
- $labelData = [];
- foreach ($user['tagid_list'] as $tag) {
- $labelData[] = [$fans->id, $tag, 0, Yii::$app->services->merchant->getId()];
- }
- Yii::$app->wechatService->fansTagMap->add($fans->id, $labelData);
- } else {
- $fans->follow = FansFollowEnum::OFF;
- !$fans->save() && $this->error($fans);
- }
- }
- /**
- * 同步所有粉丝 openid
- *
- * @return array
- * @throws \EasyWeChat\Kernel\Exceptions\HttpException
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
- * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
- * @throws \Psr\SimpleCache\InvalidArgumentException
- * @throws \yii\db\Exception
- * @throws \yii\web\UnprocessableEntityHttpException
- */
- public function syncAllOpenid($nextOpenId = null)
- {
- // 获取全部列表
- $fans_list = Yii::$app->wechat->app->user->list($nextOpenId);
- Yii::$app->services->base->getWechatError($fans_list);
- $fans_count = $fans_list['total'];
- $total_page = ceil($fans_count / 500);
- for ($i = 0; $i < $total_page; $i++) {
- if (!isset($fans_list['data']) || empty($fans_list['data'])) {
- break;
- }
- $fans = array_slice($fans_list['data']['openid'], $i * 500, 500);
- // 系统内的粉丝
- $system_fans = Yii::$app->wechatService->fans->findByOpenids($fans);
- $new_system_fans = ArrayHelper::arrayKey($system_fans, 'openid');
- $add_fans = [];
- foreach ($fans as $openid) {
- if (empty($new_system_fans) || empty($new_system_fans[$openid])) {
- $add_fans[] = [
- 0,
- $openid,
- FansFollowEnum::ON,
- 0,
- '',
- Yii::$app->services->merchant->getNotNullId(),
- time(),
- time()
- ];
- }
- }
- if (!empty($add_fans)) {
- // 批量插入数据
- $field = [
- 'member_id',
- 'openid',
- 'follow',
- 'follow_time',
- 'tag',
- 'merchant_id',
- 'created_at',
- 'updated_at'
- ];
- Yii::$app->db->createCommand()->batchInsert(Fans::tableName(), $field, $add_fans)->execute();
- }
- // 更新当前粉丝为关注
- Fans::updateAll(['follow' => 1], ['in', 'openid', $fans]);
- }
- return [$fans_list['total'], !empty($fans_list['data']['openid']) ? $fans_count : 0, $fans_list['next_openid']];
- }
- /**
- * @param $fan_id
- * @return array|null|\yii\db\ActiveRecord
- */
- public function findByIdWithTag($fan_id)
- {
- return Fans::find()
- ->where(['id' => $fan_id])
- ->andFilterWhere(['merchant_id' => $this->getMerchantId()])
- ->with('tags')
- ->asArray()
- ->one();
- }
- /**
- * @param $openid
- * @return array|null|\yii\db\ActiveRecord
- */
- public function findByOpenId($openid)
- {
- return Fans::find()
- ->where(['openid' => $openid])
- ->andFilterWhere(['merchant_id' => $this->getMerchantId()])
- ->one();
- }
- /**
- * @param array $openids
- * @return array|\yii\db\ActiveRecord[]
- */
- public function findByOpenids(array $openids)
- {
- return Fans::find()
- ->where(['in', 'openid', $openids])
- ->andFilterWhere(['merchant_id' => $this->getMerchantId()])
- ->select('openid')
- ->asArray()
- ->all();
- }
- /**
- * @param int $page
- * @return array|\yii\db\ActiveRecord[]
- */
- public function findFollowByPage($page = 0)
- {
- return Fans::find()
- ->where(['follow' => FansFollowEnum::ON])
- ->andFilterWhere(['merchant_id' => $this->getMerchantId()])
- ->offset(10 * $page)
- ->orderBy('id desc')
- ->limit(10)
- ->asArray()
- ->all();
- }
- /**
- * @param int $page
- * @return array|\yii\db\ActiveRecord[]
- */
- public function findNotFollowTime()
- {
- return Fans::find()
- ->where(['follow_time' => 0])
- ->andFilterWhere(['follow' => FansFollowEnum::ON])
- ->andFilterWhere(['merchant_id' => $this->getMerchantId()])
- ->orderBy('id desc')
- ->limit(10)
- ->asArray()
- ->all();
- }
- /**
- * 获取关注的人数
- *
- * @return int|string
- */
- public function findFollowCount()
- {
- return Fans::find()
- ->where(['follow' => FansFollowEnum::ON])
- ->andFilterWhere(['merchant_id' => $this->getMerchantId()])
- ->select(['follow'])
- ->count();
- }
- /**
- * 获取用户信息
- *
- * @param $openid
- * @return array|Fans|null|\yii\db\ActiveRecord
- */
- protected function findModel($openid)
- {
- if (empty($openid) || empty($model = Fans::find()
- ->where(['openid' => $openid])
- ->one()
- )) {
- return new Fans();
- }
- return $model;
- }
- }
|