[feat] BpConsumption

This commit is contained in:
Lkeme 2022-06-06 17:09:57 +08:00
parent 41f47d7ff3
commit 00122921bb
5 changed files with 295 additions and 1 deletions

View File

@ -0,0 +1,153 @@
<?php declare(strict_types=1);
/**
* Website: https://mudew.com/
* Author: Lkeme
* License: The MIT License
* Email: Useri@live.cn
* Updated: 2022 ~ 2023
*
* _____ _ _ _ _ _ _____ _ _____ _____ _____
* | _ \ | | | | | | | | | | | ____| | | | _ \ | ____| | _ \ & l、
* | |_| | | | | | | | | |_| | | |__ | | | |_| | | |__ | |_| | (゚、
* | _ { | | | | | | | _ | | __| | | | ___/ | __| | _ /   \、゙ ~ *
* | |_| | | | | |___ | | | | | | | |___ | |___ | | | |___ | | \ \  じしf_, )
* |_____/ |_| |_____| |_| |_| |_| |_____| |_____| |_| |_____| |_| \_\
*/
use Bhp\Api\Pay\ApiPay;
use Bhp\Api\Pay\ApiWallet;
use Bhp\Log\Log;
use Bhp\Plugin\BasePlugin;
use Bhp\Plugin\Plugin;
use Bhp\TimeLock\TimeLock;
use Bhp\User\User;
class BpConsumption extends BasePlugin
{
/**
* 插件信息
* @var array|string[]
*/
protected ?array $info = [
'hook' => __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电池下限 -4B币不足
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);
}
}

View File

@ -34,7 +34,7 @@ class Judge extends BasePlugin
'version' => '0.0.1', // 插件版本
'desc' => '風機委員', // 插件描述
'author' => 'Lkeme',// 作者
'priority' => 1126, // 插件优先级
'priority' => 1106, // 插件优先级
'cycle' => '15-30(分钟)', // 运行周期
];

View File

@ -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
#######################
# 通知设置 #
#######################

82
src/Api/Pay/ApiPay.php Normal file
View File

@ -0,0 +1,82 @@
<?php declare(strict_types=1);
/**
* Website: https://mudew.com/
* Author: Lkeme
* License: The MIT License
* Email: Useri@live.cn
* Updated: 2022 ~ 2023
*
* _____ _ _ _ _ _ _____ _ _____ _____ _____
* | _ \ | | | | | | | | | | | ____| | | | _ \ | ____| | _ \ & l、
* | |_| | | | | | | | | |_| | | |__ | | | |_| | | |__ | |_| | (゚、
* | _ { | | | | | | | _ | | __| | | | ___/ | __| | _ /   \、゙ ~ *
* | |_| | | | | |___ | | | | | | | |___ | |___ | | | |___ | | \ \  じしf_, )
* |_____/ |_| |_____| |_| |_| |_| |_____| |_____| |_| |_____| |_| \_\
*/
namespace Bhp\Api\Pay;
use Bhp\Request\Request;
use Bhp\User\User;
use Bhp\Util\Common\Common;
class ApiPay
{
/**
* @use 金瓜子
* @param int $num
* @return array
*/
public static function gold(int $num): array
{
$user = User::parseCookie();
//
$url = 'https://api.live.bilibili.com/xlive/revenue/v1/order/createOrder';
$payload = [
'platform' => '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);
}
}

49
src/Api/Pay/ApiWallet.php Normal file
View File

@ -0,0 +1,49 @@
<?php declare(strict_types=1);
/**
* Website: https://mudew.com/
* Author: Lkeme
* License: The MIT License
* Email: Useri@live.cn
* Updated: 2022 ~ 2023
*
* _____ _ _ _ _ _ _____ _ _____ _____ _____
* | _ \ | | | | | | | | | | | ____| | | | _ \ | ____| | _ \ & l、
* | |_| | | | | | | | | |_| | | |__ | | | |_| | | |__ | |_| | (゚、
* | _ { | | | | | | | _ | | __| | | | ___/ | __| | _ /   \、゙ ~ *
* | |_| | | | | |___ | | | | | | | |___ | |___ | | | |___ | | \ \  じしf_, )
* |_____/ |_| |_____| |_| |_| |_| |_____| |_____| |_| |_____| |_| \_\
*/
namespace Bhp\Api\Pay;
use Bhp\Request\Request;
use Bhp\Util\Common\Common;
class ApiWallet
{
/**
* @use 获取用户钱包
* @return array
*/
public static function getUserWallet(): array
{
$url = 'https://pay.bilibili.com/paywallet/wallet/getUserWallet';
$ts = Common::getUnixTimestamp();
$payload = [
'panelType' => 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);
}
}