123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- <?php
- // +----------------------------------------------------------------------
- // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
- // +----------------------------------------------------------------------
- // | Author: 萤火科技 <admin@yiovo.com>
- // +----------------------------------------------------------------------
- declare (strict_types=1);
- namespace app\index\controller;
- use app\index\model\Order as OrderModel;
- use app\index\model\Setting as SettingModel;
- use app\store\model\Express as ExpressModel;
- use app\common\enum\order\PayType as OrderPayTypeEnum;
- use cores\exception\BaseException;
- use think\facade\Session;
- use think\response\Json;
- /**
- * 我的订单控制器
- * Class Order
- * @package app\api\controller
- */
- class Order extends Controller
- {
- /**
- * 获取当前用户待处理的订单数量
- * @return Json
- * @throws BaseException
- */
- public function todoCounts(): Json
- {
- $model = new OrderModel;
- $counts = $model->getTodoCounts();
- return $this->renderSuccess(compact('counts'));
- }
- /**
- * 我的订单列表
- * @param string $dataType 订单类型 (all全部 payment待付款 received待发货 deliver待收货 comment待评价)
- * @return Json
- * @throws \think\db\exception\DbException
- * @throws BaseException
- */
- public function list(string $dataType): Json
- {
- $model = new OrderModel;
- $list = $model->getList($dataType);
- return $this->renderSuccess(compact('list'));
- }
- /**
- * 订单详情信息
- * @param int $orderId 订单ID
- * @return Json
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws BaseException
- */
- public function detail(int $orderId): Json
- {
- // 订单详情
- $model = OrderModel::getUserOrderDetail($orderId);
- return $this->renderSuccess([
- 'order' => $model, // 订单详情
- 'setting' => [
- // 积分名称
- 'points_name' => SettingModel::getPointsName(),
- ],
- ]);
- }
- /**
- * 获取物流信息
- * @param int $orderId 订单ID
- * @return Json
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws BaseException
- */
- public function express(int $orderId): Json
- {
- // 订单信息
- $order = OrderModel::getDetail($orderId);
- if (!$order['express_no']) {
- return $this->renderError('没有物流信息');
- }
- // 获取物流信息
- $model = ExpressModel::detail($order['express_id']);
- $express = $model->dynamic($model['express_name'], $model['kuaidi100_code'], $order['express_no']);
- if ($express === false) {
- return $this->renderError($model->getError());
- }
- return $this->renderSuccess(compact('express'));
- }
- /**
- * 获取物流信息
- * @param int $orderId 订单ID
- * @return Json
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws BaseException
- */
- public function expressUsps(int $orderId): Json
- {
- // 订单信息
- // $order = OrderModel::getDetail($orderId);
- // if (!$order['express_no']) {
- // return $this->renderError('No logistics information');
- // }
- //$expressUserId = config('usps.exp_user_id');
- $order['express_no'] = 'AT354585357CN';
- // 获取物流信息
- $model = new ExpressModel();
- $express = $model->dynamicUsps($order['express_no']);
- if ($express === false) {
- return $this->renderError($model->getError());
- }
- return $this->renderSuccess(compact('express'));
- }
- /**
- * 取消订单
- * @return Json
- * @throws BaseException
- */
- public function cancel(): Json
- {
- $userId = Session::get('user_id');
- if (empty($userId)) {
- return $this->renderJson(config('status.not_logged'), 'Log in please!');
- }
- $orderId = $this->request->param('orderId');
- if (empty($orderId)){
- return $this->renderError('Something error!');
- }
- $model = OrderModel::getDetail(intval($orderId));
- if ($model->cancel()) {
- return $this->renderSuccess('订单取消成功');
- }
- return $this->renderError($model->getError() ?: '订单取消失败');
- }
- /**
- * 确认收货
- * @param int $orderId
- * @return Json
- * @throws BaseException
- */
- public function receipt(int $orderId): Json
- {
- $model = OrderModel::getDetail($orderId);
- if ($model->receipt()) {
- return $this->renderSuccess('Confirm receipt of goods successfully');
- }
- return $this->renderError($model->getError());
- }
- /**
- * 立即支付
- * @param int $orderId 订单ID
- * @param int $payType 支付方式
- * @return Json
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws BaseException
- */
- public function pay(int $orderId, int $payType = OrderPayTypeEnum::WECHAT): Json
- {
- // 获取订单详情
- $model = OrderModel::getUserOrderDetail($orderId);
- // 订单支付事件
- if (!$model->onPay($payType)) {
- return $this->renderError($model->getError() ?: 'Order creation failed, please try again later');
- }
- // 构建微信支付请求
- $payment = $model->onOrderPayment($model, $payType);
- if ($payType == OrderPayTypeEnum::POINTS) {
- if (!$payment['flag']) {
- return $this->renderError($payment['message'] ?? 'Redemption failed, please try again later');
- }
- }
- // 支付状态提醒
- return $this->renderSuccess([
- 'order_id' => $model['order_id'], // 订单id
- 'pay_type' => $payType, // 支付方式
- 'payment' => $payment // 微信支付参数
- ]);
- }
- }
|