FansService.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. namespace addons\Wechat\services;
  3. use Yii;
  4. use common\helpers\ArrayHelper;
  5. use common\components\Service;
  6. use addons\Wechat\common\models\Fans;
  7. use addons\Wechat\common\enums\FansFollowEnum;
  8. /**
  9. * Class FansService
  10. * @package addons\Wechat\services
  11. * @author jianyan74 <751393839@qq.com>
  12. */
  13. class FansService extends Service
  14. {
  15. /**
  16. * @param $openid
  17. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  18. */
  19. public function follow($openid)
  20. {
  21. // 获取用户信息
  22. $user = Yii::$app->wechat->app->user->get($openid);
  23. $user = ArrayHelper::toArray($user);
  24. $fans = $this->findModel($openid);
  25. $fans->attributes = $user;
  26. $fans->group_id = $user['groupid'];
  27. $fans->head_portrait = $user['headimgurl'];
  28. $fans->follow_time = $user['subscribe_time'];
  29. $fans->follow = FansFollowEnum::ON;
  30. $fans->save();
  31. Yii::$app->wechatService->fansStat->upFollowNum();
  32. }
  33. /**
  34. * 取消关注
  35. *
  36. * @param $openid
  37. */
  38. public function unFollow($openid)
  39. {
  40. if ($fans = Fans::findOne(['openid' => $openid])) {
  41. $fans->follow = FansFollowEnum::OFF;
  42. $fans->unfollow_time = time();
  43. $fans->save();
  44. Yii::$app->wechatService->fansStat->upUnFollowNum();
  45. }
  46. }
  47. /**
  48. * 同步关注的用户信息
  49. *
  50. * @param $openid
  51. * @return false
  52. * @throws \EasyWeChat\Kernel\Exceptions\HttpException
  53. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  54. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  55. * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
  56. * @throws \Psr\SimpleCache\InvalidArgumentException
  57. * @throws \yii\db\Exception
  58. * @throws \yii\web\UnprocessableEntityHttpException
  59. */
  60. public function syncByOpenid($openid)
  61. {
  62. $user = Yii::$app->wechat->app->user->get($openid);
  63. Yii::$app->services->base->getWechatError($user);
  64. $fans = $this->findModel($openid);
  65. if (empty($fans)) {
  66. return false;
  67. }
  68. if ($user['subscribe'] == FansFollowEnum::ON) {
  69. $fans->attributes = $user;
  70. $fans->group_id = $user['groupid'];
  71. $fans->head_portrait = $user['headimgurl'];
  72. $fans->follow_time = $user['subscribe_time'];
  73. $fans->follow = FansFollowEnum::ON;
  74. !$fans->save() && $this->error($fans);
  75. // 同步标签
  76. $labelData = [];
  77. foreach ($user['tagid_list'] as $tag) {
  78. $labelData[] = [$fans->id, $tag, 0, Yii::$app->services->merchant->getId()];
  79. }
  80. Yii::$app->wechatService->fansTagMap->add($fans->id, $labelData);
  81. } else {
  82. $fans->follow = FansFollowEnum::OFF;
  83. !$fans->save() && $this->error($fans);
  84. }
  85. }
  86. /**
  87. * 同步所有粉丝 openid
  88. *
  89. * @return array
  90. * @throws \EasyWeChat\Kernel\Exceptions\HttpException
  91. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  92. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  93. * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
  94. * @throws \Psr\SimpleCache\InvalidArgumentException
  95. * @throws \yii\db\Exception
  96. * @throws \yii\web\UnprocessableEntityHttpException
  97. */
  98. public function syncAllOpenid($nextOpenId = null)
  99. {
  100. // 获取全部列表
  101. $fans_list = Yii::$app->wechat->app->user->list($nextOpenId);
  102. Yii::$app->services->base->getWechatError($fans_list);
  103. $fans_count = $fans_list['total'];
  104. $total_page = ceil($fans_count / 500);
  105. for ($i = 0; $i < $total_page; $i++) {
  106. if (!isset($fans_list['data']) || empty($fans_list['data'])) {
  107. break;
  108. }
  109. $fans = array_slice($fans_list['data']['openid'], $i * 500, 500);
  110. // 系统内的粉丝
  111. $system_fans = Yii::$app->wechatService->fans->findByOpenids($fans);
  112. $new_system_fans = ArrayHelper::arrayKey($system_fans, 'openid');
  113. $add_fans = [];
  114. foreach ($fans as $openid) {
  115. if (empty($new_system_fans) || empty($new_system_fans[$openid])) {
  116. $add_fans[] = [
  117. 0,
  118. $openid,
  119. FansFollowEnum::ON,
  120. 0,
  121. '',
  122. Yii::$app->services->merchant->getNotNullId(),
  123. time(),
  124. time()
  125. ];
  126. }
  127. }
  128. if (!empty($add_fans)) {
  129. // 批量插入数据
  130. $field = [
  131. 'member_id',
  132. 'openid',
  133. 'follow',
  134. 'follow_time',
  135. 'tag',
  136. 'merchant_id',
  137. 'created_at',
  138. 'updated_at'
  139. ];
  140. Yii::$app->db->createCommand()->batchInsert(Fans::tableName(), $field, $add_fans)->execute();
  141. }
  142. // 更新当前粉丝为关注
  143. Fans::updateAll(['follow' => 1], ['in', 'openid', $fans]);
  144. }
  145. return [$fans_list['total'], !empty($fans_list['data']['openid']) ? $fans_count : 0, $fans_list['next_openid']];
  146. }
  147. /**
  148. * @param $fan_id
  149. * @return array|null|\yii\db\ActiveRecord
  150. */
  151. public function findByIdWithTag($fan_id)
  152. {
  153. return Fans::find()
  154. ->where(['id' => $fan_id])
  155. ->andFilterWhere(['merchant_id' => $this->getMerchantId()])
  156. ->with('tags')
  157. ->asArray()
  158. ->one();
  159. }
  160. /**
  161. * @param $openid
  162. * @return array|null|\yii\db\ActiveRecord
  163. */
  164. public function findByOpenId($openid)
  165. {
  166. return Fans::find()
  167. ->where(['openid' => $openid])
  168. ->andFilterWhere(['merchant_id' => $this->getMerchantId()])
  169. ->one();
  170. }
  171. /**
  172. * @param array $openids
  173. * @return array|\yii\db\ActiveRecord[]
  174. */
  175. public function findByOpenids(array $openids)
  176. {
  177. return Fans::find()
  178. ->where(['in', 'openid', $openids])
  179. ->andFilterWhere(['merchant_id' => $this->getMerchantId()])
  180. ->select('openid')
  181. ->asArray()
  182. ->all();
  183. }
  184. /**
  185. * @param int $page
  186. * @return array|\yii\db\ActiveRecord[]
  187. */
  188. public function findFollowByPage($page = 0)
  189. {
  190. return Fans::find()
  191. ->where(['follow' => FansFollowEnum::ON])
  192. ->andFilterWhere(['merchant_id' => $this->getMerchantId()])
  193. ->offset(10 * $page)
  194. ->orderBy('id desc')
  195. ->limit(10)
  196. ->asArray()
  197. ->all();
  198. }
  199. /**
  200. * @param int $page
  201. * @return array|\yii\db\ActiveRecord[]
  202. */
  203. public function findNotFollowTime()
  204. {
  205. return Fans::find()
  206. ->where(['follow_time' => 0])
  207. ->andFilterWhere(['follow' => FansFollowEnum::ON])
  208. ->andFilterWhere(['merchant_id' => $this->getMerchantId()])
  209. ->orderBy('id desc')
  210. ->limit(10)
  211. ->asArray()
  212. ->all();
  213. }
  214. /**
  215. * 获取关注的人数
  216. *
  217. * @return int|string
  218. */
  219. public function findFollowCount()
  220. {
  221. return Fans::find()
  222. ->where(['follow' => FansFollowEnum::ON])
  223. ->andFilterWhere(['merchant_id' => $this->getMerchantId()])
  224. ->select(['follow'])
  225. ->count();
  226. }
  227. /**
  228. * 获取用户信息
  229. *
  230. * @param $openid
  231. * @return array|Fans|null|\yii\db\ActiveRecord
  232. */
  233. protected function findModel($openid)
  234. {
  235. if (empty($openid) || empty($model = Fans::find()
  236. ->where(['openid' => $openid])
  237. ->one()
  238. )) {
  239. return new Fans();
  240. }
  241. return $model;
  242. }
  243. }
粤ICP备19079148号