Video.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. if (!empty($_COOKIE['SUB']) && strpos($_SERVER["HTTP_USER_AGENT"],"Weibo")) {
  20. $sub = $_COOKIE['SUB'];
  21. $isWebo = 1;
  22. } else {
  23. $isWebo = 0;
  24. // 只在调试模式下开启从POST参数中获取UID,方便测试联调
  25. if (env('weibo.mock')) {
  26. $sub = Request::get('cookie', "");
  27. }
  28. }
  29. $uid = 0;
  30. $userInfoRes = (new WeiboService($uid))->userinfo($sub);
  31. if (empty($userInfoRes) || $userInfoRes['ok'] != 1) {
  32. $this->fetch('404');
  33. }
  34. // 使用客户端信息生成token
  35. $token = md5($_SERVER['HTTP_USER_AGENT'] . $_SERVER['HTTP_ACCEPT_ENCODING'] . $_SERVER['HTTP_ACCEPT_LANGUAGE']. get_client_ip(0) . $userInfoRes['data']['uid']);
  36. $user = $userInfoRes['data'];
  37. $userInfo = Db::table('awards_user_info')->where('uid', $user['uid'])->find();
  38. $count = 0;
  39. $isShare = 0;
  40. if (empty($userInfo)) {
  41. $userAttr = [
  42. 'uid' => $user['uid'],
  43. 'portrait' => $user['profile_image_url'],
  44. 'nickname' => $user['name'],
  45. 'is_share' => 0,
  46. 'count' => 1,
  47. 'create_at' => time()
  48. ];
  49. if (0 == Db::table('awards_user_info')->insert($userAttr)) {
  50. $this->fetch('404');
  51. }
  52. } else {
  53. $count = Db::table('awards_user_task_log')->where('uid', $user['uid'])->count('id');
  54. $isShare = $userInfo['is_share'];
  55. }
  56. // 生成加密用的密钥和向量
  57. $cipher = "aes-256-gcm";
  58. $ivlen = openssl_cipher_iv_length($cipher);
  59. $iv = bin2hex(openssl_random_pseudo_bytes($ivlen));
  60. $aesKey = bin2hex(openssl_random_pseudo_bytes(32));
  61. // $user = array_merge($user, [
  62. // 'aes_key' => $aesKey,
  63. // 'ase_iv' => $iv,
  64. // ]);
  65. $cacheUser = [
  66. 'aes_key' => $aesKey,
  67. 'ase_iv' => $iv,
  68. 'uid' => $user['uid'],
  69. ];
  70. $activity = Db::table('awards_activity')->where('begin_at', '<=', time())
  71. ->where('end_at', '>=', time())->find();
  72. $isBeginActivity = 1;
  73. if (empty($activity)) {
  74. $isBeginActivity = 0;
  75. }
  76. $userInfo = [];
  77. if ($count > 0) {
  78. $userInfo = Db::table('awards_user_task_log')->alias('l')
  79. ->leftJoin('awards_user_info u', 'l.uid = u.uid')
  80. ->field(['u.nickname', 'u.portrait', 'u.uid', 'l.duration', 'l.number'])
  81. ->order('l.number', 'desc')
  82. ->order('l.duration', 'asc')
  83. ->find();
  84. }
  85. // 缓存用户信息1天
  86. Cache::set('u:' . $token, json_encode($cacheUser), 86400);
  87. $this->assign('user', json_encode($user));
  88. $this->assign('token', $token);
  89. $this->assign('isShare', $isShare);
  90. $this->assign('count', $count);
  91. $this->assign('isBeginActivity', $isBeginActivity);
  92. $this->assign('task', json_encode($userInfo));
  93. $this->assign('sub', $sub);
  94. $this->assign('isWeibo', $isWebo);
  95. $this->assign('mock', env('weibo.mock', '0'));
  96. $this->fetch();
  97. }
  98. }