Sign.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 https://www.sapixx.com All rights reserved.
  4. * @license Licensed (http://www.apache.org/licenses/LICENSE-2.0).
  5. * @author pillar<ltmn@qq.com>
  6. * 招募
  7. */
  8. namespace app\green\controller\api\v1;
  9. use app\common\controller\Api;
  10. use app\green\model\GreenSign;
  11. use app\green\model\GreenSignConfig;
  12. use app\green\model\GreenUser;
  13. use think\helper\Time;
  14. class Sign extends Api {
  15. /**
  16. * 签到数据
  17. */
  18. public function index(){
  19. $param['sign'] = $this->request->param('sign');
  20. $rel = $this->apiSign($param);
  21. if($rel['code'] != 200){
  22. return enjson(500,'签名验证失败');
  23. }
  24. list($start, $end) = Time::today();
  25. $info = GreenSign::where(['member_miniapp_id' => $this->miniapp_id,'uid' => $this->user->id])->where('signtime','>',$start)->where('signtime','<',$end)->find();
  26. if(empty($info)){
  27. list($start, $end) = Time::yesterday();
  28. $info = GreenSign::where(['member_miniapp_id' => $this->miniapp_id,'uid' => $this->user->id])->where('signtime','>',$start)->where('signtime','<',$end)->find();
  29. if(empty($info)){
  30. return enjson(204,'empty');
  31. }else{
  32. return enjson(202,'success', $info);
  33. }
  34. }
  35. return enjson(200,'success', $info);
  36. }
  37. /**
  38. * @return \think\response\Json
  39. * 签到
  40. */
  41. public function add(){
  42. $this->isUserAuth();
  43. $param['sign'] = $this->request->param('sign');
  44. $rel = $this->apiSign($param);
  45. if($rel['code'] != 200){
  46. return enjson($rel['code'],'签名验证失败');
  47. }
  48. list($start, $end) = Time::today();
  49. $today = GreenSign::where(['member_miniapp_id' => $this->miniapp_id,'uid' => $this->user->id])->where('signtime','>',$start)->where('signtime','<',$end)->find();
  50. if($today){
  51. return enjson(0,'已签到');
  52. }
  53. list($start, $end) = Time::yesterday();
  54. $info = GreenSign::where(['member_miniapp_id' => $this->miniapp_id,'uid' => $this->user->id])->where('signtime','>',$start)->where('signtime','<',$end)->find();
  55. $greenUser = GreenUser::where(['member_miniapp_id' => $this->miniapp_id,'uid' => $this->user->id])->find();
  56. if($info){
  57. $days = $info->days+1;
  58. $config = GreenSignConfig::where(['member_miniapp_id' => $this->miniapp_id,'config_id' => $days])->find();
  59. if($config){
  60. $sign = new GreenSign;
  61. $sign->member_miniapp_id = $this->miniapp_id;
  62. $sign->uid = $this->user->id;
  63. $sign->startime = $info->startime;
  64. $sign->signtime = time();
  65. $sign->days = $days;
  66. $sign->points = $config->point;
  67. $sign->save();
  68. if($greenUser){
  69. GreenUser::where(['id' => $greenUser->id])->update(['points' => $greenUser->points + $config->point,'update_time' => time()]);
  70. }else{
  71. GreenUser::create(['member_miniapp_id' =>$this->miniapp_id,'uid' => $this->user->id,'points' =>$config->point,'weight' => 0,'create_time' => time(),'update_time' => time()]);
  72. }
  73. }else{
  74. $config = GreenSignConfig::where(['member_miniapp_id' => $this->miniapp_id,'config_id' => 1])->find();
  75. if($config){
  76. $info = new GreenSign;
  77. $info->member_miniapp_id = $this->miniapp_id;
  78. $info->uid = $this->user->id;
  79. $info->startime = time();
  80. $info->signtime = time();
  81. $info->days = 1;
  82. $info->points = $config->point;
  83. $info->save();
  84. if($greenUser){
  85. GreenUser::where(['id' => $greenUser->id])->update(['points' => $greenUser->points + $config->point,'update_time' => time()]);
  86. }else{
  87. GreenUser::create(['member_miniapp_id' =>$this->miniapp_id,'uid' => $this->user->id,'weight' => 0,'points' =>$config->point,'create_time' => time(),'update_time' => time()]);
  88. }
  89. }
  90. }
  91. }else{
  92. $config = GreenSignConfig::where(['member_miniapp_id' => $this->miniapp_id,'config_id' => 1])->find();
  93. if($config){
  94. $info = new GreenSign;
  95. $info->member_miniapp_id = $this->miniapp_id;
  96. $info->uid = $this->user->id;
  97. $info->startime = time();
  98. $info->signtime = time();
  99. $info->days = 1;
  100. $info->points = $config->point;
  101. $info->save();
  102. if($greenUser){
  103. GreenUser::where(['id' => $greenUser->id])->update(['points' => $greenUser->points + $config->point,'update_time' => time()]);
  104. }else{
  105. GreenUser::create(['member_miniapp_id' =>$this->miniapp_id,'uid' => $this->user->id,'weight' => 0,'points' =>$config->point,'create_time' => time(),'update_time' => time()]);
  106. }
  107. }
  108. }
  109. if($info){
  110. return enjson(200,'成功',$info);
  111. }else{
  112. return enjson(0,'失败');
  113. }
  114. }
  115. }