UserSignService.php 16 KB

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