| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- <?php
- 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;
- /**
- * Class Index
- * @package app\index\controller
- */
- class Video extends Controller
- {
- public function index()
- {
- FacadeLog::info("index cookies: ". json_encode($_COOKIE));
- $sub = "";
- $isLogin = 1;
- if (!empty($_COOKIE['SUB']) && strpos($_SERVER["HTTP_USER_AGENT"],"Weibo")) {
- $sub = $_COOKIE['SUB'];
- $isWebo = 1;
- } else {
- $isWebo = 0;
- // 只在调试模式下开启从POST参数中获取UID,方便测试联调
- if (env('weibo.mock')) {
- $sub = Request::get('cookie', "");
- }
- }
- $this->assign('sub', '');
- $this->assign('isWeibo', $isWebo);
- $this->assign('mock', env('weibo.mock', '0'));
- $uid = 0;
- $userInfoRes = (new WeiboService($uid))->userinfo($sub);
- if (empty($userInfoRes) || $userInfoRes['ok'] != 1) {
- $isLogin = 0;
- $this->assign('isLogin', $isLogin);
- $this->assign('user', '');
- $this->assign('token', '');
- $this->assign('isShare', '');
- $this->assign('count', '');
- $this->assign('isBeginActivity', '');
- $this->assign('task', '');
- $this->fetch();
- }
- // 使用客户端信息生成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()
- ];
- Db::table('awards_user_info')->insert($userAttr);
- } 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('isLogin', $isLogin);
- $this->fetch();
- }
- }
|