diff --git a/plugin/BpConsumption/BpConsumption.php b/plugin/BpConsumption/BpConsumption.php new file mode 100644 index 0000000..556d610 --- /dev/null +++ b/plugin/BpConsumption/BpConsumption.php @@ -0,0 +1,153 @@ + __CLASS__, // hook + 'name' => 'BpConsumption', // 插件名称 + 'version' => '0.0.1', // 插件版本 + 'desc' => '大会员B币券消费', // 插件描述 + 'author' => 'Lkeme',// 作者 + 'priority' => 1108, // 插件优先级 + 'cycle' => '24(小时)', // 运行周期 + ]; + + /** + * @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('bp_consumption')) return; + // + $this->consumptionTask(); + // 定时14点 + 随机120分钟| 根据逻辑前置 + TimeLock::setTimes(TimeLock::timing(14) + mt_rand(1, 120) * 60); + } + + /** + * @use 消费 + * @return void + */ + protected function consumptionTask(): void + { + // 如果为年度大会员 + if (!User::isYearVip('消费B币券')) return; + // 获取B币余额 + $bp_balance = $this->getUserWallet(); + // 最大支持5 + if ($bp_balance != 5) return; + // 消费B币充电 + if (getConf('bp_consumption.bp2charge', false, 'bool')) { + // UID为空就切换成自己的 + $user = User::parseCookie(); + $up_mid = getConf('bp_consumption.bp2charge_uid', intval($user['uid']), 'int'); + $this->BP2charge($up_mid, $bp_balance); + return; + } + // 消费B币充值金瓜子 + if (getConf('bp_consumption.bp2gold', false, 'bool')) { + $this->BP2gold($bp_balance); + } + + } + + /** + * B币充值金瓜子 + * @param int $num + */ + protected function BP2gold(int $num): void + { + // {"code":1300014,"message":"b币余额不足","ttl":1,"data":null} + // {"code":0,"message":"0","ttl":1,"data":{"status":2,"order_id":"1234171134577071132741234","gold":0,"bp":5000}} + $response = ApiPay::gold($num); + // + if ($response['code']) { + Log::warning("消费B币券: 充值金瓜子失败 {$response['code']} -> {$response['message']}"); + } else { + Log::notice("消费B币券: 充值金瓜子成功 NUM -> {$response['data']['bp']} ORDER -> {$response['data']['order_id']}"); + } + } + + /** + * @use B币充电 + * @param int $uid + * @param int $num + */ + protected function BP2charge(int $uid, int $num = 5): void + { + // {"code":0,"message":"0","ttl":1,"data":{"mid":12324,"up_mid":1234,"order_no":"PAY4567","bp_num":"5","exp":5,"status":4,"msg":""}} + // {"code":0,"message":"0","ttl":1,"data":{"mid":12324,"up_mid":1234,"order_no":"ABCD","bp_num":2,"exp":2,"status":4,"msg":""}} + $response = ApiPay::battery($uid, $num); + // + if ($response['code']) { + Log::warning("消费B币券: 给{$uid}充电失败 {$response['code']} -> {$response['message']}"); + } else { + // data.status 4 成功 -2:低于20电池下限 -4:B币不足 + if ($response['data']['status'] == 4) { + Log::notice("消费B币券: 给{$uid}充电成功 NUM -> {$response['data']['bp_num']} EXP -> {$response['data']['exp']} ORDER -> {$response['data']['order_no']}"); + } else { + Log::warning("消费B币券: 给{$uid}充电失败 {$response['data']['status']} -> {$response['data']['msg']}"); + } + } + } + + /** + * @use 获取钱包B币券余额 + * @return int + */ + protected function getUserWallet(): int + { + // {"errno":0,"msg":"SUCCESS","showMsg":"","errtag":0,"data":{"mid":1234,"totalBp":5.00,"defaultBp":0.00,"iosBp":0.00,"couponBalance":5.00,"availableBp":5.00,"unavailableBp":0.00,"unavailableReason":"苹果设备上充值的B币不能在其他平台的设备上进行使用","tip":null}} + $response = ApiWallet::getUserWallet(); + if ($response['errno']) { + Log::warning("消费B币券: 获取用户钱包信息失败 {$response['errno']} -> {$response['msg']}"); + return 0; + } + // + $balance = $response['data']['couponBalance']; + Log::info("消费B币券: 获取用户钱包信息成功 B币券余额剩余 {$balance}"); + // + return intval($balance); + } + +} + \ No newline at end of file diff --git a/plugin/Judge/Judge.php b/plugin/Judge/Judge.php index 12490ab..79db501 100644 --- a/plugin/Judge/Judge.php +++ b/plugin/Judge/Judge.php @@ -34,7 +34,7 @@ class Judge extends BasePlugin 'version' => '0.0.1', // 插件版本 'desc' => '風機委員', // 插件描述 'author' => 'Lkeme',// 作者 - 'priority' => 1126, // 插件优先级 + 'priority' => 1106, // 插件优先级 'cycle' => '15-30(分钟)', // 运行周期 ]; diff --git a/profile/example/config/user.ini b/profile/example/config/user.ini index 758c800..53dd444 100644 --- a/profile/example/config/user.ini +++ b/profile/example/config/user.ini @@ -69,6 +69,16 @@ auto_apply = false [vip_privilege] enable = false +; B币券消费/年度大会员专享/每月默认充值数量5B币/消费方式优先级从上到下 +[bp_consumption] +enable = false +# 消费B币充电/充电的目标UID/可充值自己的UID +bp2charge = false +bp2charge_uid = 6580464 +# 消费B币充值金瓜子/5000金瓜子 +bp2gold = false + + ####################### # 通知设置 # ####################### diff --git a/src/Api/Pay/ApiPay.php b/src/Api/Pay/ApiPay.php new file mode 100644 index 0000000..2302fc5 --- /dev/null +++ b/src/Api/Pay/ApiPay.php @@ -0,0 +1,82 @@ + 'pc', + 'pay_bp' => $num * 1000, // 瓜子数量 + 'context_id' => 1, // 直播间 + 'context_type' => 11, + 'goods_id' => 1, // 商品ID + 'goods_num' => $num, // B币数量 + 'csrf_token' => $user['csrf'], + 'csrf' => $user['csrf'], + 'visit_id' => '', + ]; + $headers = [ + 'origin' => 'https://link.bilibili.com', + 'referer' => 'https://link.bilibili.com/p/center/index' + ]; + // {"code":1300014,"message":"b币余额不足","ttl":1,"data":null} + // {"code":0,"message":"0","ttl":1,"data":{"status":2,"order_id":"1234171134577071132741234","gold":0,"bp":5000}} + return Request::postJson(true, 'pc', $url, $payload, $headers); + } + + /** + * @use 电池 + * @param int $up_mid + * @param int $num + * @return array + */ + public static function battery(int $up_mid, int $num = 5): array + { + $user = User::parseCookie(); + // + $url = 'https://api.bilibili.com/x/ugcpay/web/v2/trade/elec/pay/quick'; + $payload = [ + 'bp_num' => $num, // 数量 + 'is_bp_remains_prior' => true, // 是否优先扣除B币余额 + 'up_mid' => $up_mid, // 目标UID + 'otype' => 'up', // 来源 up:空间充电 archive:视频充电 + 'oid' => $up_mid, // 目标UID or 稿件avid + 'csrf' => $user['csrf'] + ]; + // {"code":0,"message":"0","ttl":1,"data":{"mid":12324,"up_mid":1234,"order_no":"PAY4567","bp_num":"5","exp":5,"status":4,"msg":""}} + // {"code":0,"message":"0","ttl":1,"data":{"mid":12324,"up_mid":1234,"order_no":"ABCD","bp_num":2,"exp":2,"status":4,"msg":""}} + return Request::postJson(true, 'pc', $url, $payload); + } + + +} \ No newline at end of file diff --git a/src/Api/Pay/ApiWallet.php b/src/Api/Pay/ApiWallet.php new file mode 100644 index 0000000..46c435a --- /dev/null +++ b/src/Api/Pay/ApiWallet.php @@ -0,0 +1,49 @@ + 3, + 'platformType' => 3, + 'timestamp' => $ts, + 'traceId' => $ts, + 'version' => '1.0', + ]; + $headers = [ + 'Content-Type' => 'application/json;charset=utf-8', + 'origin' => 'https://pay.bilibili.com', + 'referer' => 'https://pay.bilibili.com/paywallet-fe/bb_balance.html' + ]; + // {"errno":0,"msg":"SUCCESS","showMsg":"","errtag":0,"data":{"mid":1234,"totalBp":5.00,"defaultBp":0.00,"iosBp":0.00,"couponBalance":5.00,"availableBp":5.00,"unavailableBp":0.00,"unavailableReason":"苹果设备上充值的B币不能在其他平台的设备上进行使用","tip":null}} + return Request::putJson(true, 'pc', $url, $payload, $headers); + } + +} \ No newline at end of file