Index.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use fast\Random;
  5. use think\Collection;
  6. use \think\Log;
  7. use Redis;
  8. use app\common\library\Sms as Smslib;
  9. use app\common\service\TenimService;
  10. /**
  11. * 首页接口
  12. */
  13. class Index extends Api
  14. {
  15. protected $noNeedLogin = ['index','contactus','tcpTest','getAppShare','getWebsiteInfo','getUserCharmRankList',
  16. 'getPartyHotList','searchUsers','getInviteCode','getEdition','getInviteImg','getWebsiteInfoForMini','getBankList',
  17. 'getSwitch','ageList','starList','bannerList'];
  18. protected $noNeedRight = ['*'];
  19. /**
  20. * 首页
  21. *
  22. */
  23. public function index()
  24. {
  25. // 强制关闭需要退出正在房间的用户
  26. $tenim = new \app\api\controller\Tenim();
  27. $tenim->outMemberFromRoom(4);
  28. $this->success('请求成功');
  29. }
  30. // /**
  31. // * 生成不重复的随机数字字母组合
  32. // */
  33. // function getUinqueNo($length = 8) {
  34. // $newid = Random::build("alnum",$length);
  35. //// if(in_array($newid,$nos)) {
  36. //// $this->getUinqueNo(8);
  37. //// }
  38. // return $newid;
  39. // }
  40. /**
  41. * 获取用户协议等
  42. */
  43. public function getWebsiteInfo() {
  44. $params = $this->request->request("params"); //内容
  45. if($params == "boxGiftLogo") {
  46. echo $this->success("获取成功!",$this->httpurl(config("site.".$params)));
  47. exit;
  48. }
  49. echo "<html><body>";
  50. echo config("site.".$params);
  51. echo "</body></html>";exit;
  52. }
  53. /**
  54. * 获取用户协议等 小程序
  55. */
  56. public function getWebsiteInfoForMini() {
  57. $params = $this->request->request("params"); //内容
  58. $res = config("site.".$params);
  59. if($params == "boxGiftLogo") $res = $this->httpurl(config("site.".$params));
  60. $this->success("获取成功!",$res);
  61. }
  62. /**
  63. * 联系我们
  64. */
  65. public function contactus() {
  66. $list = \app\common\model\Config::where(["group"=>"basic","name"=>["in",["email","mobile"]]])->select();
  67. $data = [];
  68. foreach($list as $k => $v) {
  69. $v["name"] == "email" && $data["email"] = $v["value"];
  70. $v["name"] == "mobile" && $data["mobile"] = $v["value"];
  71. }
  72. $this->success("获取成功!",$data);
  73. }
  74. /*
  75. * 获取系统消息列表
  76. */
  77. public function getMessage() {
  78. $page = $this->request->request('page',1); // 分页
  79. $pageNum = $this->request->request('pageNum',10); // 分页
  80. // 分页搜索构建
  81. $pageStart = ($page-1)*$pageNum;
  82. $flag = $this->request->request("flag",1,"intval"); //标识:1=只取一条,0=全部
  83. $user_id = $this->auth->id;
  84. $obj = \app\common\model\Message::where(["user_id"=>$user_id])->order("createtime","desc")->limit($pageStart,$pageNum);
  85. if($flag == 1) {
  86. $list = $obj->find();
  87. $list || $list = [];
  88. $list && $list["createtime"] = $this->get_last_time($list["createtime"]);
  89. } else {
  90. $list = $obj->select();
  91. if($list) foreach($list as $k => &$v) {
  92. $v["createtime"] = $this->get_last_time($v["createtime"]);
  93. }
  94. }
  95. $this->success("获取成功!",$list);
  96. }
  97. /**
  98. * 删除系统消息
  99. */
  100. public function delMessage() {
  101. $id = $this->request->request("id",0,"intval"); //消息ID
  102. if($id <= 0) {
  103. $this->error("参数传入错误!");
  104. }
  105. $res = \app\common\model\Message::where(["id"=>$id,"user_id"=>$this->auth->id])->delete();
  106. if($res) {
  107. $this->success("删除成功!");
  108. } else {
  109. $this->error("删除失败!");
  110. }
  111. }
  112. /**
  113. * 获取主播魅力值排行
  114. */
  115. public function getUserCharmRankList() {
  116. $time = $this->request->request("time",0,"intval"); //时间筛选 1=小时榜,2=今日榜,3=本周榜,4=月榜
  117. if(!in_array($time,[1,2,3,4])) {
  118. $this->error("参数传入错误!");
  119. }
  120. $hour = strtotime(date("Y-m-d H:00:00"));
  121. $today = strtotime(date("Y-m-d 00:00:00"));
  122. $weekend = strtotime('monday this week');
  123. // $weekend = mktime(0, 0 , 0,date("m"),date("d")-date("w")+1,date("Y"));
  124. $month = strtotime(date("Y-m-01 00:00:00"));
  125. // 剩余时间
  126. $thistime = time();
  127. switch ($time) {
  128. case 1:
  129. $redtime = 3600-($thistime - $hour);
  130. break;
  131. case 2:
  132. $redtime = 3600*24-($thistime - $today);
  133. break;
  134. case 3:
  135. $redtime = 3600*24*7-($thistime - $weekend);
  136. break;
  137. case 4:
  138. $monthend = mktime(23,59,59,date("m"),date("t"),date("Y"));
  139. $redtime = ($monthend - $month)-($thistime - $month);
  140. break;
  141. }
  142. $timeArr = [1=>$hour,2=>$today,3=>$weekend,4=>$month];
  143. $where = [];
  144. $where["a.createtime"] = ["gt",$timeArr[$time]];
  145. $list = \app\common\model\UserCharmRank::alias("a")
  146. ->field("p.id,sum(a.charm) as charm,u.avatar,u.nickname,u.gender,u.level,u.is_live")
  147. ->where($where)
  148. ->join("hx_user u","u.id = a.user_id")
  149. ->join("hx_party p","p.user_id = a.user_id")
  150. ->group("a.user_id")
  151. ->order("charm","desc")
  152. ->limit(100)
  153. ->select();
  154. $data = [];
  155. $data["redtime"] = $redtime;
  156. $data["data"] = $list;
  157. $this->success("获取成功!",$data);
  158. }
  159. /**
  160. * 获取派对热度排序
  161. */
  162. public function getPartyHotList() {
  163. // 剩余时间
  164. $thistime = time();
  165. $hour = strtotime(date("Y-m-d H:00:00"));
  166. $redtime = 3600-($thistime - $hour);
  167. $where = [];
  168. $where["a.createtime"] = ["gt",$hour];
  169. $where["p.room_type"] = 1;
  170. $list = \app\common\model\PartyHot::alias("a")
  171. ->field("sum(a.hot) as hot,p.id,u.avatar,p.party_name,p.party_logo")
  172. ->where($where)
  173. ->join("hx_party p","p.id = a.party_id")
  174. ->join("hx_user u","u.id = p.user_id")
  175. ->group("a.party_id")
  176. ->order("hot","desc")
  177. ->limit(100)
  178. ->select();
  179. $data = [];
  180. $data["redtime"] = $redtime;
  181. $data["data"] = $list;
  182. $this->success("获取成功!",$data);
  183. }
  184. /**
  185. * 首页搜索
  186. */
  187. public function searchUsers() {
  188. $search = $this->request->request("search"); //关键词筛选
  189. if(!$search) {
  190. $this->error("请输入搜索内容!");
  191. }
  192. // 搜索派对
  193. global $whereOr;
  194. $where = [];
  195. $whereOr["party_id"] = $search;
  196. $whereOr["party_name"] = ["like","%$search%"];
  197. $where["room_type"] = 1;
  198. $partyList = \app\common\model\Party::field("id,party_logo,party_id,party_name")
  199. ->where($where)
  200. ->where(function ($query) {
  201. global $whereOr;
  202. $query->whereOr($whereOr);
  203. })->order("party_hot","desc")->select();
  204. // 搜索直播间
  205. global $whereOrlive;
  206. $where = [];
  207. $whereOrlive["party_id"] = $search;
  208. $whereOrlive["party_name"] = ["like","%$search%"];
  209. $where["room_type"] = 2;
  210. $liveList = \app\common\model\Party::field("id,party_logo,party_id,party_name")
  211. ->where($where)
  212. ->where(function ($query) {
  213. global $whereOrlive;
  214. $query->whereOr($whereOrlive);
  215. })
  216. ->order("party_hot","desc")->select();
  217. // 相关用户
  218. $where = [];
  219. $where["a.nickname"] = ["like","%$search%"];
  220. $where["a.u_id"] = $search;
  221. $userList = \app\common\model\User::alias("a")->field("id,avatar,nickname,u_id,f.fans")
  222. ->join("hx_view_fans f","f.user_id = a.id","left")
  223. ->order("a.is_online,a.noble,a.level")
  224. ->whereOr($where)->select();
  225. $res = [];
  226. $res["partylist"] = $partyList;
  227. $res["livelist"] = $liveList;
  228. $res["userlist"] = $userList;
  229. $this->success("获取成功!",$res);
  230. }
  231. /**
  232. * 首页搜索
  233. */
  234. public function searchList() {
  235. $search = $this->request->request("search"); //关键词筛选
  236. if(!$search) {
  237. $this->error("请输入搜索内容!");
  238. }
  239. // 搜索房间(派对和直播)
  240. global $whereOr;
  241. $where = [];
  242. $whereOr["party_id"] = $search;
  243. $whereOr["party_name"] = ["like","%$search%"];
  244. //$where['is_online'] = 1;
  245. $where['status'] = 1;
  246. $partyList = \app\common\model\Party::field("id,party_logo,party_id,party_name")
  247. ->where($where)
  248. ->where(function ($query) {
  249. global $whereOr;
  250. $query->whereOr($whereOr);
  251. })->order("party_hot","desc")->select();
  252. $partyList = collection($partyList)->toArray();
  253. if (!empty($partyList)) {//在线信息
  254. $partyListIds = array_column($partyList,'id');
  255. $tenimService = new TenimService();
  256. $partyParams = ['party_ids'=>$partyListIds];
  257. $partyRes = $tenimService->getRoomUser($partyParams);
  258. if ($partyRes['status'] == 1) {
  259. foreach ($partyList as $pKey => &$pVal) {
  260. if (isset($partyRes['data'][$pVal['id']])) {
  261. $pVal['member_list'] = $partyRes['data'][$pVal['id']]['member_list'];
  262. $pVal['online_num'] = $partyRes['data'][$pVal['id']]['online_num'];
  263. }
  264. }
  265. }
  266. }
  267. // 家族
  268. $guildWhere['status'] = 1;
  269. $guildWhere['name'] = ["like","%$search%"];
  270. $guildList = model('Guild')->where($guildWhere)->with(['guildmember'=>function($gquery){
  271. $gquery->where(['status'=>1]);
  272. }])->field('id,g_id,name,image')->select();
  273. if (!empty($guildList)) {
  274. $userField = 'id,nickname,avatar';
  275. foreach ($guildList as $gKey => &$gVal) {
  276. $userData =[];
  277. if (isset($gVal['guildmember'])) {
  278. $memberUids = array_column($gVal['guildmember'],'user_id');
  279. $userWhere['is_online'] = 1;
  280. $userWhere['id'] = ['in',$memberUids];
  281. $userData = model('User')->field($userField)->where($userWhere)->select();
  282. }
  283. $gVal['member_list'] = $userData;
  284. $gVal['online_num'] = count($userData);
  285. unset($gVal['guildmember']);
  286. }
  287. }
  288. // 相关用户
  289. $where = [];
  290. $where["a.nickname"] = ["like","%$search%"];
  291. $where["a.u_id"] = $search;
  292. $userList = \app\common\model\User::alias("a")->field("id,avatar,nickname,u_id,f.fans")
  293. ->join("hx_view_fans f","f.user_id = a.id","left")
  294. ->order("a.is_online,a.noble,a.level")
  295. ->whereOr($where)->select();
  296. $res = [];
  297. $res["roomlist"] = $partyList;
  298. $res["guildlist"] = $guildList;
  299. $res["userlist"] = $userList;
  300. $this->success("获取成功!",$res);
  301. }
  302. /**
  303. * 获取下载二维码和邀请码
  304. */
  305. public function getInviteCode() {
  306. // 获取二维码
  307. $host = $_SERVER["REQUEST_SCHEME"]."://".$_SERVER["HTTP_HOST"];
  308. $qrcode = $this->httpurl($host.config("site.qrcode"));
  309. $miniqrcode = $this->httpurl($host.config("site.miniqrcode"));
  310. // 获取用户邀请码
  311. $inviteCode = \app\common\model\User::where(["id"=>$this->auth->id])->value("invite_no");
  312. $downlowdLink = $host."/index/index/appJump?t=wlt".rand(10,999).Random::alpha(8);
  313. $this->success("获取成功!",["qrcode"=>$qrcode,"miniqrcode"=>$miniqrcode,"inviteCode"=>$inviteCode,"downlowdLink"=>$downlowdLink]);
  314. }
  315. /**
  316. * 获取app分享海报
  317. */
  318. public function getAppShare() {
  319. $this->success("获取成功!",["url"=>$this->httpurl(config("site.appShare"))]);
  320. }
  321. /**
  322. * 获取版本更新信息
  323. */
  324. public function getEdition() {
  325. // 获取二维码
  326. $is_force = config("site.is_force");
  327. $apkUrl = config("site.apkUrl");
  328. $apkName = config("site.apkName");
  329. $desc = config("site.desc");
  330. $versionCode = config("site.versionCode");
  331. $this->success("获取成功!",["versionCode"=>$versionCode,"isForceUpdate"=>$is_force,"apkUrl"=>$apkUrl,"apkName"=>$apkName,"desc"=>$desc]);
  332. }
  333. /**
  334. * 获取邀请图片
  335. */
  336. public function getInviteImg() {
  337. $plat = $this->request->request("plat",1); //平台:1=小程序,2=app
  338. if(!in_array($plat,[1,2])) $this->error("参数错误");
  339. // 获取用户的邀请码
  340. $invitecode = \app\common\model\User::where(["id"=>$this->auth->id])->value("invite_no");
  341. // 文字图片合成
  342. $bigImgPath = $this->httpurlLocal('/assets/img/inviteimg.jpeg');
  343. $img = imagecreatefromstring(file_get_contents($bigImgPath));
  344. //字体文件
  345. $font = realpath('./assets/fonts/lato/lato-black.ttf');
  346. //字体颜色(RGB)
  347. $black = imagecolorallocate($img, 217, 76, 41);
  348. //字体大小
  349. $fontSize = 30;
  350. //旋转角度
  351. $circleSize = 0;
  352. //左边距
  353. $left = 275;
  354. //上边距
  355. $top = 540;
  356. imagefttext($img, $fontSize, $circleSize, $left, $top, $black, $font, $invitecode);
  357. $filename = date("YmdH").".jpeg";
  358. $path = "/uploads/qrcode/".$filename;
  359. $file = $_SERVER['DOCUMENT_ROOT'] . $path;//打开文件准备写入
  360. list($bgWidth, $bgHight, $bgType) = getimagesize($bigImgPath);
  361. switch ($bgType) {
  362. case 1://gif
  363. header('Content-Type:image/gif');
  364. imagegif($img,$file);
  365. break;
  366. case 2://jpg
  367. header('Content-Type:image/jpg');
  368. imagejpeg($img,$file);
  369. break;
  370. case 3://jpg
  371. header('Content-Type:image/png');
  372. imagepng($img,$file);
  373. break;
  374. default:
  375. break;
  376. }
  377. //销毁照片
  378. imagedestroy($img);
  379. // 图片和二维码合成
  380. $qrcode = $plat == 1 ? config("site.miniqrcode"):config("site.qrcode");
  381. $background = $file;
  382. $target = $this->httpurl($qrcode);
  383. $background_iamge = imagecreatefromstring(file_get_contents($background));
  384. $target_image = imagecreatefromstring(file_get_contents($target));
  385. list($target_width, $target_height, $target_type) = getimagesize($target);
  386. imagecopymerge($background_iamge , $target_image , 250, 700, 0, 0, $target_width, $target_height, 100);
  387. list($background_width, $background_height, $background_type) = getimagesize($background);
  388. switch ($background_type) {
  389. case 1://gif
  390. header('Content-Type:image/gif');
  391. imagegif($background_iamge,$file);
  392. break;
  393. case 2://jpg
  394. header('Content-Type:image/jpg');
  395. imagejpeg($background_iamge,$file);
  396. break;
  397. case 3://jpg
  398. header('Content-Type:image/png');
  399. imagepng($background_iamge,$file);
  400. break;
  401. default:
  402. break;
  403. }
  404. $savepath = $this->httpurlLocal($path);
  405. $this->success("获取成功!",$savepath);
  406. }
  407. /**
  408. * 获取银行列表
  409. */
  410. public function getBankList() {
  411. $this->success("获取成功!",["banklist"=>\app\common\model\Bank::select()]);
  412. }
  413. /**
  414. * 获取开关配置
  415. */
  416. public function getSwitch() {
  417. $this->success("获取成功!",["switch"=>config("site.switch")]);
  418. }
  419. /**
  420. * 评论时间转换
  421. * @param null $time
  422. * @return false|string
  423. */
  424. private function get_last_time($time = NULL) {
  425. $text = '';
  426. $time = $time === NULL || $time > time() ? time() : intval($time);
  427. $t = time() - $time; //时间差 (秒)
  428. $y = date('Y', $time)-date('Y', time());//是否跨年
  429. switch($t){
  430. case $t == 0:
  431. $text = '刚刚';
  432. break;
  433. case $t < 60:
  434. $text = $t . '秒前'; // 一分钟内
  435. break;
  436. case $t < 60 * 60:
  437. $text = floor($t / 60) . '分钟前'; //一小时内
  438. break;
  439. case $t < 60 * 60 * 24:
  440. $text = floor($t / (60 * 60)) . '小时前'; // 一天内
  441. break;
  442. case $t < 60 * 60 * 24 * 3:
  443. $text = floor($time/(60*60*24)) ==1 ?'昨天 ' . date('H:i', $time) : '前天 ' . date('H:i', $time) ; //昨天和前天
  444. break;
  445. case $t < 60 * 60 * 24 * 30:
  446. $text = date('m月d日 H:i', $time); //一个月内
  447. break;
  448. case $t < 60 * 60 * 24 * 365&&$y==0:
  449. $text = date('m月d日', $time); //一年内
  450. break;
  451. default:
  452. $text = date('Y年m月d日', $time); //一年以前
  453. break;
  454. }
  455. return $text;
  456. }
  457. /**
  458. * 年龄段列表
  459. * @return void
  460. */
  461. public function ageList()
  462. {
  463. try {
  464. $field = 'id,name';
  465. $where['status'] = 1;
  466. $result = model('Age')->field($field)->where($where)->order('weigh asc')->select();
  467. $this->success('获取成功',$result);
  468. } catch (Exception $e) {
  469. $this->error($e->getMessage());
  470. }
  471. }
  472. /**
  473. * 星座列表
  474. * @return void
  475. */
  476. public function starList()
  477. {
  478. try {
  479. $field = 'id,name,image';
  480. $where['status'] = 1;
  481. $result = model('Constellation')->field($field)->where($where)->order('weigh asc')->select();
  482. if (!$result) {
  483. $result = list_domain_image($result,['image']);
  484. }
  485. $this->success('获取成功',$result);
  486. } catch (Exception $e) {
  487. $this->error($e->getMessage());
  488. }
  489. }
  490. /**
  491. * 轮播图列表
  492. * @return void
  493. */
  494. public function bannerList()
  495. {
  496. try {
  497. $field = 'id,title,image';
  498. $where['status'] = 1;
  499. $result = model('Banner')->field($field)->where($where)->order('weigh asc')->select();
  500. if (!$result) {
  501. $result = list_domain_image($result,['image']);
  502. }
  503. $this->success('获取成功',$result);
  504. } catch (Exception $e) {
  505. $this->error($e->getMessage());
  506. }
  507. }
  508. }