ClearUser.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Bear
  5. * Date: 2019/1/7
  6. * Time: 下午3:49
  7. */
  8. namespace app\admin\command;
  9. use app\common\library\Redis;
  10. use app\main\constants\CacheConstants;
  11. use app\main\model\object\UserObject;
  12. use app\main\service\OfficialAccountsService;
  13. use app\main\service\UserService;
  14. use think\console\Command;
  15. use think\console\Input;
  16. use think\console\input\Option;
  17. use think\console\Output;
  18. use think\Request;
  19. class ClearUser extends Command
  20. {
  21. public function Configure()
  22. {
  23. $this
  24. ->setName('clearUser')
  25. ->addOption('user_id', 'u', Option::VALUE_REQUIRED, 'user_id', null)
  26. ->addOption('openid', 'o', Option::VALUE_REQUIRED, 'openid', null)
  27. ->addOption('type', 't', Option::VALUE_REQUIRED, '类型', 'user.cache')
  28. ->addOption('params', 'p', Option::VALUE_REQUIRED, '书币数量', '0,0')
  29. ->setDescription('clear user in db and redis');
  30. }
  31. /**
  32. * @param Input $input
  33. * @param Output $output
  34. * @return int|null|void
  35. * @throws \Exception
  36. * @throws \think\Exception
  37. */
  38. public function execute(Input $input, Output $output)
  39. {
  40. Request::instance()->module('admin');
  41. $user_id = $input->getOption('user_id');
  42. $type = $input->getOption('type');
  43. $open_id = $input->getOption('openid');
  44. $params = explode(',', $input->getOption('params'));
  45. switch ($type) {
  46. case 'user':
  47. $this->clearUser($user_id, $open_id);
  48. break;
  49. case 'user.cache':
  50. $this->clearCache($user_id);
  51. break;
  52. default:
  53. $this->clearCache($user_id);
  54. }
  55. }
  56. /**
  57. * 清除用户缓存
  58. * @param $user_id
  59. */
  60. public function clearCache($user_id)
  61. {
  62. $key = 'UN:' . $user_id;
  63. echo 'cache key: ' . $key . "\n";
  64. if (Redis::instance()->exists($key)) {
  65. if (Redis::instance()->delete($key)) {
  66. echo "cache cleared";
  67. } else {
  68. echo "cache clear error";
  69. }
  70. } else {
  71. echo 'cache not exists';
  72. }
  73. echo "\n";
  74. }
  75. /**
  76. * 删除用户信息
  77. * @param $user_id
  78. * @param $open_id
  79. * @throws \think\Exception
  80. */
  81. public function clearUser($user_id, $open_id)
  82. {
  83. //可不提供此参数
  84. $user = UserService::instance()->getUserModel()->setConnect($user_id)->where('id', '=', $user_id)->find()->toArray();
  85. $userObj = (new UserObject())->bind($user);
  86. if (!$open_id) {
  87. $open_id = $userObj->openid;
  88. }
  89. echo "\nuser_id:" . $user_id;
  90. echo "\nopenid:" . $open_id;
  91. if ($open_id == $user_id) {
  92. echo "\nuser_id and open_id is same,exit\n";
  93. return;
  94. }
  95. echo "\nchannel_id:" . $userObj->channel_id;
  96. echo "\nuser库:" . UserService::instance()->getUserDBIndex($user_id);
  97. echo "\nopen库:" . UserService::instance()->getOpenIdDBIndex($userObj->channel_id, $open_id);
  98. echo "\nstart clear...";
  99. echo "\nclear user";
  100. if (UserService::instance()->getUserModel()->setConnect($user_id)->where('id', '=', $user_id)->find()) {
  101. UserService::instance()->getUserModel()->setConnect($user_id)->where('id', '=', $user_id)->setField('openid', $user_id);
  102. } else {
  103. echo "\nuser not exists\n";
  104. }
  105. echo "\nclear open";
  106. if (OfficialAccountsService::instance()->getOpenidModel()->setConnect($userObj->channel_id, $open_id)->where('user_id', '=', $user_id)->find()) {
  107. OfficialAccountsService::instance()->getOpenidModel()->setConnect($userObj->channel_id, $open_id)->where('user_id', '=', $user_id)->setField('channel_openid', $user_id);
  108. } else {
  109. echo "\nopenid not exists";
  110. }
  111. $userKey = CacheConstants::getUserCacheKey($user_id);
  112. if (Redis::instance()->exists($userKey)) {
  113. echo "\nclear cache";
  114. Redis::instance()->del($userKey);
  115. } else {
  116. echo "\ncache not exists";
  117. }
  118. echo "\nend clear...\n";
  119. }
  120. }