diff --git a/plugin/VipPrivilege/VipPrivilege.php b/plugin/VipPrivilege/VipPrivilege.php new file mode 100644 index 0000000..2604531 --- /dev/null +++ b/plugin/VipPrivilege/VipPrivilege.php @@ -0,0 +1,131 @@ + __CLASS__, // hook + 'name' => 'VipPrivilege', // 插件名称 + 'version' => '0.0.1', // 插件版本 + 'desc' => '领取大会员权益', // 插件描述 + 'author' => 'Lkeme',// 作者 + 'priority' => 1107, // 插件优先级 + 'cycle' => '24(小时)', // 运行周期 + ]; + + /** + * @var array|string[] + */ + protected array $privilege = [ + 0 => '未知奖励', + 1 => 'B币劵', + 2 => '会员购优惠券' + ]; + + /** + * @param Plugin $plugin + */ + public function __construct(Plugin &$plugin) + { + // + TimeLock::initTimeLock(); + // $this::class + $plugin->register($this, 'execute'); + } + + /** + * @use 执行 + * @return void + */ + public function execute(): void + { + if (TimeLock::getTimes() > time() || !getEnable('vip_privilege')) return; + // + $this->receiveTask(); + // + // 定时11点 + 随机120分钟 + TimeLock::setTimes(TimeLock::timing(11) + mt_rand(1, 120) * 60); + } + + /** + * @use 领取 + * @return void + */ + protected function receiveTask(): void + { + // 如果为年度大会员 + if (!User::isYearVip('大会员权益')) return; + // + $privilege_list = $this->myVipPrivilege(); + // + foreach ($privilege_list as $privilege) { + // 是否领取状态 + if ($privilege['state'] != 0) { + continue; + } + // 领取奖励 + $this->myVipPrivilegeReceive($privilege['type']); + } + } + + /** + * @use 获取我的大会员权益列表 + * @return array + */ + protected function myVipPrivilege(): array + { + // {"code":0,"message":"0","ttl":1,"data":{"list":[{"type":1,"state":0,"expire_time":1622476799},{"type":2,"state":0,"expire_time":1622476799}]}} + $response = ApiPrivilege::my(); + // + if ($response['code']) { + Log::warning("大会员权益: 获取权益列表失败 {$response['code']} -> {$response['message']}"); + return []; + } else { + Log::info('大会员权益: 获取权益列表成功 ' . count($response['data']['list'])); + return $response['data']['list']; + } + } + + /** + * @use 领取我的大会员权益 + * @param int $type + */ + protected function myVipPrivilegeReceive(int $type): void + { + // {"code":0,"message":"0","ttl":1} + $response = ApiPrivilege::receive($type); + // + if ($response['code']) { + Log::warning("大会员权益: 领取权益 {$this->privilege[$type]} 失败 {$response['code']} -> {$response['message']}"); + } else { + Log::warning("大会员权益: 领取权益 {$this->privilege[$type]} 成功"); + } + } + +} + \ No newline at end of file diff --git a/profile/example/config/user.ini b/profile/example/config/user.ini index ac5e26a..758c800 100644 --- a/profile/example/config/user.ini +++ b/profile/example/config/user.ini @@ -65,6 +65,10 @@ enable = true enable = false auto_apply = false +; 大会员权益/年度大会员专享 +[vip_privilege] +enable = false + ####################### # 通知设置 # ####################### diff --git a/src/Api/Vip/ApiPrivilege.php b/src/Api/Vip/ApiPrivilege.php new file mode 100644 index 0000000..25b7a55 --- /dev/null +++ b/src/Api/Vip/ApiPrivilege.php @@ -0,0 +1,63 @@ + 'https://account.bilibili.com', + 'referer' => 'https://account.bilibili.com/account/big/myPackage', + ]; + // {"code":0,"message":"0","ttl":1,"data":{"list":[{"type":1,"state":0,"expire_time":1622476799},{"type":2,"state":0,"expire_time":1622476799}]}} + return Request::getJson(true, 'pc', $url, $payload, $headers); + } + + /** + * @use 领取权益 + * @param int $type + * @return array + */ + public static function receive(int $type): array + { + $user = User::parseCookie(); + // + $url = 'https://api.bilibili.com/x/vip/privilege/receive'; + $payload = [ + 'type' => $type, + 'csrf' => $user['csrf'], + ]; + $headers = [ + 'origin' => 'https://account.bilibili.com', + 'referer' => 'https://account.bilibili.com/account/big/myPackage', + ]; + // {"code":0,"message":"0","ttl":1} + return Request::postJson(true, 'pc', $url, $payload, $headers); + } + +} \ No newline at end of file diff --git a/src/Api/Vip/ApiUser.php b/src/Api/Vip/ApiUser.php new file mode 100644 index 0000000..17057a8 --- /dev/null +++ b/src/Api/Vip/ApiUser.php @@ -0,0 +1,39 @@ + 'https://account.bilibili.com', + 'referer' => 'https://account.bilibili.com/account/home' + ]; + // {"code":0,"message":"0","ttl":1,"data":{"mid":1234,"vip_type":2,"vip_status":1,"vip_due_date":1667750400000,"vip_pay_type":0,"theme_type":0,"label":{"text":"年度大会员","label_theme":"annual_vip","text_color":"#FFFFFF","bg_style":1,"bg_color":"#FB7299","border_color":""},"avatar_subscript":1,"avatar_subscript_url":"http://i0.hdslb.com/bfs/vip/icon_Certification_big_member_22_3x.png","nickname_color":"#FB7299","is_new_user":false}} + return Request::getJson(true, 'pc', $url, $payload, $headers); + } +} \ No newline at end of file diff --git a/src/User/User.php b/src/User/User.php index d8f5c71..2d15cbd 100644 --- a/src/User/User.php +++ b/src/User/User.php @@ -17,6 +17,9 @@ namespace Bhp\User; +use Bhp\Api\Vip\ApiUser; +use Bhp\Log\Log; +use Bhp\Util\Common\Common; use Bhp\Util\DesignPattern\SingleTon; use JetBrains\PhpStorm\ArrayShape; @@ -48,5 +51,27 @@ class User extends SingleTon ]; } + /** + * @use 是否为有效年度大会员 + * @param string $title + * @return bool + */ + public static function isYearVip(string $title = '用户信息'): bool + { + $response = ApiUser::userInfo(); + // + if ($response['code']) { + Log::warning("$title: 获取大会员信息失败 {$response['code']} -> {$response['message']}"); + return false; + } + // + if ($response['data']['vip_type'] == 2 && $response['data']['vip_due_date'] > Common::getUnixTimestamp()) { + Log::info("$title: 获取大会员信息成功 是有效的年度大会员"); + return true; + } + // + Log::warning("$title: 获取大会员信息成功 不是年度大会员或已过期"); + return false; + } } \ No newline at end of file