UserSignServices.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\services\user;
  13. use app\services\BaseServices;
  14. use app\dao\user\UserSignDao;
  15. use app\services\system\SystemSignRewardServices;
  16. use app\services\user\member\MemberCardServices;
  17. use crmeb\exceptions\ApiException;
  18. use think\facade\Log;
  19. /**
  20. *
  21. * Class UserSignServices
  22. * @package app\services\user
  23. */
  24. class UserSignServices extends BaseServices
  25. {
  26. /**
  27. * UserSignServices constructor.
  28. * @param UserSignDao $dao
  29. */
  30. public function __construct(UserSignDao $dao)
  31. {
  32. $this->dao = $dao;
  33. }
  34. /**
  35. * 获取用户是否签到
  36. * @param int $uid
  37. * @param string $type
  38. * @return bool
  39. * @throws \ReflectionException
  40. * @author: 吴汐
  41. * @email: 442384644@qq.com
  42. * @date: 2023/8/8
  43. */
  44. public function getIsSign(int $uid, string $type = 'today')
  45. {
  46. return (bool)$this->dao->count(['uid' => $uid, 'time' => $type]);
  47. }
  48. /**
  49. * 获取用户累计签到次数
  50. * @param int $uid
  51. * @return int
  52. * @throws \ReflectionException
  53. * @author: 吴汐
  54. * @email: 442384644@qq.com
  55. * @date: 2023/8/8
  56. */
  57. public function getSignSumDay(int $uid)
  58. {
  59. return $this->dao->count(['uid' => $uid]);
  60. }
  61. /**
  62. * 设置签到数据
  63. * @param $uid
  64. * @param string $title
  65. * @param int $number
  66. * @param int $integral_balance
  67. * @param int $exp_banlance
  68. * @param int $exp_num
  69. * @return bool
  70. * @throws \think\Exception
  71. * @author: 吴汐
  72. * @email: 442384644@qq.com
  73. * @date: 2023/8/8
  74. */
  75. public function setSignData($uid, $title = '', $number = 0, $integral_balance = 0, $exp_banlance = 0, $exp_num = 0)
  76. {
  77. $data = [];
  78. $data['uid'] = $uid;
  79. $data['title'] = $title;
  80. $data['number'] = $number;
  81. $data['balance'] = $integral_balance + $number;
  82. $data['add_time'] = time();
  83. if (!$this->dao->save($data)) {
  84. throw new ApiException(410290);
  85. }
  86. /** @var UserBillServices $userBill */
  87. $userBill = app()->make(UserBillServices::class);
  88. $data['mark'] = $title;
  89. $userBill->incomeIntegral($uid, 'sign', $data);
  90. if ($exp_num) {
  91. $data['number'] = $exp_num;
  92. $data['category'] = 'exp';
  93. $data['type'] = 'sign';
  94. $data['title'] = $data['mark'] = '签到奖励';
  95. $data['balance'] = $exp_banlance + $exp_num;
  96. $data['pm'] = 1;
  97. $data['status'] = 1;
  98. if (!$userBill->save($data)) {
  99. throw new ApiException(410291);
  100. }
  101. //检测会员等级
  102. try {
  103. //用户升级事件
  104. event('UserLevelListener', [$uid]);
  105. } catch (\Throwable $e) {
  106. Log::error('会员等级升级失败,失败原因:' . $e->getMessage());
  107. }
  108. }
  109. return true;
  110. }
  111. /**
  112. * 获取用户签到列表
  113. * @param int $uid
  114. * @param string $field
  115. * @return array
  116. * @throws \think\db\exception\DataNotFoundException
  117. * @throws \think\db\exception\DbException
  118. * @throws \think\db\exception\ModelNotFoundException
  119. * @author: 吴汐
  120. * @email: 442384644@qq.com
  121. * @date: 2023/8/8
  122. */
  123. public function getUserSignList(int $uid, string $field = '*')
  124. {
  125. [$page, $limit] = $this->getPageValue();
  126. $list = $this->dao->getList(['uid' => $uid], $field, $page, $limit);
  127. foreach ($list as &$item) {
  128. $item['add_time'] = $item['add_time'] ? date('Y-m-d', $item['add_time']) : '';
  129. }
  130. return $list;
  131. }
  132. /**
  133. * 用户签到
  134. * @param $uid
  135. * @return bool|int|mixed
  136. * @throws \think\db\exception\DataNotFoundException
  137. * @throws \think\db\exception\DbException
  138. * @throws \think\db\exception\ModelNotFoundException
  139. */
  140. public function sign(int $uid)
  141. {
  142. /** @var UserServices $userServices */
  143. $userServices = app()->make(UserServices::class);
  144. //检测用户是否存在
  145. $user = $userServices->getUserInfo($uid);
  146. if (!$user) {
  147. throw new ApiException(410032);
  148. }
  149. $userServices->offMemberLevel($uid);
  150. //检测今天是否已经签到
  151. if ($this->getIsSign($uid, 'today')) {
  152. throw new ApiException(410293);
  153. }
  154. $title = '签到奖励';
  155. //检测昨天是否签到,如果没有签到,连续签到清0
  156. if (!$this->getIsSign($uid, 'yesterday')) $user->sign_num = 0;
  157. //获取签到周期配置,如果周签到,每周一清空连续签到记录,如果月签到,每月一日清空连续签到记录
  158. $sign_mode = sys_config('sign_mode', -1);
  159. if ($sign_mode == 1 && date('w') == 1) $user->sign_num = 0;
  160. if ($sign_mode == 0 && date('d') == 1) $user->sign_num = 0;
  161. //连续签到天数
  162. $user->sign_num += 1;
  163. $continuousDays = $user->sign_num;
  164. //累积签到天数
  165. $cumulativeDays = $this->dao->getCumulativeDays($sign_mode, $uid);
  166. //基础签到奖励
  167. $sign_point = sys_config('sign_give_point', 0);
  168. $sign_exp = sys_config('member_func_status', 1) ? sys_config('sign_give_exp', 0) : 0;
  169. //连续签到和累积签到奖励
  170. $signRewardsServices = app()->make(SystemSignRewardServices::class);
  171. [$continuousStatus, $continuousRewardPoint, $continuousRewardExp] = $signRewardsServices->getSignRewards(0, $continuousDays);
  172. [$cumulativeStatus, $cumulativeRewardPoint, $cumulativeRewardExp] = $signRewardsServices->getSignRewards(1, $cumulativeDays);
  173. if ($continuousStatus && $cumulativeStatus) {
  174. $sign_point = $continuousRewardPoint + $cumulativeRewardPoint;
  175. $sign_exp = $continuousRewardExp + $cumulativeRewardExp;
  176. } elseif ($continuousStatus) {
  177. $sign_point = $continuousRewardPoint;
  178. $sign_exp = $continuousRewardExp;
  179. } elseif ($cumulativeStatus) {
  180. $sign_point = $cumulativeRewardPoint;
  181. $sign_exp = $cumulativeRewardExp;
  182. }
  183. //会员签到积分会员奖励
  184. if ($user->is_money_level > 0) {
  185. //看是否开启签到积分翻倍奖励
  186. /** @var MemberCardServices $memberCardService */
  187. $memberCardService = app()->make(MemberCardServices::class);
  188. $sign_rule_number = $memberCardService->isOpenMemberCard('sign');
  189. if ($sign_rule_number) {
  190. $up_num = (int)$sign_rule_number * $sign_point - $sign_point;
  191. $sign_point = (int)$sign_rule_number * $sign_point;
  192. if (!$this->getIsSign($uid, 'yesterday')) $title = '签到奖励(SVIP+' . $up_num . ')';
  193. }
  194. }
  195. //增加签到数据
  196. $this->transaction(function () use ($uid, $title, $sign_point, $user, $sign_exp) {
  197. $this->setSignData($uid, $title, $sign_point, $user['integral'], (int)$user['exp'], $sign_exp);
  198. $user->integral = (int)$user->integral + (int)$sign_point;
  199. // if ($sign_exp) $user->exp = (int)$user->exp + (int)$sign_exp;
  200. if ($sign_exp) $user->exp = bcadd($user->exp, $sign_exp, 2);
  201. if (!$user->save()) {
  202. throw new ApiException(410287);
  203. }
  204. });
  205. return $sign_point;
  206. }
  207. /**
  208. * 签到用户信息
  209. * @param int $uid
  210. * @param $sign
  211. * @param $integral
  212. * @param $all
  213. * @return array
  214. * @throws \think\db\exception\DataNotFoundException
  215. * @throws \think\db\exception\DbException
  216. * @throws \think\db\exception\ModelNotFoundException
  217. * @author: 吴汐
  218. * @email: 442384644@qq.com
  219. * @date: 2023/8/8
  220. */
  221. public function signUser(int $uid, $sign, $integral, $all)
  222. {
  223. /** @var UserServices $userServices */
  224. $userServices = app()->make(UserServices::class);
  225. $user = $userServices->getUserInfo($uid);
  226. if (!$user) {
  227. throw new ApiException(100026);
  228. }
  229. //是否统计签到
  230. if ($sign || $all) {
  231. $user['sum_sgin_day'] = $this->getSignSumDay($user['uid']);
  232. $user['is_day_sgin'] = false;
  233. $user['is_YesterDay_sgin'] = $this->getIsSign($user['uid'], 'yesterday');
  234. if (!$user['is_day_sgin'] && !$user['is_YesterDay_sgin']) {
  235. $user['sign_num'] = 0;
  236. }
  237. }
  238. //是否统计积分使用情况
  239. if ($integral || $all) {
  240. /** @var UserBillServices $userBill */
  241. $userBill = app()->make(UserBillServices::class);
  242. $user['sum_integral'] = intval($userBill->getRecordCount($user['uid'], 'integral', 'sign,system_add,gain,lottery_add,product_gain,pay_product_integral_back'));
  243. $user['deduction_integral'] = intval($userBill->getRecordCount($user['uid'], 'integral', 'deduction,lottery_use,order_deduction,storeIntegral_use', '', true) ?? 0);
  244. $user['today_integral'] = intval($userBill->getRecordCount($user['uid'], 'integral', 'sign,system_add,gain,product_gain,lottery_add,pay_product_integral_back', 'today'));
  245. /** @var UserBillServices $userBillServices */
  246. $userBillServices = app()->make(UserBillServices::class);
  247. $user['frozen_integral'] = $userBillServices->getBillSum(['uid' => $user['uid'], 'is_frozen' => 1]);
  248. }
  249. unset($user['pwd']);
  250. if (!$user['is_promoter']) {
  251. $user['is_promoter'] = (int)sys_config('store_brokerage_statu') == 2;
  252. }
  253. return $user->hidden(['account', 'real_name', 'birthday', 'card_id', 'mark', 'partner_id', 'group_id', 'add_time', 'add_ip', 'phone', 'last_time', 'last_ip', 'spread_uid', 'spread_time', 'user_type', 'status', 'level', 'clean_time', 'addres'])->toArray();
  254. }
  255. /**
  256. * 获取签到
  257. * @param $uid
  258. * @return array
  259. * @throws \think\db\exception\DataNotFoundException
  260. * @throws \think\db\exception\DbException
  261. * @throws \think\db\exception\ModelNotFoundException
  262. * @author: 吴汐
  263. * @email: 442384644@qq.com
  264. * @date: 2023/8/8
  265. */
  266. public function getSignMonthList($uid)
  267. {
  268. [$page, $limit] = $this->getPageValue();
  269. $data = $this->dao->getListGroup(['uid' => $uid], 'FROM_UNIXTIME(add_time,"%Y-%m") as time,group_concat(id SEPARATOR ",") ids', $page, $limit, 'time');
  270. $list = [];
  271. if ($data) {
  272. $ids = array_unique(array_column($data, 'ids'));
  273. $dataIdsList = $this->dao->getList(['id' => $ids], 'FROM_UNIXTIME(add_time,"%Y-%m-%d") as add_time,title,number,id,uid', 0, 0);
  274. foreach ($data as $item) {
  275. $value['month'] = $item['time'];
  276. $value['list'] = array_merge(array_filter($dataIdsList, function ($val) use ($item) {
  277. if (in_array($val['id'], explode(',', $item['ids']))) {
  278. return $val;
  279. }
  280. }));
  281. array_push($list, $value);
  282. }
  283. }
  284. return $list;
  285. }
  286. /**
  287. * 返回签到列表数据
  288. * @param $uid
  289. * @return array
  290. * @throws \think\db\exception\DataNotFoundException
  291. * @throws \think\db\exception\DbException
  292. * @throws \think\db\exception\ModelNotFoundException
  293. * @author: 吴汐
  294. * @email: 442384644@qq.com
  295. * @date: 2023/8/8
  296. */
  297. public function signConfig($uid)
  298. {
  299. //获取周签到还是月签到
  300. $signMode = (int)sys_config('sign_mode', 1);
  301. //获取签到列表
  302. $startDate = $signMode == 1 ? strtotime('this week Monday') : strtotime('first day of this month midnight');
  303. $endDate = $signMode == 1 ? strtotime('this week Sunday') : strtotime('last day of this month midnight');
  304. $dateList = range($startDate, $endDate, 86400);
  305. //获取已经签到的列表
  306. $list = $this->dao->getUserSignList($signMode, $uid);
  307. //获取累积签到和连续签到
  308. $cumulativeSignDays = $this->dao->getCumulativeDays($signMode, $uid);
  309. $continuousSignDays = app()->make(UserServices::class)->value($uid, 'sign_num');
  310. //获取累积签到和连续签到奖励
  311. $nextCumulativeSignRewardList = app()->make(SystemSignRewardServices::class)->selectList(['type' => 1], '*', 1, 200, 'days asc')->toArray();
  312. $nextContinuousSignRewardList = app()->make(SystemSignRewardServices::class)->selectList(['type' => 0], '*', 1, 200, 'days asc')->toArray();
  313. //下一次连续签到奖励还需签到几天
  314. $nextContinuousDays = 0;
  315. foreach ($nextContinuousSignRewardList as $continuousNext) {
  316. if ($continuousSignDays < $continuousNext['days']) {
  317. $nextContinuousDays = $continuousNext['days'] - $continuousSignDays > 0 ? $continuousNext['days'] - $continuousSignDays : 1;
  318. break;
  319. }
  320. }
  321. $nextCumulativeDays = 0;
  322. foreach ($nextCumulativeSignRewardList as $cumulativeNext) {
  323. if ($cumulativeSignDays < $cumulativeNext['days']) {
  324. $nextCumulativeDays = $cumulativeNext['days'] - $cumulativeSignDays > 0 ? $cumulativeNext['days'] - $cumulativeSignDays : 1;
  325. break;
  326. }
  327. }
  328. //整理签到列表数据
  329. $signList = [];
  330. $i = 0;
  331. $checkSign = $this->getIsSign($uid, 'today');
  332. foreach ($dateList as $key => $time) {
  333. $day = date('m.d', $time);
  334. if ($day[0] == '0') $day = substr($day, 1);
  335. $signList[$key]['day'] = $day;
  336. $signList[$key]['is_sign'] = false;
  337. $signList[$key]['type'] = 0;
  338. //判断当前签到日期
  339. $signList[$key]['sign_day'] = date('Y-m-d', $time) == date('Y-m-d', time());
  340. //判断今日是否签到
  341. foreach ($list as $value) {
  342. if (date('Y-m-d', $time) == date('Y-m-d', $value['add_time'])) {
  343. $signList[$key]['is_sign'] = true;
  344. break;
  345. }
  346. }
  347. //处理处理签到类型展示,type 0已签到,1积分,2经验,3连续,4累积
  348. $signList[$key]['type'] = sys_config('sign_give_point', 0) == 0 && sys_config('member_func_status', 1) == 1 && sys_config('sign_give_exp', 0) > 0 ? 2 : 1;
  349. $signList[$key]['point'] = (int)sys_config('sign_give_point');
  350. if (date('Y-m-d', $time) >= date('Y-m-d', time())) {
  351. foreach ($nextContinuousSignRewardList as $continuous) {
  352. if (($continuous['days'] - $continuousSignDays) == $i) {
  353. $signList[$key]['type'] = 3;
  354. $signList[$key]['point'] = $continuous['point'];
  355. }
  356. }
  357. foreach ($nextCumulativeSignRewardList as $cumulative) {
  358. if (($cumulative['days'] - $cumulativeSignDays) == $i) {
  359. $signList[$key]['type'] = 4;
  360. $signList[$key]['point'] = $cumulative['point'];
  361. }
  362. }
  363. $i++;
  364. }
  365. }
  366. //格式化签到数据
  367. $signList = array_chunk($signList, 7);
  368. //获取用户签到提醒状态
  369. $signRemindStatus = app()->make(UserServices::class)->value($uid, 'sign_remind');
  370. //是否显示签到提醒开关
  371. $signRemindSwitch = (int)sys_config('sign_remind', 0);
  372. //签到功能是否关闭
  373. $signStatus = (int)sys_config('sign_status', 0);
  374. return compact('signList', 'continuousSignDays', 'cumulativeSignDays', 'nextContinuousDays', 'nextCumulativeDays', 'signMode', 'checkSign', 'signRemindStatus', 'signRemindSwitch', 'signStatus');
  375. }
  376. /**
  377. * 签到提醒设置
  378. * @param $uid
  379. * @param $status
  380. * @return bool
  381. * @author: 吴汐
  382. * @email: 442384644@qq.com
  383. * @date: 2023/8/9
  384. */
  385. public function setSignRemind($uid, $status)
  386. {
  387. app()->make(UserServices::class)->update($uid, ['sign_remind' => $status]);
  388. return true;
  389. }
  390. }