811 lines
39 KiB
PHP
811 lines
39 KiB
PHP
<?php
|
||
|
||
namespace app\KitchenScale2\controller\app;
|
||
|
||
use think\Db;
|
||
|
||
class Usercenter extends Base{
|
||
|
||
protected $code_time = 50;
|
||
// protected $token_time = 2592000;//30天的秒数
|
||
protected $default_head_pic = 'http://tc.pcxbc.com/tsf/head_pic.png';
|
||
protected $page_num = 20;
|
||
protected $reedaw_db_msg = [
|
||
'zhanghao'=>'app_account_number',//账号表
|
||
'juese'=>'app_user_data',//角色表
|
||
];
|
||
protected $kitchenscale_db_msg = [
|
||
'cookbook'=>'app_user_cookbook',//菜谱表
|
||
'cookbook_label'=>'app_user_cookbook_label',//菜谱标签表
|
||
'uploadimg'=>'app_user_upload_img',//图片素材表
|
||
'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',//用户表
|
||
'search_history'=>'app_user_search_history',//用户搜索表
|
||
'business_cooperation'=>'app_business_cooperation_log',//商务合作
|
||
'eat_log'=>'app_user_kcal_log',//食材列表3
|
||
];
|
||
|
||
|
||
// 加 bcadd(,,20)
|
||
// 减 bcsub(,,20)
|
||
// 乘 bcmul(,,20)
|
||
// 除 bcdiv(,,20)
|
||
################################################################接口################################################################
|
||
################################################################接口################################################################
|
||
################################################################接口################################################################
|
||
|
||
// 获取角色信息
|
||
public function get_user_msg($data = ['token'=>'caadd1be045a65f30b92aa805f1de54a']){
|
||
// try { 、
|
||
if(count(input('post.')) > 0){
|
||
$data = input('post.');
|
||
}
|
||
if(!array_key_exists('token', $data)){
|
||
return $this->msg(10001);
|
||
}
|
||
if(!$this->verify_data_is_ok($data['token'],'str')){
|
||
return $this->msg(10005);
|
||
}
|
||
$return_data = $this->get_user_msg_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'] .= "接口: (get_default_config)\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 $this->msg(99999);
|
||
// }
|
||
}
|
||
// 修改用户
|
||
public function update_user_msg(){
|
||
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('nickname', $data)){
|
||
return $this->msg(10001,'nickname is miss');
|
||
}
|
||
if(!array_key_exists('gender', $data)){
|
||
return $this->msg(10001,'gender is miss');
|
||
}
|
||
if(!array_key_exists('birthday', $data)){
|
||
return $this->msg(10001,'birthday is miss');
|
||
}
|
||
if(!array_key_exists('height', $data)){
|
||
return $this->msg(10001,'height is miss');
|
||
}
|
||
if(!array_key_exists('weight', $data)){
|
||
return $this->msg(10001,'weight 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['nickname'],'str')){
|
||
return $this->msg(10005,'nickname type is error');
|
||
}
|
||
if(!$this->verify_data_is_ok($data['gender'],'intnum')){
|
||
return $this->msg(10005,'gender type is error');
|
||
}
|
||
if(!$this->verify_data_is_ok($data['birthday'],'datetime')){
|
||
return $this->msg(10005,'birthday type is error');
|
||
}
|
||
if(!$this->verify_data_is_ok($data['height'],'num')){
|
||
return $this->msg(10005,'height type is error');
|
||
}
|
||
if(!$this->verify_data_is_ok($data['weight'],'num')){
|
||
return $this->msg(10005,'weight type is error');
|
||
}
|
||
$return_data = $this->update_user_msg_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'] .= "接口: (get_default_config)\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 $this->msg(99999);
|
||
}
|
||
}
|
||
|
||
// 获取用户收藏点赞列表(OK)
|
||
public function get_user_collect_list($data = ['token'=>'caadd1be045a65f30b92aa805f1de54a','page'=>1,'search_data'=>'']){
|
||
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('page', $data)){
|
||
return $this->msg(10001,'page 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['page'],'intnum')){
|
||
return $this->msg(10005,'page type is error');
|
||
}
|
||
$return_data = $this->get_user_collect_list_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'] .= "接口: (get_default_config)\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 $this->msg(99999);
|
||
}
|
||
}
|
||
|
||
// 我的菜谱
|
||
public function get_my_cookbook($data = ['token'=>'caadd1be045a65f30b92aa805f1de54a','page'=>1,'search_data'=>'']){
|
||
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('page', $data)){
|
||
return $this->msg(10001,'page 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['page'],'intnum')){
|
||
return $this->msg(10005,'page type is error');
|
||
}
|
||
$return_data = $this->get_my_cookbook_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'] .= "接口: (get_default_config)\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 $this->msg(99999);
|
||
}
|
||
}
|
||
|
||
// 菜谱删除
|
||
public function del_my_cookbook($data = ['token'=>'caadd1be045a65f30b92aa805f1de54a','aud_id'=>1,'cookbook_id'=>'33']){
|
||
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('cookbook_id', $data)){
|
||
return $this->msg(10001,'cookbook_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['cookbook_id'],'intnum')){
|
||
return $this->msg(10005,'cookbook_id type is error');
|
||
}
|
||
$return_data = $this->del_my_cookbook_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'] .= "接口: (get_default_config)\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 $this->msg(99999);
|
||
}
|
||
}
|
||
|
||
// 搜索历史删除
|
||
public function del_search_history(){
|
||
// 尝试捕获异常
|
||
try {
|
||
if(count(input('post.')) > 0){
|
||
$data = input('post.');
|
||
}
|
||
if(!array_key_exists('token', $data)){
|
||
return $this->msg(10001,'token is miss');
|
||
}
|
||
if(!$this->verify_data_is_ok($data['token'],'str')){
|
||
return $this->msg(10005,'token type is error');
|
||
}
|
||
if(!array_key_exists('del_arr', $data)){
|
||
return $this->msg(10001,'del_arr is miss');
|
||
}
|
||
if(!$this->verify_data_is_ok($data['del_arr'],'str')){
|
||
return $this->msg(10005,'del_arr type is error');
|
||
}
|
||
$data['del_arr'] = strval($data['del_arr']);
|
||
$data['del_arr'] = trim($data['del_arr']);
|
||
// 判断是否是 "all"(不区分大小写)
|
||
if (strtolower($data['del_arr']) !== 'all' && preg_match('/^\d+(,\d+)*$/', $data['del_arr']) !== 1) {
|
||
return $this->msg(10005,'del_arr type is error');
|
||
}
|
||
|
||
$return_data = $this->del_search_history_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' => '系统错误']);
|
||
}
|
||
}
|
||
|
||
// 商务合作
|
||
public function business_cooperation(){
|
||
return $this->fetch();
|
||
}
|
||
|
||
|
||
|
||
#######################################################################action#######################################################################
|
||
#######################################################################action#######################################################################
|
||
#######################################################################action#######################################################################
|
||
|
||
public function get_user_msg_action($data){
|
||
// 获取账号下信息以及用户信息
|
||
$user_data = Db::table($this->reedaw_db_msg['zhanghao'])->where(['token'=>$data['token']])->field('id,token,nickname,head_pic,tel,email')->find();
|
||
if(!$user_data){
|
||
return $this->msg(10004);
|
||
}
|
||
// $return_data = [];
|
||
// 获取账号下信息以及用户信息start
|
||
$user_all_data['aud_id'] = '';
|
||
$user_all_data['token'] = $user_data['token'];
|
||
$user_all_data['nickname'] = $user_data['nickname'];
|
||
$user_all_data['head_pic'] = $user_data['head_pic'];
|
||
$user_all_data['gender'] = '';
|
||
$user_all_data['age'] = '';
|
||
$user_all_data['height'] = '';
|
||
$user_all_data['weight'] = '';
|
||
$user_all_data['set_kcal'] = '';
|
||
$user_all_data['is_use_set_kcal'] = '';
|
||
$user_all_data['tel'] = $user_data['tel'];
|
||
$user_all_data['email'] = $user_data['email'];
|
||
$cfc = Db::connect('cfc_db');
|
||
$user_account = $cfc->table($this->kitchenscale_db_msg['user'])
|
||
->where(["token"=>$data['token']])
|
||
->field('id as aud_id,token,nickname,head_pic,gender,age,height,weight,set_kcal,is_use_set_kcal,birthday')
|
||
->find();
|
||
if($user_account){
|
||
if($user_account['set_kcal'] == '.00'){
|
||
$user_account['set_kcal'] = 0;
|
||
}
|
||
$user_all_data['aud_id'] = $user_account['aud_id'];
|
||
$user_all_data['gender'] = $user_account['gender'];
|
||
$user_all_data['age'] = $user_account['age']?$user_account['age']:$this->calculate_age($user_account['birthday']);
|
||
$user_all_data['height'] = $user_account['height'];
|
||
$user_all_data['weight'] = $user_account['weight'];
|
||
$user_all_data['set_kcal'] = $user_account['set_kcal'];
|
||
$user_all_data['is_use_set_kcal'] = $user_account['is_use_set_kcal'];
|
||
$user_all_data['birthday'] = $user_account['birthday'];
|
||
$user_all_data['food_count'] = $this->user_that_day_food_count($user_account);
|
||
}else{
|
||
return $this->msg(10004);
|
||
}
|
||
|
||
|
||
// $return_data = $user_all_data;
|
||
return $this->msg($user_all_data);
|
||
// 获取账号下信息以及用户信息end
|
||
}
|
||
public function update_user_msg_action($data){
|
||
if($data['gender'] == 0){
|
||
return $this->msg(10005,'性别信息错误');
|
||
}
|
||
// 获取账号下信息以及用户信息
|
||
$user_data = Db::table($this->reedaw_db_msg['zhanghao'])->where(['token'=>$data['token']])->count();
|
||
if($user_data<=0){
|
||
return $this->msg(10005,'账号信息错误');
|
||
}
|
||
|
||
$cfc = Db::connect('cfc_db');
|
||
|
||
$is_user_true = $cfc->table($this->kitchenscale_db_msg['user'])->where(['token'=>$data['token']])->count();
|
||
|
||
$user_msg['nickname'] = $data['nickname'];
|
||
$user_msg['head_pic'] = $data['gender'] == 1?'https://tc.pcxbc.com/tsf/1.png':'https://tc.pcxbc.com/tsf/2.png';
|
||
$user_msg['gender'] = $data['gender'];
|
||
$user_msg['birthday'] = $data['birthday'];
|
||
$user_msg['height'] = $data['height'];
|
||
$user_msg['weight'] = $data['weight'];
|
||
|
||
if($is_user_true>0){
|
||
$user_msg['update_time'] = date('Y-m-d H:i:s');
|
||
$result = $cfc->table($this->kitchenscale_db_msg['user'])
|
||
->where(['token'=>$data['token']])
|
||
->update($user_msg);
|
||
}else{
|
||
$user_msg['token'] = $data['token'];
|
||
$result = $cfc->table($this->kitchenscale_db_msg['user'])
|
||
->insert($user_msg);
|
||
}
|
||
Db::table($this->reedaw_db_msg['zhanghao'])->where(['token'=>$data['token']])->update(['nickname'=>$user_msg['nickname']]);
|
||
if($result){
|
||
return $this->msg([]);
|
||
}else{
|
||
return $this->msg(10002);
|
||
}
|
||
}
|
||
public function get_user_collect_list_action($data){
|
||
// 获取账号下信息以及用户信息
|
||
$user_data = Db::table($this->reedaw_db_msg['zhanghao'])->where(['token'=>$data['token']])->count();
|
||
if($user_data<=0){
|
||
return $this->msg(10005,'账号信息错误');
|
||
}
|
||
|
||
$search_sql_str = "a.token = '".$data['token']."' AND a.is_del = 0 AND b.is_del = 0";
|
||
if(!array_key_exists('search_data', $data)){
|
||
$data['search_data'] = "";
|
||
}else{
|
||
if($data['search_data'] === ""){
|
||
$data['search_data'] = "";
|
||
}else{
|
||
$data['search_data'] = " AND (b.title LIKE '%".$data['search_data']."%' OR b.describe_data LIKE '%".$data['search_data']."%')";
|
||
}
|
||
}
|
||
$search_sql_str = $search_sql_str.$data['search_data'];
|
||
// "a.token = 'asdasdasdasda' AND a.is_del = 0 AND b.title LIKE '%鱼%' OR b.describe_data LIKE '%鱼%'";
|
||
|
||
$cfc = Db::connect('cfc_db');
|
||
|
||
$content_num = $cfc->table($this->kitchenscale_db_msg['collect_list'])
|
||
->alias('a')
|
||
->join($this->kitchenscale_db_msg['cookbook'].' b','a.cookbook_id = b.id','LEFT')
|
||
->join($this->kitchenscale_db_msg['uploadimg'].' c','b.cover = c.id','LEFT')
|
||
->where($search_sql_str)
|
||
->count();
|
||
$page_total = ceil($content_num/$this->page_num);;
|
||
$collect_list = $cfc->table($this->kitchenscale_db_msg['collect_list'])
|
||
->alias('a')
|
||
->join($this->kitchenscale_db_msg['cookbook'].' b','a.cookbook_id = b.id','LEFT')
|
||
->join($this->kitchenscale_db_msg['uploadimg'].' c','b.cover = c.id','LEFT')
|
||
->where($search_sql_str)
|
||
->field("b.id,b.title,b.cover as cover_id,c.pic_url as cover_url,b.likes_num,b.create_user_token,b.create_user_head_pic,b.create_user_nickname")
|
||
->page($data['page'],$this->page_num)
|
||
->select();
|
||
|
||
foreach ($collect_list as $key => $value) {
|
||
$collect_list[$key]['is_me_like_it'] = 'yes';
|
||
|
||
}
|
||
|
||
return $this->msg([
|
||
'page_now'=>$data['page'],
|
||
'page_total'=>$page_total,
|
||
'content_list'=>$collect_list
|
||
]);
|
||
}
|
||
|
||
public function get_my_cookbook_action($data){
|
||
// 获取账号下信息以及用户信息
|
||
$user_data = Db::table($this->reedaw_db_msg['zhanghao'])->where(['token'=>$data['token']])->count();
|
||
if($user_data<=0){
|
||
return $this->msg(10005,'账号信息错误');
|
||
}
|
||
|
||
$search_sql_str = "b.create_user_token = '".$data['token']."' AND b.is_del = 0";
|
||
if(!array_key_exists('search_data', $data)){
|
||
$data['search_data'] = "";
|
||
}else{
|
||
if($data['search_data'] === ""){
|
||
$data['search_data'] = "";
|
||
}else{
|
||
$data['search_data'] = " AND (b.title LIKE '%".$data['search_data']."%' OR b.describe_data LIKE '%".$data['search_data']."%')";
|
||
}
|
||
}
|
||
$search_sql_str = $search_sql_str.$data['search_data'];
|
||
|
||
$cfc = Db::connect('cfc_db');
|
||
$content_num = $cfc->table($this->kitchenscale_db_msg['cookbook'])
|
||
->alias('b')
|
||
->join($this->kitchenscale_db_msg['uploadimg'].' c','b.cover = c.id','LEFT')
|
||
->where($search_sql_str)
|
||
->count();
|
||
$page_total = ceil($content_num/$this->page_num);;
|
||
$content_list = $cfc->table($this->kitchenscale_db_msg['cookbook'])
|
||
->alias('b')
|
||
->join($this->kitchenscale_db_msg['uploadimg'].' c','b.cover = c.id','LEFT')
|
||
->where($search_sql_str)
|
||
->field("b.id,b.title,b.cover as cover_id,c.pic_url as cover_url,b.likes_num,b.create_user_token,b.create_user_head_pic,b.create_user_nickname")
|
||
->page($data['page'],$this->page_num)
|
||
->select();
|
||
|
||
// 获取用户收藏列表
|
||
$my_collect_list = $cfc->table($this->kitchenscale_db_msg['collect_list'])
|
||
->where(['token'=>$data['token'],'is_del'=>0])
|
||
->column('cookbook_id');
|
||
// dump($my_collect_list);
|
||
// 处理菜谱收藏信息
|
||
foreach ($content_list as $key => $value) {
|
||
// if(in_array($value['id'],$my_collect_list)){
|
||
if(array_key_exists($value['id'],$my_collect_list)){
|
||
$content_list[$key]['is_me_like_it'] = 'yes';
|
||
}else{
|
||
$content_list[$key]['is_me_like_it'] = 'no';
|
||
}
|
||
// if($value['cover'] == null){
|
||
// $content_list[$key]['cover'] = 'https://tc.pcxbc.com/kitchenscale_all/diule.jpg';
|
||
// }
|
||
unset($content_list[$key]['ROW_NUMBER']);
|
||
}
|
||
|
||
// foreach ($collect_list as $key => $value) {
|
||
// $collect_list[$key]['is_me_like_it'] = 'yes';
|
||
|
||
// }
|
||
|
||
return $this->msg([
|
||
'page_now'=>$data['page'],
|
||
'page_total'=>$page_total,
|
||
'content_list'=>$content_list
|
||
]);
|
||
}
|
||
|
||
public function del_my_cookbook_action($data){
|
||
// 获取账号下信息以及用户信息
|
||
$user_data = Db::table($this->reedaw_db_msg['zhanghao'])->where(['token'=>$data['token']])->count();
|
||
if($user_data<=0){
|
||
return $this->msg(10005,'账号信息错误');
|
||
}
|
||
|
||
$cfc = Db::connect('cfc_db');
|
||
|
||
$cookbook_data = $cfc->table($this->kitchenscale_db_msg['cookbook'])->where(['id'=>$data['cookbook_id'],'create_user_token'=>$data['token']])->find();
|
||
|
||
if($cookbook_data){
|
||
|
||
// 启动事务
|
||
$cfc->startTrans();
|
||
try{
|
||
$cfc->table($this->kitchenscale_db_msg['cookbook'])->where(['id'=>$data['cookbook_id']])->update(['is_del'=>1]);
|
||
$result_banner = $cfc->table($this->kitchenscale_db_msg['banner'])->where(['cookbook_id'=>$cookbook_data['id']])->count();
|
||
if($result_banner > 0){
|
||
$cfc->table($this->kitchenscale_db_msg['banner'])->where(['cookbook_id'=>$cookbook_data['id']])->update(['is_del'=>1]);
|
||
}
|
||
// 提交事务
|
||
$cfc->commit();
|
||
return $this->msg([]);
|
||
} catch (\Exception $e) {
|
||
// 回滚事务
|
||
$cfc->rollback();
|
||
return $this->msg(10002);
|
||
}
|
||
}else{
|
||
return $this->msg(10003);
|
||
}
|
||
|
||
}
|
||
|
||
|
||
public function del_search_history_action($data){
|
||
|
||
// $data['del_arr'] = strval($data['del_arr']);
|
||
// $data['del_arr'] = trim($data['del_arr']);
|
||
|
||
// // 正确的判断逻辑:如果不是all 并且 不是ID列表,就报错
|
||
// if (strtolower($data['del_arr']) !== 'all' && preg_match('/^\d+(,\d+)*$/', $data['del_arr']) !== 1) {
|
||
// return $this->msg(10005, 'del_arr type is error');
|
||
// }
|
||
|
||
|
||
|
||
$cfc = Db::connect('cfc_db');
|
||
$user = $cfc->table($this->kitchenscale_db_msg['user'])->where(['token'=>$data['token']])->field('id,token')->find();
|
||
if(!$user){
|
||
return $this->msg(20001,'账号信息错误');
|
||
}
|
||
if($data['del_arr'] == 'all'){
|
||
$result = $cfc->table($this->kitchenscale_db_msg['search_history'])
|
||
->where(['user_id'=>$user['id']])
|
||
->update(['is_del'=>1]);
|
||
}else{
|
||
$result = $cfc->table($this->kitchenscale_db_msg['search_history'])
|
||
->where("user_id = ".$user['id']." AND id IN (".$data['del_arr'].")")
|
||
->update(['is_del'=>1]);
|
||
}
|
||
if($result){
|
||
return $this->msg([]);
|
||
}else{
|
||
return $this->msg(10002);
|
||
}
|
||
|
||
|
||
// 获取菜谱分类标签end
|
||
}
|
||
|
||
public function business_cooperation_action(){
|
||
$data = input();
|
||
|
||
$cfc = Db::connect('cfc_db');
|
||
$result = $cfc->table($this->kitchenscale_db_msg['business_cooperation'])
|
||
->insert([
|
||
'name'=>$data['name'],
|
||
'tel'=>$data['phone'],
|
||
'company'=>$data['company'],
|
||
'intention_data'=> implode(',',$data['selectedValues']),
|
||
'notes_data'=>$data['remark'],
|
||
'create_time'=>date('Y-m-s H:i:s'),
|
||
]);
|
||
if($result){
|
||
return $this->msg([]);
|
||
}else{
|
||
return $this->msg(10002);
|
||
}
|
||
}
|
||
|
||
|
||
public function user_that_day_food_count($user_data){
|
||
$cfc = Db::connect('cfc_db');
|
||
if($user_data['birthday']){
|
||
$user_data['age_num'] = $this->calculate_age($user_data['birthday']);
|
||
}else{
|
||
$user_data['age_num'] = $user_data['age'];
|
||
}
|
||
$nutrition_data = $this->count_user_nutrition_all($user_data);
|
||
if($user_data['is_use_set_kcal'] == 1){
|
||
$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);
|
||
|
||
}
|
||
$day_time = date('Y-m-d');
|
||
$return_data = [
|
||
'date'=>$day_time, //时间
|
||
'suggestion'=>[ //建议
|
||
'kcal'=>$nutrition_data['kcal'], //建议摄入卡路里量
|
||
'carbohydrate'=>$nutrition_data['carbohydrate'], //建议摄入碳水量
|
||
'protein'=>$nutrition_data['protein'], //建议摄入蛋白质量
|
||
'fat'=>$nutrition_data['fat'], //建议摄入脂肪量
|
||
],
|
||
'today_intake'=>[ //今日已摄入
|
||
'kcal'=>0, //今日已摄入卡路里量
|
||
'carbohydrate'=>0, //今日已摄入碳水量
|
||
'protein'=>0, //今日已摄入蛋白质量
|
||
'fat'=>0, //今日已摄入脂肪量
|
||
],
|
||
'remaining_kcal'=>$nutrition_data['kcal'], //剩下可摄入卡路里量
|
||
'list'=>[
|
||
[
|
||
'name'=>'早餐',
|
||
'val'=>0,
|
||
'unit'=>'kcal',
|
||
'color'=>'#0992B4',
|
||
'icon'=>'https://tc.pcxbc.com/kitchenscale_all/meal_1.png',
|
||
'icon_home'=>'/static/1.png',
|
||
'bgimg_home'=>'/static/2.png',
|
||
'kcal_proportion'=>0,
|
||
'list'=>[],
|
||
],
|
||
[
|
||
'name'=>'午餐',
|
||
'val'=>0,
|
||
'unit'=>'kcal',
|
||
'color'=>'#4F9211',
|
||
'icon'=>'https://tc.pcxbc.com/kitchenscale_all/meal_2.png',
|
||
'icon_home'=>'/static/3.png',
|
||
'bgimg_home'=>'/static/4.png',
|
||
'kcal_proportion'=>0,
|
||
'list'=>[],
|
||
],
|
||
[
|
||
'name'=>'晚餐',
|
||
'val'=>0,
|
||
'unit'=>'kcal',
|
||
'color'=>'#B354B0',
|
||
'icon'=>'https://tc.pcxbc.com/kitchenscale_all/meal_3.png',
|
||
'icon_home'=>'/static/5.png',
|
||
'bgimg_home'=>'/static/6.png',
|
||
'kcal_proportion'=>0,
|
||
'list'=>[],
|
||
],
|
||
[
|
||
'name'=>'加餐',
|
||
'val'=>0,
|
||
'unit'=>'kcal',
|
||
'color'=>'#C08433',
|
||
'icon'=>'https://tc.pcxbc.com/kitchenscale_all/meal_4.png',
|
||
'icon_home'=>'/static/7.png',
|
||
'bgimg_home'=>'/static/8.png',
|
||
'kcal_proportion'=>0,
|
||
'list'=>[],
|
||
],
|
||
],
|
||
];
|
||
|
||
// 查询用户今日摄入食物
|
||
$food_content = $cfc->table($this->kitchenscale_db_msg['eat_log'])
|
||
->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 = " . $user_data['aud_id'] . " AND CAST(a.create_time AS DATE) = CAST('" . $day_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,a.food_id')
|
||
->select();
|
||
|
||
if(count($food_content) > 0){ //计算营养物质
|
||
$food_content = $this->calculate_nutrients($food_content);
|
||
// return $this->msg($food_content);
|
||
foreach ($food_content as $key => $value) {
|
||
// dump($value['nutrients_four']);
|
||
$return_data['today_intake']['kcal'] = bcadd($return_data['today_intake']['kcal'],$value['kcal_val'],2);
|
||
$return_data['today_intake']['carbohydrate'] = bcadd($return_data['today_intake']['carbohydrate'],$value['carbohydrate_val'],2);
|
||
$return_data['today_intake']['protein'] = bcadd($return_data['today_intake']['protein'],$value['protein_val'],2);
|
||
$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);
|
||
// $return_data['list'][0]['nutrients_four'][0]['value'] = bcadd($return_data['list'][0]['nutrients_four'][0]['value'],$value['kcal_val'],2);
|
||
// $return_data['list'][0]['nutrients_four'][1]['value'] = bcadd($return_data['list'][0]['nutrients_four'][1]['value'],$value['carbohydrate_val'],2);
|
||
// $return_data['list'][0]['nutrients_four'][2]['value'] = bcadd($return_data['list'][0]['nutrients_four'][2]['value'],$value['protein_val'],2);
|
||
// $return_data['list'][0]['nutrients_four'][3]['value'] = bcadd($return_data['list'][0]['nutrients_four'][3]['value'],$value['fat_val'],2);
|
||
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'],
|
||
// 'nutrients_four' => $value['nutrients_four'],
|
||
// 'nutrients_list' => $value['nutrients_list']
|
||
]);
|
||
}else if($value['meals_type'] == '午餐'){
|
||
$return_data['list'][1]['val'] = bcadd($return_data['list'][1]['val'],$value['kcal_val'],2);
|
||
// $return_data['list'][1]['nutrients_four'][0]['value'] = bcadd($return_data['list'][1]['nutrients_four'][0]['value'],$value['kcal_val'],2);
|
||
// $return_data['list'][1]['nutrients_four'][1]['value'] = bcadd($return_data['list'][1]['nutrients_four'][1]['value'],$value['carbohydrate_val'],2);
|
||
// $return_data['list'][1]['nutrients_four'][2]['value'] = bcadd($return_data['list'][1]['nutrients_four'][2]['value'],$value['protein_val'],2);
|
||
// $return_data['list'][1]['nutrients_four'][3]['value'] = bcadd($return_data['list'][1]['nutrients_four'][3]['value'],$value['fat_val'],2);
|
||
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'],
|
||
// 'nutrients_four' => $value['nutrients_four'],
|
||
// 'nutrients_list' => $value['nutrients_list']
|
||
]);
|
||
}else if($value['meals_type'] == '晚餐'){
|
||
$return_data['list'][2]['val'] = bcadd($return_data['list'][2]['val'],$value['kcal_val'],2);
|
||
// $return_data['list'][2]['nutrients_four'][0]['value'] = bcadd($return_data['list'][2]['nutrients_four'][0]['value'],$value['kcal_val'],2);
|
||
// $return_data['list'][2]['nutrients_four'][1]['value'] = bcadd($return_data['list'][2]['nutrients_four'][1]['value'],$value['carbohydrate_val'],2);
|
||
// $return_data['list'][2]['nutrients_four'][2]['value'] = bcadd($return_data['list'][2]['nutrients_four'][2]['value'],$value['protein_val'],2);
|
||
// $return_data['list'][2]['nutrients_four'][3]['value'] = bcadd($return_data['list'][2]['nutrients_four'][3]['value'],$value['fat_val'],2);
|
||
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'],
|
||
// 'nutrients_four' => $value['nutrients_four'],
|
||
// 'nutrients_list' => $value['nutrients_list']
|
||
]);
|
||
}else{
|
||
$return_data['list'][3]['val'] = bcadd($return_data['list'][3]['val'],$value['kcal_val'],2);
|
||
// $return_data['list'][3]['nutrients_four'][0]['value'] = bcadd($return_data['list'][3]['nutrients_four'][0]['value'],$value['kcal_val'],2);
|
||
// $return_data['list'][3]['nutrients_four'][1]['value'] = bcadd($return_data['list'][3]['nutrients_four'][1]['value'],$value['carbohydrate_val'],2);
|
||
// $return_data['list'][3]['nutrients_four'][2]['value'] = bcadd($return_data['list'][3]['nutrients_four'][2]['value'],$value['protein_val'],2);
|
||
// $return_data['list'][3]['nutrients_four'][3]['value'] = bcadd($return_data['list'][3]['nutrients_four'][3]['value'],$value['fat_val'],2);
|
||
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'],
|
||
// 'nutrients_four' => $value['nutrients_four'],
|
||
// 'nutrients_list' => $value['nutrients_list']
|
||
]);
|
||
}
|
||
}
|
||
// dump($return_data['list']);
|
||
// die;
|
||
$return_data['list'] = array_values($return_data['list']);
|
||
// 处理剩下可吃
|
||
$return_data['remaining_kcal'] = bcsub($return_data['suggestion']['kcal'],$return_data['today_intake']['kcal'],2)>=0?bcsub($return_data['suggestion']['kcal'],$return_data['today_intake']['kcal'],2):0;
|
||
|
||
$nameMap = [
|
||
'kcal' => ['卡路里','kcal','https://tc.pcxbc.com/kitchenscale_all/icon_kcal.png','#5180D8'],
|
||
'carbohydrate' => ['碳水','g','https://tc.pcxbc.com/kitchenscale_all/icon_carbohydrate.png','#ED7886'],
|
||
'protein' => ['蛋白质','g','https://tc.pcxbc.com/kitchenscale_all/icon_protein.png','#FFB169'],
|
||
'fat' => ['脂肪','g','https://tc.pcxbc.com/kitchenscale_all/icon_fat.png','#3CB383'],
|
||
];
|
||
$all_yy_data = bcadd($return_data['suggestion']['fat'],bcadd($return_data['suggestion']['carbohydrate'],$return_data['suggestion']['protein'],20),20);
|
||
foreach ($return_data['suggestion'] as $key => $value) {
|
||
$return_data['nutrients_four'][] = [
|
||
'name'=>$nameMap[$key][0],
|
||
'unit'=>$nameMap[$key][1],
|
||
'suggestion'=>$value,
|
||
'today_intake'=>$return_data['today_intake'][$key],
|
||
'icon'=>$nameMap[$key][2],
|
||
'color'=>$nameMap[$key][3],
|
||
'proportion'=>bcdiv($return_data['today_intake'][$key],$value,2) >= 1?'100':(bcdiv($return_data['today_intake'][$key],$value,2))*100,
|
||
'proportion_fp'=>$key == 'kcal'?0:(bcdiv($return_data['suggestion'][$key],$all_yy_data,2))*100,
|
||
];
|
||
}
|
||
unset($return_data['suggestion']);
|
||
unset($return_data['today_intake']);
|
||
|
||
|
||
// // 处理各餐卡路里占比
|
||
// $return_data = $this->calculate_kcal_proportion($return_data);
|
||
// // 计算营养物质能量占比
|
||
// $return_data = $this->calculate_energy_proportion($return_data);
|
||
// // 排序营养元素食物排行榜
|
||
// $return_data = $this->energy_food_rank($return_data);
|
||
// // 微量元素处理全天
|
||
// $return_data = $this->calculate_trace_elements($return_data);
|
||
// 处理单餐营养占比
|
||
// foreach ($return_data['list'] as $key => $value) {
|
||
// $all_yy_data_0 = bcadd($value['nutrients_four'][3]['value'],bcadd($value['nutrients_four'][1]['value'],$value['nutrients_four'][2]['value'],20),2);
|
||
// foreach ($value['nutrients_four'] as $k => $v) {
|
||
// if($k != 0){
|
||
// if($all_yy_data_0 == 0){
|
||
// $return_data['list'][$key]['nutrients_four'][$k]['proportion'] = 0;
|
||
// }else{
|
||
// $return_data['list'][$key]['nutrients_four'][$k]['proportion'] = bcdiv($value['nutrients_four'][$k]['value'],$all_yy_data_0,2) >= 1?'100':(bcdiv($value['nutrients_four'][$k]['value'],$all_yy_data_0,2))*100;
|
||
// }
|
||
// }
|
||
// }
|
||
// }
|
||
}
|
||
|
||
return $return_data;
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
} |