545 lines
27 KiB
PHP
545 lines
27 KiB
PHP
<?php
|
||
|
||
namespace app\KitchenScale\controller\app;
|
||
|
||
use think\Db;
|
||
// use app\KitchenScale\controller\app\Wechat;// 引入Wechat服务类
|
||
|
||
class Index extends Base{
|
||
|
||
protected $code_time = 50;
|
||
// protected $token_time = 2592000;//30天的秒数
|
||
protected $default_head_pic = 'http://tc.pcxbc.com/tsf/head_pic.png';
|
||
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
|
||
'foodlist4'=>'app_z_national_standard_food_type_4',//食材列表3
|
||
'collect_list'=>'app_user_collect_list',//点赞表
|
||
'banner'=>'app_banner_data',//banner
|
||
'version'=>'app_version_log',//版本表
|
||
'user'=>'app_user_data',//用户表
|
||
'kcal_log'=>'app_user_kcal_log',//饮食记录表
|
||
'search_log'=>'app_user_search_log',//搜索历史表
|
||
|
||
];
|
||
|
||
|
||
// 加 bcadd(,,20)
|
||
// 减 bcsub(,,20)
|
||
// 乘 bcmul(,,20)
|
||
// 除 bcdiv(,,20)
|
||
################################################################接口################################################################
|
||
################################################################接口################################################################
|
||
################################################################接口################################################################
|
||
|
||
// 检测版本及判断是否登录失效
|
||
public function login_invalid_version($data = ['token'=>'']){
|
||
try {
|
||
// 你的业务逻辑
|
||
if(count(input('post.')) > 0){
|
||
$data = input('post.');
|
||
}
|
||
if(!array_key_exists('token', $data)){
|
||
return $this->msg(10001);
|
||
}
|
||
$result = Db::table($this->kitchenscale_db_msg['version'])->order('is_del,id desc')->find();
|
||
if($result){
|
||
$version = $result['version_num_original'];
|
||
$url = $result['download_url'];
|
||
}else{
|
||
$version = '';
|
||
$url = '';
|
||
}
|
||
if($this->token_time_validate($data['token']) === false){
|
||
$this->record_api_log($data, null, ['code'=>-1,'msg'=>'未登录',['version'=>$version,'url'=>$url]]);
|
||
return $this->msg(-1,'未登录',['version'=>$version,'url'=>$url]);
|
||
}else{
|
||
$this->record_api_log($data, null, ['code'=>0,'msg'=>'success',['version'=>$version,'url'=>$url]]);
|
||
return $this->msg(['version'=>$version,'url'=>$url]);
|
||
}
|
||
|
||
} 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 $this->msg(99999);
|
||
}
|
||
}
|
||
|
||
// 微信手机号快捷登录
|
||
public function wechat_quick_login($data = ['code'=>'asdasdasd','encryptedData'=>'adsadasdasd','iv'=>'asdasdasdasd']){
|
||
try {
|
||
// 你的业务逻辑
|
||
if(count(input('post.')) > 0){
|
||
$data = input('post.');
|
||
}
|
||
if(!array_key_exists('code', $data)){
|
||
// return $this->msg(10001,'');
|
||
return $this->msg(10001,'code is miss');
|
||
}
|
||
if(!array_key_exists('encryptedData', $data)){
|
||
return $this->msg(10001,'encryptedData is miss');
|
||
}
|
||
if(!array_key_exists('iv', $data)){
|
||
return $this->msg(10001,'iv is miss');
|
||
}
|
||
// 校验参数
|
||
if (empty($data['code'])) {
|
||
return $this->msg(10001,'code is miss.');
|
||
}
|
||
if (empty($data['encryptedData'])) {
|
||
return $this->msg(10001,'encryptedData is miss.');
|
||
}
|
||
if (empty($data['iv'])) {
|
||
return $this->msg(10001,'iv is miss.');
|
||
}
|
||
|
||
// 调用Wechat服务类处理微信登录逻辑
|
||
$wechatService = new Wechat();
|
||
// die;
|
||
$result = $wechatService->handleWechatLogin($data['code'], $data['encryptedData'], $data['iv']);
|
||
// dump($result);
|
||
// die;
|
||
if($result['code'] == 0){
|
||
// return $this->msg($result['code'],$result['msg']);
|
||
|
||
$user_data = Db::table($this->reedaw_db_msg['zhanghao'])->where(['tel'=>$result['data']['phoneNumber'],'is_del'=>0])->find();
|
||
|
||
if($user_data){
|
||
Db::table($this->reedaw_db_msg['zhanghao'])->where(['token'=>$user_data['token']])->update(['login_time'=>date('Y-m-d H:i:s')]);
|
||
$return_data = $this->msg(['token'=>$user_data['token'],'aan_id'=>$user_data['id']]);
|
||
}else{
|
||
$set_data['password'] = '';
|
||
$set_data['tel'] = $result['data']['phoneNumber'];
|
||
$set_data['head_pic'] = $this->default_head_pic;
|
||
$set_data['nickname'] = '用户'.$result['data']['phoneNumber'];
|
||
$set_data['create_time'] = date('Y-m-d H:i:s');
|
||
$set_data['login_time'] = date('Y-m-d H:i:s');
|
||
$set_data['token'] = md5($result['data']['phoneNumber'].$this->create_random_string(12).time());
|
||
$set_user_result = Db::table($this->reedaw_db_msg['zhanghao'])->insertGetId($set_data);
|
||
if($set_user_result){
|
||
$return_data = $this->msg(['token'=>$set_data['token'],'aan_id'=>$set_user_result],'登录成功');
|
||
}else{
|
||
$return_data = $this->msg(10002);
|
||
}
|
||
}
|
||
return $return_data;
|
||
}else{
|
||
return $this->msg($result['code'],$result['msg']);
|
||
}
|
||
|
||
} 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'] .= "方法: (wechat_quick_login)" . "\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_default_config(){
|
||
// try {
|
||
$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_default_config_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 search_column($data = ['search_data'=>'鱼','token'=>'','page'=>1]){
|
||
try {
|
||
// 你的业务逻辑
|
||
if(count(input('post.')) > 0){
|
||
$data = input('post.');
|
||
}
|
||
if(!array_key_exists('search_data', $data)){
|
||
return $this->msg(10001);
|
||
}
|
||
// if(!array_key_exists('page', $data)){
|
||
// return $this->msg(10001,'page is miss');
|
||
// }
|
||
if(!$this->verify_data_is_ok($data['search_data'],'str')){
|
||
return $this->msg(10005);
|
||
}
|
||
// if(!$this->verify_data_is_ok($data['page'],'intnum')){
|
||
// return $this->msg(10005,'page type is error');
|
||
// }
|
||
$return_data = $this->search_column_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'] .= "接口: (search_column)\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);
|
||
}
|
||
}
|
||
|
||
#######################################################################action#######################################################################
|
||
#######################################################################action#######################################################################
|
||
#######################################################################action#######################################################################
|
||
|
||
// 老版
|
||
// public function get_default_config_action($data){
|
||
// $return_data = [
|
||
// 'cook_label'=>[],
|
||
// // 'account'=>[],
|
||
// 'food_list'=>[],
|
||
// 'banner'=>[],
|
||
// 'jingang_region'=>[
|
||
// ['name'=>'菜谱分类','jump_url'=>'/pages/menu/menu','icon'=>'https://tc.pcxbc.com/kitchenscale_all/vajra1.png'],
|
||
// ['name'=>'我的收藏','jump_url'=>'/pageTwo/me/mymenu','icon'=>'https://tc.pcxbc.com/kitchenscale_all/vajra2.png'],
|
||
// ['name'=>'热量计算','jump_url'=>'/pages/count/count','icon'=>'https://tc.pcxbc.com/kitchenscale_all/vajra3.png'],
|
||
// ['name'=>'健康食谱','jump_url'=>'/pages/menu/menu','icon'=>'https://tc.pcxbc.com/kitchenscale_all/vajra4.png'],
|
||
// ],
|
||
// ];
|
||
|
||
// $cfc = Db::connect('cfc_db');
|
||
// // // 获取食材分类列表start
|
||
// // $foodlist1 = $cfc->table($this->kitchenscale_db_msg['foodlist1'])->where("is_del = 0")->order('sort_num desc')->field('id,name')->select();
|
||
|
||
// $foodlist1 = $cfc->query("SELECT id,name FROM ".$this->kitchenscale_db_msg['foodlist1']." WHERE is_del = 0 ORDER BY sort_num");
|
||
// // dump($foodlist1);
|
||
// // die;
|
||
// $foodlist2 = $cfc->table($this->kitchenscale_db_msg['foodlist2'])->where("is_del = 0")->field('id,name,one_id')->select();
|
||
// // dump($foodlist3);
|
||
// foreach ($foodlist1 as $key => $value) {
|
||
// unset($foodlist1[$key]['ROW_NUMBER']);
|
||
// $foodlist1[$key]['list'] = [];
|
||
// foreach ($foodlist2 as $k => $v) {
|
||
// if($v['one_id'] == $value['id']){
|
||
// unset($foodlist2[$k]['ROW_NUMBER']);
|
||
// array_push($foodlist1[$key]['list'],$foodlist2[$k]);
|
||
// // unset($foodlist2[$k]);
|
||
// }
|
||
// }
|
||
// }
|
||
// $return_data['food_list'] = $foodlist1;
|
||
// // // 获取食材分类列表end
|
||
// // 获取菜谱分类标签start
|
||
// $cook_label = $cfc->table($this->kitchenscale_db_msg['cookbook_label'])
|
||
// ->where("is_del = 0")
|
||
// ->field('id,name')
|
||
// ->select();
|
||
// foreach ($cook_label as $key => $value) {
|
||
// unset($cook_label[$key]['ROW_NUMBER']);
|
||
// }
|
||
// $return_data['cook_label'] = $cook_label;
|
||
// // 获取菜谱分类标签end
|
||
|
||
// // 获取首页信息start
|
||
// // 获取banner
|
||
// $banner_list = $cfc->query("select b.id,b.title,b.cover,b.create_user_head_pic,b.create_user_nickname,c.pic_url
|
||
// from ".$this->kitchenscale_db_msg['banner']." as a
|
||
// LEFT JOIN ".$this->kitchenscale_db_msg['cookbook']." as b on a.cookbook_id = b.id
|
||
// LEFT JOIN ".$this->kitchenscale_db_msg['uploadimg']." as c on b.cover = c.id
|
||
// where a.is_del=0 AND b.is_del=0
|
||
// ORDER BY a.sort_num desc,a.id desc
|
||
// ");
|
||
// // dump($banner_list);
|
||
// // die;
|
||
// if($data['token'] != ''){
|
||
// // 获取账号下信息以及用户信息
|
||
// $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(20001,'账号信息错误');
|
||
// }
|
||
// if(!$this->verify_data_is_ok($data['token'],'str')){
|
||
// return $this->msg(10005);
|
||
// }
|
||
// // 获取账号下信息以及用户信息
|
||
// $user_data = Db::table($this->reedaw_db_msg['zhanghao'])->where(['token'=>$data['token']])->field('id,token,nickname,head_pic')->find();
|
||
// if(!$user_data){
|
||
// return $this->msg(20001,'账号信息错误');
|
||
// }
|
||
// $collect_list = $cfc->table($this->kitchenscale_db_msg['collect_list'])->where(['token'=>$data['token'],'is_del'=>0])->column('cookbook_id');
|
||
// foreach ($banner_list as $key => $value) {
|
||
// if(array_key_exists($value['id'],$collect_list)){
|
||
// $banner_list[$key]['is_me_like_it'] = 'yes';
|
||
// }else{
|
||
// $banner_list[$key]['is_me_like_it'] = 'no';
|
||
// }
|
||
// unset($banner_list[$key]['ROW_NUMBER']);
|
||
// }
|
||
// }else{
|
||
// foreach ($banner_list as $key => $value) {
|
||
// $banner_list[$key]['is_me_like_it'] = 'no';
|
||
// unset($banner_list[$key]['ROW_NUMBER']);
|
||
// }
|
||
// }
|
||
// $return_data['banner'] = $banner_list;
|
||
// // return $this->msg($return_data);
|
||
// // 获取首页信息end
|
||
// return $this->msg($return_data);
|
||
// }
|
||
// 新版
|
||
public function get_default_config_action($data){
|
||
$return_data = [
|
||
'user_data'=>[],
|
||
'kcal_data'=>[
|
||
'title'=>'今日已摄入热量(千卡)',
|
||
'kcal'=>['value'=>0,'unit'=>'kcal','standard'=>'不达标','color'=>'#F0AD4E'],
|
||
'other_elements'=>[
|
||
'carbohydrate'=>['value'=>0,'unit'=>'g'],
|
||
'protein'=>['value'=>0,'unit'=>'g'],
|
||
'fat'=>['value'=>0,'unit'=>'g'],
|
||
],
|
||
'list'=>[
|
||
['title'=>'早餐(千卡)','icon'=>'','value'=>0,'unit'=>'kcal'],
|
||
['title'=>'午餐(千卡)','icon'=>'','value'=>0,'unit'=>'kcal'],
|
||
['title'=>'晚餐(千卡)','icon'=>'','value'=>0,'unit'=>'kcal'],
|
||
['title'=>'加餐(千卡)','icon'=>'','value'=>0,'unit'=>'kcal'],
|
||
],
|
||
],
|
||
'banner_data'=>[],
|
||
'search_history'=>[],
|
||
'search_guess'=>[],
|
||
];
|
||
$cfc = Db::connect('cfc_db');
|
||
// 获取banner
|
||
$banner_list = $cfc->query("select b.id,b.title,b.cover,b.create_user_head_pic,b.create_user_nickname,c.pic_url
|
||
from ".$this->kitchenscale_db_msg['banner']." as a
|
||
LEFT JOIN ".$this->kitchenscale_db_msg['cookbook']." as b on a.cookbook_id = b.id
|
||
LEFT JOIN ".$this->kitchenscale_db_msg['uploadimg']." as c on b.cover = c.id
|
||
where a.is_del=0 AND b.is_del=0
|
||
ORDER BY a.sort_num desc,a.id desc
|
||
");
|
||
// 如果有账号信息
|
||
if($data['token'] != ''){
|
||
if(!$this->verify_data_is_ok($data['token'],'str')){
|
||
return $this->msg(10005);
|
||
}
|
||
// 获取账号下信息以及用户信息 start
|
||
$user = $cfc->table($this->kitchenscale_db_msg['user'])->where(["token"=>$data['token']])->find();
|
||
if(!$user){
|
||
return $this->msg(20001,'账号信息错误');
|
||
}
|
||
$return_data['user_data'] = $user;
|
||
if($return_data['user_data']['birthday']){
|
||
$return_data['user_data']['age'] = $this->calculate_age($return_data['user_data']['birthday']);
|
||
}
|
||
unset($return_data['user_data']['id']);
|
||
unset($return_data['user_data']['token']);
|
||
unset($return_data['user_data']['update_time']);
|
||
unset($return_data['user_data']['ROW_NUMBER']);
|
||
// 获取账号下信息以及用户信息 end
|
||
// 处理计食器信息 start
|
||
$kcal = $cfc->table($this->kitchenscale_db_msg['kcal_log'])->where(["aud_id"=>$user['id'],'is_del'=>0])->whereTime('create_time', 'today')->order('id desc')->select();
|
||
if(count($kcal)>0){
|
||
$return_data['kcal_data']['title'] = '今日已摄入热量(千卡)'.$kcal[0]['create_time'];
|
||
foreach ($kcal as $key => $value) {
|
||
|
||
$return_data['kcal_data']['kcal']['value'] = bcadd($return_data['kcal_data']['kcal']['value'],$value['kcal_val'],2);
|
||
$return_data['kcal_data']['other_elements']['carbohydrate']['value'] = bcadd($return_data['kcal_data']['other_elements']['carbohydrate']['value'],$value['carbohydrate_val'],2);
|
||
$return_data['kcal_data']['other_elements']['protein']['value'] = bcadd($return_data['kcal_data']['other_elements']['protein']['value'],$value['protein_val'],2);
|
||
$return_data['kcal_data']['other_elements']['fat']['value'] = bcadd($return_data['kcal_data']['other_elements']['fat']['value'],$value['fat_val'],2);
|
||
if($value['meals_type'] == '早餐'){
|
||
$return_data['kcal_data']['list'][0]['value'] = bcadd($return_data['kcal_data']['list'][0]['value'],$value['kcal_val'],2);
|
||
}else if($value['meals_type'] == '午餐'){
|
||
$return_data['kcal_data']['list'][1]['value'] = bcadd($return_data['kcal_data']['list'][1]['value'],$value['kcal_val'],2);
|
||
}else if($value['meals_type'] == '晚餐'){
|
||
$return_data['kcal_data']['list'][2]['value'] = bcadd($return_data['kcal_data']['list'][2]['value'],$value['kcal_val'],2);
|
||
}else{
|
||
$return_data['kcal_data']['list'][3]['value'] = bcadd($return_data['kcal_data']['list'][3]['value'],$value['kcal_val'],2);
|
||
}
|
||
}
|
||
foreach ($return_data['kcal_data']['list'] as $key => $value) {
|
||
if($value['value'] <= 0){
|
||
$return_data['kcal_data']['list'][$key]['value'] = '-';
|
||
}
|
||
}
|
||
}
|
||
|
||
if($user['is_use_set_kcal'] == 1){
|
||
$nutrition_data['kcal'] = $user['set_kcal'];
|
||
}else{
|
||
$user['age_num'] = $return_data['user_data']['age'];
|
||
$nutrition_data = $this->count_user_nutrition_all($user);
|
||
}
|
||
|
||
if(bcdiv($return_data['kcal_data']['kcal']['value'],$nutrition_data['kcal'],2) < 0.9){
|
||
$return_data['kcal_data']['kcal']['standard'] = '不达标';
|
||
$return_data['kcal_data']['kcal']['color'] = '#F0AD4E';
|
||
}else if(bcdiv($return_data['kcal_data']['kcal']['value'],$nutrition_data['kcal'],2) >= 0.9 && bcdiv($return_data['kcal_data']['kcal']['value'],$nutrition_data['kcal'],2) < 1.1){
|
||
$return_data['kcal_data']['kcal']['standard'] = '达标';
|
||
$return_data['kcal_data']['kcal']['color'] = '#4CD964';
|
||
}else{
|
||
$return_data['kcal_data']['kcal']['standard'] = '超标';
|
||
$return_data['kcal_data']['kcal']['color'] = '#FF0000';
|
||
}
|
||
// 处理计食器信息 end
|
||
// 处理banner信息 start
|
||
$collect_list = $cfc->table($this->kitchenscale_db_msg['collect_list'])->where(['token'=>$data['token'],'is_del'=>0])->column('cookbook_id');
|
||
foreach ($banner_list as $key => $value) {
|
||
if(array_key_exists($value['id'],$collect_list)){
|
||
$banner_list[$key]['is_me_like_it'] = 'yes';
|
||
}else{
|
||
$banner_list[$key]['is_me_like_it'] = 'no';
|
||
}
|
||
unset($banner_list[$key]['ROW_NUMBER']);
|
||
}
|
||
|
||
// 处理banner信息 end
|
||
// 处理搜索历史 start
|
||
$search_log = $cfc->table($this->kitchenscale_db_msg['search_log'])->where(["aud_id"=>$user['id'],'is_del'=>0])->order('id desc')->limit(30)->select();
|
||
|
||
$return_data['search_history'] = $search_log;
|
||
// 处理搜索历史 end
|
||
}else{
|
||
foreach ($banner_list as $key => $value) {
|
||
$banner_list[$key]['is_me_like_it'] = 'no';
|
||
unset($banner_list[$key]['ROW_NUMBER']);
|
||
}
|
||
}
|
||
|
||
$return_data['banner_data'] = $banner_list;
|
||
|
||
|
||
return $this->msg($return_data);
|
||
}
|
||
|
||
public function search_column_action($data){
|
||
|
||
// $cookbook = new Cookbook();
|
||
$cfc = Db::connect('cfc_db');
|
||
$page_now = array_key_exists('page',$data)?$data['page']:1;
|
||
$page_total = $page_now;
|
||
$page_num = 20;
|
||
// 获取菜谱信息
|
||
$content_num = $cfc->table($this->kitchenscale_db_msg['cookbook'])
|
||
->where("title LIKE '%".$data['search_data']."%' OR describe_data LIKE '%".$data['search_data']."%'")
|
||
->count();
|
||
$page_total = ceil($content_num/$page_num);
|
||
|
||
$content_list = $cfc->table($this->kitchenscale_db_msg['cookbook'])
|
||
->alias('cookbook')
|
||
->join($this->kitchenscale_db_msg['uploadimg'].' uploadimg','cookbook.cover = uploadimg.id','LEFT')
|
||
->where("cookbook.title LIKE '%".$data['search_data']."%' OR cookbook.describe_data LIKE '%".$data['search_data']."%'")
|
||
->field("cookbook.id,cookbook.title,cookbook.create_user_head_pic,cookbook.create_user_nickname,cookbook.likes_num,uploadimg.pic_url as cover")
|
||
->page("$page_now,$page_num")
|
||
->select();
|
||
|
||
if(count($content_list)<=0){
|
||
return $this->msg([]);
|
||
}
|
||
if(array_key_exists('token',$data)){
|
||
if($data['token'] != ''){
|
||
// 获取账号下信息以及用户信息
|
||
$user_data = Db::table($this->reedaw_db_msg['zhanghao'])->where(['token'=>$data['token']])->field('id,token,nickname,head_pic')->find();
|
||
if(!$user_data){
|
||
return $this->msg(20001,'账号信息错误');
|
||
}
|
||
// 获取用户收藏列表
|
||
$my_collect_list = $cfc->table($this->kitchenscale_db_msg['collect_list'])
|
||
->where(['token'=>$data['token']])
|
||
->column('cookbook_id');
|
||
// dump();
|
||
// 处理菜谱收藏信息
|
||
foreach ($content_list as $key => $value) {
|
||
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']);
|
||
}
|
||
}else{
|
||
foreach ($content_list as $key => $value) {
|
||
$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']);
|
||
}
|
||
}
|
||
}else{
|
||
foreach ($content_list as $key => $value) {
|
||
$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']);
|
||
}
|
||
}
|
||
|
||
return $this->msg([
|
||
'page_now'=>$page_now,
|
||
'page_total'=>$page_total,
|
||
'content_list'=>$content_list
|
||
]);
|
||
}
|
||
|
||
|
||
|
||
|
||
########################################################################################################################################################################
|
||
########################################################################################################################################################################
|
||
########################################################################################################################################################################
|
||
|
||
|
||
|
||
|
||
public function create_random_string($length = 12)
|
||
{
|
||
//创建随机字符
|
||
$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
||
$str = "";
|
||
for ($i = 0; $i < $length; $i++) {
|
||
$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
|
||
}
|
||
return $str;
|
||
}
|
||
|
||
} |