// +---------------------------------------------------------------------- declare (strict_types=1); namespace app\api\controller; use app\api\model\Goods as GoodsModel; use app\api\model\Order as OrderModel; use app\api\model\OrderGoods as OrderGoodsModel; use app\api\model\Code as CodeModel; use app\api\model\Merchant as MerchantModel; use app\api\model\Setting as SettingModel; use app\common\service\SybOil; use app\common\service\IdingjuOil; use app\store\model\Company as CompanyModel; use app\store\model\Express as ExpressModel; use app\common\enum\order\PayType as OrderPayTypeEnum; use app\store\model\order\QrCode as QrCodeModel; use cores\exception\BaseException; use think\facade\Request; use think\response\Json; use think\cache\driver\Redis; use GatewayWorker\Lib\Gateway; /** * 我的订单控制器 * Class Order * @package app\api\controller */ class Order extends Controller { private $redis = null; /** * 获取当前用户待处理的订单数量 * @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 * @return Json * @throws BaseException */ public function cancel(int $orderId): Json { $model = OrderModel::getDetail($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('确认收货成功'); } 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() ?: '订单支付失败'); } // 构建微信支付请求 $payment = $model->onOrderPayment($model, $payType); // 支付状态提醒 return $this->renderSuccess([ 'order_id' => $model['order_id'], // 订单id 'pay_type' => $payType, // 支付方式 'payment' => $payment // 微信支付参数 ]); } /** * 提交订单 * @return Json * @throws \think\db\exception\DbException * @throws BaseException */ public function submitOrder(): Json { $data = $this->postData(); $verify = $this->verifySign($data); if($verify['code'] > 0){ return $this->renderError($verify['msg']); }else { $data['merchant_id'] = $verify['merchant_id']; $data['callback'] = $verify['callback']; $model = new OrderModel; $data['order_no'] = $this->insertNo($data['order_no']); $data['ip'] = Request::instance()->ip(); if(!$bool = $model->putInto($data)) { $this->saveLog("提交订单,订单:" . $data['order_no'] . ",数据:".json_encode($data,320).",结果:" . $bool); return $this->renderError('插入订单失败'); } $domain = Request::instance()->domain(); $qrcode = $domain.'/pay?passageway='.$data['passageway'].'&orderId='.$model->order_id; $res = [ 'qrcode'=>$qrcode, ]; return $this->renderSuccess($res, '提交成功'); } } public function generateRandomString($length = 10, $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ') { $charactersLength = strlen($characters); $randomString = ''; for ($i = 0; $i < $length; $i++) { $randomString .= $characters[rand(0, $charactersLength - 1)]; } return $randomString; } /** * 查询订单 * @return Json * @throws \think\db\exception\DbException * @throws BaseException */ public function searchOrder(): Json { $data = $this->postData(); $verify = $this->verifySign($data); if($verify['code'] > 0){ return $this->renderError($verify['msg']); }else { $order = OrderModel::detail(['order_no'=>$data['order_no']]); if(empty($order['order_id'])){ return $this->renderError('订单错误'); } $res = [ 'order_no'=>$order['order_no'], 'pay_status'=>$order['pay_status'], 'pay_time'=>$order['pay_time'] ]; return $this->renderSuccess($res, '提交成功'); } } /** * 提交订单 * @return * @throws \app\common\exception\BaseException * @throws \think\exception\DbException * @throws \Exception */ public function testOrder() { $data = $this->request->get(); $money = isset($data['money'])?$data['money']:299; $passageway = isset($data['passageway'])?$data['passageway']:'WX'; $shop_id = isset($data['shop_id'])?$data['shop_id']:''; $merchant = MerchantModel::detail($shop_id); if(empty($merchant['merchant_id'])){ return $this->renderError('商户不存在'); } $domain = Request::instance()->domain(); $ip = Request::instance()->ip(); $data = [ 'appid'=>$merchant['appId'], 'name'=>'系统订单', 'money'=>$money, 'order_no'=> 'XT'.$this->generateRandomString(10), 'timestamp'=>time(), 'passageway'=>$passageway, 'notifyUrl'=>$domain.'/index.php?s=/api/notify/received', 'ip'=>$ip, ]; $data['merchant_id'] = $merchant['merchant_id']; $data['shop_id'] = 10033; $model = new OrderModel; $data['order_no'] = $this->insertNo($data['order_no']); if(!$bool = $model->putInto($data)) { $this->saveLog("提交订单,订单:" . $data['order_no'] . ",数据:".json_encode($data,320).",结果:" . $bool); return $this->renderError('插入系统订单失败'); } $domain = Request::instance()->domain(); $qrcode = $domain.'/pay?passageway='.$data['passageway'].'&orderId='.$model->order_id; /*$qrcode = $domain.'/index.php?s=/store/passport/detail&orderId='.$model->order_id; return $qrcode;*/ header('location: '.$qrcode.''); exit(); } /** 测试发送数据 */ public function send() { //Gateway::sendToClient("7f00000107d00000001b", json_encode(['type' => 'test_send'])); var_export(Gateway::getAllUidList()); //Gateway::sendToClient('c0a8008a07d000000002',json_encode(['type' => 'test_send'])); if(Gateway::isUidOnline(10001)) { // 在线就转发消息给对应的uid Gateway::sendToUid(10001, json_encode(['type' => 'test_send'])); } var_export(Gateway::isUidOnline(10001)); //Gateway::sendToUid('10001', json_encode(['type' => 'test_send'])); //return $this->renderSuccess([], '提交成功'); } /** * 获取支付二维码 * @return Json * @throws \think\db\exception\DbException */ public function qrCode(): Json { // 企油通改造:小程序油品发放/核销链路已停用(上游定聚已下线)。 // 保留接口避免小程序端报错,统一返回提示;如需恢复请改走 QytOil。 return $this->renderError('该功能已停用'); } /** * 获取支付二维码图片 * @return Json * @throws \think\db\exception\DbException */ public function qrCodeImg(): Json { // 企油通改造:小程序油品发放/核销链路已停用(上游定聚已下线)。 // 保留接口避免小程序端报错,统一返回提示;如需恢复请改走 QytOil。 return $this->renderError('该功能已停用'); } public function getBase64($url){ $publicPath = app()->getRootPath() . 'public/'; $escaped_url = escapeshellarg($url); $command = "node {$publicPath}myscript.js {$escaped_url} 2>&1"; $content = shell_exec($command); return (string)$content; } }