123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Bear
- * Date: 2019/1/7
- * Time: 下午3:49
- */
- namespace app\admin\command;
- use app\common\library\Redis;
- use app\main\constants\CacheConstants;
- use app\main\model\object\UserObject;
- use app\main\service\OfficialAccountsService;
- use app\main\service\UserService;
- use think\console\Command;
- use think\console\Input;
- use think\console\input\Option;
- use think\console\Output;
- use think\Request;
- class ClearUser extends Command
- {
- public function Configure()
- {
- $this
- ->setName('clearUser')
- ->addOption('user_id', 'u', Option::VALUE_REQUIRED, 'user_id', null)
- ->addOption('openid', 'o', Option::VALUE_REQUIRED, 'openid', null)
- ->addOption('type', 't', Option::VALUE_REQUIRED, '类型', 'user.cache')
- ->addOption('params', 'p', Option::VALUE_REQUIRED, '书币数量', '0,0')
- ->setDescription('clear user in db and redis');
- }
- /**
- * @param Input $input
- * @param Output $output
- * @return int|null|void
- * @throws \Exception
- * @throws \think\Exception
- */
- public function execute(Input $input, Output $output)
- {
- Request::instance()->module('admin');
- $user_id = $input->getOption('user_id');
- $type = $input->getOption('type');
- $open_id = $input->getOption('openid');
- $params = explode(',', $input->getOption('params'));
- switch ($type) {
- case 'user':
- $this->clearUser($user_id, $open_id);
- break;
- case 'user.cache':
- $this->clearCache($user_id);
- break;
- default:
- $this->clearCache($user_id);
- }
- }
- /**
- * 清除用户缓存
- * @param $user_id
- */
- public function clearCache($user_id)
- {
- $key = 'UN:' . $user_id;
- echo 'cache key: ' . $key . "\n";
- if (Redis::instance()->exists($key)) {
- if (Redis::instance()->delete($key)) {
- echo "cache cleared";
- } else {
- echo "cache clear error";
- }
- } else {
- echo 'cache not exists';
- }
- echo "\n";
- }
- /**
- * 删除用户信息
- * @param $user_id
- * @param $open_id
- * @throws \think\Exception
- */
- public function clearUser($user_id, $open_id)
- {
- //可不提供此参数
- $user = UserService::instance()->getUserModel()->setConnect($user_id)->where('id', '=', $user_id)->find()->toArray();
- $userObj = (new UserObject())->bind($user);
- if (!$open_id) {
- $open_id = $userObj->openid;
- }
- echo "\nuser_id:" . $user_id;
- echo "\nopenid:" . $open_id;
- if ($open_id == $user_id) {
- echo "\nuser_id and open_id is same,exit\n";
- return;
- }
- echo "\nchannel_id:" . $userObj->channel_id;
- echo "\nuser库:" . UserService::instance()->getUserDBIndex($user_id);
- echo "\nopen库:" . UserService::instance()->getOpenIdDBIndex($userObj->channel_id, $open_id);
- echo "\nstart clear...";
- echo "\nclear user";
- if (UserService::instance()->getUserModel()->setConnect($user_id)->where('id', '=', $user_id)->find()) {
- UserService::instance()->getUserModel()->setConnect($user_id)->where('id', '=', $user_id)->setField('openid', $user_id);
- } else {
- echo "\nuser not exists\n";
- }
- echo "\nclear open";
- if (OfficialAccountsService::instance()->getOpenidModel()->setConnect($userObj->channel_id, $open_id)->where('user_id', '=', $user_id)->find()) {
- OfficialAccountsService::instance()->getOpenidModel()->setConnect($userObj->channel_id, $open_id)->where('user_id', '=', $user_id)->setField('channel_openid', $user_id);
- } else {
- echo "\nopenid not exists";
- }
- $userKey = CacheConstants::getUserCacheKey($user_id);
- if (Redis::instance()->exists($userKey)) {
- echo "\nclear cache";
- Redis::instance()->del($userKey);
- } else {
- echo "\ncache not exists";
- }
- echo "\nend clear...\n";
- }
- }
|