Files
amb_wechatapp/application/common/model/page/Decorate.php
T

628 lines
23 KiB
PHP

<?php
namespace app\common\model\page;
use think\Model;
use think\Db;
use think\Cache;
/**
* 页面装修
*/
class Decorate extends Model
{
// 表名
protected $name = 'page_decorate';
public function getIndexData(){
$cacheData = Cache::get(\app\common\constant\cache\Keys::PAGE_INDEX);
if($cacheData){
return $cacheData;
}
$data = self::where([
'type'=>'index',
'status'=>1,
'default_page'=>0
])->find();
if(!$data){
$data = self::where([
'default_page'=>1
])->find();
}
if(!$data){
return false;
}
$data = $data->toArray();
Cache::tag(\app\common\constant\cache\Keys::PAGE_TAG)->set(\app\common\constant\cache\Keys::PAGE_INDEX,$data);
return $data;
}
/**
* 获取页面数据
* @param $id
* @return false|null
*/
public function getPageData($id){
$data = $this->getOtherPage($id);
if(!$data){
return $data;
}
return $this->parseData($data);
}
/**
* 获取其他页面数据(非主页
* @param $id
* @return false
*/
public function getOtherPage($id){
$cacheKey = \app\common\constant\cache\Keys::PAGE_OTHER.$id;
$cacheData = Cache::get($cacheKey);
if($cacheData){
return $cacheData;
}
$where = [
'status'=>1
];
if($id){
$where['id'] = $id;
$data = self::where($where)->find();
if(!$data){
$data = $this->getIndexData();
}else{
$data = $data->toArray();
}
}else{
$data = $this->getIndexData();
}
if(!$data){
return false;
}
Cache::tag(\app\common\constant\cache\Keys::PAGE_TAG)->set($cacheKey,$data);
return $data;
}
/**
* 解析页面数据
* @param $id
* @return void
*/
public function parseData($data,$platform = 'mobile'){
$data['compontents'] = json_decode($data['compontents'],true);
$data['page'] = json_decode($data['page'],true);
$compontents = [];
if(!empty($data['compontents'])){
foreach ($data['compontents'] as $item){
switch ($item['type']){
case 'goods':
case 'course':
if(isset($item['config']['maxShowNum'])){
$item['config']['maxShowCourseNum'] = $item['config']['maxShowNum'];
}
if(isset($item['config']['datagroup'])){
$item['config']['groupChangeValue'] = $item['config']['datagroup'];
}
if($item['config']['source'] =='group'){
$item['config']['list'] = $this->getCourse('group',$item['config']['groupChangeValue'],$item['config']['maxShowCourseNum']);
}else{
$ids = [];
if(!empty($item['config']['list'])){
foreach ($item['config']['list'] as $options){
$ids[] = [
'id'=>$options['id'],
'type'=>$options['type']
];
}
}
$item['config']['list'] = $this->getCourse('ids',$ids,$item['config']['maxShowCourseNum']);
}
break;
case 'physical':
if(isset($item['config']['maxShowNum'])){
$item['config']['maxShowGoodsNum'] = $item['config']['maxShowNum'];
}
if(isset($item['config']['datagroup'])){
$item['config']['groupChangeValue'] = $item['config']['datagroup'];
}
if(isset($item['config']['source']) && $item['config']['source'] =='group'){
$item['config']['list'] = $this->getPhysicalGoods('group',$item['config']['groupChangeValue'],$item['config']['maxShowGoodsNum']);
}else{
$ids = [];
if(!empty($item['config']['list']) && is_array($item['config']['list'])){
foreach ($item['config']['list'] as $options){
if(isset($options['id'])){
$ids[] = $options['id'];
}
}
}
$categoryIds = [];
if(isset($item['config']['category_ids']) && !empty($item['config']['category_ids'])){
$categoryIdsParam = $item['config']['category_ids'];
if(is_string($categoryIdsParam)){
$categoryIdsParam = json_decode($categoryIdsParam, true);
}
if(is_array($categoryIdsParam)){
$categoryIds = $categoryIdsParam;
}
}
$item['config']['list'] = $this->getPhysicalGoods('ids',$ids,$item['config']['maxShowGoodsNum'],$categoryIds);
}
break;
case 'parse':
$item['config']['content'] = \addons\alivod\library\Alivod::parseContentAliovdTag($item['config']['content']);
if(strpos($item['config']['content'],"><source")!==false){
$item['config']['content'] = str_replace("><source","",$item['config']['content']);
$item['config']['content'] = str_replace("/></video>","</video>",$item['config']['content']);
}
break;
case 'audio':
if($item['config']['file']){
$item['config']['file']['fullurl'] = \addons\alivod\library\Alivod::parseContentAliovdTag($item['config']['file']['fullurl']);
}
break;
case 'video':
if($item['config']['file']){
$item['config']['file']['fullurl'] = \addons\alivod\library\Alivod::parseContentAliovdTag($item['config']['file']['fullurl']);
}
break;
case 'live':
if($item['config']['source'] == 'choose'){
$ids = [];
if(!empty($item['config']['live'])){
foreach ($item['config']['live'] as $options){
$ids[] = $options['id'];
}
}
$item['config']['live'] = (new \app\admin\model\course\Live())->fieldLimit()->idsGetLives($ids,$item['config']['max_show_num']);
}else{
switch ($item['config']['source']){
case 'ing':
$item['config']['live'] = (new \app\admin\model\course\Live())->fieldLimit()->normal()->ing()->limit($item['config']['max_show_num'])->select();
break;
case 'recently':
$item['config']['live'] = (new \app\admin\model\course\Live())->fieldLimit()->normal()->recently()->limit($item['config']['max_show_num'])->select();
break;
case 'end':
$item['config']['live'] = (new \app\admin\model\course\Live())->fieldLimit()->normal()->end()->limit($item['config']['max_show_num'])->select();
break;
case 'unstart':
$item['config']['live'] = (new \app\admin\model\course\Live())->fieldLimit()->normal()->unstart()->limit($item['config']['max_show_num'])->select();
break;
}
}
break;
case "teacher":
if($platform == 'pc'){
if(!empty($item['config']['list'])){
foreach ($item['config']['list'] as &$options){
$ids = [];
foreach ($options['course'] as $course){
$ids[] = [
'id'=>$course['id'],
'type'=>$course['type']
];
}
$options['course'] = $this->getCourse('ids',$ids,999);
}
}
}else{
$ids = [];
if(!empty($item['config']['course'])){
foreach ($item['config']['course'] as $options){
$ids[] = [
'id'=>$options['id'],
'type'=>$options['type']
];
}
}
$item['config']['course'] = $this->getCourse('ids',$ids,999);
}
break;
case 'coupon':
//最新发布的优惠券
if($item['config']['source'] =='new'){
$item['config']['coupon'] = (new \app\admin\model\app\coupon\Coupon())->getNewCoupon($item['config']['max_show_num']);
}else{
//手动选择的优惠券
$ids = [];
if(!empty($item['config']['coupon'])){
foreach ($item['config']['coupon'] as $options){
$ids[] = $options['id'];
}
}
$item['config']['coupon'] = (new \app\admin\model\app\coupon\Coupon())->idsGetCoupon($ids,$item['config']['max_show_num']);
}
break;
case 'composite':
//组合商品
$ids = [];
if(!empty($item['config']['list']) && is_array($item['config']['list'])){
foreach ($item['config']['list'] as $options){
if(isset($options['id'])){
$ids[] = $options['id'];
}
}
}
$item['config']['list'] = $this->getCompositeGoods($ids);
break;
}
$compontents[] = $item;
}
}
$data['compontents'] = $compontents;
$data = (new \app\common\model\page\Navigation)->oldIndexToNew($data);
return $data;
}
/**
* 获取课程
* @param $type ids 商品id group 商品分组
* @param $ids 商品id group 商品分组ID
* @param $length 数量
* @return mixed
*/
public function getCourse($type = 'ids',$goods=[],$length=10){
$cacheData = Cache::get(\app\common\constant\cache\Keys::PAGE_COURSE($type,$goods,$length));
if($cacheData){
return $cacheData;
}
$showCourseFields = ['course.id','course.type','course.name','course.cover','course.pay_type','course.price','course.price_marking'];
$where = [
'course.status'=>1,
'sales_type'=>['like',"%alone%"],
'hide'=>0
];
if($type=='ids'){
$where = [];
$where['status'] = 1;
$where['sales_type'] = ['like',"%alone%"];
$where['hide'] = 0;
$list = [];
$query = \app\common\model\goods\Handle::getGoodsQuery();
if($goods){
foreach ($goods as $item){
$query->whereOr(function ($query) use ($item,$where){
$where = array_merge($where,[
'id'=>$item['id'],
'type'=>$item['type']
]);
$query->where($where);
});
}
$data = $query->select();
//重新设置下排序
if($data){
foreach ($goods as $item){
foreach ($data as $option){
if($option['type'] == $item['type'] && $option['id'] == $item['id'] && !in_array($option,$list)){
$list[] = $option;
}
}
}
}
}
}else{
$where['group_id'] = ['in',$goods];
$GroupBindModel = new \app\admin\model\course\GroupBind();
$list = $GroupBindModel
->with(['course'])
->where($where)
->group('group_bind.course_id')
->order('sort', 'desc')
->field($showCourseFields)
->limit($length)
->select();
}
if(!empty($list)){
$vipAppConfig = \app\common\model\app\Config::getConfig('vip');
foreach ($list as &$item){
if(is_object($item)){
$item = $item->toArray();
}
unset($item['course']);
//这里要对不同的商品类型进行字段适配。
$extendInfo = \app\common\model\goods\Handle::parseGoodsExtendInfo($item['id'],$item['type']);
if($extendInfo){
$item['is_vip_goods'] = false;
if($vipAppConfig['status'] == 1){
//判断是否为会员权益课程
$item['is_vip_goods'] = \app\api\model\app\vip\CardPrivilege::checkVipGoods($item['id']);
}
$item = array_merge($item,$extendInfo);
}
$goodsType = $item['type'] ?? '';
$isVirtualPay = \app\common\library\pay\VirtualPayService::isUseVirtualPay($goodsType);
$item['is_virtual_pay'] = $isVirtualPay ? 1 : 0;
if ($isVirtualPay) {
$item = \app\common\library\pay\VirtualPayService::convertPriceFields($item, ['price', 'price_marking']);
}
}
unset($item);
}
Cache::tag(\app\common\constant\cache\Keys::PAGE_TAG)->set(\app\common\constant\cache\Keys::PAGE_COURSE($type,$goods,$length),$list,\app\common\constant\cache\Timeout::PAGE_COURSE);
return $list;
}
/**
* 获取实物商品
* @param $type ids 商品id group 商品分组
* @param $goods 商品id group 商品分组ID
* @param $length 数量
* @param $categoryIds 分类ID数组
* @return mixed
*/
public function getPhysicalGoods($type = 'ids',$goods=[],$length=10,$categoryIds=[]){
// 如果goods为空,直接返回空数组
if(empty($goods) || !is_array($goods)){
return [];
}
$cacheData = Cache::get(\app\common\constant\cache\Keys::PAGE_PHYSICAL_GOODS($type,$goods,$length,$categoryIds));
if($cacheData){
return $cacheData;
}
$physicalConfig = \app\common\model\app\Config::getConfig('physical');
$showSales = $physicalConfig['show_sales'] ?? 1;
$showGoodsFields = ['id', 'name', 'cover', 'price', 'stock', 'virtual_sales', 'weigh', 'createtime', 'spec_type'];
if ($showSales == 1) {
$showGoodsFields[] = 'total_sales';
}
$where = [
'status'=>1,
'is_hidden'=>0,
'uniacid'=>UNIACID,
'stock'=>['>',0]
];
if($type=='ids'){
$list = [];
if(!empty($goods) && is_array($goods)){
$where['id'] = ['in', $goods];
$query = \app\common\model\app\physical\Goods::where($where);
// 根据配置决定是否查询销量字段
$query->field($showGoodsFields);
// 如果有分类过滤条件
if(!empty($categoryIds) && is_array($categoryIds)){
$query->where(function ($q) use ($categoryIds) {
foreach ($categoryIds as $cid) {
$q->whereOrRaw("find_in_set($cid, category_id)");
}
});
}
$data = $query->select();
//重新设置下排序
if($data){
foreach ($goods as $goodsId){
foreach ($data as $option){
if($option['id'] == $goodsId && !in_array($option,$list)){
$list[] = $option;
}
}
}
}
}
}else{
// 分组模式:goods 为分类ID数组
$list = [];
if(!empty($goods) && is_array($goods)){
// 获取所有分类及其子分类
$allCategoryIds = [];
foreach ($goods as $categoryId) {
$childIds = $this->getPhysicalGoodsChildIds($categoryId);
$allCategoryIds = array_merge($allCategoryIds, $childIds);
}
$allCategoryIds = array_unique($allCategoryIds);
$query = \app\common\model\app\physical\Goods::where($where);
// 根据配置决定是否查询销量字段
$query->field($showGoodsFields);
// 添加分类过滤条件
$query->where(function ($q) use ($allCategoryIds) {
foreach ($allCategoryIds as $cid) {
$q->whereOrRaw("find_in_set($cid, category_id)");
}
});
$data = $query->select();
if ($data) {
$list = is_object($data) ? $data->toArray() : $data;
}
}
}
if(!empty($list)){
$isVirtualPay = \app\common\library\pay\VirtualPayService::isUseVirtualPay('physical');
foreach ($list as &$item){
if(is_object($item)){
$item = $item->toArray();
}
if ($isVirtualPay) {
$item['is_virtual_pay'] = 1;
$item = \app\common\library\pay\VirtualPayService::convertPriceFields($item, ['price']);
} else {
$item['is_virtual_pay'] = 0;
}
}
unset($item);
}
Cache::tag(\app\common\constant\cache\Keys::PAGE_TAG)->set(\app\common\constant\cache\Keys::PAGE_PHYSICAL_GOODS($type,$goods,$length),$list,\app\common\constant\cache\Timeout::PAGE_COURSE);
return $list;
}
/**
* 获取分类及其所有子分类ID
* @param int $categoryId
* @return array
*/
protected function getPhysicalGoodsChildIds($categoryId)
{
$categoryIds = [$categoryId];
$this->getPhysicalGoodsChildCategoryIds($categoryId, $categoryIds);
return $categoryIds;
}
/**
* 递归获取子分类ID
* @param int $parentId
* @param array $categoryIds
*/
protected function getPhysicalGoodsChildCategoryIds($parentId, &$categoryIds)
{
$children = \app\admin\model\app\physical\Category::where('parent_id', $parentId)
->where('uniacid', UNIACID)
->column('id');
foreach ($children as $childId) {
$categoryIds[] = $childId;
$this->getPhysicalGoodsChildCategoryIds($childId, $categoryIds);
}
}
/**
* 获取组合商品(自定义页面用)
* @param array $ids 商品ID数组
* @return array
*/
public function getCompositeGoods($ids = [])
{
if (empty($ids) || !is_array($ids)) {
return [];
}
if (!\app\admin\library\project\App::isInstall('composite')) {
return [];
}
$list = \app\admin\model\app\composite\Goods::where('id', 'in', $ids)
->where('status', 1)
->where('uniacid', UNIACID)
->field(['id', 'name', 'images', 'sell_point', 'min_price'])
->select();
$result = [];
foreach ($list as $item) {
$item = is_object($item) ? $item->toArray() : $item;
if (isset($item['images']) && is_string($item['images'])) {
$images = json_decode($item['images'], true);
$item['cover'] = is_array($images) && !empty($images) ? $images[0] : '';
}
$result[] = $item;
}
// 按传入 ids 顺序排序
$sorted = [];
foreach ($ids as $id) {
foreach ($result as $row) {
if ($row['id'] == $id) {
$sorted[] = $row;
break;
}
}
}
// 虚拟支付价格转换
$isVirtualPay = \app\common\library\pay\VirtualPayService::isUseVirtualPay('composite');
if ($isVirtualPay) {
foreach ($sorted as &$row) {
$row['is_virtual_pay'] = 1;
$row = \app\common\library\pay\VirtualPayService::convertPriceFields($row, ['min_price']);
}
unset($row);
} else {
foreach ($sorted as &$row) {
$row['is_virtual_pay'] = 0;
}
unset($row);
}
return $sorted;
}
}