dlh_test_server/app/api/controller/Goods.php
2024-12-09 09:36:09 +08:00

374 lines
17 KiB
PHP
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
// +----------------------------------------------------------------------
// | likeshop开源商城系统
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
// | gitee下载:https://gitee.com/likeshop_gitee
// | github下载:https://github.com/likeshop-github
// | 访问官网:https://www.likeshop.cn
// | 访问社区:https://home.likeshop.cn
// | 访问手册:http://doc.likeshop.cn
// | 微信公众号:likeshop技术社区
// | likeshop系列产品在gitee、github等公开渠道开源版本可免费商用,未经许可不能去除前后端官方版权标识
// | likeshop系列产品收费版本务必购买商业授权,购买去版权授权后,方可去除前后端官方版权标识
// | 禁止对系统程序代码以任何目的,任何形式的再发布
// | likeshop团队版权所有并拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeshop.cn.team
// +----------------------------------------------------------------------
namespace app\api\controller;
use app\api\logic\DistributionLogic;
use app\common\basics\Api;
use app\api\logic\GoodsLogic;
use app\common\model\goods\GoodsItem;
use app\common\model\goods\Goodswholesale;
use app\common\model\goods\Goodspurchase;
use app\common\model\procurement\Procurementset;
use app\common\model\procurement\Mprocurement;
use app\common\model\user\User;
use app\common\model\user\UserAuth;
use app\common\server\JsonServer;
use think\facade\Validate;
use weixin\Getopenid;
use think\facade\Db;
class Goods extends Api
{
public $like_not_need_login = ['getGoodsDetail', 'getHotList', 'getGoodsList', 'getGoodsListTemplate', 'getGoodsListByColumnId', 'hotList','newGoods'];
/**
* 商品详情
*/
public function getGoodsDetail()
{
if($this->request->isGet()) {
$goodsId = $this->request->get('goods_id', '', 'trim');
$p_id = $this->request->get('p_id',0);
$set_id = $this->request->get('set',0);
$invite_code = $this->request->get('invite_code',0);
$code = $this->request->get('code',0);
$act_id = $this->request->get('act_id',0);
$open=new Getopenid();
$info=$open->getAcc($code);
$divide_state = 0;
$regional_state = 0;
if(isset($info['openid'])) {
$openid = $info['openid'];
$user_msg = UserAuth::where("openid",$openid)->find();
$firend_msg = User::where("distribution_code",$invite_code)->find();
if($user_msg==null){
$divide_second_user_id = 0;
$divide_role_user_id = 0;
$firend_id = 0;
$is_d = Db::name("distribution")->where('user_id', $firend_msg['id'] )->where('is_distribution', 1 )->find();
if($is_d != null){
$firend_id = $firend_msg['id'];
if($firend_msg['divide_role_user_id']!==0){
$divide_role_user_id = $firend_msg['divide_role_user_id'];
$divide_state = 1;
}else{
if($firend_msg['first_leader'] !=0){
$divide_role_user_id = $firend_msg['first_leader'];
}else{
$divide_role_user_id = $firend_id;
}
}
if($firend_msg['regional_state'] === 1){
$divide_second_user_id = $firend_msg['id'];
$regional_state = 1;
}elseif($firend_msg['divide_second_user_id']!==0){
$divide_second_user_id = $firend_msg['divide_second_user_id'];
$regional_state = 1;
}
}
$data = [
'sn' => create_user_sn(),
'create_time' => time(),
"first_leader" => $firend_id,
'distribution_code' => generate_invite_code(),//分销邀请码
'is_distribution' => DistributionLogic::isDistributionMember(),
'client' => 1,
'is_new_user' => 1,
'user_ad_cret' => 1,
'divide_role_user_id' => $divide_role_user_id,
'divide_second_user_id'=>$divide_second_user_id,
'divide_state' => $divide_state,
'regional_state'=>$regional_state
];
$user = User::create($data);
$user_id = $user->id;
$UserAuth = UserAuth::create([
"user_id"=>$user_id,
"openid"=>$openid,
"create_time"=>time(),
"client"=>1
]);
if($is_d != null){
$firend_id = $firend_msg['id'];
$dat['f_user_id'] = $firend_id;
$dat['user_id'] = $user_id;
$dat['ditme'] = date("Y-m-d H:i:s");
Db::name("user_firster")->save($dat);
}
//生成会员分销扩展表
DistributionLogic::createUserDistribution($user_id);
// 生成分销基础信息表
\app\common\logic\DistributionLogic::add($user_id);
}
}
// trace($set_id."-".$p_id, 'ceshi');
$validate = Validate::rule('goods_id', 'require|integer|gt:0');
if(!$validate->check(['goods_id'=>$goodsId])) {
return JsonServer::error($validate->getError());
}
$goodsDetail = GoodsLogic::getGoodsDetail($goodsId, $this->user_id,$p_id,$set_id);
if(false === $goodsDetail) {
$error = GoodsLogic::getError() ?? '获取商品详情失败';
return JsonServer::error($error);
}
if($act_id!=1){
$goodsDetail['market_price'] = round($goodsDetail['min_price'],2);
$goodsDetail['min_price'] = round(0.88*$goodsDetail['min_price'],2);
foreach ($goodsDetail['goods_item'] as $k => $v) {
$goodsDetail['goods_item'][$k]['market_price'] = round($v['price'], 2);
$goodsDetail['goods_item'][$k]['price'] = round($v['price'] * 0.88, 2);
}
}
$set = null;
$settpye = 0;
if($p_id != 0&&$set_id!=0){
$set = Procurementset::where("set_id",$set_id)->where("set_state",1)->find();
}
if($p_id != 0&&$set_id==0){
$settpye = 1;
$set = Procurementset::where("u_id",$p_id)->where("set_state",1)->order("set_update desc")->limit(1)->find();
}
$set_price_type = 0;
if ($set != null&&$settpye!=1) {
if($set['price_type']==1){
$set_price_type = 1;
}
if($set['price_type']==2){
$set_price_type = 2;
}
if($set['price_type']==3){
$set_price_type = 3;
}
if ($set['set_pro'] != 0) {
// $set_price_type = 1;
if ($set['set_pro_type'] == 1) {
$goodsDetail['purchase_price'] = round($goodsDetail['purchase_price'] * (100 + $set['set_pro']) / 100, 2);
}
if ($set['set_pro_type'] == 3) {
$goodsDetail['purchase_price'] = round($goodsDetail['purchase_price'] + $set['set_pro'], 2);
}
}
if ($set['set_issued'] != 0) {
// $set_price_type = 2;
if ($set['set_issued_type'] == 1) {
$goodsDetail['issuing_price'] = round($goodsDetail['issuing_price'] * (100 + $set['set_issued']) / 100, 2);
}
if ($set['set_issued_type'] == 3) {
$goodsDetail['issuing_price'] = round($goodsDetail['issuing_price'] + $set['set_issued'], 2);
}
}
if ($set['set_wholesale'] != 0) {
// $set_price_type = 3;
if ($set['set_wholesale_type'] == 1) {
$goodsDetail['wholesale_price'] = round($goodsDetail['wholesale_price'] * (100 + $set['set_wholesale']) / 100, 2);
}
if ($set['set_wholesale_type'] == 3) {
$goodsDetail['wholesale_price'] = round($goodsDetail['wholesale_price'] + $set['set_wholesale'], 2);
}
}
$rst = Mprocurement::where("user_id",$this->user_id)->where("u_state",1)->where("u_examine_state",1)->where("wholesale_state",2)->find();
foreach ($goodsDetail['goods_item'] as $k => $v) {
if($rst!=null){
$number = Goodswholesale::where('item_id',$v['id'])->select();
$goodsDetail['goods_item'][$k]['number'] = $number;
}else{
$goodsDetail['goods_item'][$k]['number'] = [];
}
$itp = GoodsItem::where("id",$v['id'])->find();
if ($set['set_pro'] != 0) {
if ($set['set_pro_type'] == 1) {
$goodsDetail['goods_item'][$k]['purchase_price'] = round($itp['purchase_price'] * (100 + $set['set_pro']) / 100, 2);
}
if ($set['set_pro_type'] == 3) {
if ($itp['purchase_price'] > 0) {
$goodsDetail['goods_item'][$k]['purchase_price'] = round($itp['purchase_price'] + $set['set_pro'], 2);
} else {
$goodsDetail['goods_item'][$k]['purchase_price'] = "0.00";
}
}
}
if ($set['set_issued'] != 0) {
if ($set['set_issued_type'] == 1) {
$goodsDetail['goods_item'][$k]['issuing_price'] = round($itp['issuing_price'] * (100 + $set['set_issued']) / 100, 2);
}
if ($set['set_issued_type'] == 3) {
if($itp['issuing_price'] > 0){
$goodsDetail['goods_item'][$k]['issuing_price'] = round($itp['issuing_price'] + $set['set_issued'], 2);
}else{
$goodsDetail['goods_item'][$k]['issuing_price'] = "0.00";
}
}
}
if ($set['set_wholesale'] != 0) {
if ($set['set_wholesale_type'] == 1) {
$goodsDetail['goods_item'][$k]['wholesale_pce'] = round($itp['wholesale_pce'] * (100 + $set['set_wholesale']) / 100, 2);
}
if ($set['set_wholesale_type'] == 3) {
if($itp['wholesale_pce'] > 0){
$goodsDetail['goods_item'][$k]['wholesale_pce'] = round($itp['wholesale_pce'] + $set['set_wholesale'], 2);
}else{
$goodsDetail['goods_item'][$k]['wholesale_pce'] = "0.00";
}
}
}
}
}
$goodsDetail['set_price_type'] = $set_price_type;
$rst = Mprocurement::where("user_id",$this->user_id)->where("u_state",1)->where("u_examine_state",1)->where("wholesale_state",2)->find();
if($rst==null){
// foreach($goodsDetail['goods_item'] as $k => $v){
// if(isset($goodsDetail['wholesale_price'] )){
// $goodsDetail['wholesale_price'] = 0;
// }
// }
if($p_id > 0){
foreach ($goodsDetail['goods_item'] as $k => $v) {
$number = Goodswholesale::where('item_id', $v['id'])->select();
$goodsDetail['goods_item'][$k]['number'] = $number;
$purchase_sku= Goodspurchase::where('item_id', $v['id'])->select();
$goodsDetail['goods_item'][$k]['purchase_sku'] = $purchase_sku;
}
}
}else{
foreach ($goodsDetail['goods_item'] as $k => $v) {
$number = Goodswholesale::where('item_id', $v['id'])->select();
$goodsDetail['goods_item'][$k]['number'] = $number;
$purchase_sku= Goodspurchase::where('item_id', $v['id'])->select();
$goodsDetail['goods_item'][$k]['purchase_sku'] = $purchase_sku;
}
}
return JsonServer::success('获取商品详情成功', $goodsDetail);
}else{
return JsonServer::error('请求方式错误');
}
}
// 备用接口调整集采规格
public function getPurchasesku(){
$post = $this->request->post();
$purchase_sku= Goodspurchase::where('item_id', $post['id'])->select();
if(!empty($purchase_sku)){
if($purchase_sku[0]['purchase_content'] == "" || $purchase_sku[0]['purchase_content'] == null){
return JsonServer::error('暂无数据');
}else{
return JsonServer::success('集采规格成功', $purchase_sku);
}
}else{
return JsonServer::error('暂无数据');
}
}
/**
* 热销榜单
*/
public function getHotList()
{
return $this->getGoodsListTemplate($this->request, 'getHotList');
}
/**
* 商品列表
*/
public function getGoodsList(){
return $this->getGoodsListTemplate($this->request, 'getGoodsList');
}
/**
* 商品列表模板
* 作用:代码复用
*/
public function getGoodsListTemplate($request, $methodName)
{
if($request->isGet()) {
$get = $this->request->get();
$get['user_id'] = $this->user_id;
$get['page_no'] = $this->page_no;
$get['page_size'] = $this->page_size;
$data = GoodsLogic::$methodName($get); // 可变方法
$user_id = $get['user_id'];
$rst = Mprocurement::where("user_id",$user_id)->where("u_state",1)->where("u_examine_state",1)->find();
if($rst==null){
foreach($data as $key=>$value){
if(isset($data[$key]['wholesale_price'])){
$data[$key]['wholesale_price'] = 0;
}
}
}
return JsonServer::success('获取成功', $data);
}else{
return JsonServer::error('请求方式错误');
}
}
/**
* 根据商品栏目获取商品列表
*/
public function getGoodsListByColumnId()
{
if($this->request->isGet()) {
$columnId = $this->request->get('column_id', '', 'trim');
$p_id = $this->request->get('p_id',0);
$set_id = $this->request->get('set',0);
$validate = Validate::rule('column_id', 'require|integer|gt:0');
if(!$validate->check(['column_id'=>$columnId])) {
return JsonServer::error($validate->getError());
}
$data = GoodsLogic::getGoodsListByColumnId($columnId,$this->page_no, $this->page_size,$p_id,$set_id);
return JsonServer::success('获取成功', $data);
}else{
return JsonServer::error('请求方式错误');
}
}
// 根据商品名称搜索
public function searchByName()
{
if($this->request->isGet()) {
$shop_name = $this->request->get('shop_name');
$data = GoodsLogic::searchByName($shop_name, $this->user_id);
return JsonServer::success('获取成功', $data);
}else{
return JsonServer::error('请求方式错误');
}
}
// 2024-07-26 新版获取热销榜单
public function hotList()
{
$page = $this->request->get('page_no', $this->page_no);
$size = $this->request->get('page_size', $this->page_size);
$list = GoodsLogic::hotList($page, $size);
return JsonServer::success('获取成功', $list);
}
public function newGoods()
{
// 等于1是获取4条推荐商品 等于2是获取全部的商品
$type = $this->request->get('type');
$page = $this->request->get('page_no', $this->page_no);
$size = $this->request->get('page_size', $this->page_size);
$list = GoodsLogic::goodsList($type, $page, $size);
return JsonServer::success('获取成功', $list);
}
}