UserSignService.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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. return 'v2';
  158. /*list($version, $sign_day, $count) = ReadRecordService::getByMultiField($uid, 'sign_version', 'sign_day', 'sign_counts');
  159. if ($version == 'v2') {
  160. return 'v2';
  161. }
  162. if ($version == 'v1') {
  163. if ($sign_day == date('Y-m-d')) {
  164. return $version;
  165. }
  166. if ($sign_day == date('Y-m-d', time() - 86400)) {
  167. if ($count % 15 == 0 ) {
  168. ReadRecordService::setByMultiField($uid, ['sign_counts' => 0, 'sign_version' => 'v2']);
  169. return 'v2';
  170. }
  171. return $version;
  172. }
  173. ReadRecordService::setByMultiField($uid, ['sign_version' => 'v2']);
  174. return 'v2';
  175. }
  176. if (!$count || !$sign_day) {
  177. ReadRecordService::setByMultiField($uid, ['sign_version' => 'v2']);
  178. return 'v2';
  179. }
  180. if ($sign_day == date('Y-m-d')) {
  181. self::setUserSignVersion($uid, 'v1');
  182. return 'v1';
  183. }
  184. if ($sign_day == date('Y-m-d', time() - 86400)) {
  185. if ($count % 15 == 0) {
  186. ReadRecordService::setByMultiField($uid, ['sign_counts' => 0, 'sign_version' => 'v2']);
  187. return 'v2';
  188. }
  189. self::setUserSignVersion($uid, 'v1');
  190. return 'v1';
  191. }
  192. self::setUserSignVersion($uid, 'v2');
  193. return 'v2';*/
  194. }
  195. public static function setUserSignVersion($uid, $version)
  196. {
  197. ReadRecordService::setByField($uid, 'sign_version', $version);
  198. }
  199. public static function signToday($uid,$version='')
  200. {
  201. return self::signV2($uid, date('Y-m-d'));
  202. /*if(!$version){
  203. $version = self::getUserSignVersion($uid);
  204. }
  205. if($version == 'v1'){
  206. return self::sign($uid, date('Y-m-d'));
  207. }
  208. if($version == 'v2'){
  209. return self::signV2($uid, date('Y-m-d'));
  210. }
  211. return 0;*/
  212. }
  213. /**
  214. * 新签到回复
  215. * @param $openid
  216. * @param bool $check_sign
  217. * @param int $price
  218. * @return string
  219. */
  220. public static function userSignReturnContent3($openid, $distribution_channel_id = '')
  221. {
  222. $content = '';
  223. $day = date('Y-m-d');
  224. $user = ForceSubscribeUsers::getOneForceSubscribeUsersByOpenid($openid);
  225. if ($user) {
  226. $user_wechat = User::where('id', $user->uid)->first();
  227. $new_user_activity_content = '';
  228. if (!Order::where('uid', $user->uid)->where('status', 'PAID')->select('id')->first()) {
  229. $new_user_activity_content = self::newUserActivity($user);
  230. }
  231. $encode_distribution_channel_id = encodeDistributionChannelId($user->distribution_channel_id);
  232. $attach_content = self::signCallBackPushActivityInfo($user->uid, $user->distribution_channel_id);
  233. $continueReadUrl = env('PROTOCOL') . '://site' . $encode_distribution_channel_id . '.' . env('WECHAT_CUSTOM_HOST') . '.com/continue';
  234. $sign_url = env('PROTOCOL') . '://site' . $encode_distribution_channel_id . '.' . env('WECHAT_CUSTOM_HOST') . '.com/sign';
  235. $sign_stat = self::isSign($user->uid);
  236. if ($sign_stat) {
  237. $content = '今日已经签到过了,明日继续签到得书币哦~';
  238. //$content .= '<a href=' . '"' . $continueReadUrl . '"' . '> ☞ 点我继续上次阅读</a>';
  239. } else {
  240. $content = '尊敬的会员:' . ($user_wechat ? $user_wechat->nickname : '') . "\n\n" . "<a href='" . $sign_url . "'>💰点击此处签到领书币</a>";
  241. //$content .= '继续阅读\n\n<a href=' . '"' . $continueReadUrl . '"' . '> ☞ 点我继续上次阅读</a>';
  242. }
  243. $res = ReadRecordService::getReadRecord($user->uid);
  244. // foreach ($res as $key => $record) {
  245. // if ($key == 1) break;
  246. // $url = env('PROTOCOL') . '://site' . $encode_distribution_channel_id . '.' . env('CUSTOM_HOST') . '.com/reader?bid=' . Hashids::encode($record['bid']) . '&cid=' . $record['cid'];
  247. // $content .= "\n\n" . '<a href="' . $url . '"> ☞ 《' . $record['book_name'] . '》</a>';
  248. // }
  249. $read_bid_arr = [];
  250. $read_bid_arr[] = -1;
  251. foreach ($res as $vbook) {
  252. $read_bid_arr[] = $vbook['bid'];
  253. }
  254. $user_detail = UserService::getById($user->uid);
  255. $sign_recomm_bid_key = '男频';
  256. if ($user_detail && isset($user_detail->sex)) {
  257. if ($user_detail->sex == 2) {
  258. $sign_recomm_bid_key = '女频';
  259. }
  260. }
  261. $hot_book_num = 3;
  262. $recomm_books = BookConfigService::getSignRecommendBooks($read_bid_arr, $sign_recomm_bid_key, $hot_book_num);
  263. $content .= "\n\n" . '热门书籍推荐';
  264. if ($recomm_books) {
  265. foreach ($recomm_books as $book) {
  266. $url = env('PROTOCOL') . '://site' . $encode_distribution_channel_id . '.' . env('WECHAT_CUSTOM_HOST') . '.com/reader?bid=' . Hashids::encode($book->bid) . '&cid=' . $book->first_cid;
  267. $content .= "\n\n" . '<a href="' . $url . '"> ☞ 《' . $book->book_name . '》</a>';
  268. }
  269. }
  270. //$content .= "\n\n" . '为方便下次阅读,请置顶公众号';
  271. $content .= "\n\n" . '为方便下次阅读,请<a href="' . 'https://help.' . env('WECHAT_CUSTOM_HOST') . '.com/top.html"' . '>置顶公众号</a>';
  272. if ($attach_content) {
  273. $content .= "\n\n" . $attach_content;
  274. }
  275. if ($new_user_activity_content) {
  276. $content .= $new_user_activity_content;
  277. }
  278. } // 空用户推默认的文案
  279. else {
  280. $encode_distribution_channel_id = encodeDistributionChannelId($distribution_channel_id);
  281. $url = env('PROTOCOL') . '://site' . $encode_distribution_channel_id . '.' . env('WECHAT_CUSTOM_HOST') . '.com/sign';
  282. $content = "尊敬的会员:\n\n" . '<a href="' . $url . '"> 💰点击此处签到领书币</a>';
  283. }
  284. return $content;
  285. }
  286. private static function newUserActivity($user)
  287. {
  288. $content = '';
  289. $status = self::newUserActivityStatus($user->uid);
  290. $record = [];
  291. //新关未付费用户42小时后充推送活动 68元的活动 文案:全年免费看书
  292. if (strtotime($user->created_at) + 42 * 3600 < time() &&
  293. $user->distribution_channel_id == 123 &&
  294. !in_array(1, $status)
  295. ) {
  296. $url = env('PROTOCOL') . '://site' . encodeDistributionChannelId($user->distribution_channel_id) . '.' . env('CUSTOM_HOST') . '.com/sale/seYearActivity?fromtype=signcallback_forever&send_time=' . time();
  297. $content .= "\n\n" . '<a href="' . $url . '"> 💰全年免费看书</a>';
  298. $record[] = ['uid' => $user->uid, 'type' => 1, 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s')];
  299. }
  300. //20小时的 文字链的文案:书币充值特惠
  301. if (strtotime($user->created_at) + 20 * 3600 < time() && !in_array(2, $status)) {
  302. $url = env('PROTOCOL') . '://site' . encodeDistributionChannelId($user->distribution_channel_id) . '.' . env('CUSTOM_HOST') . '.com/sale/newUserSale?fromtype=signcallback_newUserSale&send_time=' . time();
  303. $content .= "\n\n" . '<a href="' . $url . '"> 💰书币充值特惠</a>';
  304. $record[] = ['uid' => $user->uid, 'type' => 2, 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s')];
  305. }
  306. //36小时的。文案:充9.9送2000书币
  307. if (strtotime($user->created_at) + 36 * 3600 < time() && !in_array(3, $status)) {
  308. $url = env('PROTOCOL') . '://site' . encodeDistributionChannelId($user->distribution_channel_id) . '.' . env('CUSTOM_HOST') . '.com/sale/newUserActivity?fromtype=signcallback_newUserActivity&send_time=' . time();
  309. $content .= "\n\n" . '<a href="' . $url . '"> 💰充9.9得2000书币</a>';
  310. $record[] = ['uid' => $user->uid, 'type' => 3, 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s')];
  311. }
  312. self::recordnewUserActivityPush($record);
  313. return $content;
  314. }
  315. private static function newUserActivityStatus($uid)
  316. {
  317. $data = [-1];
  318. if (!$uid) {
  319. return $data;
  320. }
  321. $result = DB::table('user_sign_push_activity')->where('uid', $uid)->select('type')->get();
  322. if ($result) {
  323. foreach ($result as $v) {
  324. array_push($data, $v->type);
  325. }
  326. }
  327. return $data;
  328. }
  329. private static function recordnewUserActivityPush($data)
  330. {
  331. if ($data) {
  332. try {
  333. DB::table('user_sign_push_activity')->insert($data);
  334. } catch (\Exception $e) {
  335. }
  336. }
  337. }
  338. /**
  339. *
  340. * @param $openid
  341. * @return string
  342. */
  343. public static function userSignReturnContent($openid)
  344. {
  345. $content = '';
  346. $day = date('Y-m-d');
  347. $user = ForceSubscribeUsers::getOneForceSubscribeUsersByOpenid($openid);
  348. if ($user) {
  349. $encode_distribution_channel_id = encodeDistributionChannelId($user->distribution_channel_id);
  350. $attach_content = self::signCallBackPushActivityInfo($user->uid);
  351. $continueReadUrl = env('PROTOCOL') . '://site' . $encode_distribution_channel_id . '.' . env('CUSTOM_HOST') . '.com/continue';
  352. $is_sign = self::isSign($user->uid);
  353. if ($is_sign) {
  354. $content = '今日已经签到过了,明日继续签到得书币哦~' . "\n\n" . '<a href=' . '"' . $continueReadUrl . '"' . '> >>点我继续上次阅读</a>';
  355. /*if($attach_content){
  356. $content .= "\n\n" .$attach_content;
  357. }*/
  358. //return $content;
  359. } else {
  360. $sign_stat = self::sign($user->uid, $day);
  361. $content = '今日签到成功,赠送' . $sign_stat . '书币,明日继续签到得书币哦~' . "\n\n" . '<a href=' . '"' . $continueReadUrl . '"' . '> >>点我继续上次阅读</a>' . "\n\n" . '阅读记录:';
  362. if ($sign_stat == 30) {
  363. $content = '今日签到成功,赠送30书币,连续签到2日后,赠送书币增加至50哦~' . "\n\n" . '<a href=' . '"' . $continueReadUrl . '"' . '> >>点我继续上次阅读</a>' . "\n\n" . '阅读记录:';
  364. }
  365. }
  366. $new_user_activity_content = '';
  367. if (!Order::where('uid', $user->uid)->where('status', 'PAID')->select('id')->first()) {
  368. $new_user_activity_content = self::newUserActivity($user);
  369. }
  370. $res = ReadRecordService::getReadRecord($user->uid);
  371. foreach ($res as $key => $record) {
  372. if ($key == 3) break;
  373. $url = env('PROTOCOL') . '://site' . $encode_distribution_channel_id . '.' . env('CUSTOM_HOST') . '.com/reader?bid=' . Hashids::encode($record['bid']) . '&cid=' . $record['cid'];
  374. $content .= "\n\n" . '<a href="' . $url . '"> >>《' . $record['book_name'] . '》</a>';
  375. }
  376. $read_bid_arr = [];
  377. $read_bid_arr[] = -1;
  378. foreach ($res as $vbook) {
  379. $read_bid_arr[] = $vbook['bid'];
  380. }
  381. $user_detail = UserService::getById($user->uid);
  382. $sign_recomm_bid_key = '男频';
  383. if ($user_detail && isset($user_detail->sex)) {
  384. if ($user_detail->sex == 2) {
  385. $sign_recomm_bid_key = '女频';
  386. }
  387. }
  388. $recomm_books = BookConfigService::getSignRecommendBooks($read_bid_arr, $sign_recomm_bid_key);
  389. $content .= "\n\n" . '热门书籍推荐';
  390. if ($recomm_books) {
  391. foreach ($recomm_books as $book) {
  392. $url = env('PROTOCOL') . '://site' . $encode_distribution_channel_id . '.' . env('CUSTOM_HOST') . '.com/reader?bid=' . Hashids::encode($book->bid) . '&cid=' . $book->first_cid;
  393. $content .= "\n\n" . '<a href="' . $url . '"> >>《' . $book->book_name . '》</a>';
  394. }
  395. }
  396. //$content .= "\n\n" . '为方便下次阅读,请置顶公众号';
  397. $content .= "\n\n" . '为方便下次阅读,请<a href="' . 'https://help.leyuee.com/top.html"' . '>置顶公众号</a>';
  398. if ($attach_content) {
  399. $content .= "\n\n" . $attach_content;
  400. }
  401. if ($new_user_activity_content) {
  402. $content .= "\n\n" . $new_user_activity_content;
  403. }
  404. }
  405. return $content;
  406. }
  407. public static function signCallBackPushActivityInfo($uid, $distribution_channel_id)
  408. {
  409. $activity_setting = ActivityService::getActivitySetting();
  410. if (!$activity_setting)
  411. return false;
  412. $acyivity_id = isset($activity_setting['activity_id']) ? $activity_setting['activity_id'] : 0;
  413. $other = env('OTHER_ACTIVITY_ID', 0);
  414. if ($acyivity_id == $other && !in_array($distribution_channel_id, explode(',', env('OTHER_ACTIVITY_CHANNEL', '1')))) {
  415. return false;
  416. }
  417. if (!$acyivity_id)
  418. return false;
  419. $activity_title = isset($activity_setting['sign_call_back_text']) ? $activity_setting['sign_call_back_text'] : '';;
  420. if (empty($activity_title))
  421. return false;
  422. $activity_info = ActivityService::getById($acyivity_id);
  423. if (empty($activity_info))
  424. return false;
  425. if (time() < strtotime($activity_info->start_time) || time() > strtotime($activity_info->end_time)) {
  426. return false;
  427. }
  428. $user = UserService::getById($uid);
  429. if (empty($user))
  430. return false;
  431. if (!ActivitySwitchService::isShowInPage($acyivity_id, $distribution_channel_id, 'sign')) {
  432. return false;
  433. }
  434. /*
  435. $no_participate_activity = env('no_participate_activity','');
  436. if($no_participate_activity && in_array($user->distribution_channel_id, explode(',',$no_participate_activity))){
  437. return false;
  438. }*/
  439. if ($user && isset($user->created_at) && (time() - strtotime($user->created_at)) >= 86400 * 2) {
  440. $url = env('PROTOCOL') . '://site' . encodeDistributionChannelId($distribution_channel_id) . '.' . env('CUSTOM_HOST') . '.com' . $activity_info->activity_page . '&fromtype=signcallback';
  441. return '<a href="' . $url . '"> ' . $activity_title . '</a>';
  442. }
  443. return false;
  444. }
  445. }