dggmyyc/application/api/controller/IndexController.php

355 lines
14 KiB
PHP
Executable File

<?php
/**
* @author yupoxiong<i@yufuping.com>
* @title 首页
*/
namespace app\api\controller;
use app\common\model\User;
use app\common\model\AccountLog;
use app\common\model\Goods;
use app\common\model\Order;
use app\common\model\MallImage;
use app\common\model\GoodsCategory;
use app\common\model\ShopConfig;
use tools\Crypt;
use think\Request;
use think\Db;
use think\facade\Log;
class IndexController extends Controller
{
protected $authExcept = [
'home','recommend_goods','goods_info','goods_list','category_list','order'
];
public function home(Request $request,AccountLog $almodel,Goods $gmodel,Order $omodel,User $umodel)
{
// echo $token = $this->getToken(1).'|';
// echo $uid=Crypt::encrypt(1);
$param = $request->param();
$uid = $param['uid']?Crypt::decrypt($param['uid']):'';
//首页轮播
$carousel = get_ad('carousel',6);
//精品推荐
$merchant = get_ad('merchant',6);
//每日收益
$profit_num=ShopConfig::get('shouyi', 'num');
//弹窗配置
$goods_config=array();
$amount_today='0.00';
$amount_yesterday='0.00';
$amount_tuiguang='0.00';
if($uid){
$where[]=array('source_type','in','100,110,120');
$where[]=array('user_id','eq',$uid);
//今日收益
$amount_today = $almodel
->where($where)
->whereTime('create_time', 'today')
->sum('change_amount');
//昨天收益
$amount_yesterday = $almodel
->where($where)
->whereTime('create_time', 'yesterday')
->sum('change_amount');
//推广收益
$amount_tuiguang = $almodel
->where(['source_type' =>100,'user_id'=>$uid])
->sum('change_amount');
$goods_config['is_open']=ShopConfig::get('goods', 'is_open');
$is_first=ShopConfig::get('goods', 'is_first');
$goods_id=ShopConfig::get('goods', 'goods_id');
if($is_first==1){
$user_count=$umodel->where(array('user_level_id'=>1,'id'=>$uid))->count();
if($user_count==0){
$goods_config['is_open']=0;
}
}
$ty_count=$omodel->where(array('order_type'=>2,'goods_id'=>$goods_id,'user_id'=>$uid))->count();
if($ty_count>0){
$goods_config['is_open']=0;
}
$goods_config['day']=ShopConfig::get('goods', 'day');
if($goods_id){
$goods_info= $gmodel
->alias('g')
->field("mi.thumb_image as img, g.name")
->join('mall_image mi', 'mi.union_id= g.id and is_cover=1 and mi.type=1', 'LEFT')
->where(array('g.id'=>$goods_id))
->find();
$goods_info['img']=$this->img_url.$goods_info['img'];
$goods_config['goods_info'] =$goods_info;
}
}
$data = [
'carousel' =>$carousel,//首页轮播
'merchant' => $merchant,//精品推荐
'profit_num'=>$profit_num,//每日收益
'amount_today'=>$amount_today,//今日收益
'amount_yesterday'=>$amount_yesterday,//昨天收益
'amount_tuiguang'=>$amount_tuiguang,//推广收益
'goods_config'=>$goods_config,//弹窗配置
];
return success($data);
}
//精品推荐
public function recommend_goods(Request $request,Goods $model)
{
$param = $request->param();
$size=$param['size']>0?$param['size']:12;
$page_l=$param['page']>1?$param['page']:1;
$page=$size * ($page_l - 1);
$goods_count = $model
->alias('g')
->where(array('is_recommend'=>1))
->join('mall_image mi', 'mi.union_id= g.id and is_cover=1 and mi.type=1', 'LEFT')
->count();
$totalPages = ceil($goods_count / $size);
$goods_list = $model
->alias('g')
->field("mi.thumb_image as img, g.name,g.attr,g.price,g.order_num,g.id as goods_id")
->join('mall_image mi', 'mi.union_id= g.id and is_cover=1 and mi.type=1', 'LEFT')
->where(array('is_recommend'=>1))
->limit($page, $size)
->order('g.sort_number ASC')
->select();
if (!$goods_list) {
return client_error('暂无数据');
}
foreach ($goods_list as $k1 => $v1) {
$goods_list[$k1]['img'] =$this->img_url.$v1['img'];
$goods_list[$k1]['goods_id'] = Crypt::encrypt($v1['goods_id']);
}
$page=array(
'count'=>$goods_count,
'num'=>$page_l,
'size'=>$size,
'page'=>$totalPages
);
return success(array('list'=>$goods_list,'page'=>$page));
}
//商品列表
public function goods_list(Request $request,Goods $model)
{
$param = $request->param();
$size=$param['size']>0?$param['size']:12;
$page_l=$param['page']>1?$param['page']:1;
$page=$size * ($page_l - 1);
$where[]=array('status','=',1);
$cid = $param['cid']?Crypt::decrypt($param['cid']):'';
if($cid){
$where[]=array('goods_category_id','=',$cid);
}
$goods_count = $model
->alias('g')
->where($where)
->join('mall_image mi', 'mi.union_id= g.id and is_cover=1 and mi.type=1', 'LEFT')
->count();
$totalPages = ceil($goods_count / $size);
$goods_list = $model
->alias('g')
->field("mi.thumb_image as img,g.name,g.attr,g.price,g.order_num,g.id as goods_id")
->join('mall_image mi', 'mi.union_id= g.id and is_cover=1 and mi.type=1', 'LEFT')
->where($where)
->limit($page, $size)
->order('g.sort_number ASC')
->select();
if (!$goods_list) {
return client_error('暂无数据');
}
foreach ($goods_list as $k1 => $v1) {
$goods_list[$k1]['img'] =$this->img_url.$v1['img'];
$goods_list[$k1]['goods_id'] = Crypt::encrypt($v1['goods_id']);
}
$page=array(
'count'=>$goods_count,
'num'=>$page_l,
'size'=>$size,
'page'=>$totalPages
);
return success(array('list'=>$goods_list,'page'=>$page));
}
//商品详情
public function goods_info(Request $request,Goods $model,MallImage $mimodel)
{
$param = $request->param();
$gid = $param['gid']?Crypt::decrypt($param['gid']):'';
if(!$gid){
return client_error('该设备已下架,请联系客服进行售后处理!');
}
$goods_info=$model->field('name,attr,price,order_num,id as goods_id,detail,status')->where(array('id'=>$gid))->find();
if(!$goods_info){
return client_error('该设备已下架,请联系客服进行售后处理!');
}
$goods_info['goods_id'] = Crypt::encrypt($goods_info['goods_id']);
$img_list=$mimodel->field('thumb_image,ori_image')->where(array('union_id'=>$gid,'type'=>1))->select();
foreach ($img_list as $key => $value) {
$img_list[$key]['thumb_image'] =$this->img_url.$value['thumb_image'];
$img_list[$key]['ori_image'] =$this->img_url.$value['ori_image'];
}
return success(array('info'=>$goods_info,'img_list'=>$img_list));
}
//商品分类
public function category_list(Request $request,GoodsCategory $gcmodel)
{
$category_list=$gcmodel->field('name,id as cid')->select();
foreach ($category_list as $k1 => $v1) {
$category_list[$k1]['cid'] = Crypt::encrypt($v1['cid']);
}
//广告
$ad = get_ad('goods',1);
return success(array('list'=>$category_list,'ad'=>$ad));
}
//定时产生收益
public function order(Request $request,Order $omodel,User $umodel,AccountLog $almodel)
{
//产生收益时间
$profit_time=ShopConfig::get('shouyi', 'time');
$xiaoshi=date('G');
// if($xiaoshi!=$profit_time){
// Log::write('未到收益时间');
// return '';
// }
//每日收益
$profit_num=ShopConfig::get('shouyi', 'num');
//一级分销比例
$first_ratio=ShopConfig::get('fenxiao', 'first_ratio');
//二级分销比例
$second_ratio=ShopConfig::get('fenxiao', 'second_ratio');
$now = time();
$user_list=$umodel
->alias('u')
->field("user_id,attr,number,order_type,is_experience,first_leader,second_leader")
->join('order o','o.user_id= u.id')
->where(array('o.status'=>1))
->where(Db::raw("end_time >= $now"))
->select();
$order_list=array();
foreach ($user_list as $key => $value) {
$order_list[$value['user_id']][]=$value;
}
$orders_list=array();
$i=0;
foreach ($order_list as $key1 => $value1) {
$ty_amount=0;//体验收益
$zs_amount=0;//正式收益
foreach ($value1 as $key2 => $value2) {
$orders_list[$i]['is_experience']=$value2['is_experience'];
if($value2['order_type']==1){
//正式收益
$zs_amount+=$value2['attr']*$profit_num*$value2['number'];
}else{
//体验处理
$ty_amount+=$value2['attr']*$profit_num*$value2['number'];
}
}
$orders_list[$i]['uid']=$key1;
$orders_list[$i]['first_leader']=$value2['first_leader'];
$orders_list[$i]['second_leader']=$value2['second_leader'];
$orders_list[$i]['zs_amount']=$zs_amount;
$orders_list[$i]['ty_amount']=$ty_amount;
$i++;
}
Db::startTrans();
try{
//print_r($orders_list);
foreach ($orders_list as $key3 => $value3) {
$is_profit=0;
if($value3['is_experience']==1){
$accountlog_info=$almodel->field("sum(change_amount) as change_amount")->where(array('user_id'=>$value3['uid'],'is_profit'=>0,'source_type'=>120))->find();
if($accountlog_info['change_amount']>0){
//更新往期体验收益已产出
$almodel->where(array('user_id'=>$value3['uid'],'is_profit'=>0,'source_type'=>120))->update(['is_profit'=>1]);
$umodel->where('id', $value3['uid'])->setInc('integral', $accountlog_info['change_amount']);
//添加用户往期体验收益变动记录
AccountLog::AccountRecord(
$value3['uid'],
$accountlog_info['change_amount'],
1,
120,
'添加用户往期体验收益'
);
}
//添加用户当前体验收益
if($value3['ty_amount']>0){
$umodel->where('id', $value3['uid'])->setInc('integral', $value3['ty_amount']);
$is_profit=1;
}
}
//添加用户往期体验收益变动记录
if($value3['ty_amount']>0){
AccountLog::AccountRecord(
$value3['uid'],
$value3['ty_amount'],
1,
120,
'添加用户当前体验收益',
'',
'',
'',
$is_profit
);
}
//添加用户当前体验收益
if($value3['zs_amount']>0){
$umodel->where('id', $value3['uid'])->setInc('integral', $value3['zs_amount']);
AccountLog::AccountRecord(
$value3['uid'],
$value3['zs_amount'],
1,
110,
'添加用户当前正式收益'
);
//一级分销
if($value3['first_leader']>0){
$first_ratio_sum=$value3['zs_amount']*($first_ratio/100);
$umodel->where('id', $value3['first_leader'])->setInc('integral', $first_ratio_sum);
AccountLog::AccountRecord(
$value3['first_leader'],
$first_ratio_sum,
1,
100,
'添加用户分销收益'
);
}
//二级分销
if($value3['second_leader']>0){
$second_ratio_sum=$value3['zs_amount']*($second_ratio/100);
$umodel->where('id', $value3['second_leader'])->setInc('integral', $second_ratio_sum);
AccountLog::AccountRecord(
$value3['second_leader'],
$second_ratio_sum,
1,
100,
'添加用户分销收益'
);
}
}
}
Db::commit();
} catch (\Exception $e){
Log::write('订单自动收益失败,失败原因:'.$e->getMessage());
Db::rollback();
}
}
}