Browse Source

Merge branch 'master' into dev

Gorden 5 months ago
parent
commit
50f0164316

+ 14 - 10
app/admin/controller/order/RefundController.php

@@ -306,16 +306,16 @@ class RefundController extends Curd
             if (sprintf("%.2f", $amount) == sprintf("%.2f", $order->order_amount_pay)) {
                 if (!empty($order->order_discount_json)) {
                     $discountJson = json_decode($order->order_discount_json, true);
-                    foreach ($discountJson as $item){
-                        if (empty($item['coupon_id'])){
+                    foreach ($discountJson as $item) {
+                        if (empty($item['coupon_id'])) {
                             continue;
                         }
                         // 是否有其他订单一起使用优惠券
-                        if(!Order::where('order_groupby',$order->order_groupby)->where('order_id','<>',$order->order_id)->where('order_is_complete','<>','R')->exists()){
-                            foreach ($item['coupon_detail_id'] as $detailId){
-                                CouponDetail::where('coupon_detail_id',$detailId)->update([
-                                    'coupon_detail_status'=>'ACTIVED',
-                                    'coupon_detail_used_datetime'=>''
+                        if (!Order::where('order_groupby', $order->order_groupby)->where('order_id', '<>', $order->order_id)->where('order_is_complete', '<>', 'R')->exists()) {
+                            foreach ($item['coupon_detail_id'] as $detailId) {
+                                CouponDetail::where('coupon_detail_id', $detailId)->update([
+                                    'coupon_detail_status' => 'ACTIVED',
+                                    'coupon_detail_used_datetime' => ''
                                 ]);
                             }
                         }
@@ -356,6 +356,7 @@ class RefundController extends Curd
                     $response = $this->refundToWx($payDetail0, $returnId, $amount);
                 } elseif ($payWay[0] == 'ALIPAY') {
                     $prepayid = 'ALIPAY';
+                    $payDetail0['order_id'] = $order->order_id;
                     $response = $this->refundToAlipay($payDetail0, $amount);
                 } elseif ($payWay[0] == 'MONEY') {
                     $prepayid = 'MONEY';
@@ -395,11 +396,11 @@ class RefundController extends Curd
             return json_success('success');
         } catch (BusinessException $e) {
             Db::rollBack();
-            dump($e->getMessage());
+            Log::info("退款失败", ['msg' => $e->getMessage()]);
             return json_fail($e->getMessage());
         } catch (\Exception $e) {
             Db::rollBack();
-            dump($e->getMessage());
+            Log::info("退款失败", ['msg' => $e->getMessage()]);
             return json_fail("退款失败");
         }
     }
@@ -538,7 +539,10 @@ class RefundController extends Curd
         $data = [
             'out_trade_no' => $params['join_pay_order_id'],
             'refund_amount' => $amount,
+            'out_request_no' => $params['order_id']
         ];
+
+        Log::error("支付宝退款参数", $data);
         try {
             $res = Pay::alipay(config('payment.alipay'))->refund($data);
             $resArray = json_decode($res, true);
@@ -549,7 +553,7 @@ class RefundController extends Curd
 
             return $resArray;
         } catch (\Exception $e) {
-
+            Log::error("支付宝退款失败", ['msg' => $e->getMessage()]);
             throw new BusinessException("退款失败");
         }
 

+ 41 - 25
app/admin/service/coupon/CouponDetailService.php

@@ -7,6 +7,7 @@ use app\model\CouponDetail;
 use app\model\SysSerial;
 use support\Db;
 use support\exception\BusinessException;
+use support\Log;
 
 class CouponDetailService
 {
@@ -91,6 +92,8 @@ class CouponDetailService
                 throw new BusinessException("优惠券余量不足");
             }
             $periodJson['now_nbr'] = 1;
+            // 手动发券会传来时间
+            $periodJson['gain_datetime'] = $params['coupon_detail_gain_datetime'] ?? date('Y-m-d H:i:s');
             if (!empty($periodJson['nbr'])) {
                 for ($i = 0; $i < $periodJson['nbr']; $i++) {
                     $periodParams = self::generatePeriod($periodJson);
@@ -111,10 +114,11 @@ class CouponDetailService
             Db::commit();
         } catch (BusinessException $e) {
             Db::rollBack();
+            Log::error("发券失败:" . $e->getMessage());
             throw new BusinessException($e->getMessage());
         } catch (\Exception $e) {
-            dump($e->getMessage());
             Db::rollBack();
+            Log::error("发券失败:" . $e->getMessage());
             throw new BusinessException("优惠券发放失败");
         }
     }
@@ -201,63 +205,75 @@ class CouponDetailService
             $val = $periodJson['val'] - 1;
             if ($val < 1) {
                 if ($now_nbr == 1) {
-                    $params['coupon_detail_gain_datetime'] = date('Y-m-d 00:00:00');
-                    $params['coupon_detail_deadline_datetime'] = date('Y-m-d 23:59:59');
+                    $params['coupon_detail_gain_datetime'] = date('Y-m-d 00:00:00', strtotime($periodJson['gain_datetime']));
+                    $params['coupon_detail_deadline_datetime'] = date('Y-m-d 23:59:59', strtotime($periodJson['gain_datetime']));
                 } else {
                     $now_nbr -= 1;
-                    $params['coupon_detail_gain_datetime'] = date('Y-m-d 00:00:00', strtotime("+" . $now_nbr . ' day'));
-                    $params['coupon_detail_deadline_datetime'] = date('Y-m-d 23:59:59', strtotime("+" . $now_nbr . ' day'));
+                    $params['coupon_detail_gain_datetime'] = date('Y-m-d 00:00:00', strtotime($periodJson['gain_datetime'] . "+" . $now_nbr . ' day'));
+                    $params['coupon_detail_deadline_datetime'] = date('Y-m-d 23:59:59', strtotime($periodJson['gain_datetime'] . "+" . $now_nbr . ' day'));
                 }
             } else {
                 if ($now_nbr == 1) {
-                    $params['coupon_detail_gain_datetime'] = date('Y-m-d 00:00:00');
-                    $params['coupon_detail_deadline_datetime'] = date('Y-m-d 23:59:59', strtotime("+" . $val . ' day'));
+                    $params['coupon_detail_gain_datetime'] = date('Y-m-d 00:00:00', strtotime($periodJson['gain_datetime']));
+                    $params['coupon_detail_deadline_datetime'] = date('Y-m-d 23:59:59', strtotime($periodJson['gain_datetime'] . "+" . $val . ' day'));
                 } else {
                     $now_nbr -= 1;
-                    $params['coupon_detail_gain_datetime'] = date('Y-m-d 00:00:00', strtotime("+" . ((($val + 1) * $now_nbr)) . " day"));
-                    $params['coupon_detail_deadline_datetime'] = date('Y-m-d 23:59:59', strtotime("+" . ((($val + 1) * $now_nbr) + $val) . " day"));
+                    $params['coupon_detail_gain_datetime'] = date('Y-m-d 00:00:00', strtotime($periodJson['gain_datetime'] . "+" . ((($val + 1) * $now_nbr)) . " day"));
+                    $params['coupon_detail_deadline_datetime'] = date('Y-m-d 23:59:59', strtotime($periodJson['gain_datetime'] . "+" . ((($val + 1) * $now_nbr) + $val) . " day"));
                 }
             }
         } elseif ($periodJson['unit'] == 'week') {
             $val = $periodJson['val'] - 1;
+            // 选的日期的周一距离今周周一是几个周
+            $timestamp = strtotime($periodJson['gain_datetime']);
+            $gainWeekDay = date("w", $timestamp);
+            $gainWeekAdd = date("w", $gainWeekDay) == 1 ? 0 : 1 - $gainWeekDay;
+            $gainMonday = date("Y-m-d 00:00:00", strtotime("$gainWeekAdd days", $timestamp));
+            $nowMonday = date('Y-m-d 00:00:00', strtotime('this week Monday'));
+
+            $interval = (new \DateTime(date($gainMonday)))->diff(new \DateTime($nowMonday));
+            $weekCut = $interval->days / 7;
             if ($val < 1) {
                 if ($now_nbr == 1) {
-                    $params['coupon_detail_gain_datetime'] = date('Y-m-d 00:00:00', strtotime('this week Monday'));
-                    $params['coupon_detail_deadline_datetime'] = date('Y-m-d 23:59:59', strtotime('this week Sunday'));
+                    $params['coupon_detail_gain_datetime'] = $gainMonday;
+                    $params['coupon_detail_deadline_datetime'] = date('Y-m-d 23:59:59', strtotime($gainMonday . "+6 days"));
                 } else {
                     $now_nbr -= 1;
-                    $params['coupon_detail_gain_datetime'] = date('Y-m-d 00:00:00', strtotime(date('Y-m-d 00:00:00', strtotime('this week Monday')) . "+" . $now_nbr . ' week'));
-                    $params['coupon_detail_deadline_datetime'] = date('Y-m-d 23:59:59', strtotime(date('Y-m-d 23:59:59', strtotime('this week Sunday')) . "+" . $now_nbr . ' week'));
+                    $params['coupon_detail_gain_datetime'] = date('Y-m-d 00:00:00', strtotime(date('Y-m-d 00:00:00', strtotime('this week Monday')) . "+" . ($now_nbr + $weekCut) . ' week'));
+                    $params['coupon_detail_deadline_datetime'] = date('Y-m-d 23:59:59', strtotime(date('Y-m-d 23:59:59', strtotime('this week Sunday')) . "+" . ($now_nbr + $weekCut) . ' week'));
                 }
             } else {
                 if ($now_nbr == 1) {
-                    $params['coupon_detail_gain_datetime'] = date('Y-m-d 00:00:00', strtotime('this week Monday'));
-                    $params['coupon_detail_deadline_datetime'] = date('Y-m-d 23:59:59', strtotime(date('Y-m-d 23:59:59', strtotime('this week Sunday')) . "+" . $val . ' week'));
+                    $params['coupon_detail_gain_datetime'] = $gainMonday;
+                    $params['coupon_detail_deadline_datetime'] = date('Y-m-d 23:59:59', strtotime(date('Y-m-d 23:59:59', strtotime('this week Sunday')) . "+" . ($val + $weekCut) . ' week'));
                 } else {
                     $now_nbr -= 1;
-                    $params['coupon_detail_gain_datetime'] = date('Y-m-d 00:00:00', strtotime(date('Y-m-d 00:00:00', strtotime('this week Monday')) . "+" . ((($val + 1) * $now_nbr)) . ' week'));
-                    $params['coupon_detail_deadline_datetime'] = date('Y-m-d 23:59:59', strtotime(date('Y-m-d 23:59:59', strtotime('this week Sunday')) . "+" . ((($val + 1) * $now_nbr) + $val) . ' week'));
+                    $params['coupon_detail_gain_datetime'] = date('Y-m-d 00:00:00', strtotime(date('Y-m-d 00:00:00', strtotime('this week Monday')) . "+" . ((($val + 1) * $now_nbr) + $weekCut) . ' week'));
+                    $params['coupon_detail_deadline_datetime'] = date('Y-m-d 23:59:59', strtotime(date('Y-m-d 23:59:59', strtotime('this week Sunday')) . "+" . ((($val + 1) * $now_nbr) + $val + $weekCut) . ' week'));
                 }
             }
         } elseif ($periodJson['unit'] == 'month') {
             $val = $periodJson['val'] - 1;
+            // 选的日期的1号距离今月1号是几个月
+            $interval = (new \DateTime(date('Y-m-01 00:00:00', strtotime($periodJson['gain_datetime']))))->diff(new \DateTime(date('Y-m-01 00:00:00')));
+            $monthCut = $interval->m + ($interval->y * 12);
             if ($val < 1) {
                 if ($now_nbr == 1) {
-                    $params['coupon_detail_gain_datetime'] = date('Y-m-01 00:00:00');
-                    $params['coupon_detail_deadline_datetime'] = date('Y-m-t 23:59:59');
+                    $params['coupon_detail_gain_datetime'] = date('Y-m-01 00:00:00', strtotime($periodJson['gain_datetime']));
+                    $params['coupon_detail_deadline_datetime'] = date('Y-m-t 23:59:59', strtotime($periodJson['gain_datetime']));
                 } else {
                     $now_nbr -= 1;
-                    $params['coupon_detail_gain_datetime'] = date('Y-m-01 00:00:00', strtotime("+" . $now_nbr . ' month'));
-                    $params['coupon_detail_deadline_datetime'] = date('Y-m-t 23:59:59', strtotime(date('Y-m-01 00:00:00')."+" . $now_nbr . ' month'));
+                    $params['coupon_detail_gain_datetime'] = date('Y-m-01 00:00:00', strtotime("+" . ($now_nbr + $monthCut) . ' month'));
+                    $params['coupon_detail_deadline_datetime'] = date('Y-m-t 23:59:59', strtotime(date('Y-m-01 00:00:00') . "+" . ($now_nbr + $monthCut) . ' month'));
                 }
             } else {
                 if ($now_nbr == 1) {
-                    $params['coupon_detail_gain_datetime'] = date('Y-m-01 00:00:00');
-                    $params['coupon_detail_deadline_datetime'] = date('Y-m-t 23:59:59', strtotime(date('Y-m-01 00:00:00')."+" . $val . ' month'));
+                    $params['coupon_detail_gain_datetime'] = date('Y-m-01 00:00:00', strtotime($periodJson['gain_datetime']));
+                    $params['coupon_detail_deadline_datetime'] = date('Y-m-t 23:59:59', strtotime(date('Y-m-01 00:00:00') . "+" . ($val + $monthCut) . ' month'));
                 } else {
                     $now_nbr -= 1;
-                    $params['coupon_detail_gain_datetime'] = date('Y-m-01 00:00:00', strtotime( "+" . ((($val + 1) * $now_nbr)) . ' month'));
-                    $params['coupon_detail_deadline_datetime'] = date('Y-m-t 23:59:59', strtotime(date('Y-m-01 00:00:00')."+" . ((($val + 1) * $now_nbr) + $val) . ' month'));
+                    $params['coupon_detail_gain_datetime'] = date('Y-m-01 00:00:00', strtotime("+" . ((($val + 1) * $now_nbr) + $monthCut) . ' month'));
+                    $params['coupon_detail_deadline_datetime'] = date('Y-m-t 23:59:59', strtotime(date('Y-m-01 00:00:00') . "+" . ((($val + 1) * $now_nbr) + $val + $monthCut) . ' month'));
                 }
             }
         }

+ 25 - 16
app/functions.php

@@ -28,25 +28,34 @@ if (!function_exists('http_post')) {
 if (!function_exists('http_post_json')) {
     function http_post_json($url, $params)
     {
-        $ch = curl_init();
-        $this_header = array('Content-Type: application/json;charset=UTF-8');
-        curl_setopt($ch, CURLOPT_HTTPHEADER, $this_header);
-        curl_setopt($ch, CURLOPT_URL, $url);
-        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
-        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
-        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
+        try {
+            $ch = curl_init();
+            $this_header = array('Content-Type: application/json;charset=UTF-8');
+            curl_setopt($ch, CURLOPT_HTTPHEADER, $this_header);
+            curl_setopt($ch, CURLOPT_URL, $url);
+            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
+            curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
+            curl_setopt($ch, CURLOPT_TIMEOUT, 30);
 
-        curl_setopt($ch, CURLOPT_POST, 1);
-        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
-        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//如果不加验证,就设false,商户自行处理
-        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
+            curl_setopt($ch, CURLOPT_POST, 1);
+            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
+            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//如果不加验证,就设false,商户自行处理
+            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
 
-        $output = curl_exec($ch);
-        curl_close($ch);
-        // 写日志
-        \support\Log::info('模拟请求:',json_decode($output,true));
+            $output = curl_exec($ch);
+            curl_close($ch);
+            // 写日志
+            if (is_json($output)) {
+                \support\Log::info('模拟请求:', json_decode($output, true));
+            } else {
+                \support\Log::info('模拟请求,返回异常:', ['msg' => (string)$output]);
+            }
 
-        return $output;
+            return $output;
+
+        } catch (\Exception $e) {
+            \support\Log::info('模拟请求失败:', ['msg' => $e->getMessage()]);
+        }
     }
 }