123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- <?php
- namespace app\command;
- use think\console\Command;
- use think\console\Input;
- use think\console\Output;
- use think\Db;
- /**
- * 工行积分队列处理命令
- * 命令行运行: php think icbc_queue
- */
- class IcbcQueue extends Command
- {
- protected function configure()
- {
- $this->setName('icbc_queue')
- ->setDescription('处理工行积分推送队列');
- }
- protected function execute(Input $input, Output $output)
- {
- $output->writeln("开始处理工行积分队列...");
-
- // 获取待处理的任务
- $tasks = Db::name('icbc_queue')
- ->where('status', 0)
- ->where('retry_count', '<', 3)
- ->limit(50)
- ->select();
-
- if (empty($tasks)) {
- $output->writeln("没有待处理的任务");
- return;
- }
-
- $output->writeln("找到 " . count($tasks) . " 个待处理任务");
-
- foreach ($tasks as $task) {
- try {
- // 更新状态为处理中
- Db::name('icbc_queue')->where('id', $task['id'])->update([
- 'status' => 1,
- 'updatetime' => time()
- ]);
-
- $output->writeln("处理任务 ID: {$task['id']}, 手机号: {$task['mobile_phone']}, 积分: {$task['integral_value']}");
-
- // 调用工行接口
- $result = $this->callIcbcApi($task);
-
- // 根据结果更新状态
- if (isset($result['return_code']) && $result['return_code'] == 0) {
- // 成功
- Db::name('icbc_queue')->where('id', $task['id'])->update([
- 'status' => 2,
- 'result' => json_encode($result, JSON_UNESCAPED_UNICODE),
- 'updatetime' => time()
- ]);
- $output->writeln("✓ 任务 {$task['id']} 处理成功");
- } else {
- // 失败,增加重试次数
- $retry_count = $task['retry_count'] + 1;
- $status = $retry_count >= 3 ? 3 : 0;
-
- Db::name('icbc_queue')->where('id', $task['id'])->update([
- 'status' => $status,
- 'retry_count' => $retry_count,
- 'result' => json_encode($result, JSON_UNESCAPED_UNICODE),
- 'error_msg' => isset($result['return_msg']) ? $result['return_msg'] : '未知错误',
- 'updatetime' => time()
- ]);
-
- $output->writeln("✗ 任务 {$task['id']} 处理失败: " . (isset($result['return_msg']) ? $result['return_msg'] : '未知错误'));
- }
-
- } catch (\Exception $e) {
- // 异常处理
- Db::name('icbc_queue')->where('id', $task['id'])->update([
- 'status' => 0,
- 'retry_count' => $task['retry_count'] + 1,
- 'error_msg' => $e->getMessage(),
- 'updatetime' => time()
- ]);
-
- $output->writeln("✗ 任务 {$task['id']} 异常: " . $e->getMessage());
- }
-
- // 避免请求过快
- usleep(100000); // 0.1秒
- }
-
- $output->writeln("队列处理完成");
- }
- /**
- * 调用工行接口
- */
- private function callIcbcApi($task)
- {
- include_once EXTEND_PATH . 'icbc/DefaultIcbcClient.php';
-
- // 生成16位唯一序列号
- $fSeqNo = time() . str_pad(mt_rand(0, 999999), 6, '0', STR_PAD_LEFT);
-
- // 构建业务参数
- $biz_content = [
- 'fSeqNo' => $fSeqNo,
- 'corpCode' => 'xingfulishequ',
- 'mobilePhone' => $task['mobile_phone'],
- 'integralValue' => (string)$task['integral_value'],
- 'integralType' => $task['integral_type'],
- 'remark1' => $task['nickname'],
- ];
-
- // 工行RSA密钥
- $public_key = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCwFgHD4kzEVPdOj03ctKM7KV+16bWZ5BMNgvEeuEQwfQYkRVwI9HFOGkwNTMn5hiJXHnlXYCX+zp5r6R52MY0O7BsTCLT7aHaxsANsvI9ABGx3OaTVlPB59M6GPbJh0uXvio0m1r/lTW3Z60RU6Q3oid/rNhP3CiNgg0W6O3AGqwIDAQAB';
- $private_key = 'MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCR8/ZvKPAdZzsyvapySvztQm56s1N59ynKMOWpUbK4c5MNWMl+q3dvsp+UiSAx2TAeGkYfW3W6nO/3Y0hAZki99fmuzpPujEeHhs79HNwGZYQjN71Vck2JeflTq8jpL+9/up0Kz2nbwtZDMKTTE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lI0fFEZycxYj65UlAoGAD9p1RZlkLfuGgf2llRgOF4zK3o+MHYXiuep0PioUkECFE4ixpGh0Vtf6nkbjHTgteYK6O1iQsppPfCgRrheQBkp9WhTZMfkbP6p2u+nof4ET2PrUQ16naj1eL655erLpKypADORZVMSVxDhAPdKLAfuHH1DI5ed8qXsF4PGKb7kCgYEAm/d+daT6YsbHDZlJ/J9Q8rRkKmiqj43NGQSHKg6Z6BEDibm8wRmj3Itu1N6XVChuaH+ekJzvUnZ/q1nyYzGvy6bOHYn3ziF9aH7wuhcRZ4qARmKDnzTBLg2QXBK1+400O3LJ+sAH/yuH/Y4hzRE6YMxBQpdYfnlJcIFVimAsT1w=';
-
- // 构建通用请求参数
- $data = [
- 'app_id' => '10000000000004096993',
- 'msg_id' => createUniqueNo('msg', time()),
- 'format' => 'json',
- 'charset' => 'UTF-8',
- 'encrypt_type' => 'AES',
- 'sign_type' => 'RSA2',
- ];
-
- try {
- // 创建工行客户端
- $client = new \DefaultIcbcClient(
- $data['app_id'],
- $private_key,
- $data['sign_type'],
- $data['charset'],
- $data['format'],
- $public_key,
- '', '', '', ''
- );
-
- // 构建请求参数
- $request = [
- 'serviceUrl' => 'https://gw.dccnet.com.cn:8084/api/mybank/farm/farmplatf/updateVillagerIntegral/V1',
- 'method' => 'POST',
- 'isNeedEncrypt' => false,
- 'extraParams' => null,
- 'biz_content' => $biz_content,
- ];
-
- // 发送请求
- $response = $client->execute($request, $data['msg_id'], '');
-
- // 解析响应
- if (empty($response)) {
- return ['return_code' => '-1', 'return_msg' => '接口无响应'];
- }
-
- $result = json_decode($response, true);
-
- if (json_last_error() !== JSON_ERROR_NONE) {
- return ['return_code' => '-2', 'return_msg' => 'JSON解析失败'];
- }
-
- // 写入日志
- $this->writeLog($result['return_code'] == 0, $result, $biz_content);
-
- return $result;
-
- } catch (\Exception $e) {
- return ['return_code' => '-99', 'return_msg' => '异常: ' . $e->getMessage()];
- }
- }
- /**
- * 写入日志
- */
- private function writeLog($is_success, $result, $request_data)
- {
- $log_dir = RUNTIME_PATH . 'icbc_log/';
- if (!is_dir($log_dir)) {
- mkdir($log_dir, 0755, true);
- }
-
- $filename = $is_success ? 'suc.txt' : 'failicbc.txt';
- $filepath = $log_dir . $filename;
-
- $log_content = [
- '时间' => date('Y-m-d H:i:s'),
- '请求数据' => $request_data,
- '返回数据' => $result,
- '分隔线' => str_repeat('-', 80)
- ];
-
- $log_text = "\n" . json_encode($log_content, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) . "\n";
- file_put_contents($filepath, $log_text, FILE_APPEND);
- }
- }
|