Browse Source

添加404页面

allen 3 years ago
parent
commit
33b7756ef5

+ 1 - 1
.example.env

@@ -1,4 +1,4 @@
-APP_DEBUG = true
+APP_DEBUG = false
 
 [APP]
 DEFAULT_TIMEZONE = Asia/Shanghai

+ 4 - 4
app/index/controller/Index.php

@@ -93,10 +93,10 @@ class Index extends Controller
         $ivlen = openssl_cipher_iv_length($cipher);
         $iv = bin2hex(openssl_random_pseudo_bytes($ivlen));
         $aesKey = bin2hex(openssl_random_pseudo_bytes(32));
-        $user = array_merge($user, [
-            'aes_key' => $aesKey,
-            'ase_iv' => $iv,
-        ]);
+//        $user = array_merge($user, [
+//            'aes_key' => $aesKey,
+//            'ase_iv' => $iv,
+//        ]);
         $cacheUser = [
             'aes_key' => $aesKey,
             'ase_iv' => $iv,

+ 77 - 1
app/index/controller/Video.php

@@ -2,6 +2,9 @@
 
 namespace app\index\controller;
 
+use app\service\WeiboService;
+use think\facade\Cache;
+use think\facade\Db;
 use think\facade\Request;
 use think\admin\Controller;
 use think\facade\Log as FacadeLog;
@@ -23,9 +26,82 @@ class Video extends Controller
             $isWebo = 0;
             // 只在调试模式下开启从POST参数中获取UID,方便测试联调
             if (env('weibo.mock')) {
-                $sub = Request::get('cookie', "")?:'testcookie';
+                $sub = Request::get('cookie', "");
             }
         }
+
+        $uid = 0;
+        $userInfoRes = (new WeiboService($uid))->userinfo($sub);
+        if (empty($userInfoRes) || $userInfoRes['ok'] != 1) {
+            $this->fetch('404');
+        }
+
+        // 使用客户端信息生成token
+        $token = md5($_SERVER['HTTP_USER_AGENT'] . $_SERVER['HTTP_ACCEPT_ENCODING'] . $_SERVER['HTTP_ACCEPT_LANGUAGE']. get_client_ip(0) . $userInfoRes['data']['uid']);
+        $user = $userInfoRes['data'];
+
+        $userInfo = Db::table('awards_user_info')->where('uid', $user['uid'])->find();
+        $count = 0;
+        $isShare = 0;
+        if (empty($userInfo)) {
+            $userAttr = [
+                'uid' => $user['uid'],
+                'portrait' => $user['profile_image_url'],
+                'nickname' => $user['name'],
+                'is_share' => 0,
+                'count' => 1,
+                'create_at' => time()
+            ];
+            if (0 == Db::table('awards_user_info')->insert($userAttr)) {
+                $this->fetch('404');
+            }
+        } else {
+            $count = Db::table('awards_user_task_log')->where('uid', $user['uid'])->count('id');
+            $isShare = $userInfo['is_share'];
+        }
+
+        // 生成加密用的密钥和向量
+        $cipher = "aes-256-gcm";
+        $ivlen = openssl_cipher_iv_length($cipher);
+        $iv = bin2hex(openssl_random_pseudo_bytes($ivlen));
+        $aesKey = bin2hex(openssl_random_pseudo_bytes(32));
+//        $user = array_merge($user, [
+//            'aes_key' => $aesKey,
+//            'ase_iv' => $iv,
+//        ]);
+        $cacheUser = [
+            'aes_key' => $aesKey,
+            'ase_iv' => $iv,
+            'uid' => $user['uid'],
+        ];
+
+        $activity = Db::table('awards_activity')->where('begin_at', '<=', time())
+            ->where('end_at', '>=', time())->find();
+
+        $isBeginActivity = 1;
+        if (empty($activity)) {
+            $isBeginActivity = 0;
+        }
+
+        $userInfo = [];
+        if ($count > 0) {
+            $userInfo = Db::table('awards_user_task_log')->alias('l')
+                ->leftJoin('awards_user_info u', 'l.uid = u.uid')
+                ->field(['u.nickname', 'u.portrait', 'u.uid', 'l.duration', 'l.number'])
+                ->order('l.number', 'desc')
+                ->order('l.duration', 'asc')
+                ->find();
+        }
+
+        // 缓存用户信息1天
+        Cache::set('u:' . $token, json_encode($cacheUser), 86400);
+
+        $this->assign('user', json_encode($user));
+        $this->assign('token', $token);
+        $this->assign('isShare', $isShare);
+        $this->assign('count', $count);
+        $this->assign('isBeginActivity', $isBeginActivity);
+        $this->assign('task', json_encode($userInfo));
         $this->assign('sub', $sub);
         $this->assign('isWeibo', $isWebo);
         $this->assign('mock', env('weibo.mock', '0'));

+ 45 - 0
app/index/view/video/404.html

@@ -0,0 +1,45 @@
+<!DOCTYPE html >
+<html>
+<head>
+    <meta charset=utf-8" />
+    <title>简简单单404文字页</title>
+    <style type="text/css">
+        body {
+            background-color: #ECECEC;
+            color: #3c3c3c;
+            text-align:center
+        }
+        .demo{width:100%;margin:0 auto;}
+        .demo p{ font-size: 2.2rem;}
+        .demo .title{ font-size: 10rem;}
+    </style>
+    <script type="text/javascript">
+        var run_404 = function(count_down_num,congge_home){
+            var go_to_home = document.getElementById('go_to_home');
+            go_to_home.innerHTML=count_down_num;
+            the_num = --count_down_num;
+            if(the_num>0){
+                setTimeout("run_404("+the_num+",'"+congge_home+"')",1000);
+            }
+            else{
+                location.href=congge_home;
+            }
+        }
+    </script>
+</head>
+
+<body>
+
+<div class="demo">
+    <p class="title">404</p>
+    <p>
+        该页面不存在,正在跳转到首页
+        <span id="go_to_home">500</span>
+        秒后跳转到首页
+    </p>
+</div>
+<script type="text/javascript">
+    run_404(500,'/');
+</script>
+</body>
+</html>

+ 1 - 1
app/middleware/Safe.php

@@ -94,7 +94,7 @@ class Safe
             return null;
         }
         // 简单检查token合法性,防止抓包拿到token放到代码里面跑
-        $loginToken = md5($_SERVER['HTTP_USER_AGENT'] . $_SERVER['HTTP_ACCEPT_ENCODING'] . $_SERVER['HTTP_ACCEPT_LANGUAGE'] . $_SERVER['HTTP_REFERER'] . get_client_ip(0) . $user['uid']);
+        $loginToken = md5($_SERVER['HTTP_USER_AGENT'] . $_SERVER['HTTP_ACCEPT_ENCODING'] . $_SERVER['HTTP_ACCEPT_LANGUAGE'] . get_client_ip(0) . $user['uid']);
         if ($loginToken != $token) {
             abort(
                 json(

+ 2 - 1
public/index.php

@@ -4,7 +4,8 @@ namespace think;
 
 use think\admin\service\SystemService;
 
-header("Access-Control-Allow-Origin: *");
+header("Access-Control-Allow-Origin: *.weibo.com");
+header("Access-Control-Allow-Methods: GET,POST,HEAD,OPTIONS");
 // 加载基础文件
 require __DIR__ . '/../vendor/autoload.php';
 // WEB应用初始化