manghe/app/api/controller/Order.php
2025-05-23 15:46:19 +08:00

288 lines
9.6 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
namespace app\api\controller;
use app\common\model\Order as OrderModel;
use app\common\model\OrderList;
use app\common\model\OrderNotify;
use think\facade\Db;
use app\common\helper\EnvHelper;
/**
* 订单控制器
*/
class Order extends Base
{
/**
* 获取微信支付订单列表
*
* @return \think\Response
*/
public function getOrderList()
{
// 获取当前登录用户
$user = $this->getUser();
// 获取分页参数
$page = $this->request->param('page', 1);
$pageSize = $this->request->param('page_size', 10);
// 获取订单列表
$orderQuery = Db::name('order')
->field('goods_id, goods_title, goods_price, order_total, order_zhe_total, price, use_money, use_integral, use_money2, addtime, pay_time, prize_num, use_coupon, order_num')
->where([
'user_id' => $user['id'],
'status' => 1
])
->order('addtime desc');
// 获取总数
$total = $orderQuery->count();
// 获取分页数据
$orderList = $orderQuery->page($page, $pageSize)->select()->toArray();
// 格式化数据
foreach ($orderList as &$order) {
// 格式化时间
$order['addtime'] = date('Y-m-d H:i:s', $order['addtime']);
$order['pay_time'] = $order['pay_time'] ? date('Y-m-d H:i:s', $order['pay_time']) : '';
// 格式化价格数据
$order['goods_price'] = (float) $order['goods_price'];
$order['order_total'] = (float) $order['order_total'];
$order['order_zhe_total'] = (float) $order['order_zhe_total'];
$order['price'] = (float) $order['price'];
$order['use_money'] = (float) $order['use_money'];
$order['use_money2'] = (float) $order['use_money2'];
}
unset($order);
// 返回分页数据
$data = [
'list' => $orderList,
'total' => $total,
'page' => (int) $page,
'page_size' => (int) $pageSize,
'total_pages' => ceil($total / $pageSize)
];
return $this->renderSuccess('获取成功', $data);
}
/**
* 获取订单详情
*
* @return \think\Response
*/
public function getOrderDetail()
{
// 获取订单编号
$orderNum = $this->request->param('order_num', '');
if (empty($orderNum)) {
return $this->renderError('订单编号不能为空');
}
// 获取当前登录用户
$user = $this->getUser();
// 获取订单详情
$orderInfo = Db::name('order')
->field('order_num,goods_id, goods_title, goods_price, id, order_total, order_zhe_total, price, use_money, use_integral, use_money2, addtime, pay_time, prize_num, use_coupon')
->where([
'status' => 1,
'order_num' => $orderNum
])
->find();
if (!$orderInfo) {
return $this->renderError('订单不存在');
}
// 格式化时间
$orderInfo['addtime'] = date('Y-m-d H:i:s', $orderInfo['addtime']);
$orderInfo['pay_time'] = $orderInfo['pay_time'] ? date('Y-m-d H:i:s', $orderInfo['pay_time']) : '';
// 格式化价格数据
$orderInfo['goods_price'] = (float) $orderInfo['goods_price'];
$orderInfo['order_total'] = (float) $orderInfo['order_total'];
$orderInfo['order_zhe_total'] = (float) $orderInfo['order_zhe_total'];
$orderInfo['price'] = (float) $orderInfo['price'];
$orderInfo['use_money'] = (float) $orderInfo['use_money'];
$orderInfo['use_money2'] = (float) $orderInfo['use_money2'];
// 获取订单商品列表
$orderGoods = Db::name('order_list')
->field('goodslist_title, goodslist_imgurl, goodslist_price, addtime')
->where([
'order_id' => $orderInfo['id']
])
->select()->toArray();
// 格式化订单商品数据
foreach ($orderGoods as &$goods) {
$goods['addtime'] = date('Y-m-d H:i:s', $goods['addtime']);
$goods['goodslist_price'] = (float) $goods['goodslist_price'];
$goods['goodslist_imgurl'] = imageUrl($goods['goodslist_imgurl']);
}
unset($goods);
$orderInfo['goods_list'] = $orderGoods;
return $this->renderSuccess('获取成功', $orderInfo);
}
/**
* 发送网页支付订单
*
* @return \think\Response
*/
public function sendWebPayOrder()
{
// sendCustomerServiceMessage
// 获取当前登录用户
$user = $this->getUser();
// 获取订单编号
$orderNum = $this->request->param('order_num', '');
if (empty($orderNum)) {
return $this->renderError('订单编号不能为空');
}
$platform = \app\common\server\platform\PlatformFactory::create();
$res = $platform->sendCustomerServiceMessage($user, $orderNum);
if ($res['status'] == 1) {
return $this->renderSuccess('发送成功',$res['data']);
} else {
return $this->renderError($res['msg']);
}
}
/**
* 获取订单状态
*
* @return \think\Response
*/
public function getOrderStatus()
{
// 获取当前登录用户
$user = $this->getUser();
// 获取订单编号
$orderNum = $this->request->param('order_num', '');
if (empty($orderNum)) {
return $this->renderError('订单编号不能为空');
}
$orderNotify = OrderNotify::where([
'order_no' => $orderNum,
])->find();
if (!$orderNotify) {
return $this->renderError('订单不存在');
}
if ($orderNotify['status'] == 1) {
return $this->renderSuccess('支付成功', $orderNotify['status']);
}
return $this->renderSuccess('支付未完成请10秒后重试', $orderNotify['status']);
}
/**
* 创建H5支付订单通知
*
* @return \think\Response
*/
public function createWebPayOrderNotify()
{
$orderNum = $this->request->param('order_num', '');
if (empty($orderNum)) {
return $this->renderError('订单编号不能为空');
}
$cache_key = "order:webpay:" . $orderNum;
$redis = (new \app\common\server\RedisHelper())->getRedis();
$redis_key_info = $redis->get($cache_key);
$orderNotify = OrderNotify::where([
'order_no' => $orderNum,
])->find();
if (!$orderNotify) {
if ($redis_key_info) {
$redis->del($cache_key);
}
return $this->renderError('订单不存在');
}
if ($orderNotify['status'] == 1) {
if ($redis_key_info) {
$redis->del($cache_key);
}
return $this->renderError('订单已支付');
}
if ($orderNotify['status'] == 2) {
if ($redis_key_info) {
$redis->del($cache_key);
}
return $this->renderError('订单已失效');
}
$res = [];
if ($redis_key_info) {
$res = json_decode($redis_key_info, true);
} else {
$platform = new \app\common\server\platform\H5Platform();
$res = $platform->createWebPayOrderNotify($orderNum);
if ($res['status'] == 1) {
$redis->set($cache_key, json_encode($res), 60 * 30);
}
}
if ($res['status'] == 1) {
return $this->renderSuccess('创建成功', $res['data']);
} else {
return $this->renderError($res['msg']);
}
}
public function getOrderUrlLink()
{
$order_num = $this->request->param('order_num', '');
if (!empty($orderNum)) {
return $this->renderError('订单编号不能为空');
}
$order_notify = OrderNotify::where('order_no', $order_num)->find();
$user_id = 0;
$extend = [];
if ($order_notify == null) {
return ['status' => 0, 'msg' => '未找到订单'];
}
if ($order_notify['status'] == 2) {
return ['status' => 0, 'msg' => '订单已失效'];
}
// $this->appid
$web_domain = EnvHelper::getWebDomain();
$returnUrl = '';
if ($order_notify) {
if ($order_notify['extend'] == '') {
return $this->renderError('订单号错误');
}
$extend = json_decode($order_notify['extend'], true);
$user_id = $extend['userId'];
$order_type = $extend['orderType'];
$returnUrl = $extend['returnUrl'];
// 1. 提取路径部分
$path = parse_url($returnUrl, PHP_URL_PATH);
$path = ltrim($path, '/');
// 2. 提取查询部分
$queryString = parse_url($returnUrl, PHP_URL_QUERY);
parse_str($queryString, $paramsArray);
$paramsArray['order_num'] = $order_num;
$query = http_build_query($paramsArray);
$platform = new \app\common\server\platform\MiniProgramPlatform();
$env_version = 'release';
if (EnvHelper::getIsTest()) {
$env_version = 'develop';
}
$res = $platform->getUrlLink($path, $query, $env_version);
if ($res['status'] == 1) {
return $this->renderSuccess('获取成功', $res['data']);
}
}
return $this->renderError('获取失败');
}
}