diff --git a/application/DeviceEnter/controller/Base.php b/application/DeviceEnter/controller/Base.php new file mode 100644 index 0000000..e3f746e --- /dev/null +++ b/application/DeviceEnter/controller/Base.php @@ -0,0 +1,185 @@ +'关键参数缺失', + '10002'=>'操作失败', + '10003'=>'信息核实错误', + '10004'=>'未找到有效数据', + '10005'=>'参数格式错误', + '10006'=>'参数不能为空', + '10007'=>'参数错误', + '10008'=>'', + '10009'=>'', + '10010'=>'自定义信息', + '20001'=>'登录失效', + '99999'=>'网络异常,请稍后重试', + ]; + // 加 bcadd(,,20) + // 减 bcsub(,,20) + // 乘 bcmul(,,20) + // 除 bcdiv(,,20) + ################################################################接口################################################################ + ################################################################接口################################################################ + ################################################################接口################################################################ + // 接口记录 + public function record_api_log($params, $error = null, $response = null){ + // dump($params); + // dump($error); + // die; + $logContent = "接口请求参数:" . json_encode($params, JSON_UNESCAPED_UNICODE) . PHP_EOL; + if ($error) { + $logContent .= "错误信息:" . $error['all_content'] . PHP_EOL; + if(!cache($error['flie']."_".$error['line'])){ + cache($error['flie']."_".$error['line'],"API错误",3600); + $this->send_email_api_error(["tsf3920322@126.com"],['title'=>'接口报错','from_user_name'=>'设备录入','content'=>$logContent]); + } + } + if ($response) { + $logContent .= "返回信息:" . json_encode($response, JSON_UNESCAPED_UNICODE) . PHP_EOL; + } + // 使用ThinkPHP的日志记录方法 + Log::record($logContent, 'api_log'); + } + + /* 接口说明(发邮件) + * $address(收件人的邮箱地址) 数组 格式: ['460834639@qq.com','460834639@qq.com'.......] + * $content(邮件的主题数据信息) 数组 格式:['title'=>'123','from_user_name'=>'123','content'=>'123'] + * $annex(附件路径信息) 字符串 + */ + public function send_email_api_error($address,$content,$annex=''){ + // $ad = '460834639@qq.com'; + $ad1 = '295155911@qq.com'; + $mail = new PHPMailer(); //实例化 + $mail->IsSMTP(); // 启用SMTP + $mail->Host = "smtp.126.com"; //SMTP服务器 163邮箱例子 + $mail->Port = 465; //邮件发送端口 + $mail->SMTPAuth = true; //启用SMTP认证 + $mail->SMTPSecure = 'ssl'; + $mail->CharSet = "UTF-8"; //字符集 + $mail->Encoding = "base64"; //编码方式 + $mail->Username = "tsf3920322@126.com"; //你的邮箱 + $mail->Password = "HLWXNRPUCTHJFIIX"; //你的密码(邮箱后台的授权密码) + $mail->From = "tsf3920322@126.com"; //发件人地址(也就是你的邮箱) + + // $mail->Subject = "微盟测试邮件"; //邮件标题 + $mail->Subject = $content['title']; //邮件标题 + + // $mail->FromName = "微盟体测中心"; //发件人姓名 + $mail->FromName = $content['from_user_name']; //发件人姓名 + + + for ($i=0; $i < count($address); $i++) { + $mail->AddAddress($address[$i], ""); //添加收件人(地址,昵称) + } + + if($annex != ''){ + // $url = ROOT_PATH. 'public' . DS . 'tsf' . DS .'demoooo.jpg'; + $mail->AddAttachment($annex,''); // 添加附件,并指定名称 + } + + $mail->IsHTML(true); //支持html格式内容 + + $neirong = $content['content']; + + $mail->Body = $neirong; //邮件主体内容 + //发送 + if (!$mail->Send()) { + + return ['code' => 10003,'msg'=>$mail->ErrorInfo]; + // return $mail->ErrorInfo; + } else { + return ['code' => 0]; + // return 'success'; + } + } + + + // 验证 + public function verify_data_is_ok($data = 2,$type){ + if($type == 'str'){ + if (is_string($data)) { + return true; + } else { + $this->record_api_log($data, null, ['code'=>10005,'msg'=>'校验参数不为字符串',[]]); + return false; + } + }else if($type == 'num'){ + if (is_numeric($data)) { + return true; + } else { + $this->record_api_log($data, null, ['code'=>10005,'msg'=>'校验参数不为数字',[]]); + return false; + } + }else if($type == 'intnum'){ + $pattern = '/^\d+$/'; + // dump($data); + if (preg_match($pattern, $data)) { + return true; // 匹配成功,返回 true + } else { + $this->record_api_log($data, null, ['code'=>10005,'msg'=>'校验参数不为整数数字',[]]); + return false; // 匹配失败,返回 false + } + }else if($type == 'datetime'){ + $formats = ['Y-m-d','Y-m-d H:i:s']; + foreach ($formats as $format) { + $dateTime = \DateTime::createFromFormat($format, $data); + // 检查时间字符串是否成功解析,并且解析后的日期时间与原始字符串表示的时间一致 + if ($dateTime && $dateTime->format($format) === $data) { + return true; + } + } + // 如果所有格式都解析失败,则返回 false + $this->record_api_log($data, null, ['code'=>10005,'msg'=>'校验参数不为日期格式',[]]); + return false; + }else if($type == 'other'){ + + } + + } + + + + public function msg($data,$str='',$result = []){ + if(is_array($data)){ + if($str != ''){ + return json(['code'=>0,'msg'=>$str,'data'=>$data]); + }else{ + return json(['code'=>0,'msg'=>'操作成功','data'=>$data]); + } + }else{ + if($str != ''){ + return json(['code'=>$data,'msg'=>$str,'data'=>$result]); + } + return json(['code'=>$data,'msg'=>$this->return_data_all[$data],'data'=>$result]); + } + } + + public function generateRandomString($length = 10) { + $characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; + $charactersLength = strlen($characters); + $randomString = ''; + for ($i = 0; $i < $length; $i++) { + $randomString .= $characters[rand(0, $charactersLength - 1)]; + } + return $randomString; + } + + + public function ceshi(){ + echo 'hello'; + } + + +} \ No newline at end of file diff --git a/application/DeviceEnter/controller/Savemsg.php b/application/DeviceEnter/controller/Savemsg.php new file mode 100644 index 0000000..e9bef3b --- /dev/null +++ b/application/DeviceEnter/controller/Savemsg.php @@ -0,0 +1,167 @@ +'c03', + ], + + ]; + 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_device_msg($data = ['mac'=>'564654564654654','device_name'=>'bl_5520','batch_code'=>'bc250826']){ + try { + // 你的业务逻辑 + if(count(input('post.')) > 0){ + $data = input('post.'); + } + if(!array_key_exists('mac', $data)){ + return $this->msg(10001); + } + if(!array_key_exists('device_name', $data)){ + return $this->msg(10001); + } + if(!array_key_exists('batch_code', $data)){ + return $this->msg(10001); + } + if(!$this->verify_data_is_ok($data['mac'],'str')){ + return $this->msg(10005); + } + if(!$this->verify_data_is_ok($data['device_name'],'str')){ + return $this->msg(10005); + } + if(!$this->verify_data_is_ok($data['batch_code'],'str')){ + return $this->msg(10005); + } + $result = $this->save_device_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 device_batch_list(){ + // try { + + $result = $this->device_batch_list_action(); + 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([], $logContent, null); + // return $this->msg(99999); + // } + + } + + + + + + + ########################################################################action######################################################################## + ########################################################################action######################################################################## + ########################################################################action######################################################################## + + public function config_msg_action(){ + $data = [ + 'name_list'=>$this->name_list, + 'name_default_key'=>$this->name_default, + + ]; + return $data; + } + + public function save_device_msg_action($data){ + $device_enter = Db::connect('device_enter_db'); + + $mac_code = $device_enter->table('device_C02pro')->where(['device_mac'=>$data['mac']])->count(); + if($mac_code > 0){ + return $this->msg(10002,'该MAC地址已录入'); + } + $result = $device_enter->table('device_C02pro')->insert([ + 'device_mac'=>$data['mac'], + 'device_name'=>$data['device_name'], + 'batch_code'=>$data['batch_code'], + 'create_time'=>date('Y-m-d H:i:s'), + ]); + if($result){ + return $this->msg([]); + }else{ + return $this->msg(10002); + } + } + + public function device_batch_list_action(){ + $device_enter = Db::connect('device_enter_db'); + $result = $device_enter->query(" + SELECT device_name, batch_code, COUNT(*) as num + FROM device_C02pro + GROUP BY batch_code,device_name + ORDER BY MAX(id) desc + "); + return $this->msg($result); + } + + +} \ No newline at end of file diff --git a/application/KitchenScale/controller/app/Aipart.php b/application/KitchenScale/controller/app/Aipart.php index 46c651d..9208c71 100644 --- a/application/KitchenScale/controller/app/Aipart.php +++ b/application/KitchenScale/controller/app/Aipart.php @@ -11,7 +11,7 @@ class Aipart extends Base{ protected $page_num = 10; protected $kitchenscale_db_msg = [ 'cookbook'=>'app_user_cookbook',//菜谱表 - 'foodlist3'=>'app_food_type_three',//食材列表3 + 'foodlist3'=>'app_z_national_standard_food_type_3',//食材列表3 'user'=>'app_user_data',//banner ]; diff --git a/application/KitchenScale/controller/app/Cookbook.php b/application/KitchenScale/controller/app/Cookbook.php index 5a76f76..af28f05 100644 --- a/application/KitchenScale/controller/app/Cookbook.php +++ b/application/KitchenScale/controller/app/Cookbook.php @@ -424,7 +424,7 @@ class Cookbook extends Base{ } } // 获取所有食材列表 - public function get_food_list($data=['food_level2_id'=>'4','search_data'=>'','page'=>1]){ + public function get_food_list($data=['food_level2_id'=>'2','search_data'=>'','page'=>1]){ // 尝试捕获异常 try { if(count(input('post.')) > 0){ @@ -1209,11 +1209,6 @@ class Cookbook extends Base{ $cp_page_num = 30; $cfc = Db::connect('cfc_db'); - // 获取食材分类列表start - // $foodlist1 = $cfc->table($this->kitchenscale_db_msg['foodlist1'])->where("is_del = 0")->field('id,name')->select(); - // $foodlist2 = $cfc->table($this->kitchenscale_db_msg['foodlist2'])->where("is_del = 0")->field('id,name,one_id')->select(); - // $foodlist3 = $cfc->table($this->kitchenscale_db_msg['foodlist3'])->where("is_del = 0")->field('id,name,two_id,kcal,unit')->select(); - $search_sql_str = "is_del = 0 AND two_id = ".$data['food_level2_id']; if(!array_key_exists('search_data', $data)){ $data['search_data'] = ""; @@ -1225,47 +1220,87 @@ class Cookbook extends Base{ } } $search_sql_str = $search_sql_str.$data['search_data']; - - // dump($data); - // dump($search_sql_str); + $content_num = $cfc->table($this->kitchenscale_db_msg['foodlist3']) - // ->alias('a') - // ->join($this->kitchenscale_db_msg['foodlist2'].' b','a.two_id = b.id','LEFT') - // ->join($this->kitchenscale_db_msg['foodlist1'].' c','b.one_id = c.id','LEFT') ->where($search_sql_str) ->count(); $page_total = ceil($content_num/$cp_page_num);; + $collect_list = $cfc->table($this->kitchenscale_db_msg['foodlist3']) - // ->alias('a') - // ->join($this->kitchenscale_db_msg['foodlist2'].' b','a.two_id = b.id','LEFT') - // ->join($this->kitchenscale_db_msg['foodlist1'].' c','b.one_id = c.id','LEFT') ->where($search_sql_str) - ->field('id,food_name as name,pic_url,Calorie_val as kcal,weight_unit as unit') + ->field('id,record_id,food_name as name,pic_url,Calorie_val as kcal,weight_unit as unit') ->page($data['page'],$cp_page_num) ->select(); - - + $nutrients_list_id = []; + $nutrients_list_temporary = []; foreach ($collect_list as $key => $value) { $collect_list[$key]['pic_url'] = "https://tc.pcxbc.com/kitchenscale_all".$collect_list[$key]['pic_url']; + array_push($nutrients_list_id,$collect_list[$key]['record_id']); + $nutrients_list_temporary[$collect_list[$key]['record_id']] = []; + if (!is_numeric($collect_list[$key]['kcal'])) { + $collect_list[$key]['kcal'] = 0; + } + } + + + // 处理营养物质 + $nutrients_list_data = $cfc->table($this->kitchenscale_db_msg['foodlist4']) + ->where("father_id in (". implode(',',$nutrients_list_id) .")") + ->field("father_id,name,name_ch,unit,value,type") + ->select(); + + // dump($collect_list); + // die; + + + for ($i=0; $i < count($nutrients_list_data); $i++) { + if(!is_numeric($nutrients_list_data[$i]['value']) || $nutrients_list_data[$i]['value'] == 0){ + continue; + }else{ + array_push($nutrients_list_temporary[$nutrients_list_data[$i]['father_id']],[ + 'name'=>$nutrients_list_data[$i]['name'], + 'name_ch'=>$nutrients_list_data[$i]['name_ch'], + 'unit'=>$nutrients_list_data[$i]['unit'], + 'value'=>$nutrients_list_data[$i]['value'], + 'type'=>$nutrients_list_data[$i]['type'], + 'type_name'=>$nutrients_list_data[$i]['type'] == 1?'能量及宏量营养素':($nutrients_list_data[$i]['type'] == 2?'维生素':'矿物质'), + 'color'=>$nutrients_list_data[$i]['type'] == 1?'#C4FFE0':($nutrients_list_data[$i]['type'] == 2?'#FFEFB7':'#7DA8E0') + ]); + } + } + // dump($nutrients_list_temporary); + // die; + for ($i=0; $i < count($collect_list); $i++) { + $collect_list[$i]['nutrients_four'] = []; + $collect_list[$i]['nutrients_list'] = []; + $temporary_calculate = 0; + if(array_key_exists($collect_list[$i]['record_id'],$nutrients_list_temporary)){ + $collect_list[$i]['nutrients_list'] = $nutrients_list_temporary[$collect_list[$i]['record_id']]; + foreach ($nutrients_list_temporary[$collect_list[$i]['record_id']] as $key => $value) { + if($value['type'] == 1){ + array_push($collect_list[$i]['nutrients_four'],[ + 'name'=>$value['name'] == 'Carbohydrate'?'碳水':$value['name_ch'], + 'unit'=>$value['unit'], + 'color'=>$value['name'] == 'Carbohydrate'?'#FFB169':($value['name'] == 'Fat'?'#ED7886':($value['name'] == 'Protein'?'#5180D8':'')), + 'value'=>!is_numeric($value['value'])?0:$value['value'], + ]); + // 如果不为卡路里 + if($value['name'] != 'Calorie'){ + $temporary_calculate = $temporary_calculate+$value['value']; + } + } + } + // 处理详情当中的环数据占比 + foreach ($collect_list[$i]['nutrients_four'] as $k => $v) { + if($v['name'] != '卡路里'){ + $collect_list[$i]['nutrients_four'][$k]['proportion'] = bcmul(bcdiv($v['value'],$temporary_calculate,2),100,0); + }else{ + $collect_list[$i]['nutrients_four'][$k]['proportion'] = 0; + } + } + } } - // $return_data = []; - // $temporary_arr = []; - // foreach ($collect_list as $key => $value) { - // unset($collect_list[$key]['ROW_NUMBER']); - // if(!array_key_exists($value['two_id'], $temporary_arr)){ - // $temporary_arr[$value['two_id']] = [ - // 'id'=>$value['two_id'], - // 'name'=>$value['two_name'], - // 'list'=>[['id'=>$value['id'],'name'=>$value['name'],'kcal'=>$value['kcal'],'unit'=>$value['unit']]], - // ]; - // }else{ - // array_push($temporary_arr[$value['two_id']]['list'],['id'=>$value['id'],'name'=>$value['name'],'kcal'=>$value['kcal'],'unit'=>$value['unit']]); - // } - // } - // foreach ($temporary_arr as $key => $value) { - // array_push($return_data,$value); - // } return $this->msg([ 'page_now'=>$data['page'], 'page_total'=>$page_total, diff --git a/application/KitchenScale/controller/app/Countfood.php b/application/KitchenScale/controller/app/Countfood.php index c9677dc..54554fe 100644 --- a/application/KitchenScale/controller/app/Countfood.php +++ b/application/KitchenScale/controller/app/Countfood.php @@ -18,7 +18,7 @@ class Countfood extends Base{ 'uploadimg'=>'app_user_upload_img',//素材表 'followlist'=>'app_user_follow_list',//关注列表 'collect_list'=>'app_user_collect_list',//收藏列表 - 'foodlist3'=>'app_standard_food_type_three',//食材列表3 + 'foodlist3'=>'app_z_national_standard_food_type_3',//食材列表3 'eat_log'=>'app_user_kcal_log',//食材列表3 'user'=>'app_user_data',//banner ]; @@ -74,7 +74,7 @@ class Countfood extends Base{ } } // 获取记食器板块内容 - public function get_countfoot_content($data=['token'=>'caadd1be045a65f30b92aa805f1de54a','aud_id'=>61,'time'=>'2025-03-17']){ + public function get_countfoot_content($data=['token'=>'caadd1be045a65f30b92aa805f1de54a','aud_id'=>1,'time'=>'2025-08-25']){ // 尝试捕获异常 try { if(count(input('post.')) > 0){ @@ -245,6 +245,48 @@ class Countfood extends Base{ return json(['status' => 'error', 'message' => '系统错误']); } } + public function del_user_eat_log($data=['token'=>'caadd1be045a65f30b92aa805f1de54a','aud_id'=>6,'eat_log_id'=>160]){ + // 尝试捕获异常 + try { + if(count(input('post.')) > 0){ + $data = input('post.'); + } + if(!array_key_exists('token', $data)){ + return $this->msg(10001,'token is miss'); + } + if(!array_key_exists('aud_id', $data)){ + return $this->msg(10001,'aud_id is miss'); + } + if(!array_key_exists('eat_log_id', $data)){ + return $this->msg(10001,'eat_log_id is miss'); + } + if(!$this->verify_data_is_ok($data['token'],'str')){ + return $this->msg(10005,'token type is error'); + } + if(!$this->verify_data_is_ok($data['aud_id'],'intnum')){ + return $this->msg(10005,'aud_id type is error'); + } + if(!$this->verify_data_is_ok($data['eat_log_id'],'num')){ + return $this->msg(10005,'eat_log_id type is error'); + } + + $return_data = $this->del_user_eat_log_action($data); + return $return_data; + } 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'] .= "行号: " . $e->getLine() . "\n"; + $logContent['all_content'] .= "跟踪信息:\n" . $e->getTraceAsString() . "\n"; + // 记录日志 + $this->record_api_log($data, $logContent, null); + return json(['status' => 'error', 'message' => '系统错误']); + } + } #######################################################################action####################################################################### #######################################################################action####################################################################### #######################################################################action####################################################################### @@ -291,7 +333,7 @@ class Countfood extends Base{ $food_content = $cfc->table($this->kitchenscale_db_msg['foodlist3']) ->where("id in (".implode(',',$food_id_arr).")") - ->field('id,name,kcal,carbohydrate,protein,fat') + ->field('id,food_name as name,Calorie_val as kcal,Carbohydrate_val as carbohydrate,Protein_val as protein,Fat_val as fat') ->select(); // 整理食物信息 @@ -360,15 +402,26 @@ class Countfood extends Base{ $nutrition_data = $this->count_user_nutrition_all($user_data); if($user_data['is_use_set_kcal'] == 1){ + // 加 bcadd(,,20) + // 减 bcsub(,,20) + // 乘 bcmul(,,20) + // 除 bcdiv(,,20) + $proportion = bcdiv($user_data['set_kcal'],$nutrition_data['kcal'],20); $nutrition_data['kcal'] = $user_data['set_kcal']; + $nutrition_data['carbohydrate'] = bcmul($nutrition_data['carbohydrate'],$proportion,2); + $nutrition_data['protein'] = bcmul($nutrition_data['protein'],$proportion,2); + $nutrition_data['fat'] = bcmul($nutrition_data['fat'],$proportion,2); + } - // 查询用户今日摄入食物 $food_content = $cfc->table($this->kitchenscale_db_msg['eat_log']) - ->where("aud_id = " . $data['aud_id'] . " AND CAST(create_time AS DATE) = CAST('" . $data['time'] . "' AS DATE)") - ->field('meals_type,food_name,weight,kcal_val,carbohydrate_val,protein_val,fat_val') + ->alias('a') + ->join('app_z_national_standard_food_type_3 b','a.food_id = b.id','LEFT') + ->where("a.is_del = 0 AND a.aud_id = " . $data['aud_id'] . " AND CAST(a.create_time AS DATE) = CAST('" . $data['time'] . "' AS DATE)") + ->field('a.meals_type,a.food_name,a.weight,a.kcal_val,a.carbohydrate_val,a.protein_val,a.fat_val,a.id,\'https://tc.pcxbc.com\' + b.pic_url as pic_url') ->select(); - + // dump($food_content); + // die; $date = date('Y-m-d H:i:s'); $return_data = [ 'suggestion'=>[ @@ -435,22 +488,58 @@ class Countfood extends Base{ $return_data['today_intake']['fat'] = bcadd($return_data['today_intake']['fat'],$value['fat_val'],2); if($value['meals_type'] == '早餐'){ $return_data['list'][0]['val'] = bcadd($return_data['list'][0]['val'],$value['kcal_val'],2); - array_push($return_data['list'][0]['list'],['name'=>$value['food_name'],'weight'=>$value['weight'].'克','val'=>$value['kcal_val'].'kcal']); + array_push($return_data['list'][0]['list'],[ + 'name'=>$value['food_name'], + 'weight'=>$value['weight'].'克', + 'id'=>$value['id'], + 'pic_url'=>$value['pic_url'], + 'val'=>$value['kcal_val'].'kcal' + ]); }else if($value['meals_type'] == '午餐'){ $return_data['list'][1]['val'] = bcadd($return_data['list'][1]['val'],$value['kcal_val'],2); - array_push($return_data['list'][1]['list'],['name'=>$value['food_name'],'weight'=>$value['weight'].'克','val'=>$value['kcal_val'].'kcal']); + array_push($return_data['list'][1]['list'],[ + 'name'=>$value['food_name'], + 'weight'=>$value['weight'].'克', + 'id'=>$value['id'], + 'pic_url'=>$value['pic_url'], + 'val'=>$value['kcal_val'].'kcal' + ]); }else if($value['meals_type'] == '晚餐'){ $return_data['list'][2]['val'] = bcadd($return_data['list'][2]['val'],$value['kcal_val'],2); - array_push($return_data['list'][2]['list'],['name'=>$value['food_name'],'weight'=>$value['weight'].'克','val'=>$value['kcal_val'].'kcal']); + array_push($return_data['list'][2]['list'],[ + 'name'=>$value['food_name'], + 'weight'=>$value['weight'].'克', + 'id'=>$value['id'], + 'pic_url'=>$value['pic_url'], + 'val'=>$value['kcal_val'].'kcal' + ]); }else if($value['meals_type'] == '早加餐'){ $return_data['list'][3]['val'] = bcadd($return_data['list'][3]['val'],$value['kcal_val'],2); - array_push($return_data['list'][3]['list'],['name'=>$value['food_name'],'weight'=>$value['weight'].'克','val'=>$value['kcal_val'].'kcal']); + array_push($return_data['list'][3]['list'],[ + 'name'=>$value['food_name'], + 'weight'=>$value['weight'].'克', + 'id'=>$value['id'], + 'pic_url'=>$value['pic_url'], + 'val'=>$value['kcal_val'].'kcal' + ]); }else if($value['meals_type'] == '午加餐'){ $return_data['list'][4]['val'] = bcadd($return_data['list'][4]['val'],$value['kcal_val'],2); - array_push($return_data['list'][4]['list'],['name'=>$value['food_name'],'weight'=>$value['weight'].'克','val'=>$value['kcal_val'].'kcal']); + array_push($return_data['list'][4]['list'],[ + 'name'=>$value['food_name'], + 'weight'=>$value['weight'].'克', + 'id'=>$value['id'], + 'pic_url'=>$value['pic_url'], + 'val'=>$value['kcal_val'].'kcal' + ]); }else if($value['meals_type'] == '晚加餐'){ $return_data['list'][5]['val'] = bcadd($return_data['list'][5]['val'],$value['kcal_val'],2); - array_push($return_data['list'][5]['list'],['name'=>$value['food_name'],'weight'=>$value['weight'].'克','val'=>$value['kcal_val'].'kcal']); + array_push($return_data['list'][5]['list'],[ + 'name'=>$value['food_name'], + 'weight'=>$value['weight'].'克', + 'id'=>$value['id'], + 'pic_url'=>$value['pic_url'], + 'val'=>$value['kcal_val'].'kcal' + ]); }else{ } @@ -681,6 +770,17 @@ class Countfood extends Base{ return $this->msg(10002); } } + public function del_user_eat_log_action($data){ + $cfc = Db::connect('cfc_db'); + $user_data = $cfc->table($this->kitchenscale_db_msg['eat_log']) + ->where(["id"=>$data['eat_log_id']]) + ->update(['is_del'=>1]); + if($user_data){ + return $this->msg([]); + }else{ + return $this->msg(10002); + } + } #######################################################################工具####################################################################### #######################################################################工具####################################################################### @@ -751,4 +851,6 @@ class Countfood extends Base{ ]; } + + } \ No newline at end of file diff --git a/application/KitchenScale/controller/app/Usercenter.php b/application/KitchenScale/controller/app/Usercenter.php index f7eae3d..7437243 100644 --- a/application/KitchenScale/controller/app/Usercenter.php +++ b/application/KitchenScale/controller/app/Usercenter.php @@ -18,9 +18,9 @@ class Usercenter extends Base{ 'cookbook'=>'app_user_cookbook',//菜谱表 'cookbook_label'=>'app_user_cookbook_label',//菜谱标签表 'uploadimg'=>'app_user_upload_img',//图片素材表 - 'foodlist1'=>'app_food_type_one',//食材列表1 - 'foodlist2'=>'app_food_type_two',//食材列表2 - 'foodlist3'=>'app_food_type_three',//食材列表3 + 'foodlist1'=>'app_z_national_standard_food_type_1',//食材列表1 + 'foodlist2'=>'app_z_national_standard_food_type_2',//食材列表2 + 'foodlist3'=>'app_z_national_standard_food_type_3',//食材列表3 'collect_list'=>'app_user_collect_list',//点赞表 'banner'=>'app_banner_data',//banner 'user'=>'app_user_data',//banner diff --git a/application/ZengJieCode/controller/admin/Base.php b/application/ZengJieCode/controller/admin/Base.php new file mode 100644 index 0000000..8ecf21c --- /dev/null +++ b/application/ZengJieCode/controller/admin/Base.php @@ -0,0 +1,172 @@ +'test_app_data_log', + ]; + protected $return_data_all = [ + '10001'=>'关键参数缺失', + '10002'=>'操作失败', + '10003'=>'信息核实错误', + '10004'=>'未找到有效数据', + '10005'=>'参数格式错误', + '10006'=>'参数不能为空', + '10007'=>'参数错误', + '10008'=>'', + '10009'=>'', + '10010'=>'自定义信息', + '20001'=>'登录失效', + '99999'=>'网络异常,请稍后重试', + ]; + protected $file_max = 1024*1024*2;//xxxMB + // 加 bcadd(,,20) + // 减 bcsub(,,20) + // 乘 bcmul(,,20) + // 除 bcdiv(,,20) + ################################################################接口################################################################ + ################################################################接口################################################################ + ################################################################接口################################################################ + + + + // 验证 + public function verify_data_is_ok($data = 2,$type){ + if($type == 'str'){ + if (is_string($data)) { + return true; + } else { + $this->record_api_log($data, null, ['code'=>10005,'msg'=>'校验参数不为字符串',[]]); + return false; + } + }else if($type == 'num'){ + if (is_numeric($data)) { + return true; + } else { + $this->record_api_log($data, null, ['code'=>10005,'msg'=>'校验参数不为数字',[]]); + return false; + } + }else if($type == 'intnum'){ + $pattern = '/^\d+$/'; + if (preg_match($pattern, $data)) { + return true; // 匹配成功,返回 true + } else { + $this->record_api_log($data, null, ['code'=>10005,'msg'=>'校验参数不为整数数字',[]]); + return false; // 匹配失败,返回 false + } + }else if($type == 'datetime'){ + $formats = ['Y-m-d','Y-m-d H:i:s']; + foreach ($formats as $format) { + $dateTime = \DateTime::createFromFormat($format, $data); + // 检查时间字符串是否成功解析,并且解析后的日期时间与原始字符串表示的时间一致 + if ($dateTime && $dateTime->format($format) === $data) { + return true; + } + } + // 如果所有格式都解析失败,则返回 false + $this->record_api_log($data, null, ['code'=>10005,'msg'=>'校验参数不为日期格式',[]]); + return false; + }else if($type == 'other'){ + + } + } + + ####################################################图片选择上传start############################################################## + public function pic_index($page = 1) { + $data = input(); + $pd = true; + if(array_key_exists('page',$data)){ + $page = $data['page']; + $pd = false; + } + $cfc = Db::connect('cfc_db'); + $num = $cfc->table('app_user_upload_img')->where(['special_record_str'=>'admin'])->count(); + $result = $cfc->table('app_user_upload_img')->where(['special_record_str'=>'admin'])->order('id desc')->page($page,20)->field('id,pic_url')->select(); + if(!$pd){ + $return_result['num'] = $num; + $return_result['result'] = $result; + return $this->msg(0,'success',$return_result); + } + $this->assign([ + 'result' => $result, + 'num' => $num, + ]); + return $this->fetch(); + } + public function pic_upload_action(){ + + $save_data = []; + $error_data = []; + // 获取表单上传文件 + $files = request()->file('image'); + foreach($files as $file){ + $name = $file->getInfo()['name']; + // 使用 pathinfo() 函数获取文件名的扩展名 + $pathinfo = pathinfo($name); + $extension = strtolower($pathinfo['extension']); // 转换为小写以进行不区分大小写的比较 + $file_name = $pathinfo['filename']; + // 判断扩展名是否不是 .png 或 .gif + if ($extension !== 'png' && $extension !== 'gif') { + // 修改文件名,将扩展名改为 .jpg + $new_filename = date('YmdHis').$file_name . '.jpg'; + } else { + $new_filename = date('YmdHis').$name; + } + $info = $file->validate(['size'=>$this->file_max,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'kitchenscale_all' . DS . 'user_upload',$new_filename); + if($info){ + array_push($save_data,[ + 'user_token'=>'caadd1be045a65f30b92aa805f1de54a', + 'pic_name'=>$new_filename, + 'pic_url'=>'https://tc.pcxbc.com/kitchenscale_all/user_upload/'.$new_filename, + 'create_time'=>date('Y-m-d H:i:s'), + 'special_record_str'=>'admin', + 'operate_log'=>1 + ]); + }else{ + array_push($error_data,[ + 'pic_name'=>$name, + 'error_msg'=>$file->getError(), + ]); + } + } + $cfc = Db::connect('cfc_db'); + $pic_result = $cfc->table('app_user_upload_img')->insertAll ($save_data); + if($pic_result){ + return $this->msg(['success'=>count($save_data),'error_data'=>$error_data]); + }else{ + for ($i=0; $i < count($save_data); $i++) { + unlink(ROOT_PATH . 'public' . DS . 'kitchenscale_all' . DS . 'user_upload' . DS . $new_filename); + } + return $this->msg(10002,'图片数据保存失败'); + } + } + + ####################################################图片选择上传end############################################################## + + + public function msg($data,$str='',$result = []){ + if(is_array($data)){ + if($str != ''){ + return json(['code'=>0,'msg'=>$str,'data'=>$data]); + }else{ + return json(['code'=>0,'msg'=>'操作成功','data'=>$data]); + } + }else{ + if($str != ''){ + return json(['code'=>$data,'msg'=>$str,'data'=>$result]); + } + return json(['code'=>$data,'msg'=>$this->return_data_all[$data],'data'=>$result]); + } + } + + + +} \ No newline at end of file diff --git a/application/ZengJieCode/controller/admin/Printaction.php b/application/ZengJieCode/controller/admin/Printaction.php new file mode 100644 index 0000000..e54caa1 --- /dev/null +++ b/application/ZengJieCode/controller/admin/Printaction.php @@ -0,0 +1,318 @@ + 'app_user_cookbook', // 菜谱表 + 'foodlist3' => 'app_z_national_standard_food_type_3', // 食材列表3 + 'user' => 'app_user_data', // banner + ]; + + // 自定义字体路径(相对于项目根目录) + protected $fontPath = 'public/tsf/jc.ttf'; // 请替换为你的字体文件路径 + + // 显示条形码生成页面 + public function print_device_barcode_index() + { + return $this->fetch(); + } + // 显示外箱码生成页面 + public function print_outside_box_index() + { + return $this->fetch(); + } + + public function print_device_barcode() + { + $zengjie = Db::connect('zengjie_db'); + $img_content = $zengjie->table('sn_code_all')->where(['is_print' => 0])->field('sn_code')->find(); + + if (empty($img_content)) { + return $this->msg(10002, '没有可用的条形码'); + } else { + $img_content = $img_content['sn_code']; + } + // dump($img_content); + // die; + return $this->barcode_action($img_content,1); + + } + public function print_scan_barcode() + { + $zengjie = Db::connect('zengjie_db'); + $img_content = $zengjie->table('sn_code_all')->where(['is_print' => 3])->field('sn_code')->find(); + + if (empty($img_content)) { + return $this->msg(10002, '没有可用的条形码'); + } else { + $img_content = $img_content['sn_code']; + } + return $this->barcode_action($img_content,4); + + } + + public function barcode_action($img_content,$typr_num_data){ + $zengjie = Db::connect('zengjie_db'); + $generator = new BarcodeGeneratorPNG(); + + try { + /******************************* + * 可调整参数区域 - 开始 + *******************************/ + + // 1. 字体文件路径 + $fontFile = ROOT_PATH . $this->fontPath; + + // 2. 图像尺寸参数 + $imageWidth = 709; // 图像宽度 + $imageHeight = 236; // 图像高度 + + // 3. 条形码参数 + $barcodeScaleFactor = 0.9; // 条形码缩放比例 (0-1) + $barcodeHeightScale = 1.5; // 条形码高度比例 + $barcodeYOffset = 10; // 条形码Y轴偏移量 + + // 4. 文本参数 + $textFontSize = 35; // 文本字体大小 + $textYOffset = 35; // 文本Y轴偏移量 + + // 5. 布局参数 + $topBoxRatio = 0.65; // 上区域高度比例 + $bottomBoxRatio = 0.25; // 下区域高度比例 + $verticalMargin = 0.05; // 垂直边距比例 + + /******************************* + * 可调整参数区域 - 结束 + *******************************/ + + // 生成条形码 + $barcodeData = $generator->getBarcode($img_content, BarcodeGeneratorPNG::TYPE_CODE_128); + $barcodeImage = imagecreatefromstring($barcodeData); + + // 创建画布 + $image = imagecreatetruecolor($imageWidth, $imageHeight); + $white = imagecolorallocate($image, 255, 255, 255); + $black = imagecolorallocate($image, 0, 0, 0); + imagefill($image, 0, 0, $white); + + // 计算布局区域 + $verticalSpace = $imageHeight * (1 - $verticalMargin * 2); + $topBoxHeight = $verticalSpace * $topBoxRatio; + $bottomBoxHeight = $verticalSpace * $bottomBoxRatio; + $startY = $imageHeight * $verticalMargin; + + // 处理条形码 + $barcodeWidth = imagesx($barcodeImage); + $barcodeHeight = imagesy($barcodeImage); + + $targetBarcodeWidth = $imageWidth * $barcodeScaleFactor; + $scale = $targetBarcodeWidth / $barcodeWidth; + $newBarcodeWidth = $targetBarcodeWidth; + $newBarcodeHeight = $barcodeHeight * $scale * $barcodeHeightScale; + + $resizedBarcode = imagecreatetruecolor($newBarcodeWidth, $newBarcodeHeight); + imagefill($resizedBarcode, 0, 0, $white); + imagecopyresampled($resizedBarcode, $barcodeImage, + 0, 0, 0, 0, + $newBarcodeWidth, $newBarcodeHeight, + $barcodeWidth, $barcodeHeight); + + // 放置条形码(上区域居中) + $barcodeX = ($imageWidth - $newBarcodeWidth) / 2; + $barcodeY = $startY + ($topBoxHeight - $newBarcodeHeight) / 2 + $barcodeYOffset; + imagecopy($image, $resizedBarcode, $barcodeX, $barcodeY, 0, 0, $newBarcodeWidth, $newBarcodeHeight); + + // 放置文本(下区域居中) + $textBox = imagettfbbox($textFontSize, 0, $fontFile, $img_content); + $textWidth = $textBox[2] - $textBox[0]; + $textHeight = $textBox[1] - $textBox[7]; + + $textX = ($imageWidth - $textWidth) / 2; + $textY = $startY + $topBoxHeight + ($bottomBoxHeight - $textHeight) / 2 + $textYOffset; + imagettftext($image, $textFontSize, 0, $textX, $textY, $black, $fontFile, $img_content); + + // 输出图像 + ob_start(); + imagepng($image); + $imageData = ob_get_clean(); + + // 标记为已打印 + $result1 = $zengjie->table('sn_code_all') + ->where(['sn_code' => $img_content]) + ->update(['is_print' => $typr_num_data]); + + if($result1){ + return $this->msg([ + 'image' => 'data:image/png;base64,' . base64_encode($imageData) + ]); + }else{ + return $this->msg(10002,'保存条码失败'); + } + + } catch (\Exception $e) { + return $this->msg(10003, "生成条形码失败: " . $e->getMessage()); + } + } + + public function print_combined_code() + { + $zengjie = Db::connect('zengjie_db'); + // 获取可以打印的大箱条形码信息 + $box_data = $zengjie->table('box_code_all')->where(['is_print' => 0])->field('id,box_code,box_num,content_str')->find(); + + $box_barcode_data = ''; + if (empty($box_data)) { + return $this->msg(10002, '没有可用的编码'); + } else { + $box_barcode_data = $box_data['box_code'].'-'.sprintf("%03d", (int)$box_data['box_num']); + } + // 获取大箱信息配套的sn二维码码信息 + // $sn_data = $zengjie->table('sn_code_all')->where(['batch_id' => $box_data['id']])->field('sn_code')->select(); + // $sn_qrcode_data = []; + // for ($i=0; $i < count($sn_data); $i++) { + // array_push($sn_qrcode_data,$sn_data[$i]['sn_code']); + // } + // $sn_qrcode_data = implode(',',$sn_qrcode_data); + + $sn_qrcode_data = $box_data['content_str']; + try { + /******************************* + * 可调整参数区域 - 开始 + *******************************/ + + // 1. 文字相关参数 + $fontFile = ROOT_PATH . $this->fontPath; // 字体文件路径 + + // "SN号集成码"文字参数 + $snTitleText = "SN号集成码"; + $snTitleFontSize = 31; // 字体大小 + $snTitleYPosition = 60; // Y轴位置 + + // "外箱条码"文字参数 + $boxTitleText = "外箱条码"; + $boxTitleFontSize = 31; // 字体大小 + $boxTitleYPosition = 460; // Y轴位置 + + // 外箱条码数字参数 + $barcodeTextFontSize = 25; // 字体大小 + $barcodeTextYPosition = 620; // Y轴位置 + + // 2. 二维码参数 + $qrCodeSize = 320; // 二维码尺寸 + $qrCodeMargin = 2; // 二维码边距 + $qrCodeYPosition = 90; // Y轴位置 + + // 3. 条形码参数 + $barcodeScaleFactor = 0.85; // 条形码缩放比例 (0-1) + $barcodeHeightScale = 2.0; // 条形码高度比例 (调整黑色条码高度) + $barcodeYPosition = 480; // Y轴位置 + + // 4. 图像尺寸参数 + $imageWidth = 543; // 图像宽度 (45mm @ 300dpi) + $imageHeight = 649; // 图像高度 (55mm @ 300dpi) + + /******************************* + * 可调整参数区域 - 结束 + *******************************/ + + // 生成二维码 + $qrCode = new QrCode($sn_qrcode_data); + $qrCode->setSize($qrCodeSize); + $qrCode->setMargin($qrCodeMargin); + $writer = new PngWriter(); + $qrCodeResult = $writer->write($qrCode); + $qrCodeData = $qrCodeResult->getString(); + + // 生成条形码 + $generator = new BarcodeGeneratorPNG(); + $barcodeData = $generator->getBarcode($box_barcode_data, BarcodeGeneratorPNG::TYPE_CODE_128); + + // 创建画布 + $image = imagecreatetruecolor($imageWidth, $imageHeight); + $white = imagecolorallocate($image, 255, 255, 255); + $black = imagecolorallocate($image, 0, 0, 0); + imagefill($image, 0, 0, $white); + + // 添加"SN号集成码"文字 + $snTitleBox = imagettfbbox($snTitleFontSize, 0, $fontFile, $snTitleText); + $snTitleWidth = $snTitleBox[2] - $snTitleBox[0]; + $snTitleX = ($imageWidth - $snTitleWidth) / 2; + imagettftext($image, $snTitleFontSize, 0, $snTitleX, $snTitleYPosition, $black, $fontFile, $snTitleText); + + // 添加二维码 + $qrcodeImage = imagecreatefromstring($qrCodeData); + $qrcodeWidth = imagesx($qrcodeImage); + $qrcodeHeight = imagesy($qrcodeImage); + + $targetQrcodeSize = $qrCodeSize; + $resizedQrcode = imagecreatetruecolor($targetQrcodeSize, $targetQrcodeSize); + imagefill($resizedQrcode, 0, 0, $white); + imagecopyresampled($resizedQrcode, $qrcodeImage, + 0, 0, 0, 0, + $targetQrcodeSize, $targetQrcodeSize, + $qrcodeWidth, $qrcodeHeight); + + $qrcodeX = ($imageWidth - $targetQrcodeSize) / 2; + imagecopy($image, $resizedQrcode, $qrcodeX, $qrCodeYPosition, 0, 0, $targetQrcodeSize, $targetQrcodeSize); + + // 添加"外箱条码"文字 + $boxTitleBox = imagettfbbox($boxTitleFontSize, 0, $fontFile, $boxTitleText); + $boxTitleWidth = $boxTitleBox[2] - $boxTitleBox[0]; + $boxTitleX = ($imageWidth - $boxTitleWidth) / 2; + imagettftext($image, $boxTitleFontSize, 0, $boxTitleX, $boxTitleYPosition, $black, $fontFile, $boxTitleText); + + // 添加条形码 + $barcodeImage = imagecreatefromstring($barcodeData); + $barcodeWidth = imagesx($barcodeImage); + $barcodeHeight = imagesy($barcodeImage); + + $targetBarcodeWidth = $imageWidth * $barcodeScaleFactor; + $scale = $targetBarcodeWidth / $barcodeWidth; + $newBarcodeWidth = $targetBarcodeWidth; + $newBarcodeHeight = $barcodeHeight * $scale * $barcodeHeightScale; + + $resizedBarcode = imagecreatetruecolor($newBarcodeWidth, $newBarcodeHeight); + imagefill($resizedBarcode, 0, 0, $white); + imagecopyresampled($resizedBarcode, $barcodeImage, + 0, 0, 0, 0, + $newBarcodeWidth, $newBarcodeHeight, + $barcodeWidth, $barcodeHeight); + + $barcodeX = ($imageWidth - $newBarcodeWidth) / 2; + imagecopy($image, $resizedBarcode, $barcodeX, $barcodeYPosition, 0, 0, $newBarcodeWidth, $newBarcodeHeight); + + // 添加外箱条码文字 + $codeTextBox = imagettfbbox($barcodeTextFontSize, 0, $fontFile, $box_barcode_data); + $codeTextWidth = $codeTextBox[2] - $codeTextBox[0]; + $codeTextX = ($imageWidth - $codeTextWidth) / 2; + imagettftext($image, $barcodeTextFontSize, 0, $codeTextX, $barcodeTextYPosition, $black, $fontFile, $box_barcode_data); + + // 输出图像 + ob_start(); + imagepng($image); + $imageData = ob_get_clean(); + + // 标记为已打印 + $zengjie->table('box_code_all') + ->where(['id' => $box_data['id']]) + ->update(['is_print' => 1]); + + return $this->msg([ + 'image' => 'data:image/png;base64,' . base64_encode($imageData) + ]); + + } catch (\Exception $e) { + return $this->msg(10003, "生成集成码失败: " . $e->getMessage()); + } + } +} \ No newline at end of file diff --git a/application/ZengJieCode/controller/app/Base.php b/application/ZengJieCode/controller/app/Base.php new file mode 100644 index 0000000..6da6fc6 --- /dev/null +++ b/application/ZengJieCode/controller/app/Base.php @@ -0,0 +1,183 @@ +'关键参数缺失', + '10002'=>'操作失败', + '10003'=>'信息核实错误', + '10004'=>'未找到有效数据', + '10005'=>'参数格式错误', + '10006'=>'参数不能为空', + '10007'=>'参数错误', + '10008'=>'', + '10009'=>'', + '10010'=>'自定义信息', + '20001'=>'登录失效', + '99999'=>'网络异常,请稍后重试', + ]; + // 加 bcadd(,,20) + // 减 bcsub(,,20) + // 乘 bcmul(,,20) + // 除 bcdiv(,,20) + ################################################################接口################################################################ + ################################################################接口################################################################ + ################################################################接口################################################################ + // 接口记录 + public function record_api_log($params, $error = null, $response = null){ + // dump($params); + // dump($error); + // die; + $logContent = "接口请求参数:" . json_encode($params, JSON_UNESCAPED_UNICODE) . PHP_EOL; + if ($error) { + $logContent .= "错误信息:" . $error['all_content'] . PHP_EOL; + if(!cache($error['flie']."_".$error['line'])){ + cache($error['flie']."_".$error['line'],"API错误",3600); + $this->send_email_api_error(["tsf3920322@126.com"],['title'=>'接口报错','from_user_name'=>'厨房秤(后台)','content'=>$logContent]); + } + } + if ($response) { + $logContent .= "返回信息:" . json_encode($response, JSON_UNESCAPED_UNICODE) . PHP_EOL; + } + // 使用ThinkPHP的日志记录方法 + Log::record($logContent, 'api_log'); + } + + /* 接口说明(发邮件) + * $address(收件人的邮箱地址) 数组 格式: ['460834639@qq.com','460834639@qq.com'.......] + * $content(邮件的主题数据信息) 数组 格式:['title'=>'123','from_user_name'=>'123','content'=>'123'] + * $annex(附件路径信息) 字符串 + */ + public function send_email_api_error($address,$content,$annex=''){ + // $ad = '460834639@qq.com'; + $ad1 = '295155911@qq.com'; + $mail = new PHPMailer(); //实例化 + $mail->IsSMTP(); // 启用SMTP + $mail->Host = "smtp.126.com"; //SMTP服务器 163邮箱例子 + $mail->Port = 465; //邮件发送端口 + $mail->SMTPAuth = true; //启用SMTP认证 + $mail->SMTPSecure = 'ssl'; + $mail->CharSet = "UTF-8"; //字符集 + $mail->Encoding = "base64"; //编码方式 + $mail->Username = "tsf3920322@126.com"; //你的邮箱 + $mail->Password = "HLWXNRPUCTHJFIIX"; //你的密码(邮箱后台的授权密码) + $mail->From = "tsf3920322@126.com"; //发件人地址(也就是你的邮箱) + + // $mail->Subject = "微盟测试邮件"; //邮件标题 + $mail->Subject = $content['title']; //邮件标题 + + // $mail->FromName = "微盟体测中心"; //发件人姓名 + $mail->FromName = $content['from_user_name']; //发件人姓名 + + + for ($i=0; $i < count($address); $i++) { + $mail->AddAddress($address[$i], ""); //添加收件人(地址,昵称) + } + + if($annex != ''){ + // $url = ROOT_PATH. 'public' . DS . 'tsf' . DS .'demoooo.jpg'; + $mail->AddAttachment($annex,''); // 添加附件,并指定名称 + } + + $mail->IsHTML(true); //支持html格式内容 + + $neirong = $content['content']; + + $mail->Body = $neirong; //邮件主体内容 + //发送 + if (!$mail->Send()) { + + return ['code' => 10003,'msg'=>$mail->ErrorInfo]; + // return $mail->ErrorInfo; + } else { + return ['code' => 0]; + // return 'success'; + } + } + + + // 验证 + public function verify_data_is_ok($data = 2,$type){ + if($type == 'str'){ + if (is_string($data)) { + return true; + } else { + $this->record_api_log($data, null, ['code'=>10005,'msg'=>'校验参数不为字符串',[]]); + return false; + } + }else if($type == 'num'){ + if (is_numeric($data)) { + return true; + } else { + $this->record_api_log($data, null, ['code'=>10005,'msg'=>'校验参数不为数字',[]]); + return false; + } + }else if($type == 'intnum'){ + $pattern = '/^\d+$/'; + // dump($data); + if (preg_match($pattern, $data)) { + return true; // 匹配成功,返回 true + } else { + $this->record_api_log($data, null, ['code'=>10005,'msg'=>'校验参数不为整数数字',[]]); + return false; // 匹配失败,返回 false + } + }else if($type == 'datetime'){ + $formats = ['Y-m-d','Y-m-d H:i:s']; + foreach ($formats as $format) { + $dateTime = \DateTime::createFromFormat($format, $data); + // 检查时间字符串是否成功解析,并且解析后的日期时间与原始字符串表示的时间一致 + if ($dateTime && $dateTime->format($format) === $data) { + return true; + } + } + // 如果所有格式都解析失败,则返回 false + $this->record_api_log($data, null, ['code'=>10005,'msg'=>'校验参数不为日期格式',[]]); + return false; + }else if($type == 'num&letter'){ + if (preg_match('/^[a-zA-Z0-9]+$/', $data)) { + return true; + } else { + return false; + } + }else if($type == 'other'){ + + } + + } + + public function msg($data,$str='',$result = []){ + if(is_array($data)){ + if($str != ''){ + return json(['code'=>0,'msg'=>$str,'data'=>$data]); + }else{ + return json(['code'=>0,'msg'=>'操作成功','data'=>$data]); + } + }else{ + if($str != ''){ + return json(['code'=>$data,'msg'=>$str,'data'=>$result]); + } + return json(['code'=>$data,'msg'=>$this->return_data_all[$data],'data'=>$result]); + } + } + + public function generateRandomString($length = 10) { + $characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; + $charactersLength = strlen($characters); + $randomString = ''; + for ($i = 0; $i < $length; $i++) { + $randomString .= $characters[rand(0, $charactersLength - 1)]; + } + return $randomString; + } + + + +} \ No newline at end of file diff --git a/application/ZengJieCode/controller/app/Savemsg.php b/application/ZengJieCode/controller/app/Savemsg.php new file mode 100644 index 0000000..a6676fd --- /dev/null +++ b/application/ZengJieCode/controller/app/Savemsg.php @@ -0,0 +1,305 @@ +'LS-2502', + 'character_uuid'=>'2A25', + 'service_uuid'=>'180A' + ], + + ]; + protected $name_default = 0; + protected $kitchenscale_db_msg = [ + 'cookbook'=>'app_user_cookbook',//菜谱表 + 'foodlist3'=>'app_z_national_standard_food_type_3',//食材列表3 + 'user'=>'app_user_data',//banner + ]; + + // 加 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','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(!$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); + } + $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){ + $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'], + '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){ + $zengjie = Db::connect('zengjie_db'); + + $box_num = $zengjie->table('box_code_all')->where(['box_code'=>$data['box_serial_number']])->count(); + $box_num = $box_num+1; + $result = $zengjie->table('box_code_all')->insert([ + 'box_code'=>$data['box_serial_number'], + 'box_num'=>$box_num, + 'create_time'=>date('Y-m-d H:i:s'), + 'content_str'=>$data['sn_code_all'], + ]); + + if($result){ + return $this->msg([]); + }else{ + return $this->msg(10002,'录入失败'); + } + + // try { + // // 使用事务闭包,TP5会自动管理事务 + // $result = $zengjie->transaction(function() use ($zengjie, $data) { + // $sn_code = $zengjie->table('box_code_all')->where(['box_code'=>$data['box_serial_number']])->count(); + // $num = $sn_code+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'), + // ]); + + // // 使用安全的参数绑定方式 + // $snCodes = explode(',', $data['sn_code_all']); + // $zengjie->table('sn_code_all') + // ->where('sn_code', 'in', $snCodes) + // ->update(['batch_id' => $box_id]); + + // return $box_id; + // }); + + // return $this->msg(['id'=>$result]); + // } catch (\Exception $e) { + // 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,'打印失败'); + } + } + +} \ No newline at end of file diff --git a/application/ZengJieCode/view/admin/printaction/print_device_barcode_index.html b/application/ZengJieCode/view/admin/printaction/print_device_barcode_index.html new file mode 100644 index 0000000..42144b0 --- /dev/null +++ b/application/ZengJieCode/view/admin/printaction/print_device_barcode_index.html @@ -0,0 +1,122 @@ + + +
+ + +