浏览代码

优化重置&过期日期显示

兔姬桑 3 年之前
父节点
当前提交
1649b1c8a0

+ 21 - 36
app/Channels/WeChatChannel.php

@@ -13,40 +13,11 @@ use Str;
 
 class WeChatChannel
 {
-    private $access_token;
-
-    public function __construct()
-    {
-        $this->access_token = $this->getAccessToken();
-    }
-
-    private function getAccessToken()
-    {
-        if (Cache::has('wechat_access_token')) {
-            $access_token = Cache::get('wechat_access_token');
-        } else {
-            // https://work.weixin.qq.com/api/doc/90000/90135/91039
-            $response = Http::get('https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid='.sysConfig('wechat_cid').'&corpsecret='.sysConfig('wechat_secret'));
-            if ($response->ok() && isset($response->json()['access_token'])) {
-                $access_token = $response->json()['access_token'];
-                Cache::put('wechat_access_token', $response->json()['access_token'], 7200); // 2小时
-            } else {
-                Log::critical('Wechat消息推送异常:获取access_token失败!'.PHP_EOL.'携带访问参数:'.$response->body());
-                abort(400);
-            }
-        }
-
-        return $access_token ?? null;
-    }
-
     public function send($notifiable, Notification $notification)
     { // route('message.show', ['type' => 'markdownMsg', 'msgId' => ''])
         $message = $notification->toCustom($notifiable);
-        if (! $this->access_token) {
-            $this->access_token = $this->getAccessToken();
-        }
 
-        $url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token='.$this->access_token;
+        $url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token='.$this->getAccessToken();
 
         if (isset($message['button'])) { // 按钮交互型
             // https://work.weixin.qq.com/api/doc/90000/90135/90236#%E6%8C%89%E9%92%AE%E4%BA%A4%E4%BA%92%E5%9E%8B
@@ -89,12 +60,7 @@ class WeChatChannel
                 ],
             ];
         } else { // 文本消息
-            $body = [
-                'touser'                   => '@all',
-                'agentid'                  => sysConfig('wechat_aid'),
-                'msgtype'                  => 'text',
-                'text'                     => ['content' => $message['content']],
-                'duplicate_check_interval' => 600,
+            $body = ['touser' => '@all', 'agentid' => sysConfig('wechat_aid'), 'msgtype' => 'text', 'text' => ['content' => $message['content']], 'duplicate_check_interval' => 600,
             ];
         }
 
@@ -119,6 +85,25 @@ class WeChatChannel
         return false;
     }
 
