Checkin.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Elton
  5. * Date: 2019/4/28
  6. * Time: 18:17
  7. */
  8. namespace app\clientwebapi\controller;
  9. use app\common\controller\ClientWebApi;
  10. use app\main\service\LogService;
  11. use app\main\service\UserService;
  12. use think\Cookie;
  13. class Checkin extends ClientWebApi
  14. {
  15. //加入书架
  16. public function bookshelf()
  17. {
  18. LogService::info('[ Checkin ] 全部加入书架');
  19. //获取当前登录用户ID
  20. $uid =Cookie::get('user_id');
  21. //如果用户UID为空,不做处理
  22. if(empty($uid)){
  23. $this->error('请先登录');
  24. }
  25. if($this->request->isPost()){
  26. $bookshelf = $this->request->param('bookshelf');
  27. LogService::info('[ Checkin ] bookshelf:'.$bookshelf);
  28. $bookshelf = json_decode($bookshelf, true);
  29. $data = [];
  30. foreach ($bookshelf as $k => $v){
  31. //保存到书架即保存到最近阅读记录
  32. $data[] = [
  33. 'user_id' => $uid,
  34. 'book_id' => $v['book_id'],
  35. 'chapter_id' => $v['first_chapter_id'],
  36. 'chapter_name' => $v['first_chapter_name']
  37. ];
  38. }
  39. if($data){
  40. model('UserRecentlyRead')->setConnect($uid)->saveAll($data);
  41. }
  42. LogService::info('[ Checkin ] 全部加入书架 成功');
  43. $this->success('成功');
  44. }
  45. }
  46. // APP 签到
  47. public function appSign()
  48. {
  49. LogService::info('[ Checkin ] 执行签到');
  50. //判断用户是否已经登录
  51. $uid =Cookie::get('user_id');
  52. if(empty($uid)){
  53. $this->error('请先登录');
  54. }
  55. //判断今天是否已经签到
  56. //$todayDate = Date('Ymd',time());
  57. $isSign = $this->isSign();
  58. if($isSign){
  59. $this->error('今日已经签到了,不用重复签到');
  60. }
  61. //没有签到,执行签到 | Sign表中写数据 | 签到送积分 | 修改今日签到的标记
  62. UserService::instance()->getSignModel()->setConnect($uid)->writeSignLog();
  63. LogService::info('[ Checkin ] 签到完成');
  64. }
  65. /**
  66. * 判断当前用户今日有没有签到
  67. * @return bool
  68. */
  69. public function isSign(){
  70. $uid = Cookie::get('user_id');
  71. $todayDate = Date('Ymd',time());
  72. if(Cookie::get('sign'.$todayDate)=='1'){
  73. return true;
  74. }else{
  75. $isSign = model('Sign')->setConnect($uid)->where(['uid'=>$uid,'createdate'=>$todayDate])->find();
  76. if(empty($isSign)){
  77. return false;
  78. }else{
  79. Cookie::set('sign'.$todayDate,'1',86400); //已经签到了,存cookie里
  80. return true;
  81. }
  82. }
  83. }
  84. }