UserSignService.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. <?php
  2. namespace App\Modules\User\Services;
  3. use App\Modules\Activity\Services\ActivitySwitchService;
  4. use App\Modules\Subscribe\Models\Order;
  5. use App\Modules\User\Models\User;
  6. use App\Modules\User\Models\UserSign;
  7. use App\Modules\OfficialAccount\Models\ForceSubscribeUsers;
  8. use Hashids;
  9. use App\Modules\Book\Services\BookConfigService;
  10. use DB;
  11. use Redis;
  12. use App\Modules\User\Services\UserService;
  13. use App\Modules\Activity\Services\ActivityService;
  14. class UserSignService
  15. {
  16. protected $table = 'user_sign';
  17. protected $fillable = ['uid', 'price', 'day', 'sign_time'];
  18. /**
  19. * 用户是否已签到
  20. * @param $uid
  21. * @param $day
  22. * @return mixed
  23. */
  24. public static function isSign($uid)
  25. {
  26. //return UserSign::isSign($uid, $day);
  27. $sign_day = ReadRecordService::getSignDay($uid);
  28. //异常
  29. if ($sign_day == -1) {
  30. return true;
  31. }
  32. if ($sign_day && $sign_day == date('Y-m-d')) {
  33. return true;
  34. }
  35. return false;
  36. }
  37. /**
  38. * 用户签到记录
  39. */
  40. public static function getUserSignRecord($uid)
  41. {
  42. $UserSignModel = new UserSign();
  43. $UserSignModel->setCurrentTable(date('Ym'));
  44. return $UserSignModel->where('uid',$uid)->where('day','<',date('Y-m-d'))->select('price','sign_time')->orderBy('sign_time','desc')->paginate();
  45. }
  46. /**
  47. * 签到
  48. * @param $uid
  49. * @param $day
  50. * @return mixed
  51. */
  52. public static function sign($uid, $day)
  53. {
  54. \Log::info('sign:uid:' . $uid . ' day:' . $day);
  55. //查看签到日期
  56. $sign_day = ReadRecordService::getSignDay($uid);
  57. if ($sign_day == -1) {
  58. return false;
  59. }
  60. //已经签过到
  61. if ($sign_day == $day) {
  62. return false;
  63. }
  64. $count = ReadRecordService::getSignCountSimple($uid);
  65. //记录签到日期
  66. if ($sign_day && $sign_day == date('Y-m-d', time() - 86400)) {
  67. $continue = true;
  68. //昨天有签过到
  69. ReadRecordService::sign((int)$uid, true);
  70. $count += 1;
  71. } else {
  72. $continue = false;
  73. //昨天没有签过到
  74. ReadRecordService::sign((int)$uid, false);
  75. $count = 1;
  76. }
  77. $return_fee = $fee = 30;
  78. //连续签到两天 50书币
  79. if ($continue && $count >= 3) {
  80. $return_fee = $fee = 50;
  81. }
  82. if ($count % 15 == 7) {
  83. $fee += 100;
  84. }
  85. if ($count % 15 == 0) {
  86. $fee += 150;
  87. }
  88. UserService::addBalance($uid, $fee, 0, $fee);
  89. // 先扔到redis里面,异步更新user_sign表
  90. $use_redis_user_sign = true;
  91. if ($use_redis_user_sign) {
  92. $sign_data = ['uid' => $uid, 'price' => $fee, 'day' => $day, 'sign_time' => time(), 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s')];
  93. Redis::sadd('user_sign:uid', $uid);
  94. Redis::hset('user_sign:uid:info', $uid, json_encode($sign_data));
  95. ReadRecordService::setSignInfo($uid, json_encode($sign_data));
  96. } else {
  97. $user_sign_model = new UserSign();
  98. $user_sign_model->setCurrentTable(date('Ym'));
  99. $data = ['uid'=>$uid,'price'=>$fee,'day'=>$day,'sign_time'=>time()];
  100. $user_sign_model->create($data);
  101. }
  102. return $return_fee;
  103. }
  104. public static function signV2($uid, $day)
  105. {
  106. \Log::info('signv2:uid:' . $uid . ' day:' . $day);
  107. //查看签到日期
  108. $sign_day = ReadRecordService::getSignDay($uid);
  109. if ($sign_day == -1) {
  110. return false;
  111. }
  112. //已经签过到
  113. if ($sign_day == $day) {
  114. return false;
  115. }
  116. $count = ReadRecordService::getSignCountSimple($uid);
  117. //记录签到日期
  118. if ($sign_day && $sign_day == date('Y-m-d', time() - 86400)) {
  119. $continue = true;
  120. //昨天有签过到
  121. ReadRecordService::sign((int)$uid, true);
  122. $count += 1;
  123. } else {
  124. $continue = false;
  125. //昨天没有签过到
  126. ReadRecordService::sign((int)$uid, false);
  127. $count = 1;
  128. }
  129. $fee = 30;
  130. if ($count % 7 == 1 && $count<=7) {
  131. $fee = 30;
  132. } elseif ($count % 7 == 3) {
  133. $fee = 120;
  134. } elseif ($count % 7 == 0) {
  135. $fee = 150;
  136. } else {
  137. $fee = 50;
  138. }
  139. UserService::addBalance($uid, $fee, 0, $fee);
  140. // 先扔到redis里面,异步更新user_sign表
  141. $use_redis_user_sign = true;
  142. if ($use_redis_user_sign) {
  143. $sign_data = ['uid' => $uid, 'price' => $fee, 'day' => $day, 'sign_time' => time(), 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s')];
  144. Redis::sadd('user_sign:uid', $uid);
  145. Redis::hset('user_sign:uid:info', $uid, json_encode($sign_data));
  146. ReadRecordService::setSignInfo($uid, json_encode($sign_data));
  147. } else {
  148. $user_sign_model = new UserSign();
  149. $user_sign_model->setCurrentTable(date('Ym'));
  150. $data = ['uid'=>$uid,'price'=>$fee,'day'=>$day,'sign_time'=>time()];
  151. $user_sign_model->create($data);
  152. }
  153. return $fee;
  154. }
  155. public static function getUserSignVersion($uid)
  156. {
  157. list($version, $sign_day, $count) = ReadRecordService::getByMultiField($uid, 'sign_version', 'sign_day', 'sign_counts');
  158. if ($version == 'v2') {
  159. return 'v2';
  160. }
  161. if ($version == 'v1') {
  162. if ($sign_day == date('Y-m-d')) {
  163. return $version;
  164. }
  165. if ($sign_day == date('Y-m-d', time() - 86400)) {
  166. if ($count % 15 == 0 ) {
  167. ReadRecordService::setByMultiField($uid, ['sign_counts' => 0, 'sign_version' => 'v2']);
  168. return 'v2';
  169. }
  170. return $version;
  171. }
  172. ReadRecordService::setByMultiField($uid, ['sign_version' => 'v2']);
  173. return 'v2';
  174. }
  175. if (!$count || !$sign_day) {
  176. ReadRecordService::setByMultiField($uid, ['sign_version' => 'v2']);
  177. return 'v2';
  178. }
  179. if ($sign_day == date('Y-m-d')) {
  180. self::setUserSignVersion($uid, 'v1');
  181. return 'v1';
  182. }
  183. if ($sign_day == date('Y-m-d', time() - 86400)) {
  184. if ($count % 15 == 0) {
  185. ReadRecordService::setByMultiField($uid, ['sign_counts' => 0, 'sign_version' => 'v2']);
  186. return 'v2';
  187. }
  188. self::setUserSignVersion($uid, 'v1');
  189. return 'v1';
  190. }
  191. self::setUserSignVersion($uid, 'v2');
  192. return 'v2';
  193. }
  194. public static function setUserSignVersion($uid, $version)
  195. {
  196. ReadRecordService::setByField($uid, 'sign_version', $version);
  197. }
  198. public static function signToday($uid,$version='')
  199. {
  200. return self::signV2($uid, date('Y-m-d'));
  201. /*if(!$version){
  202. $version = self::getUserSignVersion($uid);
  203. }
  204. if($version == 'v1'){
  205. return self::sign($uid, date('Y-m-d'));
  206. }
  207. if($version == 'v2'){
  208. return self::signV2($uid, date('Y-m-d'));
  209. }
  210. return 0;*/
  211. }
  212. /**
  213. * 新签到回复
  214. * @param $openid
  215. * @param bool $check_sign
  216. * @param int $price
  217. * @return string
  218. */
  219. public static function userSignReturnContent3($openid, $distribution_channel_id = '')
  220. {
  221. $content = '';
  222. $day = date('Y-m-d');
  223. $user = ForceSubscribeUsers::getOneForceSubscribeUsersByOpenid($openid);
  224. if ($user) {
  225. $user_wechat = User::where('id', $user->uid)->first();
  226. $new_user_activity_content = '';
  227. if (!Order::where('uid', $user->uid)->where('status', 'PAID')->select('id')->first()) {
  228. $new_user_activity_content = self::newUserActivity($user);
  229. }
  230. $encode_distribution_channel_id = encodeDistributionChannelId($user->distribution_channel_id);
  231. $attach_content = self::signCallBackPushActivityInfo($user->uid, $user->distribution_channel_id);
  232. $continueReadUrl = env('PROTOCOL') . '://site' . $encode_distribution_channel_id . '.' . env('WECHAT_CUSTOM_HOST') . '.com/continue';
  233. $sign_url = env('PROTOCOL') . '://site' . $encode_distribution_channel_id . '.' . env('WECHAT_CUSTOM_HOST') . '.com/sign';
  234. $sign_stat = self::isSign($user->uid);
  235. if ($sign_stat) {
  236. $content = '今日已经签到过了,明日继续签到得书币哦~';
  237. //$content .= '<a href=' . '"' . $continueReadUrl . '"' . '> ☞ 点我继续上次阅读</a>';
  238. } else {
  239. $content = '尊敬的会员:' . ($user_wechat ? $user_wechat->nickname : '') . "\n\n" . "<a href='" . $sign_url . "'>💰点击此处签到领书币</a>";
  240. //$content .= '继续阅读\n\n<a href=' . '"' . $continueReadUrl . '"' . '> ☞ 点我继续上次阅读</a>';
  241. }
  242. $res = ReadRecordService::getReadRecord($user->uid);
  243. // foreach ($res as $key => $record) {
  244. // if ($key == 1) break;
  245. // $url = env('PROTOCOL') . '://site' . $encode_distribution_channel_id . '.' . env('CUSTOM_HOST') . '.com/reader?bid=' . Hashids::encode($record['bid']) . '&cid=' . $record['cid'];
  246. // $content .= "\n\n" . '<a href="' . $url . '"> ☞ 《' . $record['book_name'] . '》</a>';
  247. // }
  248. $read_bid_arr = [];
  249. $read_bid_arr[] = -1;
  250. foreach ($res as $vbook) {
  251. $read_bid_arr[] = $vbook['bid'];
  252. }
  253. $user_detail = UserService::getById($user->uid);
  254. $sign_recomm_bid_key = '男频';
  255. if ($user_detail && isset($user_detail->sex)) {
  256. if ($user_detail->sex == 2) {
  257. $sign_recomm_bid_key = '女频';
  258. }
  259. }
  260. $hot_book_num = 3;
  261. $recomm_books = BookConfigService::getSignRecommendBooks($read_bid_arr, $sign_recomm_bid_key, $hot_book_num);
  262. $content .= "\n\n" . '热门书籍推荐';
  263. if ($recomm_books) {
  264. foreach ($recomm_books as $book) {
  265. $url = env('PROTOCOL') . '://site' . $encode_distribution_channel_id . '.' . env('WECHAT_CUSTOM_HOST') . '.com/reader?bid=' . Hashids::encode($book->bid) . '&cid=' . $book->first_cid;
  266. $content .= "\n\n" . '<a href="' . $url . '"> ☞ 《' . $book->book_name . '》</a>';
  267. }
  268. }
  269. //$content .= "\n\n" . '为方便下次阅读,请置顶公众号';
  270. $content .= "\n\n" . '为方便下次阅读,请<a href="' . 'https://help.' . env('WECHAT_CUSTOM_HOST') . '.com/top.html"' . '>置顶公众号</a>';
  271. if ($attach_content) {
  272. $content .= "\n\n" . $attach_content;
  273. }
  274. if ($new_user_activity_content) {
  275. $content .= $new_user_activity_content;
  276. }
  277. } // 空用户推默认的文案
  278. else {
  279. $encode_distribution_channel_id = encodeDistributionChannelId($distribution_channel_id);
  280. $url = env('PROTOCOL') . '://site' . $encode_distribution_channel_id . '.' . env('WECHAT_CUSTOM_HOST') . '.com/sign';
  281. $content = "尊敬的会员:\n\n" . '<a href="' . $url . '"> 💰点击此处签到领书币</a>';
  282. }
  283. return $content;
  284. }
  285. private static function newUserActivity($user)
  286. {
  287. $content = '';
  288. $status = self::newUserActivityStatus($user->uid);
  289. $record = [];
  290. //新关未付费用户42小时后充推送活动 68元的活动 文案:全年免费看书
  291. if (strtotime($user->created_at) + 42 * 3600 < time() &&
  292. $user->distribution_channel_id == 123 &&
  293. !in_array(1, $status)
  294. ) {
  295. $url = env('PROTOCOL') . '://site' . encodeDistributionChannelId($user->distribution_channel_id) . '.' . env('CUSTOM_HOST') . '.com/sale/seYearActivity?fromtype=signcallback_forever&send_time=' . time();
  296. $content .= "\n\n" . '<a href="' . $url . '"> 💰全年免费看书</a>';
  297. $record[] = ['uid' => $user->uid, 'type' => 1, 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s')];
  298. }
  299. //20小时的 文字链的文案:书币充值特惠
  300. if (strtotime($user->created_at) + 20 * 3600 < time() && !in_array(2, $status)) {
  301. $url = env('PROTOCOL') . '://site' . encodeDistributionChannelId($user->distribution_channel_id) . '.' . env('CUSTOM_HOST') . '.com/sale/newUserSale?fromtype=signcallback_newUserSale&send_time=' . time();
  302. $content .= "\n\n" . '<a href="' . $url . '"> 💰书币充值特惠</a>';
  303. $record[] = ['uid' => $user->uid, 'type' => 2, 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s')];
  304. }
  305. //36小时的。文案:充9.9送2000书币
  306. if (strtotime($user->created_at) + 36 * 3600 < time() && !in_array(3, $status)) {
  307. $url = env('PROTOCOL') . '://site' . encodeDistributionChannelId($user->distribution_channel_id) . '.' . env('CUSTOM_HOST') . '.com/sale/newUserActivity?fromtype=signcallback_newUserActivity&send_time=' . time();
  308. $content .= "\n\n" . '<a href="' . $url . '"> 💰充9.9得2000书币</a>';
  309. $record[] = ['uid' => $user->uid, 'type' => 3, 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s')];
  310. }
  311. self::recordnewUserActivityPush($record);
  312. return $content;
  313. }
  314. private static function newUserActivityStatus($uid)
  315. {
  316. $data = [-1];
  317. if (!$uid) {
  318. return $data;
  319. }
  320. $result = DB::table('user_sign_push_activity')->where('uid', $uid)->select('type')->get();
  321. if ($result) {
  322. foreach ($result as $v) {
  323. array_push($data, $v->type);
  324. }
  325. }
  326. return $data;
  327. }
  328. private static function recordnewUserActivityPush($data)
  329. {
  330. if ($data) {
  331. try {
  332. DB::table('user_sign_push_activity')->insert($data);
  333. } catch (\Exception $e) {
  334. }
  335. }
  336. }
  337. /**
  338. *
  339. * @param $openid
  340. * @return string
  341. */
  342. public static function userSignReturnContent($openid)
  343. {
  344. $content = '';
  345. $day = date('Y-m-d');
  346. $user = ForceSubscribeUsers::getOneForceSubscribeUsersByOpenid($openid);
  347. if ($user) {
  348. $encode_distribution_channel_id = encodeDistributionChannelId($user->distribution_channel_id);
  349. $attach_content = self::signCallBackPushActivityInfo($user->uid);
  350. $continueReadUrl = env('PROTOCOL') . '://site' . $encode_distribution_channel_id . '.' . env('CUSTOM_HOST') . '.com/continue';
  351. $is_sign = self::isSign($user->uid);
  352. if ($is_sign) {
  353. $content = '今日已经签到过了,明日继续签到得书币哦~' . "\n\n" . '<a href=' . '"' . $continueReadUrl . '"' . '> >>点我继续上次阅读</a>';
  354. /*if($attach_content){
  355. $content .= "\n\n" .$attach_content;
  356. }*/
  357. //return $content;
  358. } else {
  359. $sign_stat = self::sign($user->uid, $day);
  360. $content = '今日签到成功,赠送' . $sign_stat . '书币,明日继续签到得书币哦~' . "\n\n" . '<a href=' . '"' . $continueReadUrl . '"' . '> >>点我继续上次阅读</a>' . "\n\n" . '阅读记录:';
  361. if ($sign_stat == 30) {
  362. $content = '今日签到成功,赠送30书币,连续签到2日后,赠送书币增加至50哦~' . "\n\n" . '<a href=' . '"' . $continueReadUrl . '"' . '> >>点我继续上次阅读</a>' . "\n\n" . '阅读记录:';
  363. }
  364. }
  365. $new_user_activity_content = '';
  366. if (!Order::where('uid', $user->uid)->where('status', 'PAID')->select('id')->first()) {
  367. $new_user_activity_content = self::newUserActivity($user);
  368. }
  369. $res = ReadRecordService::getReadRecord($user->uid);
  370. foreach ($res as $key => $record) {
  371. if ($key == 3) break;
  372. $url = env('PROTOCOL') . '://site' . $encode_distribution_channel_id . '.' . env('CUSTOM_HOST') . '.com/reader?bid=' . Hashids::encode($record['bid']) . '&cid=' . $record['cid'];
  373. $content .= "\n\n" . '<a href="' . $url . '"> >>《' . $record['book_name'] . '》</a>';
  374. }
  375. $read_bid_arr = [];
  376. $read_bid_arr[] = -1;
  377. foreach ($res as $vbook) {
  378. $read_bid_arr[] = $vbook['bid'];
  379. }
  380. $user_detail = UserService::getById($user->uid);
  381. $sign_recomm_bid_key = '男频';
  382. if ($user_detail && isset($user_detail->sex)) {
  383. if ($user_detail->sex == 2) {
  384. $sign_recomm_bid_key = '女频';
  385. }
  386. }
  387. $recomm_books = BookConfigService::getSignRecommendBooks($read_bid_arr, $sign_recomm_bid_key);
  388. $content .= "\n\n" . '热门书籍推荐';
  389. if ($recomm_books) {
  390. foreach ($recomm_books as $book) {
  391. $url = env('PROTOCOL') . '://site' . $encode_distribution_channel_id . '.' . env('CUSTOM_HOST') . '.com/reader?bid=' . Hashids::encode($book->bid) . '&cid=' . $book->first_cid;
  392. $content .= "\n\n" . '<a href="' . $url . '"> >>《' . $book->book_name . '》</a>';
  393. }
  394. }
  395. //$content .= "\n\n" . '为方便下次阅读,请置顶公众号';
  396. $content .= "\n\n" . '为方便下次阅读,请<a href="' . 'https://help.leyuee.com/top.html"' . '>置顶公众号</a>';
  397. if ($attach_content) {
  398. $content .= "\n\n" . $attach_content;
  399. }
  400. if ($new_user_activity_content) {
  401. $content .= "\n\n" . $new_user_activity_content;
  402. }
  403. }
  404. return $content;
  405. }
  406. public static function signCallBackPushActivityInfo($uid, $distribution_channel_id)
  407. {
  408. $activity_setting = ActivityService::getActivitySetting();
  409. if (!$activity_setting)
  410. return false;
  411. $acyivity_id = isset($activity_setting['activity_id']) ? $activity_setting['activity_id'] : 0;
  412. $other = env('OTHER_ACTIVITY_ID', 0);
  413. if ($acyivity_id == $other && !in_array($distribution_channel_id, explode(',', env('OTHER_ACTIVITY_CHANNEL', '1')))) {
  414. return false;
  415. }
  416. if (!$acyivity_id)
  417. return false;
  418. $activity_title = isset($activity_setting['sign_call_back_text']) ? $activity_setting['sign_call_back_text'] : '';;
  419. if (empty($activity_title))
  420. return false;
  421. $activity_info = ActivityService::getById($acyivity_id);
  422. if (empty($activity_info))
  423. return false;
  424. if (time() < strtotime($activity_info->start_time) || time() > strtotime($activity_info->end_time)) {
  425. return false;
  426. }
  427. $user = UserService::getById($uid);
  428. if (empty($user))
  429. return false;
  430. if (!ActivitySwitchService::isShowInPage($acyivity_id, $distribution_channel_id, 'sign')) {
  431. return false;
  432. }
  433. /*
  434. $no_participate_activity = env('no_participate_activity','');
  435. if($no_participate_activity && in_array($user->distribution_channel_id, explode(',',$no_participate_activity))){
  436. return false;
  437. }*/
  438. if ($user && isset($user->created_at) && (time() - strtotime($user->created_at)) >= 86400 * 2) {
  439. $url = env('PROTOCOL') . '://site' . encodeDistributionChannelId($distribution_channel_id) . '.' . env('CUSTOM_HOST') . '.com' . $activity_info->activity_page . '&fromtype=signcallback';
  440. return '<a href="' . $url . '"> ' . $activity_title . '</a>';
  441. }
  442. return false;
  443. }
  444. }