dlhmaster/app/api/controller/Goods.php
2024-06-06 11:06:09 +08:00

331 lines
15 KiB
PHP
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;
class Goods extends Api
{
public $like_not_need_login = ['getGoodsDetail', 'getHotList', 'getGoodsList', 'getGoodsListTemplate', 'getGoodsListByColumnId'];
/**
* 商品详情
*/
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);
$firend_invite_code = $this->request->get('firend_invite_code',0);
$code = $this->request->get('code',0);
$open=new Getopenid();
$info=$open->getAcc($code);
if(isset($info['openid'])) {
$openid = $info['openid'];
$user_msg = UserAuth::where("openid",$openid)->find();
$firend_msg = User::where("distribution_code",$firend_invite_code)->find();
if($user_msg==null){
$data = [
'sn' => create_user_sn(),
'create_time' => time(),
"first_leader" => $firend_msg['id'],
'distribution_code' => generate_invite_code(),//分销邀请码
'is_distribution' => DistributionLogic::isDistributionMember(),
'client' => 1,
'is_new_user' => 1,
'user_ad_cret' => 1
];
$user = User::create($data);
$user_id = $user->id;
$UserAuth = UserAuth::create([
"user_id"=>$user_id,
"openid"=>$openid,
"create_time"=>time(),
"client"=>1
]);
//生成会员分销扩展表
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);
}
$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 pdTypeList(){
$result = GoodsLogic::getGoodsTypeList();
$data = $this -> list_to_tree($result,'id','pid');
if(!empty($result)){
return JsonServer::success('获取成功', $data);
}else{
return JsonServer::error('暂无数据');
}
}
//无限级栏目
function list_to_tree($list, $pk='type_id',$pid = 'type_parents',$child = 'msg_sub_menu',$root=0) {
// 创建Tree
$tree = array();
if(is_array($list)) {
// 创建基于主键的数组引用
$refer = array();
foreach ($list as $key => $data) {
$refer[$data[$pk]] =& $list[$key];
}
//循环所有的结果集
foreach ($list as $key => $data) {
// 判断是否存在parent
$parentId = $data[$pid];
//如果ID是0,那么就是根目录
if ($root == $parentId) {
$list[$key][$child] = array();
$tree[] =& $list[$key];
}else{
if (isset($refer[$parentId])) {
$parent =& $refer[$parentId];
$parent[$child][] =& $list[$key];
}
}
}
}
return $tree;
}
}