Video.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace app\index\controller;
  3. use app\service\WeiboService;
  4. use think\facade\Cache;
  5. use think\facade\Db;
  6. use think\facade\Request;
  7. use think\admin\Controller;
  8. use think\facade\Log as FacadeLog;
  9. /**
  10. * Class Index
  11. * @package app\index\controller
  12. */
  13. class Video extends Controller
  14. {
  15. public function index()
  16. {
  17. FacadeLog::info("index cookies: ". json_encode($_COOKIE));
  18. $sub = "";
  19. $isLogin = 1;
  20. if (!empty($_COOKIE['SUB']) && strpos($_SERVER["HTTP_USER_AGENT"],"Weibo")) {
  21. $sub = $_COOKIE['SUB'];
  22. $isWebo = 1;
  23. } else {
  24. $isWebo = 0;
  25. // 只在调试模式下开启从POST参数中获取UID,方便测试联调
  26. if (env('weibo.mock')) {
  27. $sub = Request::get('cookie', "");
  28. }
  29. }
  30. $this->assign('sub', '');
  31. $this->assign('isWeibo', $isWebo);
  32. $this->assign('mock', env('weibo.mock', '0'));
  33. $uid = 0;
  34. $userInfoRes = (new WeiboService($uid))->userinfo($sub);
  35. if (empty($userInfoRes) || $userInfoRes['ok'] != 1) {
  36. $isLogin = 0;
  37. $this->assign('isLogin', $isLogin);
  38. $this->assign('user', '');
  39. $this->assign('token', '');
  40. $this->assign('isShare', '');
  41. $this->assign('count', '');
  42. $this->assign('isBeginActivity', '');
  43. $this->assign('task', '');
  44. $this->fetch();
  45. }
  46. // 使用客户端信息生成token
  47. $token = md5($_SERVER['HTTP_USER_AGENT'] . $_SERVER['HTTP_ACCEPT_ENCODING'] . $_SERVER['HTTP_ACCEPT_LANGUAGE']. get_client_ip(0) . $userInfoRes['data']['uid']);
  48. $user = $userInfoRes['data'];
  49. $userInfo = Db::table('awards_user_info')->where('uid', $user['uid'])->find();
  50. $count = 0;
  51. $isShare = 0;
  52. if (empty($userInfo)) {
  53. $userAttr = [
  54. 'uid' => $user['uid'],
  55. 'portrait' => $user['profile_image_url'],
  56. 'nickname' => $user['name'],
  57. 'is_share' => 0,
  58. 'count' => 3,
  59. 'create_at' => time()
  60. ];
  61. Db::table('awards_user_info')->insert($userAttr);
  62. } else {
  63. $count = Db::table('awards_user_task_log')->where('uid', $user['uid'])->count('id');
  64. $isShare = $userInfo['is_share'];
  65. }
  66. // 生成加密用的密钥和向量
  67. $cipher = "aes-256-gcm";
  68. $ivlen = openssl_cipher_iv_length($cipher);
  69. $iv = bin2hex(openssl_random_pseudo_bytes($ivlen));
  70. $aesKey = bin2hex(openssl_random_pseudo_bytes(32));
  71. // $user = array_merge($user, [
  72. // 'aes_key' => $aesKey,
  73. // 'ase_iv' => $iv,
  74. // ]);
  75. $cacheUser = [
  76. 'aes_key' => $aesKey,
  77. 'ase_iv' => $iv,
  78. 'uid' => $user['uid'],
  79. ];
  80. $activity = Db::table('awards_activity')->where('begin_at', '<=', time())
  81. ->where('end_at', '>=', time())->find();
  82. $isBeginActivity = 1;
  83. if (empty($activity)) {
  84. $isBeginActivity = 0;
  85. }
  86. $userInfo = [];
  87. if ($count > 0) {
  88. $userInfo = Db::table('awards_user_task_log')->alias('l')
  89. ->leftJoin('awards_user_info u', 'l.uid = u.uid')
  90. ->field(['u.nickname', 'u.portrait', 'u.uid', 'l.duration', 'l.number'])
  91. ->order('l.number', 'desc')
  92. ->order('l.duration', 'asc')
  93. ->find();
  94. }
  95. // 缓存用户信息1天
  96. Cache::set('u:' . $token, json_encode($cacheUser), 86400);
  97. $this->assign('user', json_encode($user));
  98. $this->assign('token', $token);
  99. $this->assign('isShare', $isShare);
  100. $this->assign('count', $count);
  101. $this->assign('isBeginActivity', $isBeginActivity);
  102. $this->assign('task', json_encode($userInfo));
  103. $this->assign('isLogin', $isLogin);
  104. $this->fetch();
  105. }
  106. }