UserSignServices.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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 think\facade\Log;
  16. use think\exception\ValidateException;
  17. /**
  18. *
  19. * Class UserSignServices
  20. * @package app\services\user
  21. */
  22. class UserSignServices extends BaseServices
  23. {
  24. /**
  25. * UserSignServices constructor.
  26. * @param UserSignDao $dao
  27. */
  28. public function __construct(UserSignDao $dao)
  29. {
  30. $this->dao = $dao;
  31. }
  32. /**
  33. * 获取用户是否签到
  34. * @param $uid
  35. * @return bool
  36. */
  37. public function getIsSign(int $uid, string $type = 'today')
  38. {
  39. return $this->dao->count(['uid' => $uid, 'time' => $type]) ? true : false;
  40. }
  41. /**
  42. * 获取用户累计签到次数
  43. * @Parma int $uid 用户id
  44. * @return int
  45. * */
  46. public function getSignSumDay(int $uid)
  47. {
  48. return $this->dao->count(['uid' => $uid]);
  49. }
  50. /**
  51. * 设置签到数据
  52. * @param int $uid 用户uid
  53. * @param string $title 签到说明
  54. * @param int $number 签到获得积分
  55. * @param int $balance 签到前剩余积分
  56. * @return object
  57. * */
  58. public function setSignData($uid, $title = '', $number = 0, $integral_balance = 0, $exp_banlance = 0, $exp_num = 0)
  59. {
  60. $data = [];
  61. $data['uid'] = $uid;
  62. $data['title'] = $title;
  63. $data['number'] = $number;
  64. $data['balance'] = $integral_balance;
  65. $data['add_time'] = time();
  66. if (!$this->dao->save($data)) {
  67. throw new ValidateException('添加签到数据失败');
  68. }
  69. /** @var UserBillServices $userBill */
  70. $userBill = app()->make(UserBillServices::class);
  71. $data['mark'] = $title;
  72. $userBill->incomeIntegral($uid, 'sign', $data);
  73. $data['number'] = $exp_num;
  74. $data['category'] = 'exp';
  75. $data['type'] = 'sign';
  76. $data['title'] = $data['mark'] = '签到奖励';
  77. $data['balance'] = $exp_banlance;
  78. $data['pm'] = 1;
  79. $data['status'] = 1;
  80. if (!$userBill->save($data)) {
  81. throw new ValidateException('赠送经验失败');
  82. }
  83. //检测会员等级
  84. try {
  85. //用户升级事件
  86. event('user.userLevel', [$uid]);
  87. } catch (\Throwable $e) {
  88. Log::error('会员等级升级失败,失败原因:' . $e->getMessage());
  89. }
  90. return true;
  91. }
  92. /**
  93. * 获取用户签到列表
  94. * @param int $uid
  95. * @param string $field
  96. * @throws \think\db\exception\DataNotFoundException
  97. * @throws \think\db\exception\DbException
  98. * @throws \think\db\exception\ModelNotFoundException
  99. */
  100. public function getUserSignList(int $uid, string $field = '*')
  101. {
  102. [$page, $limit] = $this->getPageValue();
  103. $list = $this->dao->getList(['uid' => $uid], $field, $page, $limit);
  104. foreach ($list as &$item) {
  105. $item['add_time'] = $item['add_time'] ? date('Y-m-d', $item['add_time']) : '';
  106. }
  107. return $list;
  108. }
  109. /**
  110. * 用户签到
  111. * @param $uid
  112. * @return bool|int|mixed
  113. * @throws \think\db\exception\DataNotFoundException
  114. * @throws \think\db\exception\DbException
  115. * @throws \think\db\exception\ModelNotFoundException
  116. */
  117. public function sign(int $uid)
  118. {
  119. $sign_list = \crmeb\services\GroupDataService::getData('sign_day_num') ?: [];
  120. if (!count($sign_list)) {
  121. throw new ValidateException('请先配置签到天数');
  122. }
  123. /** @var UserServices $userServices */
  124. $userServices = app()->make(UserServices::class);
  125. $user = $userServices->getUserInfo($uid);
  126. if (!$user) {
  127. throw new ValidateException('用户不存在');
  128. }
  129. if ($this->getIsSign($uid, 'today')) {
  130. throw new ValidateException('已经签到');
  131. }
  132. $sign_num = 0;
  133. $user_sign_num = $user['sign_num'];
  134. //检测昨天是否签到
  135. if ($this->getIsSign($uid, 'yesterday')) {
  136. if ($user->sign_num > (count($sign_list) - 1)) {
  137. $user->sign_num = 0;
  138. }
  139. } else {
  140. $user->sign_num = 0;
  141. }
  142. foreach ($sign_list as $key => $item) {
  143. if ($key == $user->sign_num) {
  144. $sign_num = $item['sign_num'];
  145. break;
  146. }
  147. }
  148. $user->sign_num += 1;
  149. if ($user->sign_num == count($sign_list)) {
  150. $title = '连续签到奖励';
  151. } else {
  152. $title = '签到奖励';
  153. }
  154. $exp_num = sys_config('sign_give_exp');
  155. //增加签到数据
  156. $this->transaction(function () use ($uid, $title, $sign_num, $user, $exp_num) {
  157. $this->setSignData($uid, $title, $sign_num, $user['integral'], (int)$user['exp'], $exp_num);
  158. $user->integral = (int)$user->integral + (int)$sign_num;
  159. $user->exp = (int)$user->exp + (int)$exp_num;
  160. if (!$user->save()) {
  161. throw new ValidateException('修改用户信息失败');
  162. }
  163. });
  164. return $sign_num;
  165. }
  166. /**
  167. * 签到用户信息
  168. * @param int $uid
  169. * @param $sign
  170. * @param $integral
  171. * @param $all
  172. * @return mixed
  173. */
  174. public function signUser(int $uid, $sign, $integral, $all)
  175. {
  176. /** @var UserServices $userServices */
  177. $userServices = app()->make(UserServices::class);
  178. $user = $userServices->getUserInfo($uid);
  179. if (!$user) {
  180. throw new ValidateException('数据不存在');
  181. }
  182. //是否统计签到
  183. if ($sign || $all) {
  184. $user['sum_sgin_day'] = $this->getSignSumDay($user['uid']);
  185. $user['is_day_sgin'] = $this->getIsSign($user['uid']);
  186. $user['is_YesterDay_sgin'] = $this->getIsSign($user['uid'], 'yesterday');
  187. if (!$user['is_day_sgin'] && !$user['is_YesterDay_sgin']) {
  188. $user['sign_num'] = 0;
  189. }
  190. }
  191. //是否统计积分使用情况
  192. if ($integral || $all) {
  193. /** @var UserBillServices $userBill */
  194. $userBill = app()->make(UserBillServices::class);
  195. $user['sum_integral'] = intval($userBill->getRecordCount($user['uid'], 'integral', 'sign,system_add,gain'));
  196. $user['deduction_integral'] = intval($userBill->getRecordCount($user['uid'], 'integral', 'deduction', '', true) ?? 0);
  197. $user['today_integral'] = intval($userBill->getRecordCount($user['uid'], 'integral', 'sign,system_add,gain', 'today'));
  198. }
  199. unset($user['pwd']);
  200. if (!$user['is_promoter']) {
  201. $user['is_promoter'] = (int)sys_config('store_brokerage_statu') == 2 ? true : false;
  202. }
  203. 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();
  204. }
  205. /**
  206. * 获取签到
  207. * @param $uid
  208. * @return array
  209. */
  210. public function getSignMonthList($uid)
  211. {
  212. [$page, $limit] = $this->getPageValue();
  213. $data = $this->dao->getListGroup(['uid' => $uid], 'FROM_UNIXTIME(add_time,"%Y-%m") as time,group_concat(id SEPARATOR ",") ids', $page, $limit, 'time');
  214. $list = [];
  215. if ($data) {
  216. $ids = array_unique(array_column($data, 'ids'));
  217. $dataIdsList = $this->dao->getList(['id' => $ids], 'FROM_UNIXTIME(add_time,"%Y-%m-%d") as add_time,title,number,id,uid', 0, 0);
  218. foreach ($data as $item) {
  219. $value['month'] = $item['time'];
  220. $value['list'] = array_merge(array_filter($dataIdsList, function ($val) use ($item) {
  221. if (in_array($val['id'], explode(',', $item['ids']))) {
  222. return $val;
  223. }
  224. }));
  225. array_push($list, $value);
  226. }
  227. }
  228. return $list;
  229. }
  230. }