FansController.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. namespace addons\Wechat\merchant\controllers;
  3. use Yii;
  4. use yii\web\Response;
  5. use common\enums\StatusEnum;
  6. use common\models\base\SearchModel;
  7. use common\traits\MerchantCurd;
  8. use common\helpers\ArrayHelper;
  9. use common\helpers\ResultHelper;
  10. use addons\Wechat\common\models\Fans;
  11. use addons\Wechat\common\models\FansTagMap;
  12. use addons\Wechat\common\enums\FansFollowEnum;
  13. /**
  14. * Class FansController
  15. * @package addons\Wechat\merchant\controllers
  16. * @author jianyan74 <751393839@qq.com>
  17. */
  18. class FansController extends BaseController
  19. {
  20. use MerchantCurd;
  21. /**
  22. * @var Fans
  23. */
  24. public $modelClass = Fans::class;
  25. /**
  26. * 首页
  27. *
  28. * @return string
  29. * @throws \yii\web\NotFoundHttpException
  30. */
  31. public function actionIndex()
  32. {
  33. $searchModel = new SearchModel([
  34. 'model' => $this->modelClass,
  35. 'scenario' => 'default',
  36. 'partialMatchAttributes' => ['nickname'], // 模糊查询
  37. 'relations' => ['tags' => ['tag_id']], // 关联 tags 表的 tag_id 字段
  38. 'defaultOrder' => [
  39. 'follow_time' => SORT_DESC,
  40. 'id' => SORT_DESC,
  41. ],
  42. 'pageSize' => $this->pageSize,
  43. ]);
  44. $dataProvider = $searchModel
  45. ->search(Yii::$app->request->queryParams);
  46. $dataProvider->query
  47. ->andWhere(['>=', 'status', StatusEnum::DISABLED])
  48. ->andFilterWhere(['merchant_id' => $this->getMerchantId()])
  49. ->with(['auth']);
  50. $tags = Yii::$app->wechatService->fansTags->getList();
  51. $params = Yii::$app->request->get('SearchModel');
  52. return $this->render($this->action->id, [
  53. 'dataProvider' => $dataProvider,
  54. 'searchModel' => $searchModel,
  55. 'tagId' => $params['tags.tag_id'] ?? 0,
  56. 'fansCount' => Yii::$app->wechatService->fans->findFollowCount(),
  57. 'fansTags' => $tags,
  58. 'allTag' => ArrayHelper::map($tags, 'id', 'name'),
  59. ]);
  60. }
  61. /**
  62. * 备注
  63. *
  64. * @return mixed|string|\yii\web\Response
  65. * @throws \yii\base\ExitException
  66. */
  67. public function actionAjaxEdit()
  68. {
  69. $id = Yii::$app->request->get('id');
  70. $model = $this->findModel($id);
  71. // ajax 校验
  72. $this->activeFormValidate($model);
  73. if ($model->load(Yii::$app->request->post())) {
  74. Yii::$app->wechat->app->user->remark($model->openid, $model->remark);
  75. return $model->save()
  76. ? $this->redirect(Yii::$app->request->referrer)
  77. : $this->message($this->getError($model), $this->redirect(Yii::$app->request->referrer), 'error');
  78. }
  79. return $this->renderAjax($this->action->id, [
  80. 'model' => $model,
  81. ]);
  82. }
  83. /**
  84. * 发送消息
  85. *
  86. * @param $id
  87. * @return array|string
  88. * @throws \EasyWeChat\Kernel\Exceptions\HttpException
  89. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  90. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  91. * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
  92. * @throws \Psr\SimpleCache\InvalidArgumentException
  93. * @throws yii\web\UnprocessableEntityHttpException
  94. */
  95. public function actionSendMessage($openid)
  96. {
  97. if (Yii::$app->request->isPost) {
  98. $data = Yii::$app->request->post();
  99. try {
  100. $media_id = $data[$data['type']] ?? $data['content'];
  101. Yii::$app->wechatService->message->customer($openid, $data['type'], $media_id);
  102. return ResultHelper::json(200, '发送成功');
  103. } catch (\Exception $e) {
  104. return ResultHelper::json(422, $e->getMessage());
  105. }
  106. }
  107. return $this->renderAjax('send-message', [
  108. 'model' => Yii::$app->wechatService->fans->findByOpenId($openid)
  109. ]);
  110. }
  111. /**
  112. * 贴标签
  113. *
  114. * @param $fan_id
  115. * @return string|Response
  116. * @throws \EasyWeChat\Kernel\Exceptions\HttpException
  117. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  118. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  119. * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
  120. * @throws \GuzzleHttp\Exception\GuzzleException
  121. * @throws \Psr\SimpleCache\InvalidArgumentException
  122. * @throws \yii\web\UnprocessableEntityHttpException
  123. */
  124. public function actionMoveTag($fan_id)
  125. {
  126. $fans = Yii::$app->wechatService->fans->findByIdWithTag($fan_id);
  127. // 用户当前标签
  128. $fansTags = array_column($fans['tags'], 'tag_id');
  129. if (Yii::$app->request->isPost) {
  130. $tags = Yii::$app->request->post('tag_id', []);
  131. FansTagMap::deleteAll(['fans_id' => $fan_id]);
  132. // 添加标签
  133. foreach ($tags as $tag_id) {
  134. !in_array($tag_id, $fansTags) && Yii::$app->wechat->app->user_tag->tagUsers([$fans['openid']], $tag_id);
  135. $model = new FansTagMap();
  136. $model->fans_id = $fan_id;
  137. $model->tag_id = $tag_id;
  138. $model->save();
  139. }
  140. // 移除标签
  141. foreach ($fansTags as $tag_id) {
  142. !in_array($tag_id, $tags) && Yii::$app->wechat->app->user_tag->untagUsers([$fans['openid']], $tag_id);
  143. }
  144. // 更新标签
  145. Yii::$app->wechatService->fansTags->getList(true);
  146. return $this->redirect(['index']);
  147. }
  148. return $this->renderAjax('move-tag', [
  149. 'tags' => Yii::$app->wechatService->fansTags->getList(),
  150. 'fansTags' => $fansTags,
  151. ]);
  152. }
  153. /**
  154. * 获取全部粉丝的 openid
  155. *
  156. * @return array
  157. * @throws \Psr\SimpleCache\InvalidArgumentException
  158. */
  159. public function actionSyncAllOpenid()
  160. {
  161. $nextOpenid = Yii::$app->request->get('next_openid', '');
  162. // 设置关注全部为为关注
  163. empty($nextOpenid) && Fans::updateAll(['follow' => FansFollowEnum::OFF], ['merchant_id' => Yii::$app->services->merchant->getNotNullId()]);
  164. try {
  165. list($total, $count, $nextOpenid) = Yii::$app->wechatService->fans->syncAllOpenid($nextOpenid);
  166. return ResultHelper::json(200, '同步粉丝 openid 完成', [
  167. 'total' => $total,
  168. 'count' => $count,
  169. 'next_openid' => $nextOpenid,
  170. ]);
  171. } catch (\Exception $e) {
  172. return ResultHelper::json(422, $e->getMessage());
  173. }
  174. }
  175. /**
  176. * 开始同步粉丝数据
  177. *
  178. * @return array
  179. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  180. * @throws yii\db\Exception
  181. */
  182. public function actionSync()
  183. {
  184. $type = Yii::$app->request->post('type', 'all');
  185. $page = Yii::$app->request->post('page', 0);
  186. // 全部同步
  187. if ($type == 'all' && !empty($models = Yii::$app->wechatService->fans->findFollowByPage($page))) {
  188. // 同步粉丝信息
  189. foreach ($models as $fans) {
  190. Yii::$app->wechatService->fans->syncByOpenid($fans['openid']);
  191. }
  192. return ResultHelper::json(200, '同步完成', [
  193. 'page' => $page + 1
  194. ]);
  195. }
  196. // 选中同步
  197. if ($type == 'check') {
  198. if (empty($openIds = Yii::$app->request->post('openids')) || !is_array($openIds)) {
  199. return ResultHelper::json(404, '请选择粉丝');
  200. }
  201. // 系统内的粉丝
  202. if (!empty($syncFans = Yii::$app->wechatService->fans->findByOpenids($openIds))) {
  203. // 同步粉丝信息
  204. foreach ($syncFans as $fans) {
  205. Yii::$app->wechatService->fans->syncByOpenid($fans['openid']);
  206. }
  207. }
  208. }
  209. return ResultHelper::json(200, '同步完成');
  210. }
  211. /**
  212. * @return array|mixed
  213. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  214. * @throws \yii\db\Exception
  215. */
  216. public function actionSyncInfo()
  217. {
  218. if (!empty($syncFans = Yii::$app->wechatService->fans->findNotFollowTime())) {
  219. // 同步粉丝信息
  220. foreach ($syncFans as $fans) {
  221. Yii::$app->wechatService->fans->syncByOpenid($fans['openid']);
  222. }
  223. return ResultHelper::json(200, '同步完成', ArrayHelper::map($syncFans, 'id', 'openid'));
  224. }
  225. return ResultHelper::json(201, '同步完成');
  226. }
  227. }
粤ICP备19079148号