| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- <?php
- namespace app\controller\wap;
- use app\extra\basic\Base;
- use app\extra\jhfPay\Pay;
- use app\extra\service\saas\OrderService;
- use app\middleware\WxMiddleware;
- use app\model\saas\SaasOrder;
- use app\model\saas\SaasOrderDetail;
- use app\model\saas\SaasUser;
- use app\model\saas\SaasUserLog;
- use DI\Attribute\Inject;
- use LinFly\Annotation\Route\Controller;
- use LinFly\Annotation\Route\Middleware;
- use LinFly\Annotation\Route\Route;
- use support\Request;
- use support\Response;
- #[Controller(prefix: "/wap/order"),Middleware(WxMiddleware::class)]
- class Order extends Base
- {
- #[Inject]
- protected OrderService $service;
- #[Inject]
- protected SaasOrder $model;
- #[Route(path: "list",methods: "get")]
- public function getPriceList(Request $request): Response
- {
- try {
- $param = $request->get();
- if (empty($param['shop'])) {
- $param['shop'] = $request->user['shop_id'];
- }
- if (!empty($param['order'])) {
- $orderType = explode("-",$param['order']);
- if (count($orderType)>1) { // 搜索取件码
- $param['sn'] = $param['order'];
- } else {
- $param['orderid'] = $param['order'];
- }
- }
- if (empty($param['status'])) $param['statusGt'] = 1;
- $list = $this->service->getList($param);
- return successTrans("success.data",pageFormat($list),200);
- } catch (\Throwable $throwable) {
- return error($throwable->getMessage());
- }
- }
- #[Route(path: "detail",methods: "get")]
- public function getOrderDetail(Request $request): Response
- {
- try {
- $param = $this->_valid([
- "id.require" => trans("empty.require")
- ],$request->method());
- if (!is_array($param)) return error($param);
- $detail = $this->model->where("id",$param['id'])->with(['detail','shop' => function($query){
- $query->field("shop_id,shop_name");
- }])->findOrEmpty();
- if ($detail->isEmpty()) return errorTrans("error.data");
- if ($detail['shop_id'] <> $request->user['shop_id']) return errorTrans("error.data");
- $startTime = strtotime(date('Y-m-d 00:00:00'));
- $endTime = strtotime(date('Y-m-d 23:59:58'));
- $orderTime = strtotime($detail['create_at']);
- $detail['today'] = (($orderTime > $startTime && $orderTime < $endTime) ? 1 : 0);
- return success("ok",$detail->toArray());
- } catch (\Throwable $throwable) {
- return error($throwable->getMessage());
- }
- }
- #[Route(path: "refund",methods: "post")]
- public function orderRefund(Request $request): Response
- {
- try {
- $param = $this->_valid([
- "id.require" => trans("empty.require")
- ],$request->method());
- if (!is_array($param)) return error($param);
- $detail = $this->model->where("id",$param['id'])->findOrEmpty();
- if ($detail->isEmpty()) return errorTrans("error.data");
- if ($detail['shop_id'] <> $request->user['shop_id']) return errorTrans("error.data");
- if (!in_array($detail['status'],[1,2,3])) return error("该订单不支持退款");
- if ($detail['pay_type'] == 2) {
- $card = (new SaasUser)->where("openid",$detail['openid'])->where("shop_id",$detail['shop_id'])->findOrEmpty();
- if ($card->isEmpty()) return error("会员卡失效");
- $card->balance = ($card['balance'] + $detail['money']);
- $card->total_consume = ($card['total_consume'] - $detail['money']);
- $card->save();
- (new SaasUserLog)->insertGetId([
- "openid" => $detail['openid'],
- "shop_id" => $detail['shop_id'],
- "order_sn" => $detail['order_sn'],
- "money" => $detail['money'],
- "card_no" => strtoupper(md5($detail['openid'].$detail['shop_id'])),
- "type" => 2,
- "remark" => "订单退款",
- "balance" => $card['balance'] - $detail['money'],
- ]);
- $detail->status = 6;
- $detail->refund_at = getDateFull();
- $detail->save();
- (new SaasOrderDetail)->where("order_sn",$detail['order_sn'])->update(['status' => 6]);
- return success("退款成功");
- }
- $respJhf = (new Pay)->config([
- "appid" => sConf("wechat.jhf_appid"),
- "mch_id" => sConf("wechat.jhf_mch_id"),
- "aeskey" => sConf("wechat.jhf_aeskey"),
- "pubkey" => sConf("wechat.jhf_pubkey"),
- "prikey" => sConf("wechat.jhf_prikey"),
- ])->createRefund([
- "payment_id" => $detail['payment_id'],
- "order_no" => $detail['order_sn'],
- "notify_url" => "https://panel.huiyinduo.cn/notify/refund",
- "refund_amt" => format_money($detail['money']/100,2)
- ]);
- if (isset($respJhf['code'])) {
- return error("发起退款失败");
- }
- $detail->status = 4;
- $state = $detail->save();
- if (!$state) return errorTrans("error.data");
- (new SaasOrderDetail)->where("order_sn",$detail['order_sn'])->update(['status' => 4]);
- return successTrans("success.data");
- } catch (\Throwable $throwable) {
- return error($throwable->getMessage());
- }
- }
- }
|