Browse Source

首页、推荐商品

master
weigang 6 years ago
parent
commit
2996763294
  1. 17
      app/Constants/v3/Banner.php
  2. 6
      app/Constants/v3/SsdbKeys.php
  3. 10
      app/Constants/v3/Tabs.php
  4. 15
      app/Controller/v3/GoodsRecommendController.php
  5. 156
      app/Controller/v3/HomeController.php
  6. 21
      app/Model/v3/Banner.php
  7. 49
      app/Model/v3/GoodsActivity.php
  8. 110
      app/Service/v3/Implementations/ActivityService.php
  9. 14
      app/Service/v3/Implementations/BannerService.php
  10. 18
      app/Service/v3/Implementations/CategoryService.php
  11. 11
      app/Service/v3/Interfaces/ActivityServiceInterface.php
  12. 8
      app/Service/v3/Interfaces/BannerServiceInterface.php
  13. 1
      app/Service/v3/Interfaces/CategoryServiceInterface.php
  14. 1
      config/autoload/dependencies.php

17
app/Constants/v3/Banner.php

@ -0,0 +1,17 @@
<?php
namespace App\Constants\v3;
use Hyperf\Constants\AbstractConstants;
use Hyperf\Constants\Annotation\Constants;
/**
* @Constants
*/
class Banner extends AbstractConstants
{
/**
* @Message("首页Banner")
*/
const TYPE_APPLET_INDEX = 1;
}

6
app/Constants/v3/SsdbKeys.php

@ -44,4 +44,10 @@ class SsdbKeys extends AbstractConstants
* @Message("店铺被收藏数量")
*/
const COUNT_COLLECT_STORE = 'count_collect_store_';
/**
* @Message("首页分类")
*/
const APPLET_INDEX_CATEGORY = 'applet_index_category';
}

10
app/Constants/v3/Tabs.php

@ -44,4 +44,14 @@ class Tabs extends AbstractConstants
* @Message("个人中心为你推荐")
*/
const RECOMMEND_FOR_USER_INDEX = 'recommend_user_index';
/**
* @Message("商品详情页为你推荐")
*/
const RECOMMEND_FOR_GOODS_DETAIL = 'recommend_goods_detail';
/**
* @Message("商品详情页猜你喜欢")
*/
const RECOMMEND_LIKE_FOR_GOODS_DETAIL = 'recommend_like_goods_detail';
}

15
app/Controller/v3/GoodsRecommendController.php

@ -4,6 +4,7 @@ namespace App\Controller\v3;
use App\Constants\v3\Tabs;
use App\Controller\BaseController;
use App\Model\v3\Goods;
/**
* 推荐商品相关
@ -35,8 +36,9 @@ class GoodsRecommendController extends BaseController
'cart_num' => 0,
'is_effective' => 2,
'noneffective_note' => '已卖完',
'store' => ['id' => 1, 'logo' => '', 'name' => '我是一个商家']
],
'store' => ['id' => 1, 'logo' => '', 'name' => '我是一个商家']
],
[
'goods' => [
@ -55,8 +57,8 @@ class GoodsRecommendController extends BaseController
'cart_num' => 0,
'is_effective' => 2,
'noneffective_note' => '已卖完',
'store' => ['id' => 222, 'logo' => '', 'name' => '我是一个商家222']
],
'store' => ['id' => 222, 'logo' => '', 'name' => '我是一个商家222']
],
[
'goods' => [
@ -75,8 +77,9 @@ class GoodsRecommendController extends BaseController
'cart_num' => 0,
'is_effective' => 2,
'noneffective_note' => '已卖完',
'store' => ['id' => 2, 'logo' => '', 'name' => '我是一个商家333']
],
'store' => ['id' => 2, 'logo' => '', 'name' => '我是一个商家333']
],
[
'goods' => [
@ -221,7 +224,11 @@ class GoodsRecommendController extends BaseController
*/
public function getByTab()
{
return $this->success(['tab_data' => $this->data]);
$goods = Goods::query()
->with(['store'])
->limit(6)
->get()->toArray();
return $this->success(['tab_data' => $goods]);
}

156
app/Controller/v3/HomeController.php

