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.

376 lines
14 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace app\integral\service;
use app\integral\model\IntegralCart;
use app\integral\model\IntegralOrder;
use app\integral\model\IntegralOrderProduct;
use app\integral\model\IntegralProduct;
use app\integral\model\IntegralProductSku;
use think\facade\Db;
use think\facade\Request;
class Order extends Base
{
/**
* 获取下单时购买商品
* @param int $buy_type 购买方式 1--购物车 2--立即购买
* @param int $product_id buy_type=2时产品ID
* @param string $product_sku buy_type=2时产品SKU
* @param int $number buy_type=2时购买数量
* @date 2022-11-17
*/
public function getOrderProduct($buy_type, $product_id = '', $product_sku = '', $number = 1)
{
$cart_model = new IntegralCart();
$product_model = new IntegralProduct();
$sku_model = new IntegralProductSku();
$cart_product = [];
// 兑换车结算
if ($buy_type == 1) {
$where = [
['uid', '=', $this->mid],
['user_id', '=', $this->userId],
// 是否选中 0-未选中 1--选中
['is_select', '=', 1],
// 是否失效 0--失效 1--有效
['is_effective', '=', 1]
];
$cart_product = $cart_model->listCartProduct($where, 'id,product_id,is_product_spec_open,product_sku,number');
$cart_product = empty($cart_product) ? [] : $cart_product->toArray();
// 释放无用字段
foreach ($cart_product as $k => &$v) {
unset($v['product_is_publish']);
unset($v['product_delete_time']);
}
// 立即购买
} else if ($buy_type == 2) {
// 产品
$product = $product_model->getOneData([
['id', '=', $product_id],
['is_publish', '=', 1]
], 'id,type,name,is_spec_open,stock,cover_img,price,integral,freight_type,freight_id,freight_money,weight');
// 商品有规格
if (!empty($product_sku)) {
$product_sku_list = $sku_model->getOneData([
['product_id', '=', $product_id],
['sku', '=', $product_sku],
], 'id,product_id,sku,cover_img,price,integral,stock');
}
$cart_product[] = [
// 产品ID
'product_id' => $product['id'],
// 产品是否开启规格
'is_product_spec_open' => $product['is_spec_open'],
// 产品SKU
'product_sku' => $product_sku,
// 产品购买数量
'number' => $number,
// 产品类型 1--实物 2--虚拟
'product_type' => $product['type'],
// 产品名称
'product_name' => $product['name'],
// 产品SKU名称
'product_sku_name' => $product['is_spec_open'] == 0 ? '' : $product_sku_list['sku_name'],
// 产品封面图
'product_cover_img' => $product['cover_img'],
// 产品价钱
'product_price' => $product['is_spec_open'] == 0 ? $product['price'] : $product_sku_list['price'],
// 产品积分
'product_integral' => $product['is_spec_open'] == 0 ? $product['integral'] : $product_sku_list['integral'],
// 产品库存
'product_stock' => $product['is_spec_open'] == 0 ? $product['stock'] : $product_sku_list['stock'],
// 运费类型
'freight_type' => $product['freight_type'],
// 运费模板ID
'freight_id' => $product['freight_id'],
// 运费金额
'freight_money' => $product['freight_money'],
// 产品总重量
'weight' => $product['weight']
];
}
return $cart_product;
}
/**
* 调起支付
* @param int $order_id 订单ID
* @date 2022-11-18
*/
public function createPayOrder($order_id)
{
$order_model = new IntegralOrder();
// 获取订单信息
$order = $order_model->getOneData([
['id', '=', $order_id]
], 'id,user_agent,user_id,order_number,pay_type,pay_money');
// 调起支付
switch ($order['pay_type']) {
// 微信支付
case 1:
$wxpay_param = get_wxpay_config_with_weixin($order['user_agent']);
$wxpay_class = new \tencent\wechat\pay\Wxpay($wxpay_param);
$openid = $this->openid;
$notify_url = Request::domain() . '/index.php/integral/api/Callback/weixinNotify/agent/' . $order['user_agent'] . '/uid/' . $this->mid;
// 生成支付订单
$result = $wxpay_class->createOrder($openid, $order['pay_money'], $order['order_number'], '积分商城支付订单', $notify_url, $order['user_agent']);
if ($result['return_code'] == 'FAIL' || $result['result_code'] == 'FAIL') {
return sendErrorArray(3100, $result['return_code'] != 'SUCCESS' ? $result['return_msg'] : $result['err_code_des']);
}
/* --H5平台微信支付--
直接返回 */
if ($order['user_agent'] == 'h5') {
// mweb_url为拉起微信支付收银台的中间页面可通过访问该url来拉起微信客户端完成支付,mweb_url的有效期为5分钟
return sendSuccessArray(['need_pay' => 1, 'pay_params' => $result['mweb_url']]);
}
/* --其他平台微信支付--
获取支付参数 */
$result_pay = $wxpay_class->wxPay($result['prepay_id'], $order['user_agent']);
return sendSuccessArray([
// 是否需要支付
'need_pay' => 1,
// 支付参数
'pay_params' => $result_pay
]);
break;
// 支付宝支付
case 2:
switch ($order['user_agent']) {
// app 支付宝支付
case 'app':
$ali_pay_param = get_app_alipay_config();
$ali_pay_class = new \ali\alipay\pay\AliPay($ali_pay_param);
$notify_url = Request::domain() . '/index.php/integral/api/Callback/alipayNotify/agent/' . $order['user_agent'] . '/uid/' . $this->mid;
$result = $ali_pay_class->aliPay($order['pay_money'], $order['order_number'], '积分商城支付订单', $notify_url);
if (empty($result['body'])) {
return sendErrorArray(3101, '生成支付宝订单失败');
}
return sendSuccessArray([
'need_pay' => 1,
'pay_params' => $result['body']
]);
break;
// 支付宝小程序 支付宝支付
case 'mp_alipay':
$ali_pay_param = get_mp_alipay_config();
$ali_pay_class = new \ali\alipay\pay\AliPay($ali_pay_param);
$notify_url = Request::domain() . '/index.php/integral/api/Callback/alipayNotify/agent/' . $order['user_agent'] . '/uid/' . $this->mid;
// 统一下单
$result = $ali_pay_class->mpAliPay($order['pay_money'], $order['order_number'], $this->openid, '积分商城支付订单', $notify_url);
if ($result['code'] != 10000) {
return sendErrorArray($result['code'], $result['sub_msg']);
}
return sendSuccessArray([
'need_pay' => 1,
'pay_params' => $result['trade_no']
]);
break;
}
break;
// 余额支付
case 3:
$money_service = new \app\money\service\Money();
Db::startTrans();
//支付成功
$res = $this->afterOrderPaySuccess($order_id);
if ($res['code'] != 0) {
Db::rollback();
return $res;
}
// 扣除余额
$res = $money_service->change($order['user_id'], 3, -$order['pay_money'], '积分商城购物', $order['order_number'], [
'integral_order_id' => $order_id
]);
if ($res['code'] != 0) {
Db::rollback();
return $res;
}
Db::commit();
return sendSuccessArray([
'need_pay' => 0
], '支付成功');
break;
// 其他
default:
break;
}
}
/**
* 订单支付成功
* @param int $order_id 订单ID
* @param string $transaction_id 微信支付环境:微信支付系统生成的订单号 支付宝支付环境:支付宝交易号
* @date 2022-11-18
*/
public function afterOrderPaySuccess($order_id, $transaction_id = '')
{
$order_model = new IntegralOrder();
$order_product_model = new IntegralOrderProduct();
$integral_service = new \app\integral\service\Integral();
// 查找订单
$order = $order_model->getOneData([
['id', '=', $order_id]
], 'id,uid,user_agent,order_number,product_type,delivery_type,user_id,pay_money,pay_integral');
// 自提单生成核销码
$delivery_code = '';
if ($order['delivery_type'] == 2) {
// 带内容的二维码
$delivery_code = create_qr_code_to_oss($order_id, 'uid' . $this->mid . '/integral', 'delivery_' . $order_id . '.jpg');
}
// 1 - 更改订单状态
$data_update = [
'id' => $order['id'],
'is_pay' => 1,
'pay_time' => time(),
'transaction_id' => $transaction_id,
'delivery_code' => $delivery_code
];
// 实物自提订单直接更改为待收货
if ($order['product_type'] == 1 && $order['delivery_type'] == 2) {
$data_update['status'] = 3;
$data_update['express_time'] = time();
} else {
$data_update['status'] = 2;
}
$res = $order_model->dataUpdate($data_update);
if (!$res) {
return sendErrorArray(2001, '订单状态修改失败');
}
// 订单产品表
$order_product = $order_product_model->getAllData([
['order_id', '=', $order_id]
], 'id,product_id,number');
// 2 - 付款之后,产品增加销量
foreach ($order_product as $k => $v) {
$res = $this->productSalesChange($v['product_id'], $v['number']);
if ($res['code'] != 0) {
return $res;
}
}
// 3 - 扣除对应积分
if ($order['pay_integral']) {
$res = $integral_service->change($order['user_id'], 1, -$order['pay_integral'], '积分商城购物减少', $order['order_number'], [
'integral_order_id' => $order['id']
]);
if ($res['code'] != 0) {
return $res;
}
}
return sendSuccessArray();
}
/**
* 订单产品减少库存
* @param int $product_id 产品ID
* @param string $product_sku 产品SKU
* @param int $number 变动数量 >0 增加 <0 减少
* @date 2022-11-18
*/
public function productStockChange($product_id, $product_sku, $number)
{
$product_model = new IntegralProduct();
$product_sku_model = new IntegralProductSku();
// 符号
$symbol = $number > 0 ? '+' : '-';
// 更新产品表库存
$product_update_data = [
'id' => $product_id,
'stock' => Db::raw('stock' . $symbol . abs($number))
];
$res = $product_model->dataUpdate($product_update_data);
if (!$res) {
return sendErrorArray(2001, '更新产品库存失败');
}
// 商品有SKU
if (!empty($product_sku)) {
// 获取SKU_ID
$sku_id = $product_sku_model->getOneData([
['product_id', '=', $product_id],
['sku', '=', $product_sku]
], 'id');
// 如果还能找到该规格项 则减库存 否则暂时不减
if (!empty($sku_id)) {
// 更改产品SKU库存
$product_data_sku = [
'stock' => Db::raw('stock' . $symbol . abs($number))
];
$res = $product_sku_model->dataUpdate($product_data_sku, [
['product_id', '=', $product_id],
['sku', '=', $product_sku]
]);
if (!$res) {
return sendErrorArray(2003, '更新产品SKU库存失败');
}
}
}
return sendSuccessArray();
}
/**
* 产品销量变动
* @param int $product_id 产品ID
* @param int $number 变动数量 >0 增加 <0 减少
* @date 2022-11-18
*/
public function productSalesChange($product_id, $number)
{
$product_model = new IntegralProduct();
$symbol = $number > 0 ? '+' : '-';
// 更新产品表销量
$product_update_data = [
'id' => $product_id,
'sales_actual_number' => Db::raw('sales_actual_number' . $symbol . abs($number))
];
$res = $product_model->dataUpdate($product_update_data);
if (!$res) {
return sendErrorArray(2001, '更新产品销量失败');
}
return sendSuccessArray();
}
}