UserSign.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <?php
  2. namespace app\models\user;
  3. use crmeb\basic\BaseModel;
  4. use crmeb\services\SystemConfigService;
  5. use crmeb\traits\ModelTrait;
  6. /**
  7. * TODO 用户签到模型 Model
  8. * Class UserSign
  9. * @package app\models\user
  10. */
  11. class UserSign extends BaseModel
  12. {
  13. /**
  14. * 数据表主键
  15. * @var string
  16. */
  17. protected $pk = 'id';
  18. /**
  19. * 模型名称
  20. * @var string
  21. */
  22. protected $name = 'user_sign';
  23. use ModelTrait;
  24. /**
  25. * 获取用户是否签到
  26. * @param $uid
  27. * @return bool
  28. */
  29. public static function getIsSign($uid, string $type = 'today')
  30. {
  31. return self::where('uid', $uid)->whereTime('add_time', $type)->count() ? true : false;
  32. }
  33. /**
  34. * 设置签到数据
  35. * @param int $uid 用户uid
  36. * @param string $title 签到说明
  37. * @param int $number 签到获得积分
  38. * @param int $balance 签到前剩余积分
  39. * @return object
  40. * */
  41. public static function setSignData($uid, $title = '', $number = 0, $balance = 0)
  42. {
  43. $add_time = time();
  44. return self::create(compact('uid', 'title', 'number', 'balance', 'add_time')) && UserBill::income($title, $uid, 'integral', 'sign', $number, 0, $balance, $title);
  45. }
  46. /**
  47. * 分页获取用户签到数据
  48. * @param int $uid 用户uid
  49. * @param int $page 页码
  50. * @param int $limit 显示多少条
  51. * @return array
  52. * */
  53. public static function getSignList($uid, $page, $limit)
  54. {
  55. if (!$limit) return [];
  56. if ($page) {
  57. return UserBill::where('a.category', 'integral')
  58. ->where('a.type', 'sign')
  59. ->where('a.status', 1)
  60. ->where('a.uid', $uid)
  61. ->alias('a')
  62. ->join("user u", 'u.uid=a.uid')
  63. ->order('a.add_time desc')
  64. ->field('FROM_UNIXTIME(a.add_time,"%Y-%m-%d") as add_time,a.title,a.number')
  65. ->page((int)$page, (int)$limit)
  66. ->select();
  67. } else {
  68. return UserBill::where('a.category', 'integral')
  69. ->where('a.type', 'sign')
  70. ->where('a.status', 1)
  71. ->where('a.uid', $uid)
  72. ->alias('a')
  73. ->join("user u", 'u.uid=a.uid')
  74. ->order('a.add_time desc')
  75. ->field('FROM_UNIXTIME(a.add_time,"%Y-%m-%d") as add_time,a.title,a.number')
  76. ->select();
  77. }
  78. }
  79. /**
  80. * 获取用户累计签到次数
  81. * @Parma int $uid 用户id
  82. * @return int
  83. * */
  84. public static function getSignSumDay($uid)
  85. {
  86. return self::where('uid', $uid)->count();
  87. }
  88. /**
  89. * 获取用户今天是否签到
  90. * @param int $uid
  91. * */
  92. public static function getToDayIsSign($uid)
  93. {
  94. return self::where('uid', $uid)->whereTime('add_time', 'today')->count() ? true : false;
  95. }
  96. /**
  97. * 获取用户昨天是否签到
  98. * @param int $uid
  99. * */
  100. public static function getYesterDayIsSign($uid)
  101. {
  102. return self::where('uid', $uid)->whereTime('add_time', 'yesterday')->count() ? true : false;
  103. }
  104. /**
  105. * 获取签到配置
  106. * @param string
  107. * */
  108. public static function getSignSystemList($key = 'sign_day_num', $mer_id = '')
  109. {
  110. return \crmeb\services\GroupDataService::getData($key, 0, false, $mer_id) ?: [];
  111. }
  112. /*
  113. * 用户签到
  114. * @param int $uid 用户uid
  115. * @return boolean
  116. * */
  117. public static function sign($uid, $mer_id = '')
  118. {
  119. $sign_list = self::getSignSystemList('sign_day_num', $mer_id);
  120. if (!count($sign_list)) return self::setErrorInfo('请先配置签到天数');
  121. $user = User::where('uid', $uid)->find();
  122. $sign_num = 0;
  123. //检测昨天是否签到
  124. if (self::getYesterDayIsSign($uid)) {
  125. if ($user->sign_num > (count($sign_list) - 1)) $user->sign_num = 0;
  126. } else {
  127. //如果昨天没签到,回退到第一天
  128. $user->sign_num = 0;
  129. }
  130. foreach ($sign_list as $key => $item) {
  131. if ($key == $user->sign_num) {
  132. $sign_num = $item['sign_num'];
  133. break;
  134. }
  135. }
  136. $user->sign_num += 1;
  137. if ($user->sign_num == count($sign_list))
  138. $res1 = self::setSignData($uid, '连续签到奖励', $sign_num, $user->integral);
  139. else
  140. // $res1 = self::setSignData($uid,'用户连续签到第'.(self::getSignSumDay($uid)+1).'天',$sign_num,$user->integral);
  141. $res1 = self::setSignData($uid, '签到奖励', $sign_num, $user->integral);
  142. $res2 = User::bcInc($uid, 'integral', $sign_num, 'uid');
  143. $res3 = $user->save();
  144. $res = $res1 && $res2 && $res3 !== false;
  145. BaseModel::checkTrans($res);
  146. event('UserLevelAfter', [$user]);
  147. if ($res)
  148. return $sign_num;
  149. else
  150. return false;
  151. }
  152. /*
  153. * 获取签到列表按月加载
  154. * @param int $uid 用户uid
  155. * @param int $page 页码
  156. * @param int $limit 显示多少条
  157. * @return array
  158. * */
  159. public static function getSignMonthList($uid, $page = 1, $limit = 8)
  160. {
  161. if (!$limit) return [];
  162. if ($page) {
  163. $list = UserBill::where('uid', $uid)
  164. ->where('category', 'integral')
  165. ->where('type', 'sign')
  166. ->field('FROM_UNIXTIME(add_time,"%Y-%m") as time,group_concat(id SEPARATOR ",") ids')
  167. ->group('time')
  168. ->order('time DESC')
  169. ->page($page, $limit)
  170. ->select();
  171. } else {
  172. $list = UserBill::where('uid', $uid)
  173. ->where('category', 'integral')
  174. ->where('type', 'sign')
  175. ->field('FROM_UNIXTIME(add_time,"%Y-%m") as time,group_concat(id SEPARATOR ",") ids')
  176. ->group('time')
  177. ->order('time DESC')
  178. ->select();
  179. }
  180. $data = [];
  181. foreach ($list as $key => &$item) {
  182. $value['month'] = $item['time'];
  183. $value['list'] = UserBill::where('id', 'in', $item['ids'])->field('FROM_UNIXTIME(add_time,"%Y-%m-%d") as add_time,title,number')->order('add_time DESC')->select();
  184. array_push($data, $value);
  185. }
  186. return $data;
  187. }
  188. public static function checkUserSigned($uid)
  189. {
  190. return UserBill::be(['uid' => $uid, 'add_time' => ['>', strtotime('today')], 'category' => 'integral', 'type' => 'sign']);
  191. }
  192. public static function userSignedCount($uid)
  193. {
  194. return self::userSignBillWhere($uid)->count();
  195. }
  196. /**
  197. * @param $uid
  198. * @return UserBill
  199. */
  200. public static function userSignBillWhere($uid)
  201. {
  202. return UserBill::where('uid', $uid)->where('category', 'integral')->where('type', 'sign');
  203. }
  204. public static function signEbApi($userInfo)
  205. {
  206. $uid = $userInfo['uid'];
  207. $min = SystemConfigService::get('sx_sign_min_int') ?: 0;
  208. $max = SystemConfigService::get('sx_sign_max_int') ?: 5;
  209. $integral = rand($min, $max);
  210. BaseModel::beginTrans();
  211. $res1 = UserBill::income('用户签到', $uid, 'integral', 'sign', $integral, 0, $userInfo['integral'], '签到获得' . floatval($integral) . '积分');
  212. $res2 = User::bcInc($uid, 'integral', $integral, 'uid');
  213. $res = $res1 && $res2;
  214. BaseModel::checkTrans($res);
  215. if ($res)
  216. return $integral;
  217. else
  218. return false;
  219. }
  220. }