SchoolPhysicalExamination/application/OutsideInterface/controller/app/Savemsg.php

420 lines
16 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\ZengJieCode\controller\app;
use think\Db;
use think\Cache;
class Savemsg extends Base{
protected $default_head_pic = 'http://tc.pcxbc.com/tsf/head_pic.png';
protected $max_box_num = 10;
protected $name_list = [
[
'name'=>'LS-2502',
'character_uuid'=>'2A25',
'service_uuid'=>'180A'
],
];
protected $name_default = 0;
// 加 bcadd(,,20)
// 减 bcsub(,,20)
// 乘 bcmul(,,20)
// 除 bcdiv(,,20)
################################################################百度接口################################################################
################################################################百度接口################################################################
################################################################百度接口################################################################
// 获取配置信息
public function config_msg(){
try {
$result = $this->config_msg_action();
return $this->msg($result);
} catch (\Exception $e) {
// 捕获异常
$logContent["flie"] = $e->getFile();
$logContent["line"] = $e->getLine();
$logContent['all_content'] = "异常信息:\n";
$logContent['all_content'] .= "消息: " . $e->getMessage() . "\n";
$logContent['all_content'] .= "代码: " . $e->getCode() . "\n";
$logContent['all_content'] .= "文件: " . $e->getFile() . "\n";
$logContent['all_content'] .= "函数名: " . __FUNCTION__ . "\n";
$logContent['all_content'] .= "行号: " . $e->getLine() . "\n";
$logContent['all_content'] .= "跟踪信息:\n" . $e->getTraceAsString() . "\n";
$this->record_api_log($data, $logContent, null);
return $this->msg(99999);
}
}
public function save_sn_msg($data = ['sn_code'=>'564654564654654','mac_code'=>'ce:sh:yo:ng:d1','bl_name'=>'bl_5520']){
try {
// 你的业务逻辑
if(count(input('post.')) > 0){
$data = input('post.');
}
if(!array_key_exists('sn_code', $data)){
return $this->msg(10001);
}
if(!array_key_exists('bl_name', $data)){
return $this->msg(10001);
}
if(!array_key_exists('mac_code', $data)){
return $this->msg(10001);
}
if(!$this->verify_data_is_ok($data['sn_code'],'str')){
return $this->msg(10005);
}
if(!$this->verify_data_is_ok($data['bl_name'],'str')){
return $this->msg(10005);
}
if(!$this->verify_data_is_ok($data['mac_code'],'str')){
return $this->msg(10005);
}
$result = $this->save_sn_msg_action($data);
return $result;
} catch (\Exception $e) {
// 捕获异常
$logContent["flie"] = $e->getFile();
$logContent["line"] = $e->getLine();
$logContent['all_content'] = "异常信息:\n";
$logContent['all_content'] .= "消息: " . $e->getMessage() . "\n";
$logContent['all_content'] .= "代码: " . $e->getCode() . "\n";
$logContent['all_content'] .= "文件: " . $e->getFile() . "\n";
$logContent['all_content'] .= "函数名: " . __FUNCTION__ . "\n";
$logContent['all_content'] .= "行号: " . $e->getLine() . "\n";
$logContent['all_content'] .= "跟踪信息:\n" . $e->getTraceAsString() . "\n";
// dump($data);
// die;
$this->record_api_log($data, $logContent, null);
return $this->msg(99999);
}
}
public function save_box_msg($data = ['sn_code_all'=>'564654564654654,564654564654654','box_serial_number'=>'996589585']){
// try {
// 你的业务逻辑
if(count(input('post.')) > 0){
$data = input('post.');
}
if(!array_key_exists('sn_code_all', $data)){
return $this->msg(10001);
}
if(!array_key_exists('box_serial_number', $data)){
return $this->msg(10001);
}
if(!$this->verify_data_is_ok($data['sn_code_all'],'str')){
return $this->msg(10005);
}
if(!$this->verify_data_is_ok($data['box_serial_number'],'num&letter')){
return $this->msg(10005);
}
$result = $this->save_box_msg_action($data);
return $result;
// } catch (\Exception $e) {
// // 捕获异常
// $logContent["flie"] = $e->getFile();
// $logContent["line"] = $e->getLine();
// $logContent['all_content'] = "异常信息:\n";
// $logContent['all_content'] .= "消息: " . $e->getMessage() . "\n";
// $logContent['all_content'] .= "代码: " . $e->getCode() . "\n";
// $logContent['all_content'] .= "文件: " . $e->getFile() . "\n";
// $logContent['all_content'] .= "函数名: " . __FUNCTION__ . "\n";
// $logContent['all_content'] .= "行号: " . $e->getLine() . "\n";
// $logContent['all_content'] .= "跟踪信息:\n" . $e->getTraceAsString() . "\n";
// // dump($data);
// // die;
// $this->record_api_log($data, $logContent, null);
// return $this->msg(99999);
// }
}
public function print_again($data = ['code'=>'564654564654654','type'=>'sn']){
try {
// 你的业务逻辑
if(count(input('post.')) > 0){
$data = input('post.');
}
if(!array_key_exists('code', $data)){
return $this->msg(10001);
}
if(!array_key_exists('type', $data)){
return $this->msg(10001);
}
if(!$this->verify_data_is_ok($data['code'],'str')){
return $this->msg(10005);
}
if(!$this->verify_data_is_ok($data['type'],'str')){
return $this->msg(10005);
}
$result = $this->print_again_action($data);
return $result;
} catch (\Exception $e) {
// 捕获异常
$logContent["flie"] = $e->getFile();
$logContent["line"] = $e->getLine();
$logContent['all_content'] = "异常信息:\n";
$logContent['all_content'] .= "消息: " . $e->getMessage() . "\n";
$logContent['all_content'] .= "代码: " . $e->getCode() . "\n";
$logContent['all_content'] .= "文件: " . $e->getFile() . "\n";
$logContent['all_content'] .= "函数名: " . __FUNCTION__ . "\n";
$logContent['all_content'] .= "行号: " . $e->getLine() . "\n";
$logContent['all_content'] .= "跟踪信息:\n" . $e->getTraceAsString() . "\n";
// dump($data);
// die;
$this->record_api_log($data, $logContent, null);
return $this->msg(99999);
}
}
########################################################################action########################################################################
########################################################################action########################################################################
########################################################################action########################################################################
public function config_msg_action(){
// $length = strlen("0065601800007037");
// // 生成200个随机数字字符串
// $randomStrings = [];
// for ($i = 0; $i < 200; $i++) {
// $randomString = '';
// for ($j = 0; $j < $length; $j++) {
// $randomString .= rand(0, 9); // 生成0-9的随机数字
// }
// $randomStrings[] = [
// 'bluetooth_name'=>'bl_5520',
// 'sn_code'=>$randomString,
// 'create_time'=>date('Y-m-d H:i:s'),
// ];
// }
// $zengjie = Db::connect('zengjie_db');
// $result = $zengjie->table('sn_code_all')->insertAll($randomStrings);
// dump($result);
// die;
$data = [
'max_box_num'=>$this->max_box_num,
'name_list'=>$this->name_list,
'name_default_key'=>$this->name_default,
];
return $data;
}
public function save_sn_msg_action($data){
$data = $this->chuli_str_kongbai($data);
$zengjie = Db::connect('zengjie_db');
$sn_code = $zengjie->table('sn_code_all')->where(['sn_code'=>$data['sn_code']])->count();
if($sn_code > 0){
return $this->msg(10002,'该码已录入');
}
$result = $zengjie->table('sn_code_all')->insert([
'sn_code'=>$data['sn_code'],
'bluetooth_name'=>$data['bl_name'],
'mac_code'=>$data['mac_code'],
'create_time'=>date('Y-m-d H:i:s'),
]);
if($result){
return $this->msg([]);
}else{
return $this->msg(10002,'录入失败');
}
}
public function save_box_msg_action($data) {
$data = $this->chuli_str_kongbai($data);
$zengjie = Db::connect('zengjie_db');
// 1. 检查sn_code_all中是否有重复数据
$snCodes = explode(',', $data['sn_code_all']);
$snCodes = $this->chuli_str_kongbai($snCodes);
if (count($snCodes) !== count(array_unique($snCodes))) {
return $this->msg(10003, 'SN码列表中存在重复数据');
}
// 2. 检查这些SN码是否都存在于sn_code_all表中
$existCount = $zengjie->table('sn_code_all')
->where('sn_code', 'in', $snCodes)
->count();
if ($existCount !== count($snCodes)) {
return $this->msg(10004, '存在未登记的SN码');
}
Db::startTrans();
try{
$count = $zengjie->table('box_code_all')
->where(['box_code'=>$data['box_serial_number']])
->count();
$num = $count + 1;
$box_id = $zengjie->table('box_code_all')->insertGetId([
'box_code' => $data['box_serial_number'],
'box_num' => $num,
'create_time' => date('Y-m-d H:i:s'),
'content_str' => json_encode($snCodes)
]);
$affectedRows = $zengjie->table('sn_code_all')
->where('sn_code', 'in', $snCodes)
->update(['batch_id' => $box_id]);
// 检查更新是否影响了预期的行数
if ($affectedRows !== count($snCodes)) {
throw new \Exception('更新SN码数量错误请刷新小程序后重新录入');
}
Db::commit();
return $this->msg(['id'=>$box_id]);
} catch (\Exception $e) {
// 回滚事务
Db::rollback();
return $this->msg(10002, '录入失败: ' . $e->getMessage());
}
// // 显式事务控制
// $zengjie->startTrans();
// try {
// $count = $zengjie->table('box_code_all')
// ->where(['box_code'=>$data['box_serial_number']])
// ->count();
// $num = $count + 1;
// $box_id = $zengjie->table('box_code_all')->insertGetId([
// 'box_code' => $data['box_serial_number'],
// 'box_num' => $num,
// 'create_time' => date('Y-m-d H:i:s'),
// 'content_str' => $data['sn_code_all'],
// ]);
// $affectedRows = $zengjie->table('sn_code_all')
// ->where('sn_code', 'in', $snCodes)
// ->update(['batch_id' => $box_id]);
// // 检查更新是否影响了预期的行数
// if ($affectedRows !== count($snCodes)) {
// throw new \Exception('更新SN码数量不匹配');
// }
// $zengjie->commit();
// return $this->msg(['id'=>$box_id]);
// } catch (\Exception $e) {
// if ($zengjie->getPdo()->inTransaction()) {
// $zengjie->rollback();
// }
// trace('保存盒信息失败: ' . $e->getMessage(), 'error');
// return $this->msg(10002, '录入失败: ' . $e->getMessage());
// }
}
public function print_again_action($data){
$zengjie = Db::connect('zengjie_db');
if($data['type'] == 'sn'){
$result = $zengjie->table('sn_code_all')->where(['sn_code'=>$data['code']])->count();
}else{
$result = $zengjie->table('box_code_all')->where(['id'=>$data['code']])->count();
}
if($result <= 0){
return $this->msg(10002,'未找到该码录入');
}
if($data['type'] == 'sn'){
$result2 = $zengjie->table('sn_code_all')->where(['sn_code'=>$data['code']])->update([
'is_print'=>3
]);
}else{
$result2 = $zengjie->table('box_code_all')->where(['id'=>$data['code']])->update([
'is_print'=>0
]);
}
if($result){
return $this->msg([]);
}else{
return $this->msg(10002,'打印失败');
}
}
public function chuli_str_kongbai($data){
foreach ($data as $key => $value) {
// 1. 移除所有空字符和不可见字符
$data[$key] = preg_replace('/[\x00-\x1F\x7F]/', '', $value);
// 2. 去除首尾空白
$data[$key] = trim($value);
}
return $data;
}
######################################################测试#########################################################
######################################################测试#########################################################
######################################################测试#########################################################
public function ceshiyong(){
$data = input('post.');
dump($data);
// // 添加测试一维码数据
// $data = [];
// for ($i = 0; $i < 500; $i++) {
// // 生成15位随机数字作为sn_code
// $sn_code = '';
// for ($j = 0; $j < 15; $j++) {
// $sn_code .= mt_rand(0, 9);
// }
// // 生成随机的MAC地址
// $mac_parts = [];
// for ($k = 0; $k < 6; $k++) {
// $mac_parts[] = sprintf('%02x', mt_rand(0, 255));
// }
// $mac_code = implode(':', $mac_parts);
// // 构建数据数组
// $data[] = [
// 'sn_code' => $sn_code,
// 'mac_code' => $mac_code,
// 'create_time' => date('Y-m-d H:i:s'),
// 'is_print' => 1,
// 'bluetooth_name' => 'bl_5520'
// ];
// }
// $zengjie = Db::connect('zengjie_db');
// $result = $zengjie->table('sn_code_all')->insertAll($data);
// dump($result);
// die;
// // 添加测试一维码数据
// 添加测试打包数据
// $zengjie = Db::connect('zengjie_db');
// $records = $zengjie->table('sn_code_all')
// ->where('batch_id', 'null') // 或者使用 ->whereNull('batch_id')
// ->orderRaw('NEWID()') // SQL Server的随机排序函数
// ->limit(10)
// ->select();
// $data['sn_code_all'] = [];
// $data['box_serial_number'] = '3ST011527003';
// for ($i=0; $i < count($records); $i++) {
// $data['sn_code_all'][] = $records[$i]['sn_code'];
// }
// $data['sn_code_all'] = implode(',',$data['sn_code_all']);
// $result = $this->save_box_msg_action($data);
// dump($result);
// dump($data);
// 添加测试打包数据
// return $data;
// '0386737300007235,0039376500007203,0059052100007102,0124589700007184,0032838000007113,0170457400007162,0170448500007207,0170466000006965,0426058900007228,0006613600007105'
}
}