UserSign.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 用户uid
  27. * @param string $title 签到说明
  28. * @param int $number 签到获得积分
  29. * @param int $balance 签到前剩余积分
  30. * @return bool
  31. */
  32. public static function setSignData($uid,$title='',$number=0,$balance=0)
  33. {
  34. $add_time=time();
  35. return self::create(compact('uid','title','number','balance','add_time')) && UserBill::income($title,$uid,'integral','sign',$number,0,$balance,$title);
  36. }
  37. /**
  38. * 分页获取用户签到数据
  39. * @param $uid 用户uid
  40. * @param $page 页码
  41. * @param $limit 展示条数
  42. * @return array|\think\Collection
  43. * @throws \think\db\exception\DataNotFoundException
  44. * @throws \think\db\exception\ModelNotFoundException
  45. * @throws \think\exception\DbException
  46. */
  47. public static function getSignList($uid,$page,$limit)
  48. {
  49. if(!$limit) return [];
  50. $billModel = UserBill::where('a.category','integral')->where('a.type','sign')
  51. ->where('a.status',1)->where('a.uid',$uid)->alias('a')
  52. ->join("__user__ u",'u.uid=a.uid')
  53. ->order('a.add_time desc')->field('FROM_UNIXTIME(a.add_time,"%Y-%m-%d") as add_time,a.title,a.number');
  54. if($page) $billModel = $billModel->page((int)$page,(int)$limit);
  55. return $billModel->select();
  56. }
  57. /**
  58. * 获取用户累计签到次数
  59. * @param $uid
  60. * @return int
  61. */
  62. public static function getSignSumDay($uid)
  63. {
  64. return self::where('uid', $uid)->count();
  65. }
  66. /**
  67. * 获取用户是否签到
  68. * @param $uid
  69. * @return bool
  70. */
  71. public static function getIsSign($uid,string $type = 'today')
  72. {
  73. return self::where('uid', $uid)->whereTime('add_time',$type)->count() ? true : false;
  74. }
  75. /**
  76. * 获取签到配置
  77. * @param string $key
  78. * @return array
  79. * @throws \think\db\exception\DataNotFoundException
  80. * @throws \think\db\exception\ModelNotFoundException
  81. * @throws \think\exception\DbException
  82. */
  83. public static function getSignSystemList($key='sign_day_num')
  84. {
  85. return \crmeb\services\GroupDataService::getData($key) ? : [];
  86. }
  87. /**
  88. * 用户签到
  89. * @param $uid
  90. * @return bool|int
  91. * @throws \think\db\exception\DataNotFoundException
  92. * @throws \think\db\exception\ModelNotFoundException
  93. * @throws \think\exception\DbException
  94. */
  95. public static function sign($uid)
  96. {
  97. $sign_list=self::getSignSystemList();
  98. if(!count($sign_list)) return self::setErrorInfo('请先配置签到天数');
  99. $user=User::where('uid',$uid)->find();
  100. $sign_num=0;
  101. //检测昨天是否签到
  102. if(self::getIsSign($uid,'yesterday')){
  103. if($user->sign_num > (count($sign_list) -1)) $user->sign_num=0;
  104. }else{
  105. //如果昨天没签到,回退到第一天
  106. $user->sign_num=0;
  107. }
  108. foreach ($sign_list as $key=>$item){
  109. if($key==$user->sign_num){
  110. $sign_num=$item['sign_num'];
  111. break;
  112. }
  113. }
  114. $user->sign_num+=1;
  115. if($user->sign_num == count($sign_list))
  116. $res1 = self::setSignData($uid,'连续签到奖励',$sign_num,$user->integral);
  117. else
  118. $res1 = self::setSignData($uid,'签到奖励',$sign_num,$user->integral);
  119. $res2= User::bcInc($uid,'integral',$sign_num,'uid');
  120. $res3= $user->save();
  121. $res = $res1 && $res2 && $res3 !== false;
  122. BaseModel::checkTrans($res);
  123. event('UserLevelAfter',[$user]);
  124. if($res)
  125. return $sign_num;
  126. else
  127. return false;
  128. }
  129. /*
  130. * 获取签到列表按月加载
  131. * @param int $uid 用户uid
  132. * @param int $page 页码
  133. * @param int $limit 显示多少条
  134. * @return array
  135. * */
  136. public static function getSignMonthList($uid,$page=1,$limit=8)
  137. {
  138. if(!$limit) return [];
  139. if($page){
  140. $list = UserBill::where('uid', $uid)
  141. ->where('category', 'integral')
  142. ->where('type', 'sign')
  143. ->field('FROM_UNIXTIME(add_time,"%Y-%m") as time,group_concat(id SEPARATOR ",") ids')
  144. ->group('time')
  145. ->order('time DESC')
  146. ->page($page, $limit)
  147. ->select();
  148. }else{
  149. $list = UserBill::where('uid', $uid)
  150. ->where('category', 'integral')
  151. ->where('type', 'sign')
  152. ->field('FROM_UNIXTIME(add_time,"%Y-%m") as time,group_concat(id SEPARATOR ",") ids')
  153. ->group('time')
  154. ->order('time DESC')
  155. ->select();
  156. }
  157. $data=[];
  158. foreach ($list as $key=>&$item){
  159. $value['month'] = $item['time'];
  160. $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();
  161. array_push($data,$value);
  162. }
  163. return $data;
  164. }
  165. public static function checkUserSigned($uid)
  166. {
  167. return UserBill::be(['uid'=>$uid,'add_time'=>['>',strtotime('today')],'category'=>'integral','type'=>'sign']);
  168. }
  169. public static function userSignedCount($uid)
  170. {
  171. return self::userSignBillWhere($uid)->count();
  172. }
  173. /**
  174. * @param $uid
  175. * @return UserBill
  176. */
  177. public static function userSignBillWhere($uid)
  178. {
  179. return UserBill::where('uid', $uid)->where('category', 'integral')->where('type', 'sign');
  180. }
  181. public static function signEbApi($userInfo)
  182. {
  183. $uid = $userInfo['uid'];
  184. $min = SystemConfigService::get('sx_sign_min_int')?:0;
  185. $max = SystemConfigService::get('sx_sign_max_int')?:5;
  186. $integral = rand($min,$max);
  187. BaseModel::beginTrans();
  188. $res1 = UserBill::income('用户签到',$uid,'integral','sign',$integral,0,$userInfo['integral'],'签到获得'.floatval($integral).'积分');
  189. $res2 = User::bcInc($uid,'integral',$integral,'uid');
  190. $res = $res1 && $res2;
  191. BaseModel::checkTrans($res);
  192. if($res)
  193. return $integral;
  194. else
  195. return false;
  196. }
  197. }