和蕙健康小程序后端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

274 lines
8.3 KiB

10 months ago
<?php
namespace addons\shopro\controller\goods;
use addons\shopro\controller\Common;
use addons\shopro\service\goods\GoodsService;
use app\admin\model\shopro\commission\Agent;
use app\admin\model\shopro\order\Order;
10 months ago
use app\admin\model\shopro\user\GoodsLog;
use app\admin\model\shopro\activity\Activity;
use app\admin\model\shopro\user\User;
10 months ago
class Goods extends Common
{
protected $noNeedLogin = ['index', 'ids', 'detail', 'activity', 'activityList'];
10 months ago
protected $noNeedRight = ['*'];
const LEVEL_1 = 1;
const LEVEL_2 = 2;//会员
const LEVEL_3 = 3;//初级代理
const LEVEL_4 = 4;//中级代理
const LEVEL_5 = 5;//高级代理
const LEVEL_6 = 6;//公司账户
10 months ago
public function index()
{
$params = $this->request->param();
$keyword = $params['keyword'] ?? '';
$ids = $params['ids'] ?? '';
$category_id = $params['category_id'] ?? '';
$is_category_deep = $params['is_category_deep'] ?? true;
$sort = $params['sort'] ?? 'weigh';
$order = $params['order'] ?? 'desc';
$service = new GoodsService(function ($goods) {
$goods->activities = $goods->activities;
$goods->promos = $goods->promos;
return $goods;
});
$service->up()->with(['max_sku_price' => function ($query) { // 计算价格区间用(不知道为啥 with 必须再 show 后面)
$query->where('status', 'up');
}]);
if ($keyword) {
$service->search($keyword);
}
if ($ids) {
$service->whereIds($ids);
}
if ($category_id) {
$service->category($category_id, $is_category_deep);
}
if ($sort) {
$service->order($sort, $order);
}
$goods = $service->paginate();
$this->success('获取成功', $goods);
}
/**
* 通过 ids 获取商品(不分页)
*
* @return void
*/
public function ids()
{
$params = $this->request->param();
$ids = $params['ids'] ?? '';
$service = new GoodsService(function ($goods) {
$goods->activities = $goods->activities;
$goods->promos = $goods->promos;
return $goods;
});
$service->show()->with(['max_sku_price' => function ($query) {
$query->where('status', 'up');
}]);
if ($ids) {
$service->whereIds($ids);
}
$goods = $service->select();
$this->success('获取成功', $goods);
}
public function detail()
{
$user = auth_user();
$id = $this->request->param('id');
$activity_id = $this->request->param('activity_id');
// 存一下,获取器获取指定活动的时候会用到
session('goods-activity_id:' . $id, $activity_id);
$service = new GoodsService(function ($goods, $service) use ($activity_id) {
$goods->service = $goods->service;
$goods->skus = $goods->skus;
if (!$activity_id) {
$goods->activities = $goods->activities;
$goods->promos = $goods->promos;
} else {
$goods->activity = $goods->activity;
$goods->original_goods_price = $goods->original_goods_price;
}
return $goods;
});
$goods = $service->show()->activity($activity_id)->with(['max_sku_price' => function ($query) { // 计算价格区间用(不知道为啥 with 必须再 show 后面)
$query->where('status', 'up');
}, 'favorite'])->where('id', $id)->find();
if (!$goods) {
$this->error(__('No Results were found'));
}
// 添加浏览记录
GoodsLog::addView($user, $goods);
// 处理商品规格
$skuPrices = $goods['new_sku_prices'];
$content = $goods['content'];
$goods = $goods->toArray();
$goods['sku_prices'] = $skuPrices;
$goods['content'] = $content;
unset($goods['new_sku_prices']);
$goods['back_score'] = $this->getBackScore($user, $id);
10 months ago
$this->success('获取成功', $goods);
}
/**
* 计算用户等级返现金额
*/
public function getBackScore($user, $goods_id) {
if (empty($user)) {
return 0;
}
$level = Agent::where('user_id', $user->id)->value('level');
// dd($level);
$goods_info = \app\admin\model\shopro\goods\Goods::get($goods_id);
$level_price = 0;
if ($level == self::LEVEL_1) {
$level_price = $goods_info['price'][0];
}
if ($level == self::LEVEL_2) {
$level_price = $goods_info['user_price'];
}
if ($level == self::LEVEL_3) {
$level_price = $goods_info['level1_price'];
}
if ($level == self::LEVEL_4) {
$level_price = $goods_info['level2_price'];
}
if ($level == self::LEVEL_5) {
$level_price = $goods_info['level3_price'];
}
if ($level == self::LEVEL_6) {
$level_price = $goods_info['company_price'];
}
return bcsub($goods_info['price'][0], $level_price, 2);
}
10 months ago
/**
* 获取指定活动相关商品
*
* @return void
*/
public function activity()
{
$activity_id = $this->request->param('activity_id');
$need_buyers = $this->request->param('need_buyers', 0); // 需要查询哪些人在参与活动
$activity = Activity::where('id', $activity_id)->find();
if (!$activity) {
$this->error(__('No Results were found'));
}
$goodsIds = $activity->goods_ids ? explode(',', $activity->goods_ids) : [];
// 存一下,获取器获取指定活动的时候会用到
foreach ($goodsIds as $id) {
session('goods-activity_id:' . $id, $activity_id);
}
$service = new GoodsService(function ($goods) use ($need_buyers) {
if ($need_buyers) {
$goods->buyers = $goods->buyers;
}
$goods->activity = $goods->activity;
return $goods;
});
$goods = $service->activity($activity_id)->whereIds($goodsIds)->show()->order('weigh', 'desc')->select();
$goods = collection($goods)->toArray();
foreach ($goods as &$gd) {
unset($gd['new_sku_prices'], $gd['activity']);
}
$this->success('获取成功', $goods);
}
/**
* 获取指定活动相关商品,带分页
*
* @param Request $request
* @return void
*/
public function activityList()
{
$activity_id = $this->request->param('activity_id');
$activity = Activity::where('id', $activity_id)->find();
if (!$activity) {
$this->error(__('No Results were found'));
}
$goodsIds = $activity->goods_ids ? explode(',', $activity->goods_ids) : [];
// 存一下,获取器获取指定活动的时候会用到
foreach ($goodsIds as $id) {
session('goods-activity_id:' . $id, $activity_id);
}
$service = new GoodsService(function ($goods) {
$goods->promos = $goods->promos;
return $goods;
});
$goods = $service->activity($activity_id)->whereIds($goodsIds)->show()->order('weigh', 'desc')->paginate();
$this->success('获取成功', $goods);
}
/**
* 新人商品用户是否可以购买
*/
public function isNewUser() {
$user = auth_user();
$user_id = $user->id;
$goods_id = $this->request->param('goods_id');
if(!$goods_id) {
$this->error('参数异常');
}
$goods_info = \app\admin\model\shopro\goods\Goods::get($goods_id);
if (empty($goods_info)) {
$this->error('商品不存在');
}
if ($goods_info['is_new_user'] == 1) {
$user_level = Agent::where('user_id', $user_id)->value('level');
if ($user_level != 1 ) {
$this->error('当前等级不能购买新人大礼包');
}
$is_buy_new_goods = $user->is_buy_new_goods;
if ($is_buy_new_goods) {
$this->error('您已经购买过新人大礼包了', [], 100);
} else {
$this->success('success', [], 100);
}
}
$this->success('success', []);
}
10 months ago
}