FansCommand.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. namespace App\Console\Commands\Wechat;
  3. use App\Jobs\WeChatFansJob;
  4. use Illuminate\Console\Command;
  5. class FansCommand extends Command
  6. {
  7. /**
  8. * The name and signature of the console command.
  9. *
  10. * @var string
  11. */
  12. protected $signature = 'wechat:fans';
  13. /**
  14. * The console command description.
  15. *
  16. * @var string
  17. */
  18. protected $description = '公众号粉丝入库';
  19. /**
  20. * Create a new command instance.
  21. *
  22. * @return void
  23. */
  24. public function __construct()
  25. {
  26. parent::__construct();
  27. }
  28. private $publicId;
  29. /**
  30. * Execute the console command.
  31. *
  32. * @return mixed
  33. */
  34. public function handle()
  35. {
  36. $this->publicId = $this->ask("公众号ID");
  37. $option = $this->choice('请选择执行操作', [
  38. 'openid' => '获取全部openid',
  39. 'user' => '用户信息',
  40. 'tag' => '打tag',
  41. 'user_info' => '获取openid信息'
  42. ]);
  43. switch ($option) {
  44. case "openid":
  45. $this->batchOpenid();
  46. break;
  47. case "user":
  48. $this->batchUser();
  49. break;
  50. case "tag":
  51. $this->batchTag();
  52. break;
  53. case "user_info":
  54. $this->getUserInfo();
  55. break;
  56. }
  57. }
  58. public function batchOpenid()
  59. {
  60. \DB::table("kddx_user_openid")->where('public_id', $this->publicId)->update(['subscribe' => 0]);
  61. \DB::table("pocket.kdgx_fans_" . substr($this->publicId, -1))
  62. ->where('public_id', $this->publicId)
  63. ->update(['subscribe' => 0]);
  64. $next_openid = '';
  65. $i = 0;
  66. do {
  67. $response = \WeChat::Fans($this->publicId)->getAll($next_openid);
  68. $openid_list = $response->data->openid;
  69. $next_openid = $response->next_openid;
  70. $i += 10000;
  71. foreach ($openid_list as $openid) {
  72. if (\DB::table("kddx_user_openid")->where('openid', $openid)->first()) {
  73. \DB::table("kddx_user_openid")
  74. ->where('openid', $openid)->update([
  75. 'subscribe' => 1,
  76. ]);
  77. } else {
  78. \DB::table("kddx_user_openid")
  79. ->insert([
  80. 'public_id' => $this->publicId,
  81. 'openid' => $openid,
  82. 'subscribe' => 1,
  83. ]);
  84. }
  85. if (\DB::table("pocket.kdgx_fans_" . substr($this->publicId, -1))->where('openid', $openid)->first()) {
  86. \DB::table("pocket.kdgx_fans_" . substr($this->publicId, -1))
  87. ->where('openid', $openid)->update([
  88. 'subscribe' => 1,
  89. ]);
  90. } else {
  91. \DB::table("pocket.kdgx_fans_" . substr($this->publicId, -1))
  92. ->insert([
  93. 'public_id' => $this->publicId,
  94. 'openid' => $openid,
  95. 'subscribe' => 1,
  96. ]);
  97. }
  98. }
  99. } while ($i < $response->total);
  100. }
  101. public function batchUser()
  102. {
  103. $offset = 0;
  104. while (
  105. $openids = \DB::table("pocket.kdgx_fans_" . substr($this->publicId, -1))
  106. ->where('public_id', $this->publicId)
  107. ->where("subscribe", 1)
  108. ->offset($offset)
  109. ->limit(100)
  110. ->pluck('openid')->toarray()
  111. ) {
  112. dump($offset);
  113. WeChatFansJob::dispatch($this->publicId, $openids)->onQueue("{wechat:fans:info}");
  114. $offset += 100;
  115. }
  116. }
  117. public function batchTag()
  118. {
  119. $where = [
  120. ['public_id', $this->publicId],
  121. ['subscribe', 1],
  122. ['subscribe_scene', 'ADD_SCENE_ACCOUNT_MIGRATION'],
  123. ['subscribe_time', '>', 1561021200]
  124. ];
  125. $tagId = \WeChat::Tags($this->publicId)->get("西安培华-无");
  126. if (!$tagId) {
  127. $tagId = WeChat::Tags($this->publicId)->create("西安培华-无")->tag->id;
  128. }
  129. $openids = \DB::table("pocket.kdgx_fans_" . substr($this->publicId, -1))->where($where)->where('sex', 0)
  130. ->pluck('openid')->toarray();
  131. for ($i = 0; $i < count($openids); $i += 50) {
  132. \WeChat::Tags($this->publicId)->batchTagging(array_slice($openids, $i, 50), $tagId);
  133. }
  134. dump("性别-无{$tagId}", count($openids));
  135. $tagId = \WeChat::Tags($this->publicId)->get("西安培华-男");
  136. if (!$tagId) {
  137. $tagId = \WeChat::Tags($this->publicId)->create("西安培华-男")->tag->id;
  138. }
  139. $openids = \DB::table("pocket.kdgx_fans_" . substr($this->publicId, -1))->where($where)->where('sex', 1)
  140. ->pluck('openid')->toarray();
  141. for ($i = 0; $i < count($openids); $i += 50) {
  142. \WeChat::Tags($this->publicId)->batchTagging(array_slice($openids, $i, 50), $tagId);
  143. }
  144. dump("性别-男{$tagId}", count($openids));
  145. $tagId = \WeChat::Tags($this->publicId)->get("西安培华-女");
  146. if (!$tagId) {
  147. $tagId = \WeChat::Tags($this->publicId)->create("西安培华-女")->tag->id;
  148. }
  149. $openids = \DB::table("pocket.kdgx_fans_" . substr($this->publicId, -1))->where($where)->where('sex', 2)
  150. ->pluck('openid')->toarray();
  151. for ($i = 0; $i < count($openids); $i += 50) {
  152. \WeChat::Tags($this->publicId)->batchTagging(array_slice($openids, $i, 50), $tagId);
  153. }
  154. dump("性别-女{$tagId}", count($openids));
  155. dump(\WeChat::Tags($this->publicId)->get());
  156. }
  157. public function getUserInfo()
  158. {
  159. $openid = $this->ask("输入openid");
  160. $user_info = \WeChat::Fans($this->publicId)->info($openid);
  161. dump($user_info);
  162. if ($user_info->subscribe == 1) {
  163. try {
  164. \DB::table("pocket.kdgx_fans_" . substr($this->publicId, -1))
  165. ->where('openid', $user_info->openid)
  166. ->update([
  167. 'nickname' => trim($user_info->nickname) ?: "",
  168. 'sex' => $user_info->sex,
  169. 'language' => $user_info->language ?: "",
  170. 'city' => $user_info->city ?: "",
  171. 'province' => $user_info->province ?: "",
  172. 'country' => $user_info->country ?: "",
  173. 'headimgurl' => $user_info->headimgurl ?: "",
  174. 'unionid' => isset($user_info->unionid) ? $user_info->unionid : "",
  175. 'subscribe_time' => $user_info->subscribe_time,
  176. 'remark' => $user_info->remark ?: "",
  177. 'groupid' => $user_info->groupid,
  178. 'tagid_list' => json_encode($user_info->tagid_list),
  179. 'subscribe_scene' => $user_info->subscribe_scene ?: "",
  180. 'qr_scene' => $user_info->qr_scene ?: $user_info->qr_scene_str
  181. ]);
  182. } catch (\Exception $e) {
  183. dump($e);
  184. }
  185. } else {
  186. \DB::table("pocket.kdgx_fans_" . substr($this->publicId, -1))
  187. ->where('openid', $user_info->openid)
  188. ->update([
  189. 'subscribe' => 0
  190. ]);
  191. }
  192. }
  193. }