UserSignService.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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. //已经签过到
  134. if ($sign_day == $day) {
  135. return ['fee' => $fee, 'days' => $count];
  136. }
  137. UserService::addBalance($uid, $fee, 0, $fee);
  138. // 先扔到redis里面,异步更新user_sign表
  139. $use_redis_user_sign = true;
  140. if ($use_redis_user_sign) {
  141. $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')];
  142. Redis::sadd('user_sign:uid', $uid);
  143. Redis::hset('user_sign:uid:info', $uid, json_encode($sign_data));
  144. ReadRecordService::setSignInfo($uid, json_encode($sign_data));
  145. } else {
  146. $user_sign_model = new UserSign();
  147. $user_sign_model->setCurrentTable(date('Ym'));
  148. $data = ['uid' => $uid, 'price' => $fee, 'day' => $day, 'sign_time' => time()];
  149. $user_sign_model->create($data);
  150. }
  151. return ['fee' => $fee, 'days' => $count];
  152. }
  153. public static function getUserSignVersion($uid)
  154. {
  155. return 'v2';
  156. /*list($version, $sign_day, $count) = ReadRecordService::getByMultiField($uid, 'sign_version', 'sign_day', 'sign_counts');
  157. if ($version == 'v2') {
  158. return 'v2';
  159. }
  160. if ($version == 'v1') {
  161. if ($sign_day == date('Y-m-d')) {
  162. return $version;
  163. }
  164. if ($sign_day == date('Y-m-d', time() - 86400)) {
  165. if ($count % 15 == 0 ) {
  166. ReadRecordService::setByMultiField($uid, ['sign_counts' => 0, 'sign_version' => 'v2']);
  167. return 'v2';
  168. }
  169. return $version;
  170. }
  171. ReadRecordService::setByMultiField($uid, ['sign_version' => 'v2']);
  172. return 'v2';
  173. }
  174. if (!$count || !$sign_day) {
  175. ReadRecordService::setByMultiField($uid, ['sign_version' => 'v2']);
  176. return 'v2';
  177. }
  178. if ($sign_day == date('Y-m-d')) {
  179. self::setUserSignVersion($uid, 'v1');
  180. return 'v1';
  181. }
  182. if ($sign_day == date('Y-m-d', time() - 86400)) {
  183. if ($count % 15 == 0) {
  184. ReadRecordService::setByMultiField($uid, ['sign_counts' => 0, 'sign_version' => 'v2']);
  185. return 'v2';
  186. }
  187. self::setUserSignVersion($uid, 'v1');
  188. return 'v1';
  189. }
  190. self::setUserSignVersion($uid, 'v2');
  191. return 'v2';*/
  192. }
  193. public static function setUserSignVersion($uid, $version)
  194. {
  195. ReadRecordService::setByField($uid, 'sign_version', $version);
  196. }
  197. public static function signToday($uid, $version = '')
  198. {
  199. return self::signV2($uid, date('Y-m-d'));
  200. /*if(!$version){
  201. $version = self::getUserSignVersion($uid);
  202. }
  203. if($version == 'v1'){
  204. return self::sign($uid, date('Y-m-d'));
  205. }
  206. if($version == 'v2'){
  207. return self::signV2($uid, date('Y-m-d'));
  208. }
  209. return 0;*/
  210. }
  211. /**
  212. * 新签到回复
  213. * @param $openid
  214. * @param bool $check_sign
  215. * @param int $price
  216. * @return string
  217. */
  218. public static function userSignReturnContent3($openid, $distribution_channel_id = '')
  219. {
  220. $content = '';
  221. $day = date('Y-m-d');
  222. $user = ForceSubscribeUsers::getOneForceSubscribeUsersByOpenid($openid);
  223. if ($user) {
  224. $user_wechat = User::where('id', $user->uid)->first();
  225. $new_user_activity_content = '';
  226. if (!Order::where('uid', $user->uid)->where('status', 'PAID')->select('id')->first()) {
  227. $new_user_activity_content = self::newUserActivity($user);
  228. }
  229. $encode_distribution_channel_id = encodeDistributionChannelId($user->distribution_channel_id);
  230. $attach_content = self::signCallBackPushActivityInfo($user->uid, $user->distribution_channel_id);
  231. $continueReadUrl = env('PROTOCOL') . '://site' . $encode_distribution_channel_id . '.' . env('WECHAT_CUSTOM_HOST') . '.com/continue';
  232. $sign_url = env('PROTOCOL') . '://site' . $encode_distribution_channel_id . '.' . env('WECHAT_CUSTOM_HOST') . '.com/sign';
  233. $sign_stat = self::isSign($user->uid);
  234. if ($sign_stat) {
  235. $content = '今日已经签到过了,明日继续签到得书币哦~';
  236. //$content .= '<a href=' . '"' . $continueReadUrl . '"' . '> ☞ 点我继续上次阅读</a>';
  237. } else {
  238. $content = '尊敬的会员:' . ($user_wechat ? $user_wechat->nickname : '') . "\n\n" . "<a href='" . $sign_url . "'>💰点击此处签到领书币</a>";
  239. //$content .= '继续阅读\n\n<a href=' . '"' . $continueReadUrl . '"' . '> ☞ 点我继续上次阅读</a>';
  240. }
  241. $res = ReadRecordService::getReadRecord($user->uid);
  242. // foreach ($res as $key => $record) {
  243. // if ($key == 1) break;
  244. // $url = env('PROTOCOL') . '://site' . $encode_distribution_channel_id . '.' . env('CUSTOM_HOST') . '.com/reader?bid=' . Hashids::encode($record['bid']) . '&cid=' . $record['cid'];
  245. // $content .= "\n\n" . '<a href="' . $url . '"> ☞ 《' . $record['book_name'] . '》</a>';
  246. // }
  247. $read_bid_arr = [];
  248. $read_bid_arr[] = -1;
  249. foreach ($res as $vbook) {
  250. $read_bid_arr[] = $vbook['bid'];
  251. }
  252. $user_detail = UserService::getById($user->uid);
  253. $sign_recomm_bid_key = '男频';
  254. if ($user_detail && isset($user_detail->sex)) {
  255. if ($user_detail->sex == 2) {
  256. $sign_recomm_bid_key = '女频';
  257. }
  258. }
  259. $hot_book_num = 3;
  260. $recomm_books = BookConfigService::getSignRecommendBooks($read_bid_arr, $sign_recomm_bid_key, $hot_book_num);
  261. $content .= "\n\n" . '热门书籍推荐';
  262. if ($recomm_books) {
  263. foreach ($recomm_books as $book) {
  264. $url = env('PROTOCOL') . '://site' . $encode_distribution_channel_id . '.' . env('WECHAT_CUSTOM_HOST') . '.com/reader?bid=' . Hashids::encode($book->bid) . '&cid=' . $book->first_cid;
  265. $content .= "\n\n" . '<a href="' . $url . '"> ☞ 《' . $book->book_name . '》</a>';
  266. }
  267. }
  268. //$content .= "\n\n" . '为方便下次阅读,请置顶公众号';
  269. $content .= "\n\n" . '为方便下次阅读,请<a href="' . 'https://help.' . env('WECHAT_CUSTOM_HOST') . '.com/top.html"' . '>置顶公众号</a>';
  270. if ($attach_content) {
  271. $content .= "\n\n" . $attach_content;
  272. }
  273. if ($new_user_activity_content) {
  274. $content .= $new_user_activity_content;
  275. }
  276. } // 空用户推默认的文案
  277. else {
  278. $encode_distribution_channel_id = encodeDistributionChannelId($distribution_channel_id);
  279. $url = env('PROTOCOL') . '://site' . $encode_distribution_channel_id . '.' . env('WECHAT_CUSTOM_HOST') . '.com/sign';
  280. $content = "尊敬的会员:\n\n" . '<a href="' . $url . '"> 💰点击此处签到领书币</a>';
  281. }
  282. return $content;
  283. }
  284. private static function newUserActivity($user)
  285. {
  286. $content = '';
  287. $status = self::newUserActivityStatus($user->uid);
  288. $record = [];
  289. //新关未付费用户42小时后充推送活动 68元的活动 文案:全年免费看书
  290. if (
  291. 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. * @param $openid
  339. * @return string
  340. */
  341. public static function userSignReturnContent($openid)
  342. {
  343. $content = '';
  344. $day = date('Y-m-d');
  345. $user = ForceSubscribeUsers::getOneForceSubscribeUsersByOpenid($openid);
  346. if ($user) {
  347. $encode_distribution_channel_id = encodeDistributionChannelId($user->distribution_channel_id);
  348. $attach_content = self::signCallBackPushActivityInfo($user->uid);
  349. $continueReadUrl = env('PROTOCOL') . '://site' . $encode_distribution_channel_id . '.' . env('CUSTOM_HOST') . '.com/continue';
  350. $is_sign = self::isSign($user->uid);
  351. if ($is_sign) {
  352. $content = '今日已经签到过了,明日继续签到得书币哦~' . "\n\n" . '<a href=' . '"' . $continueReadUrl . '"' . '> >>点我继续上次阅读</a>';
  353. /*if($attach_content){
  354. $content .= "\n\n" .$attach_content;
  355. }*/
  356. //return $content;
  357. } else {
  358. $sign_stat = self::sign($user->uid, $day);
  359. $content = '今日签到成功,赠送' . $sign_stat . '书币,明日继续签到得书币哦~' . "\n\n" . '<a href=' . '"' . $continueReadUrl . '"' . '> >>点我继续上次阅读</a>' . "\n\n" . '阅读记录:';
  360. if ($sign_stat == 30) {
  361. $content = '今日签到成功,赠送30书币,连续签到2日后,赠送书币增加至50哦~' . "\n\n" . '<a href=' . '"' . $continueReadUrl . '"' . '> >>点我继续上次阅读</a>' . "\n\n" . '阅读记录:';
  362. }
  363. }
  364. $new_user_activity_content = '';
  365. if (!Order::where('uid', $user->uid)->where('status', 'PAID')->select('id')->first()) {
  366. $new_user_activity_content = self::newUserActivity($user);
  367. }
  368. $res = ReadRecordService::getReadRecord($user->uid);
  369. foreach ($res as $key => $record) {
  370. if ($key == 3) break;
  371. $url = env('PROTOCOL') . '://site' . $encode_distribution_channel_id . '.' . env('CUSTOM_HOST') . '.com/reader?bid=' . Hashids::encode($record['bid']) . '&cid=' . $record['cid'];
  372. $content .= "\n\n" . '<a href="' . $url . '"> >>《' . $record['book_name'] . '》</a>';
  373. }
  374. $read_bid_arr = [];
  375. $read_bid_arr[] = -1;
  376. foreach ($res as $vbook) {
  377. $read_bid_arr[] = $vbook['bid'];
  378. }
  379. $user_detail = UserService::getById($user->uid);
  380. $sign_recomm_bid_key = '男频';
  381. if ($user_detail && isset($user_detail->sex)) {
  382. if ($user_detail->sex == 2) {
  383. $sign_recomm_bid_key = '女频';
  384. }
  385. }
  386. $recomm_books = BookConfigService::getSignRecommendBooks($read_bid_arr, $sign_recomm_bid_key);
  387. $content .= "\n\n" . '热门书籍推荐';
  388. if ($recomm_books) {
  389. foreach ($recomm_books as $book) {
  390. $url = env('PROTOCOL') . '://site' . $encode_distribution_channel_id . '.' . env('CUSTOM_HOST') . '.com/reader?bid=' . Hashids::encode($book->bid) . '&cid=' . $book->first_cid;
  391. $content .= "\n\n" . '<a href="' . $url . '"> >>《' . $book->book_name . '》</a>';
  392. }
  393. }
  394. //$content .= "\n\n" . '为方便下次阅读,请置顶公众号';
  395. $content .= "\n\n" . '为方便下次阅读,请<a href="' . 'https://help.leyuee.com/top.html"' . '>置顶公众号</a>';
  396. if ($attach_content) {
  397. $content .= "\n\n" . $attach_content;
  398. }
  399. if ($new_user_activity_content) {
  400. $content .= "\n\n" . $new_user_activity_content;
  401. }
  402. }
  403. return $content;
  404. }
  405. public static function signCallBackPushActivityInfo($uid, $distribution_channel_id)
  406. {
  407. $activity_setting = ActivityService::getActivitySetting();
  408. if (!$activity_setting)
  409. return false;
  410. $acyivity_id = isset($activity_setting['activity_id']) ? $activity_setting['activity_id'] : 0;
  411. $other = env('OTHER_ACTIVITY_ID', 0);
  412. if ($acyivity_id == $other && !in_array($distribution_channel_id, explode(',', env('OTHER_ACTIVITY_CHANNEL', '1')))) {
  413. return false;
  414. }
  415. if (!$acyivity_id)
  416. return false;
  417. $activity_title = isset($activity_setting['sign_call_back_text']) ? $activity_setting['sign_call_back_text'] : '';;
  418. if (empty($activity_title))
  419. return false;
  420. $activity_info = ActivityService::getById($acyivity_id);
  421. if (empty($activity_info))
  422. return false;
  423. if (time() < strtotime($activity_info->start_time) || time() > strtotime($activity_info->end_time)) {
  424. return false;
  425. }
  426. $user = UserService::getById($uid);
  427. if (empty($user))
  428. return false;
  429. if (!ActivitySwitchService::isShowInPage($acyivity_id, $distribution_channel_id, 'sign')) {
  430. return false;
  431. }
  432. /*
  433. $no_participate_activity = env('no_participate_activity','');
  434. if($no_participate_activity && in_array($user->distribution_channel_id, explode(',',$no_participate_activity))){
  435. return false;
  436. }*/
  437. if ($user && isset($user->created_at) && (time() - strtotime($user->created_at)) >= 86400 * 2) {
  438. $url = env('PROTOCOL') . '://site' . encodeDistributionChannelId($distribution_channel_id) . '.' . env('CUSTOM_HOST') . '.com' . $activity_info->activity_page . '&fromtype=signcallback';
  439. return '<a href="' . $url . '"> ' . $activity_title . '</a>';
  440. }
  441. return false;
  442. }
  443. }