UserSignService.php 20 KB

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