Browse Source

Merge branch 'master' of http://39.98.194.76:3000/txct/wanyue_app

yxjapp 5 months ago
parent
commit
a45e12bbeb
2 changed files with 65 additions and 43 deletions
  1. 40 27
      app/admin/service/coupon/CouponDetailService.php
  2. 25 16
      app/functions.php

+ 40 - 27
app/admin/service/coupon/CouponDetailService.php

@@ -92,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);
@@ -112,12 +114,11 @@ class CouponDetailService
             Db::commit();
         } catch (BusinessException $e) {
             Db::rollBack();
-            Log::error("发券失败:".$e->getMessage());
+            Log::error("发券失败:" . $e->getMessage());
             throw new BusinessException($e->getMessage());
         } catch (\Exception $e) {
-//            dump($e->getMessage());
             Db::rollBack();
-            Log::error("发券失败:".$e->getMessage());
+            Log::error("发券失败:" . $e->getMessage());
             throw new BusinessException("优惠券发放失败");
         }
     }
@@ -204,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()]);
+        }
     }
 }