ShareService.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. namespace app\common\service;
  3. use app\common\library\Redis;
  4. use app\common\model\ShareSource;
  5. use app\common\model\ShareUser;
  6. use app\main\constants\CampaignConstants;
  7. use app\main\service\FinancialService;
  8. use app\main\service\UserService;
  9. use think\Config;
  10. use think\Log;
  11. use think\Request;
  12. use think\Db;
  13. use think\Cache;
  14. /**
  15. * Class ShareService
  16. * @package app\common\service
  17. */
  18. class ShareService
  19. {
  20. public static $self;
  21. /**
  22. * @return ShareService
  23. */
  24. public static function instance()
  25. {
  26. if(self::$self == NULL){
  27. self::$self = new self();
  28. }
  29. return self::$self;
  30. }
  31. public function getShareUserModel(){
  32. return model('ShareUser');
  33. }
  34. /**
  35. * 获取分享页面的数据
  36. * @param $userId
  37. * @param $begin
  38. * @param $lenth
  39. * @return array
  40. */
  41. private $shareKey='SHARE';//弹幕redis
  42. private $shareList='SD:';
  43. private $shareBind='SBIND';
  44. public function getShareUserList( $userId,$begin,$lenth,$userName ){
  45. $data = [
  46. 'count'=>0,
  47. 'getKandian'=>0,
  48. 'totalKandian'=>0,
  49. 'list'=>[],
  50. ];
  51. $redisKey = $this->shareList.$userId;
  52. $redisData = Redis::instance()->hGetAll($redisKey);
  53. if ( $redisData == 'empty' ){//没有数据时
  54. return $data;
  55. }
  56. if( !$redisData ){
  57. $res = $this->getShareUserModel()->where('user_id',$userId);
  58. $data['count'] = $res->count(1);
  59. if ( $data['count'] < 1 ){
  60. Redis::instance()->set($redisKey,'empty');
  61. }else{
  62. $data['getKandian'] = $res->sum('kandian');
  63. $data['totalKandian'] = $data['count']*200;//默认书币是200
  64. Redis::instance()->hMSet($redisKey,$data);
  65. Redis::instance()->expire($redisKey,3600*24);
  66. Redis::instance()->sadd($this->shareKey.$userId,$userName.'邀请了'.$data['count'].'人,收到'.$data['totalKandian'].'书币');
  67. }
  68. }else{
  69. $data = $redisData;
  70. }
  71. if ( $data['count'] > 0 ){
  72. $data['list'] = $this->getShareUserModel()->limit($begin,$lenth)->select();
  73. }
  74. return $data;
  75. }
  76. public function userList( $userId, $pageNum, $lenNum ){
  77. $begin = $pageNum*$lenNum;
  78. $re = $this->getShareUserModel()->where('user_id',$userId)->limit($begin,$lenNum)->select();
  79. return $re;
  80. }
  81. /**
  82. * 点击分享链接,
  83. * @param $userId 被分享者
  84. * @param $fromUserId 分享者
  85. * @return int
  86. */
  87. public function ClickShareLink($userId, $fromUserId, $shareTime){
  88. $time = time();
  89. $userSource = [
  90. 'user_id'=>$fromUserId,
  91. 'to_user_id'=>$userId
  92. ];
  93. if( $this->checkBind($userId,$fromUserId) || (time()-$shareTime)>3600*24){
  94. $this->updateUserSourceTime($fromUserId,$userId,$shareTime);
  95. LogService::info('分享活动-绑定成功01:'.json_encode($userSource).'分享时间:'.$shareTime);
  96. return 2;
  97. }
  98. $re = 0;
  99. try{
  100. //再次检查数据库里是否存在
  101. $re = $this->getShareUserModel()->where($userSource)->count(1);
  102. if ( $re == 0 ){//是第一次点看链接
  103. //回写分享者的数据
  104. $shareUser = [
  105. 'user_id'=>$fromUserId,
  106. 'to_user_id'=>$userId,
  107. 'to_user_avatar'=>'/assets/img/frontend/share/default_people.png',
  108. 'to_user_nickname'=>'书友',
  109. 'share_time'=>$shareTime,
  110. 'created_at'=>date('Y-m-d H:i:s',$time),
  111. 'updated_at'=>date('Y-m-d H:i:s',$time)
  112. ];
  113. $this->getShareUserModel()->save($shareUser);
  114. //将绑定关系存在redis里面
  115. Redis::instance()->sadd($this->shareBind.$userId, $fromUserId);
  116. Redis::instance()->expire($this->shareBind.$userId, 3600*24);
  117. //清除统计redis
  118. Redis::instance()->del($this->shareList.$fromUserId);
  119. }else{
  120. $this->updateUserSourceTime($fromUserId,$userId,$shareTime);
  121. }
  122. }catch(\Throwable $th){
  123. LogService::error('分享活动-绑定error01'.json_encode($userSource).'分享时间:'.$shareTime);
  124. }
  125. return $re;
  126. }
  127. /**
  128. * 关注时回写分享者数据
  129. * @param $userId
  130. * @param $fromUserId
  131. * @return mixed
  132. */
  133. public function attentionUser($toUserId,$updateData){
  134. LogService::info('分享活动begin');
  135. $isOpenShare = Config::get('site.is_open_share');
  136. if (!$isOpenShare){
  137. return false;
  138. }
  139. //获取最早的链接分享者
  140. try{
  141. $userSource = [
  142. 'to_user_id'=>$toUserId,
  143. ];
  144. $users = $this->getShareUserModel()
  145. ->where($userSource)
  146. ->where('share_time','>',time()-3600*24)
  147. ->order('id asc')->limit(1)->find();
  148. if (!empty($users)){
  149. $id = $users['id'];
  150. $userId = $users['user_id'];
  151. $update = [
  152. 'is_fan'=>1,
  153. 'kandian'=>200,
  154. 'to_user_nickname'=>$updateData['nickname'],
  155. 'to_user_avatar'=>$updateData['headimgurl'],
  156. ];
  157. $re = $this->getShareUserModel()->where(['id'=>$id])->update($update);
  158. if ($re){
  159. FinancialService::instance()->modifyUserKandian( $userId, 1, 1,0,200,'分享链接赠送',2);
  160. //清除统计redis
  161. Redis::instance()->del($this->shareList.$userId);
  162. }
  163. LogService::info('分享活动-更新数据成功 fromUserId:'.$userId.'res'.$re);
  164. }
  165. }catch ( \Throwable $th ){
  166. LogService::error('分享活动-关注error02'.$th->getMessage());
  167. }
  168. return ;
  169. }
  170. /**
  171. * 检查两个用户是否绑定
  172. * @param $userId
  173. * @param $fromUserId
  174. * @return bool
  175. */
  176. public function checkBind($userId, $fromUserId){
  177. $redisKey = $this->shareBind.$userId;
  178. $bind = Redis::instance()->SISMEMBER($redisKey,$fromUserId);
  179. return $bind;
  180. }
  181. /**
  182. *重新邀请时,更新分享时间
  183. */
  184. public function updateUserSourceTime($userId,$toUserId,$shareTime){
  185. $re = $this->getShareUserModel()
  186. ->where(['user_id'=>$userId])
  187. ->where(['to_user_id'=>$toUserId])
  188. ->update(['share_time'=>$shareTime]);
  189. return $re;
  190. }
  191. /**
  192. * 弹幕
  193. * @return array|mixed
  194. */
  195. public function getShareBarrage($userId){
  196. $key = $this->shareKey.$userId;
  197. $data = Cache::get($key);
  198. if ( $data == false ){
  199. $data = [];
  200. $nameInit = ['果果吧','我本善良','Bear.zheng','书友','幸福','可乐要加冰块','金芳','有些梦,忘了追','杨洪旭'];
  201. $len = Redis::instance()->sCard($key);
  202. if ( $len >= 20 ){
  203. $data = Redis::instance()->spop($key, 10);
  204. }else{
  205. $data = Redis::instance()->SRANDMEMBER($key, 10);
  206. }
  207. //不够十个填充
  208. $diff = 10 - count($data);
  209. while ( $diff ){
  210. $data[] = array_pop($nameInit).'邀请了1位好友,收到200书币';
  211. $diff--;
  212. }
  213. Cache::set( $key, $data, 3600);
  214. }
  215. return $data;
  216. }
  217. }