nk-lihaink-cn/app/admin/controller/StoreProduct.php

506 lines
20 KiB
PHP
Raw Normal View History

2023-02-21 17:03:11 +08:00
<?php
/**
* @copyright Copyright (c) 2021 勾股工作室
* @license https://opensource.org/licenses/Apache-2.0
* @link https://www.gougucms.com
*/
declare (strict_types = 1);
namespace app\admin\controller;
use app\admin\BaseController;
use app\admin\model\StoreProduct as StoreProductModel;
use app\admin\validate\StoreProductValidate;
2023-03-22 16:51:51 +08:00
use EasyWeChat\Factory;
2023-02-21 17:03:11 +08:00
use think\exception\ValidateException;
use think\facade\Db;
2023-03-22 16:51:51 +08:00
use think\facade\Request;
2023-02-21 17:03:11 +08:00
use think\facade\View;
class StoreProduct extends BaseController
{
/**
* 构造函数
*/
public function __construct()
{
$this->model = new StoreProductModel();
$this->uid = get_login_admin('id');
}
/**
* 数据列表
*/
public function datalist()
{
if (request()->isAjax()) {
$param = get_params();
2023-03-22 16:51:51 +08:00
$where[] = ['admin_id','=',$this->uid];
2023-02-21 17:03:11 +08:00
if (isset($param['keywords']) && !empty($param['keywords'])){
$where[]=['store_name','like','%'.$param['keywords'].'%'];
}
$list = $this->model->getStoreProductList($where,$param);
2023-03-22 16:51:51 +08:00
foreach ($list as $k=>$v){
$www['brand_id'] = $v['brand_id'];
$list[$k]['brand_id'] = Db::connect('shop')->table('eb_store_brand')->where($www)->value('brand_name');
$www2['store_category_id'] = $v['cate_id'];
$list[$k]['cate_id'] = Db::connect('shop')->table('eb_store_category')->where($www2)->value('cate_name');
}
2023-02-21 17:03:11 +08:00
return table_assign(0, '', $list);
}
else{
return view();
}
}
/**
2023-03-27 09:22:57 +08:00
* 添加
*/
2023-02-21 17:03:11 +08:00
public function add()
2023-03-24 14:27:22 +08:00
{
2023-03-27 09:22:57 +08:00
if (request()->isAjax()) {
$param = get_params();
2023-03-24 14:27:22 +08:00
// 检验完整性
try {
validate(StoreProductValidate::class)->check($param);
} catch (ValidateException $e) {
// 验证失败 输出错误信息
return to_assign(1, $e->getError());
}
2023-03-27 09:22:57 +08:00
$param['admin_id'] = $this->uid;
2023-03-24 14:27:22 +08:00
$this->model->addStoreProduct($param);
}else{
$store_brand= Db::connect('shop')->table('eb_store_brand')->where(['is_show' => 1])
->select();
View::assign('store_brand', $store_brand);
2023-03-27 09:22:57 +08:00
return view();
}
2023-03-24 14:27:22 +08:00
}
2023-03-27 09:22:57 +08:00
// /**
// * 添加
// */
// public function add()
// {
// if (request()->isAjax()) {
// $param = get_params();
// $data=$param;
// $data['mer_id'] = 4;
// $data['status'] = 1;
// $data['mer_status'] =1;
// $data['rate'] = 3;
// $data['spec_type'] = 0;//商品规格
// if (!$data['spec_type']) {
// $data['attr'] = [];
// if (count($data['skus']) > 1) throw new ValidateException('单规格商品属性错误');
// }
// $content = [
// 'content' => $data['content'] ,
// 'type' => 0
// ];
// $productType=0;
// $conType=0;
// $product = $this->setProduct($data);
// // event('product.create.before', compact('data','productType','conType'));
// return Db::transaction(function () use ($data, $productType,$conType,$content,$product) {
// $activity_id = 0;
// $result = Db::connect('shop')->table()->create($product);
// $settleParams = $this->setAttrValue($data, $result->product_id, $productType, 0);
// $settleParams['cate'] = $this->setMerCate($data['mer_cate_id'], $result->product_id, $data['mer_id']);
// $settleParams['attr'] = $this->setAttr($data['attr'], $result->product_id);
// if ($productType ==0 ) app()->make(ParameterValueRepository::class)->create($result->product_id, $data['params'] ?? [],$data['mer_id']);
// $this->save($result->product_id, $settleParams, $content,$product,$productType);
// if (in_array($productType, [0, 1])) {
// if ($productType == 1) { //秒杀商品
// $dat = $this->setSeckillProduct($data);
// $dat['product_id'] = $result->product_id;
// $seckill = app()->make(StoreSeckillActiveRepository::class)->create($dat);
// $activity_id = $seckill->seckill_active_id;
// }
// $product['price'] = $settleParams['data']['price'];
// $product['mer_labels'] = $data['mer_labels'];
// app()->make(SpuRepository::class)->create($product, $result->product_id, $activity_id, $productType);
// }
// $product = $result;
// event('product.create',compact('product'));
// return $result->product_id;
// });
// halt($data);
//
// // 检验完整性
// try {
// validate(StoreProductValidate::class)->check($param);
// } catch (ValidateException $e) {
// // 验证失败 输出错误信息
// return to_assign(1, $e->getError());
// }
//
// $this->model->addStoreProduct($param);
// }else{
//
// $store_brand= Db::connect('shop')->table('eb_store_brand')->where(['is_show' => 1])
// ->select();
// View::assign('store_brand', $store_brand);
// return view();
// }
// }
2023-03-24 14:27:22 +08:00
public function adds()
2023-02-21 17:03:11 +08:00
{
if (request()->isAjax()) {
2023-02-22 11:51:55 +08:00
$param = get_params();
2023-02-21 17:03:11 +08:00
// 检验完整性
try {
validate(StoreProductValidate::class)->check($param);
} catch (ValidateException $e) {
// 验证失败 输出错误信息
return to_assign(1, $e->getError());
}
2023-03-22 16:51:51 +08:00
$param['admin_id'] = $this->uid;
2023-02-21 17:03:11 +08:00
$this->model->addStoreProduct($param);
}else{
$store_brand= Db::connect('shop')->table('eb_store_brand')->where(['is_show' => 1])
->select();
View::assign('store_brand', $store_brand);
return view();
}
}
2023-03-24 14:27:22 +08:00
public function category_arr($id=0){
$where[]=['is_show','=',1];
$where[]=['mer_id','=',0];
if($id!=0){
$where[1]=['mer_id','=',$id];
}
$list=Db::connect('shop')->table('eb_store_category')->where($where)
->field('store_category_id id,pid,cate_name name')->select();
$category_arr=create_tree_list(0,$list,0);
return to_assign(0,'操作成功', $category_arr);
}
2023-02-21 17:03:11 +08:00
2023-03-24 14:27:22 +08:00
/**
* 格式商品主体信息
* @Author:Qinii
* @Date: 2020/9/15
* @param array $data
* @return array
*/
public function setProduct(array $data)
{
$give_coupon_ids = '';
if (isset($data['give_coupon_ids']) && !empty($data['give_coupon_ids'])) {
$gcoupon_ids = array_unique($data['give_coupon_ids']);
$give_coupon_ids = implode(',', $gcoupon_ids);
}
if(isset($data['integral_rate'])){
$integral_rate = $data['integral_rate'];
if($data['integral_rate'] < 0) $integral_rate = -1;
if($data['integral_rate'] > 100) $integral_rate = 100;
}
$result = [
'store_name' => $data['store_name'],
'image' => $data['image'],
'slider_image' => is_array($data['slider_image']) ? implode(',', $data['slider_image']) : '',
'store_info' => $data['store_info'] ?? '',
'keyword' => $data['keyword']??'',
'brand_id' => $data['brand_id'] ?? 0,
'cate_id' => $data['cate_id'] ?? 0,
'unit_name' => $data['unit_name']??'件',
'sort' => $data['sort'] ?? 0,
'is_show' => $data['is_show'] ?? 0,
'is_used' => (isset($data['status']) && $data['status'] == 1) ? 1 : 0,
'is_good' => $data['is_good'] ?? 0,
'video_link' => $data['video_link']??'',
'temp_id' => $data['delivery_free'] ? 0 : ($data['temp_id'] ?? 0),
'extension_type' => $data['extension_type']??0,
'spec_type' => $data['spec_type'] ?? 0,
'status' => $data['status']??0,
'give_coupon_ids' => $give_coupon_ids,
'mer_status' => $data['mer_status'],
'guarantee_template_id' => $data['guarantee_template_id']??0,
'is_gift_bag' => $data['is_gift_bag'] ?? 0,
'integral_rate' => $integral_rate ?? 0,
'delivery_way' => implode(',',$data['delivery_way']),
'delivery_free' => $data['delivery_free'] ?? 0,
'once_min_count' => $data['once_min_count'] ?? 0,
'once_max_count' => $data['once_max_count'] ?? 0,
'pay_limit' => $data['pay_limit'] ?? 0,
'svip_price_type' => $data['svip_price_type'] ?? 0,
];
if (isset($data['mer_id']))
$result['mer_id'] = $data['mer_id'];
if (isset($data['old_product_id']))
$result['old_product_id'] = $data['old_product_id'];
if (isset($data['product_type']))
$result['product_type'] = $data['product_type'];
if (isset($data['type']) && $data['type'])
$result['type'] = $data['type'];
if (isset($data['param_temp_id']))
$result['param_temp_id'] = $data['param_temp_id'];
if (isset($data['extend']))
$result['extend'] = $data['extend'] ? json_encode($data['extend'], JSON_UNESCAPED_UNICODE) :[];
return $result;
}
2023-02-21 17:03:11 +08:00
/**
* 编辑
*/
public function edit()
{
$param = get_params();
2023-02-22 11:51:55 +08:00
if (request()->isAjax()) {
2023-02-21 17:03:11 +08:00
// 检验完整性
try {
validate(StoreProductValidate::class)->check($param);
} catch (ValidateException $e) {
// 验证失败 输出错误信息
return to_assign(1, $e->getError());
}
$this->model->editStoreProduct($param);
}else{
$product_id = isset($param['product_id']) ? $param['product_id'] : 0;
$detail = $this->model->getStoreProductById($product_id);
if (!empty($detail)) {
$detail['content'] = Db::table('cms_store_product_content')->where('product_id',$detail['product_id'])->value('content');
2023-02-22 11:51:55 +08:00
$detail['slider_image_arr'] = explode(',',$detail['slider_image']);
// halt($detail['slider_image_arr']);
2023-02-21 17:03:11 +08:00
View::assign('detail', $detail);
$store_brand= Db::connect('shop')->table('eb_store_brand')->where(['is_show' => 1])
->select();
View::assign('store_brand', $store_brand);
return view();
}
else{
throw new \think\exception\HttpException(404, '找不到页面');
}
}
}
/**
* 查看信息
*/
public function read()
{
$param = get_params();
$product_id = isset($param['product_id']) ? $param['product_id'] : 0;
$detail = $this->model->getStoreProductById($product_id);
if (!empty($detail)) {
$detail['content'] = Db::table('cms_store_product_content')->where('product_id',$detail['product_id'])->value('content');
2023-02-22 14:25:29 +08:00
$detail['slider_image_arr'] = explode(',',$detail['slider_image']);
2023-02-21 17:03:11 +08:00
View::assign('detail', $detail);
$store_brand= Db::connect('shop')->table('eb_store_brand')->where(['is_show' => 1])
->select();
View::assign('store_brand', $store_brand);
return view();
}
else{
throw new \think\exception\HttpException(404, '找不到页面');
}
}
/**
* 删除
* type=0,逻辑删除,默认
* type=1,物理删除
*/
public function del()
{
$param = get_params();
$product_id = isset($param['product_id']) ? $param['product_id'] : 0;
$type = isset($param['type']) ? $param['type'] : 0;
2023-03-01 15:13:10 +08:00
$this->model->delStoreProductById($product_id,1);
2023-02-21 17:03:11 +08:00
}
2023-03-22 16:51:51 +08:00
/**
* 商品列表
*/
public function index()
{
if (request()->isAjax()) {
$param = get_params();
$where = [];
if (isset($param['keywords']) && !empty($param['keywords'])){
$where[]=['store_name','like','%'.$param['keywords'].'%'];
}
if (isset($param['store_cate']) && !empty($param['store_cate'])){
$where[]=['cate_id','=',$param['store_cate']];
}
$list = $this->model->getStoreProductList($where,$param);
foreach ($list as $k=>$v){
$www['brand_id'] = $v['brand_id'];
$list[$k]['brand_id'] = Db::connect('shop')->table('eb_store_brand')->where($www)->value('brand_name');
$www2['store_category_id'] = $v['cate_id'];
$list[$k]['cate_id'] = Db::connect('shop')->table('eb_store_category')->where($www2)->value('cate_name');
}
return table_assign(0, '', $list);
}
else{
return view();
}
}
/**
* 购买下单页面
*/
public function buy()
{
$param = get_params();
$product_id = isset($param['product_id']) ? $param['product_id'] : 0;
$detail = $this->model->getStoreProductById($product_id);
if (!empty($detail)) {
$detail['content'] = Db::table('cms_store_product_content')->where('product_id',$detail['product_id'])->value('content');
$detail['slider_image_arr'] = explode(',',$detail['slider_image']);
View::assign('detail', $detail);
$store_brand= Db::connect('shop')->table('eb_store_brand')->where(['is_show' => 1])
->select();
View::assign('store_brand', $store_brand);
return view();
}
else{
throw new \think\exception\HttpException(404, '找不到页面');
}
}
// 请求微信接口的公用配置, 所以单独提出来
private function payment()
{
// 配置信息
$config = [
'app_id' => 'wx0b3defb62f0f910b',//注意这个APPID只能是公众号的id没有的话要去申请并且在微信支付平台里绑定
'mch_id' => '1635725673',//商户号
'key' => '95d195Dcf6ec66156dfeeb4E7435faef',//支付秘钥
'secret' => 'c02aa7ad9e4a5c423862e068b6cb4ad4',
'notify_url' => Request::instance()->domain().'/api/PayNotify/notify',//异步回调通知地址
];
// 这个就是 easywechat 封装的了, 一行代码搞定, 照着写就行了
$app = Factory::payment($config);
return $app;
}
// 向微信请求统一下单接口, 创建预支付订单
public function place_order()
{
$post = get_params();
$id = $post['product_id'];
$number = $post['number'];
// 因为没有先创建订单, 所以这里先生成一个随机的订单号, 存在 pay_log 里, 用来标识订单, 支付成功后再把这个订单号存到 order 表里
$order_sn = date('ymd').substr((string)time(),-5).substr(microtime(),2,5);
// 根据 id 查出价格
$where['product_id'] = $id;
Db::startTrans();//开启事务
$store_product = Db::table('cms_store_product')->where($where)->lock(true)->find();
if (empty($store_product)) {
return to_assign(0,'查询的数据不存在');
}
// 判断库存
if($store_product['stock'] < $number){
return to_assign(0,'超过库存数量');
}
$post_price = bcmul(bcmul($store_product['price'],$number),'100');
$admin_id = get_login_admin('id');
// 创建 Paylog 记录
$param =[
'appid' => 'wx0b3defb62f0f910b',
'mch_id' => '1635725673',
'out_trade_no' => $order_sn,
'product_id' => $id,
'number' => $number,
'admin_id' =>$admin_id,
];
Db::table('cms_store_product_paylog')->strict(false)->field(true)->insert($param);
add_log('buy', $id, $param);
$app = $this->payment();
$total_fee = env('APP_DEBUG') ? 1 : $post_price;
// 用 easywechat 封装的方法请求微信的统一下单接口
$result = $app->order->unify([
'trade_type' => 'NATIVE', // 原生支付即扫码支付,商户根据微信支付协议格式生成的二维码,用户通过微信“扫一扫”扫描二维码后即进入付款确认界面,输入密码即完成支付。
'body' => '采购商品-订单支付', // 这个就是会展示在用户手机上巨款界面的一句话, 随便写的
'out_trade_no' => $order_sn,
'total_fee' => $total_fee,
'spbill_create_ip' => request()->ip(), // 可选如不传该参数SDK 将会自动获取相应 IP 地址
]);
if ($result['result_code'] == 'SUCCESS') {
// 如果请求成功, 微信会返回一个 'code_url' 用于生成二维码
$code_url = $result['code_url'];
// 生成二维码
// 引用二维码生成方法
require '../vendor/phpqrcode/phpqrcode.php';
$errorCorrectionLevel = 'L'; //容错级别
$matrixPointSize = 5; //生成图片大小
//生成二维码图片
// 判断是否有这个文件夹 没有的话就创建一个
if(!is_dir("static/qrcode")){
// 创建文件加
mkdir("static/qrcode");
}
//设置二维码文件名
$filename = 'static/qrcode/'.time().rand(10000,9999999).'.png';
//生成二维码
\QRcode::png($code_url,$filename , $errorCorrectionLevel, $matrixPointSize, 2);
// 订单编号, 用于在当前页面向微信服务器发起订单状态查询请求
$data['order_sn'] = $order_sn;
$data['html'] = Request::instance()->domain().'/'.$filename;
Db::commit();
return to_assign(200,'操作成功',$data);
}
return to_assign(0,'操作失败');
}
// 查询订单支付状态
public function paid(Request $request)
{
$out_trade_no = get_params('out_trade_no');
$app = $this->payment();
// 用 easywechat 封装的方法请求微信
$result = $app->order->queryByOutTradeNumber($out_trade_no);
if ($result['trade_state'] === 'SUCCESS'){
return to_assign(200,'支付成功');
}else{
return to_assign(0,'未支付');
}
}
// 采购订单列表
public function order(){
if (request()->isAjax()) {
$param = get_params();
$where[] = ['a.admin_id','=',$this->uid];
if (isset($param['keywords']) && !empty($param['keywords'])){
$where[]=['b.store_name','like','%'.$param['keywords'].'%'];
}
if (isset($param['store_cate']) && !empty($param['store_cate'])){
$where[]=['b.cate_id','=',$param['store_cate']];
}
$rows = empty($param['limit']) ? get_config('app . page_size') : $param['limit'];
$list = Db::table('cms_store_product_order')
->alias('a')
->join('cms_store_product b','a.product_id = b.product_id')
->field('a.*,b.image,b.store_name')
->withAttr('paid_at',function ($value,$data){
return date('Y-m-d H:i:s',(int)$value);
})
->where($where)
->order('a.id desc,a.paid_at desc')
->paginate($rows, false, ['query' => $param]);;
return table_assign(0, '', $list);
}
else{
return view();
}
}
2023-02-21 17:03:11 +08:00
}