UserSignService.php 16 KB

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