@ -3,10 +3,14 @@
namespace App\Controller\v3;
use App\Constants\v3\ActivityType;
use App\Constants\v3\Banner;
use App\Constants\v3\SsdbKeys;
use App\Constants\v3\Tabs;
use App\Controller\BaseController;
use App\Request\v3\UserIndexRequest;
use App\Service\v3\Interfaces\ActivityServiceInterface;
use App\Service\v3\Interfaces\BannerServiceInterface;
use App\Service\v3\Interfaces\CategoryServiceInterface;
use App\Service\v3\Interfaces\CollectStoreServiceInterface;
use App\Service\v3\Interfaces\CouponServiceInterface;
use App\Service\v3\Interfaces\UserCenterBlockServiceInterface;
@ -47,6 +51,24 @@ class HomeController extends BaseController
*/
protected $userCenterBlockService;
/**
* @Inject
* @var BannerServiceInterface
*/
protected $bannerService;
/**
* @Inject
* @var CategoryServiceInterface
*/
protected $categoryService;
/**
* @Inject
* @var ActivityServiceInterface
*/
protected $activityService;
/**
* 小程序首页,根据market_id
* 1.banner数据
@ -56,135 +78,15 @@ class HomeController extends BaseController
*/
public function appletIndex()
{
$seconds = 25600;
$days = bcdiv($seconds, 86400);
$leftSeconds = bcsub($seconds, bcmul($days,86400));
$hours = bcdiv($leftSeconds,3600);
$leftSeconds = bcsub($leftSeconds, bcmul($hours,3600));
$minutes = bcdiv($leftSeconds,60);
$leftSeconds = bcsub($leftSeconds, bcmul($minutes,60));
$marketId = $this->request->input('market_id', 0);
$banners = $this->bannerService->all(Banner::TYPE_APPLET_INDEX, $marketId);
$categories = $this->categoryService->allForAppletIndex();
$activity = $this->activityService->allForAppletIndex(ActivityType::FLASH_SALE, $marketId);
return $this->success([
'banners' => [
[
'id' => 2,
'img' => 'https://store.lanzu.vip/attachment/images/2/2020/06/nfXxIhEgH71hcSxSz2hHnaaEaF1g3A.jpg',
'title' => '懒族生活',
'subtitle' => '满足您的美味生活',
'redirect_type' => '',
'redirect_url' => '',
'bg_color' => '#FF00FF',
],
[
'id' => 3,
'img' => 'http://lanzutest.lanzulive.com/attachment/images/2/2020/08/PY55Y3Mz17yJo17rv1Z7vImX1V5159.jpg',
'title' => '鲜菜有爱',
'subtitle' => '很实在',
'redirect_type' => 'page',
'redirect_url' => '/pages/index',
'bg_color' => '#0000FF',
],
],
'categories' => [
['id' => 1, 'icon' => config('alioss.img_host').'/attachment/types/卤菜.png', 'name' => '卤菜'],
['id' => 2, 'icon' => config('alioss.img_host').'/attachment/types/水果.png', 'name' => '水果'],
['id' => 3, 'icon' => config('alioss.img_host').'/attachment/types/肉.png', 'name' => '肉'],
['id' => 4, 'icon' => config('alioss.img_host').'/attachment/types/蔬菜.png', 'name' => '蔬菜'],
['id' => 5, 'icon' => config('alioss.img_host').'/attachment/types/蔬菜.png', 'name' => '蔬菜'],
['id' => 6, 'icon' => config('alioss.img_host').'/attachment/types/肉.png', 'name' => '肉'],
['id' => 7, 'icon' => config('alioss.img_host').'/attachment/types/水果.png', 'name' => '水果'],
['id' => 8, 'icon' => config('alioss.img_host').'/attachment/types/卤菜.png', 'name' => '卤菜'],
],
'activity' => [
'type' => ActivityType::FLASH_SALE,
'goods' => [
[
'id' => 1,
'store_id' => 111,
'cover_img' => config('alioss.img_host').'/attachment/types/c9b656181bbbc463624ca3803c5be7539f2fd62253f9a-cwVLri.png',
'name' => '【优质】大白菜',
'spec' => [
['key' => '净含量', 'value' => '500g']
],
'original_price' => 50.5,
'price' => 25.25,
'inventory' => 0,
'month_sales' => 20,
'total_sales' => 20,
'cart_num' => 0,
'is_effective' => 1,
'noneffective_note' => '已抢光',
'total_seconds' => $seconds,
'days' => $days,
'hours' => $hours,
'minutes' => $minutes,
'seconds' => $leftSeconds,
],[
'id' => 1,
'store_id' => 111,
'cover_img' => config('alioss.img_host').'/attachment/types/c9b656181bbbc463624ca3803c5be7539f2fd62253f9a-cwVLri.png',
'name' => '【优质】大白菜',
'spec' => [
['key' => '净含量', 'value' => '500g']
],
'original_price' => 50.5,
'price' => 25.25,
'inventory' => 0,
'month_sales' => 20,
'total_sales' => 20,
'cart_num' => 0,
'is_effective' => 1,
'noneffective_note' => '已抢光',
'total_seconds' => $seconds,
'days' => $days,
'hours' => $hours,
'minutes' => $minutes,
'seconds' => $leftSeconds,
],[
'id' => 1,
'store_id' => 111,
'cover_img' => config('alioss.img_host').'/attachment/types/c9b656181bbbc463624ca3803c5be7539f2fd62253f9a-cwVLri.png',
'name' => '【优质】大白菜',
'spec' => [
['key' => '净含量', 'value' => '500g']
],
'original_price' => 50.5,
'price' => 25.25,
'inventory' => 0,
'month_sales' => 20,
'total_sales' => 20,
'cart_num' => 0,
'is_effective' => 1,
'noneffective_note' => '已抢光',
'total_seconds' => $seconds,
'days' => $days,
'hours' => $hours,
'minutes' => $minutes,
'seconds' => $leftSeconds,
],[
'id' => 1,
'store_id' => 111,
'cover_img' => config('alioss.img_host').'/attachment/types/c9b656181bbbc463624ca3803c5be7539f2fd62253f9a-cwVLri.png',
'name' => '【优质】大白菜',
'spec' => [
['key' => '净含量', 'value' => '500g']
],
'original_price' => 50.5,
'price' => 25.25,
'inventory' => 0,
'month_sales' => 20,
'total_sales' => 20,
'cart_num' => 0,
'is_effective' => 1,
'noneffective_note' => '已抢光',
'total_seconds' => $seconds,
'days' => $days,
'hours' => $hours,
'minutes' => $minutes,
'seconds' => $leftSeconds,
],
]
],
'banners' => $banners,
'categories' => $categories,
'activity' => $activity,
'tabs' => [
['tab' => Tabs::APPLET_INDEX_RECOMMEND, 'title' => '推荐', 'subtitle' => '猜你喜欢', 'badge' => '', 'bg_color' => '#FF0000', 'font_color' => '#FFFFFF'],
['tab' => Tabs::APPLET_INDEX_NEW, 'title' => '懒族上新', 'subtitle' => '买点不一样', 'badge' => '限时', 'bg_color' => '#FF0000', 'font_color' => '#FFFFFF'],

21
app/Model/v3/Banner.php

@ -0,0 +1,21 @@
<?php
namespace App\Model\v3;
use App\Model\Model;
use Hyperf\Database\Model\Builder;
use Hyperf\Database\Model\SoftDeletes;
class Banner extends Model
{
use SoftDeletes;
protected $table = 'lanzu_banners';
protected function boot(): void
{
parent::boot();
self::addGlobalScope('normal', function (Builder $builder) {
return $builder->where(['status' => 1]);
});
}
}

49
app/Model/v3/GoodsActivity.php

@ -0,0 +1,49 @@
<?php
namespace App\Model\v3;
use App\Constants\v3\Goods as GoodsConstants;
use App\Constants\v3\SsdbKeys;
use App\Model\Model;
use Hyperf\Database\Model\Builder;
use Hyperf\Utils\ApplicationContext;
class GoodsActivity extends Model
{
protected $table = 'lanzu_goods_activity';
protected $casts = [
'details_imgs' => 'array',
'spec' => 'array',
'tags' => 'array',
];
protected $appends = [
'month_sales',
'cart_num',
];
protected function boot(): void
{
parent::boot();
self::addGlobalScope('normal', function (Builder $builder) {
$builder->where(['on_sale' => GoodsConstants::ON_SALE_YES]);
});
}
public function getMonthSalesAttribute()
{
$ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
return (integer)$ssdb->exec('get', SsdbKeys::GOODS_MONTH_SALES.date('YM').'_'.$this->id);
}
public function getCartNumAttribute()
{
return (integer)$this->shopCartService->check($this->id);
}
public function store()
{
return $this->belongsTo(Store::class, 'store_id', 'goods_id');
}
}

110
app/Service/v3/Implementations/ActivityService.php

@ -0,0 +1,110 @@
<?php
namespace App\Service\v3\Implementations;
use App\Constants\v3\ActivityType;
use App\Model\v3\GoodsActivity;
use App\Service\v3\Interfaces\ActivityServiceInterface;
class ActivityService implements ActivityServiceInterface
{
public function do()
{
// TODO: Implement do() method.
}
public function check()
{
// TODO: Implement check() method.
}
public function undo()
{
// TODO: Implement undo() method.
}
public function allForAppletIndex($type, $marketId)
{
return GoodsActivity::query()
->where(['type' => $type, 'market_id' => $marketId])
->addSelect('sales as total_sales')
->get()->toArray();
return [
'type' => ActivityType::FLASH_SALE,
'goods' => [
[
'id' => 1,
'store_id' => 111,
'cover_img' => config('alioss.img_host').'/attachment/types/c9b656181bbbc463624ca3803c5be7539f2fd62253f9a-cwVLri.png',
'name' => '【优质】大白菜',
'spec' => [
['key' => '净含量', 'value' => '500g']
],
'original_price' => 50.5,
'price' => 25.25,
'inventory' => 0,
'month_sales' => 20,
'total_sales' => 20,
'cart_num' => 0,
'is_effective' => 1,
'noneffective_note' => '已抢光',
'total_seconds' => $seconds,
],[
'id' => 1,
'store_id' => 111,
'cover_img' => config('alioss.img_host').'/attachment/types/c9b656181bbbc463624ca3803c5be7539f2fd62253f9a-cwVLri.png',
'name' => '【优质】大白菜',
'spec' => [
['key' => '净含量', 'value' => '500g']
],
'original_price' => 50.5,
'price' => 25.25,
'inventory' => 0,
'month_sales' => 20,
'total_sales' => 20,
'cart_num' => 0,
'is_effective' => 1,
'noneffective_note' => '已抢光',
'total_seconds' => $seconds,
],[
'id' => 1,
'store_id' => 111,
'cover_img' => config('alioss.img_host').'/attachment/types/c9b656181bbbc463624ca3803c5be7539f2fd62253f9a-cwVLri.png',
'name' => '【优质】大白菜',
'spec' => [
['key' => '净含量', 'value' => '500g']
],
'original_price' => 50.5,
'price' => 25.25,
'inventory' => 0,
'month_sales' => 20,
'total_sales' => 20,
'cart_num' => 0,
'is_effective' => 1,
'noneffective_note' => '已抢光',
'total_seconds' => $seconds,
],[
'id' => 1,
'store_id' => 111,
'cover_img' => config('alioss.img_host').'/attachment/types/c9b656181bbbc463624ca3803c5be7539f2fd62253f9a-cwVLri.png',
'name' => '【优质】大白菜',
'spec' => [
['key' => '净含量', 'value' => '500g']
],
'original_price' => 50.5,
'price' => 25.25,
'inventory' => 0,
'month_sales' => 20,
'total_sales' => 20,
'cart_num' => 0,
'is_effective' => 1,
'noneffective_note' => '已抢光',
'total_seconds' => $seconds,
],
]
];
}
}

14
app/Service/v3/Implementations/BannerService.php

@ -0,0 +1,14 @@
<?php
namespace App\Service\v3\Implementations;
use App\Model\v3\Banner;
use App\Service\v3\Interfaces\BannerServiceInterface;
class BannerService implements BannerServiceInterface
{
public function all($type, $marketId)
{
return Banner::query()->where(['type' => $type, 'market_id' => $marketId])->get()->toArray();
}
}

18
app/Service/v3/Implementations/CategoryService.php

@ -2,10 +2,13 @@
namespace App\Service\v3\Implementations;
use App\Constants\v3\SsdbKeys;
use App\Model\v3\Goods;
use App\Model\v3\GoodsType;
use App\Model\v3\StoreType;
use App\Service\v3\Interfaces\CategoryServiceInterface;
use App\TaskWorker\SSDBTask;
use Hyperf\Utils\ApplicationContext;
class CategoryService implements CategoryServiceInterface
{
@ -48,4 +51,19 @@ class CategoryService implements CategoryServiceInterface
->orderBy('id', 'DESC')
->get()->toArray();
}
public function allForAppletIndex()
{
$ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
$categories = $ssdb->exec('hgetall', SsdbKeys::APPLET_INDEX_CATEGORY);
// key是分类ID,value是icon和name的json
$returnData = [];
foreach ($categories as $key => &$category) {
$value = json_decode($category, true);
$returnData[] = ['id' => $key, 'icon' => config('alioss.img_host').$value['icon'], 'name' => $value['name']];
}
return $returnData;
}
}

11
app/Service/v3/Interfaces/ActivityServiceInterface.php

@ -0,0 +1,11 @@
<?php
namespace App\Service\v3\Interfaces;
interface ActivityServiceInterface
{
public function do();
public function check();
public function undo();
public function allForAppletIndex($type, $marketId);
}

8
app/Service/v3/Interfaces/BannerServiceInterface.php

@ -0,0 +1,8 @@
<?php
namespace App\Service\v3\Interfaces;
interface BannerServiceInterface
{
public function all($type, $marketId);
}

1
app/Service/v3/Interfaces/CategoryServiceInterface.php

@ -10,5 +10,6 @@ interface CategoryServiceInterface
public function undo();
public function all();
public function allForStore($storeId);
public function allForAppletIndex();
}

1
config/autoload/dependencies.php

@ -54,4 +54,5 @@ return [
\App\Service\v3\Interfaces\CouponServiceInterface::class => \App\Service\v3\Implementations\CouponService::class,
\App\Service\v3\Interfaces\OrderListServiceInterface::class => \App\Service\v3\Implementations\OrderListService::class,
\App\Service\v3\Interfaces\UserCenterBlockServiceInterface::class => \App\Service\v3\Implementations\UserCenterBlockService::class,
\App\Service\v3\Interfaces\BannerServiceInterface::class => \App\Service\v3\Implementations\BannerService::class,
];
Loading…
Cancel
Save