截流自动化的商城平台
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

SignLogic.php 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <?php
  2. namespace app\api\logic;
  3. use app\common\basics\Logic;
  4. use app\common\logic\AccountLogLogic;
  5. use app\common\model\AccountLog;
  6. use app\common\model\sign_daily\SignDaily;
  7. use app\common\model\sign_daily\UserSign;
  8. use app\common\model\user\User;
  9. use app\common\server\ConfigServer;
  10. use app\common\server\UrlServer;
  11. use think\facade\Db;
  12. /**
  13. * 签到逻辑
  14. * Class SignLogic
  15. * @package app\api\logic
  16. */
  17. class SignLogic extends Logic
  18. {
  19. /**
  20. * @notes 签到列表
  21. * @param $user_id
  22. * @return array
  23. * @throws \think\db\exception\DataNotFoundException
  24. * @throws \think\db\exception\DbException
  25. * @throws \think\db\exception\ModelNotFoundException
  26. * @author 段誉
  27. * @date 2022/2/17 18:29
  28. */
  29. public static function lists($user_id)
  30. {
  31. //用户信息
  32. $user = User::where(['id' => $user_id])
  33. ->field('id,nickname,avatar,user_integral')
  34. ->find();
  35. $user['avatar'] = UrlServer::getFileUrl($user['avatar']);
  36. $user['today_sign'] = 0;
  37. //今天签到记录
  38. $today_sign = UserSign::where(['del' => 0, 'user_id' => $user_id])
  39. ->whereTime('sign_time', 'today')
  40. ->find();
  41. //昨天签到记录
  42. $yester_sign = UserSign::where(['del' => 0, 'user_id' => $user_id])
  43. ->whereTime('sign_time', 'yesterday')
  44. ->find();
  45. //今天是否已签到
  46. $today_sign && $user['today_sign'] = 1;
  47. //昨天没签到,则签到中断重新计算连续天数
  48. if (!$yester_sign) {
  49. $today_start = strtotime(date('Y-m-d') . '00:00:00');
  50. UserSign::where(['del' => 0, 'user_id' => $user_id])
  51. ->where('sign_time', '<', $today_start)
  52. ->update(['del' => 1, 'update_time' => time()]);
  53. }
  54. //签到规则
  55. $sign_list = SignDaily::where(['del' => 0])
  56. ->order('type asc,days asc')
  57. ->column('*', 'days');
  58. // 根据签到奖励计算 (今天签到赠送的积分,累计签到天数,签到天列表)
  59. $data = self::formatSignList($user_id, $sign_list);
  60. // 累计签到天数
  61. $user['days'] = $data['total_sign_days'];
  62. // 赚积分描述
  63. $integral_tips = self::getInegralTips($user_id, $user['today_sign'], $data['today_sign_integral']);
  64. return [
  65. 'user' => $user,
  66. 'sign_list' => $data['days_list'],
  67. 'integral_tips' => $integral_tips,
  68. ];
  69. }
  70. /**
  71. * @notes 计算签到信息
  72. * @param $user_id
  73. * @param $sign_list
  74. * @return array
  75. * @author 段誉
  76. * @date 2022/2/17 18:23
  77. */
  78. public static function formatSignList($user_id, $sign_list)
  79. {
  80. // 今天签到赠送的积分
  81. $today_sign_integral = 0;
  82. // 累计签到天数
  83. $total_sign_days = 0;
  84. // 签到天
  85. $days_list = [];
  86. if (empty($sign_list)) {
  87. return [
  88. 'days_list' => $days_list,
  89. 'today_sign_integral' => $today_sign_integral,
  90. 'total_sign_days' => $total_sign_days
  91. ];
  92. }
  93. // 第一次签到规则
  94. $start_sign = current($sign_list);
  95. // 最后一次签到规则
  96. $end_sign = end($sign_list);
  97. // 每天赠送的积分
  98. $everyday_award_integral = 0;
  99. $start_sign['integral_status'] && $everyday_award_integral = $start_sign['integral'];
  100. // 累计签到的总天数
  101. $total_sign_days = UserSign::where(['del' => 0, 'user_id' => $user_id])
  102. ->order('id desc')
  103. ->value('days');
  104. for ($days = 1; $days <= $end_sign['days']; $days++) {
  105. $send_integral = $everyday_award_integral;
  106. // 连接签到赠送的积分
  107. if (isset($sign_list[$days]) && $sign_list[$days]['integral_status']) {
  108. $send_integral = $everyday_award_integral + $sign_list[$days]['integral'];
  109. }
  110. // 合并数据
  111. $days_list[$days] = [
  112. 'days' => $days,
  113. 'status' => 0,
  114. 'integral' => $send_integral,
  115. 'growth' => 0,
  116. ];
  117. // 更新签到天数之前的签到状态
  118. if ($days === $total_sign_days) {
  119. $today_sign_integral = $send_integral;// 今天签到获得的积分
  120. for ($sign_day = $days; $sign_day >= 1; $sign_day--) {
  121. $days_list[$sign_day]['status'] = 1;
  122. }
  123. }
  124. // 如果连续签到天数大于总天数,则全部标记为已签到状态
  125. if ($total_sign_days > $end_sign['days']) {
  126. $days_list[$days]['status'] = 1;
  127. }
  128. }
  129. return [
  130. 'days_list' => array_values($days_list),
  131. 'today_sign_integral' => $today_sign_integral,
  132. 'total_sign_days' => empty($total_sign_days) ? 0 : $total_sign_days
  133. ];
  134. }
  135. /**
  136. * @notes 赚积分描述
  137. * @param $user_id
  138. * @param $today_sign_status
  139. * @param $today_sign_integral
  140. * @return array
  141. * @author 段誉
  142. * @date 2022/2/17 18:29
  143. */
  144. public static function getInegralTips($user_id, $today_sign_status, $today_sign_integral)
  145. {
  146. //赚积分
  147. $tips[] = [
  148. 'name' => '每日签到',
  149. 'status' => $today_sign_status,
  150. 'type' => 1,//类型,主要用前端显示图标
  151. 'image' => UrlServer::getFileUrl('/static/common/image/default/sign.png')
  152. ];
  153. $open_award = ConfigServer::get('order_award', 'open_award', 0);
  154. //消费送积分
  155. if ($open_award > 0) {
  156. $order_award = AccountLog::where(['user_id' => $user_id, 'source_type' => AccountLog::consume_award_integral])
  157. ->whereDay('create_time')
  158. ->findOrEmpty();
  159. $tips[] = [
  160. 'name' => '消费送积分',
  161. 'status' => $order_award->isEmpty() ? 0 : 1,
  162. 'type' => 2,
  163. 'image' => UrlServer::getFileUrl('/static/common/image/default/place_order.png')
  164. ];
  165. }
  166. return $tips;
  167. }
  168. /**
  169. * @notes 签到
  170. * @param $user_id
  171. * @return array|false
  172. * @author 段誉
  173. * @date 2022/2/17 17:48
  174. */
  175. public static function sign($user_id)
  176. {
  177. Db::startTrans();
  178. try {
  179. // 连续签到天数
  180. $sign_list = SignDaily::where(['del' => 0, 'type' => 2])
  181. ->order('type asc,days asc')
  182. ->column('*', 'days');
  183. $now = time();
  184. $award_integral = 0; //签到赠送的总积分 (每天签到奖励 + 连续签到奖励)
  185. $award_growth = 0; //签到赠送的成长值 (每天签到奖励 + 连续签到奖励)
  186. $continuous_integral = 0; //连续签到积分
  187. $continuous_growth = 0; //连续签到成长值
  188. // 每天签到的奖励
  189. $everyday_sign = SignDaily::where(['del' => 0, 'type' => 1])->findOrEmpty();
  190. // 每天签到奖励
  191. if ($everyday_sign) {
  192. if ($everyday_sign['integral_status'] && $everyday_sign['integral'] > 0) {
  193. $award_integral += $everyday_sign['integral'];
  194. }
  195. if ($everyday_sign['growth_status'] && $everyday_sign['growth'] > 0) {
  196. $award_growth += $everyday_sign['growth'];
  197. }
  198. }
  199. // 签到记录
  200. $last_sign = UserSign::where(['del' => 0, 'user_id' => $user_id])
  201. ->order('id desc')
  202. ->findOrEmpty();
  203. // 无签到记录找是否有连续签到1天的奖励,有签到记录则找(上次签到天数+1天)的连续奖励
  204. if ($last_sign->isEmpty()) {
  205. $sign_day = 1;
  206. } else {
  207. $sign_day = $last_sign['days'] + 1;
  208. }
  209. // 累计签到天数,计算连续签到奖励
  210. $continuous_sign = $sign_list[$sign_day] ?? [];
  211. if ($continuous_sign) {
  212. if ($continuous_sign['integral_status'] && $continuous_sign['integral'] > 0) {
  213. $award_integral += $continuous_sign['integral'];
  214. $continuous_integral = $continuous_sign['integral'];
  215. }
  216. if ($continuous_sign['growth_status'] && $continuous_sign['growth'] > 0) {
  217. $award_growth += $continuous_sign['growth'];
  218. $continuous_growth = $continuous_sign['growth'];
  219. }
  220. }
  221. UserSign::create([
  222. 'user_id' => $user_id,
  223. 'days' => $sign_day,
  224. 'integral' => $everyday_sign['integral_status'] ? $everyday_sign['integral'] : 0,
  225. 'growth' => $everyday_sign['growth_status'] ? $everyday_sign['growth'] : 0,
  226. 'continuous_integral' => $continuous_integral,
  227. 'continuous_growth' => $continuous_growth,
  228. 'sign_time' => $now,
  229. ]);
  230. if ($award_integral) {
  231. User::where(['del' => 0, 'id' => $user_id])->inc('user_integral', $award_integral)->update();
  232. AccountLogLogic::AccountRecord($user_id, $award_integral, 1, AccountLog::sign_in_integral);
  233. }
  234. if ($award_growth) {
  235. User::where(['del' => 0, 'id' => $user_id])->inc('user_growth', $award_growth)->update();
  236. AccountLogLogic::AccountRecord($user_id, $award_growth, 1, AccountLog::sign_give_growth);
  237. }
  238. Db::commit();
  239. return [
  240. 'days' => $sign_day,
  241. 'integral' => $award_integral,
  242. 'growth' => $award_growth
  243. ];
  244. } catch (\Exception $e) {
  245. Db::rollback();
  246. self::$error = $e->getMessage();
  247. return false;
  248. }
  249. }
  250. /**
  251. * @notes 获取签到规则
  252. * @return array|int|mixed|string|null
  253. * @author 段誉
  254. * @date 2022/2/17 14:47
  255. */
  256. public static function getRule()
  257. {
  258. return ['rule' => ConfigServer::get('sign_rule', 'instructions', '')];
  259. }
  260. }