+    private function getAccessToken()
+    {
+        if (Cache::has('wechat_access_token')) {
+            $access_token = Cache::get('wechat_access_token');
+        } else {
+            // https://work.weixin.qq.com/api/doc/90000/90135/91039
+            $response = Http::get('https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid='.sysConfig('wechat_cid').'&corpsecret='.sysConfig('wechat_secret'));
+            if ($response->ok() && isset($response->json()['access_token'])) {
+                $access_token = $response->json()['access_token'];
+                Cache::put('wechat_access_token', $access_token, 7189); // 2小时
+            } else {
+                Log::critical('Wechat消息推送异常:获取access_token失败!'.PHP_EOL.'携带访问参数:'.$response->body());
+                abort(400);
+            }
+        }
+
+        return $access_token ?? null;
+    }
+
     public function verify(Request $request)
     {
         $errCode = (new WeChat())->VerifyURL($request->input('msg_signature'), $request->input('timestamp'), $request->input('nonce'), $request->input('echostr'), $sEchoStr);

+ 3 - 1
app/Components/Helpers.php

@@ -141,7 +141,9 @@ class Helpers
 
     public static function daysToNow($date): int
     {
-        return (new DateTime())->diff(new DateTime($date))->days;
+        $calculate = (new DateTime())->diff(new DateTime($date));
+
+        return $calculate->invert ? -$calculate->days : $calculate->days;
     }
 
     /**

+ 4 - 4
app/Console/Commands/DailyJob.php

@@ -38,8 +38,8 @@ class DailyJob extends Command
             ->where('expired_at', '<', date('Y-m-d'))
             ->chunk(config('tasks.chunk'), function ($users) use ($isBanStatus) {
                 foreach ($users as $user) {
-                    if ($isBanStatus) { // 停止服务 或 封禁账号
-                        $user->update([
+                    if ($isBanStatus) {
+                        $user->update([ // 停止服务 & 封禁账号
                             'u'               => 0,
                             'd'               => 0,
                             'transfer_enable' => 0,
@@ -57,7 +57,7 @@ class DailyJob extends Command
                         // 写入用户流量变动记录
                         Helpers::addUserTrafficModifyLog($user->id, null, $user->transfer_enable, 0, '[定时任务]账号已过期(禁止登录,清空账户)');
                     } else {
-                        $user->update([
+                        $user->update([ // 停止服务
                             'u'               => 0,
                             'd'               => 0,
                             'transfer_enable' => 0,
@@ -97,8 +97,8 @@ class DailyJob extends Command
     {
         User::where('status', '<>', -1)
             ->where('expired_at', '>', date('Y-m-d'))
-            ->where('reset_time', '<=', date('Y-m-d'))
             ->whereNotNull('reset_time')
+            ->where('reset_time', '<=', date('Y-m-d'))
             ->with('orders')->whereHas('orders')
             ->chunk(config('tasks.chunk'), function ($users) {
                 foreach ($users as $user) {

+ 2 - 2
app/Http/Controllers/UserController.php

@@ -57,8 +57,8 @@ class UserController extends Controller
         }
 
         return view('user.index', array_merge([
-            'remainDays'       => $expireTime < date('Y-m-d') ? -1 : Helpers::daysToNow($expireTime),
-            'resetDays'        => $user->reset_time ? Helpers::daysToNow($user->reset_time) : 0,
+            'remainDays'       => Helpers::daysToNow($expireTime),
+            'resetDays'        => $user->reset_time ? Helpers::daysToNow($user->reset_time) : null,
             'unusedTraffic'    => flowAutoShow($unusedTraffic),
             'expireTime'       => $expireTime,
             'banedTime'        => $user->ban_time,

+ 1 - 1
resources/views/user/buy.blade.php

@@ -20,7 +20,7 @@
                         <tr>
                             <td class="text-middle">{{$goods->name}} </td>
                             <td>{{trans('common.available_date')}}
-                                <strong>{{$goods->type === 1? $dataPlusDays:$goods->days}} {{trans_choice('validation.attributes.day', 1)}}</strong>
+                                <strong>{{$goods->type === 1? $dataPlusDays : $goods->days}} {{trans_choice('validation.attributes.day', 1)}}</strong>
                                 <br>
                                 <strong>{{$goods->traffic_label}}</strong> {{trans('user.attribute.data')}}
                                 <br>

+ 3 - 3
resources/views/user/index.blade.php

@@ -33,7 +33,7 @@
                                 <i class="wb-check green-400 font-size-40 mr-10"></i>
                                 <span class="font-size-40 font-weight-100">{{trans('user.status.enabled')}}</span>
                                 <p class="font-weight-300 m-0 green-500">{{trans('user.account.reason.normal')}}</p>
-                            @elseif($remainDays == 0)
+                            @elseif($remainDays < 0)
                                 <i class="wb-close red-400 font-size-40 mr-10"></i>
                                 <span class="font-size-40 font-weight-100">{{trans('user.status.expired')}}</span>
                                 <p class="font-weight-300 m-0 red-500">{{trans('user.account.reason.expired')}}</p>
@@ -67,7 +67,7 @@
                                     <h4>{{trans('user.account.level')}}:<code class="font-size-20">{{Auth::user()->level}}</code></h4>
                                 </div>
                                 <div class="text-center font-weight-300 blue-grey-500 mb-10">
-                                    @if($paying_user && $resetDays && $remainDays > $resetDays && sysConfig('reset_traffic'))
+                                    @if(isset($resetDays) && $resetDays >= 0)
                                         {{trans('user.account.reset', ['days' => $resetDays])}}
                                     @endif
                                 </div>
@@ -92,7 +92,7 @@
                         </button>
                         <span class="font-weight-400">{{trans('user.account.time')}}</span>
                         <div class="content-text text-center mb-0">
-                            @if($remainDays !== -1)
+                            @if($remainDays >= 0)
                                 <span class="font-size-40 font-weight-100">{{$remainDays.' '.trans_choice('validation.attributes.day', 1)}}</span>
                                 <p class="blue-grey-500 font-weight-300 m-0">{{$expireTime}}</p>
                             @else