123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- <?php
- namespace app\models\user;
- use crmeb\basic\BaseModel;
- use crmeb\services\SystemConfigService;
- use crmeb\traits\ModelTrait;
- class UserSign extends BaseModel
- {
-
- protected $pk = 'id';
-
- protected $name = 'user_sign';
- use ModelTrait;
-
- public static function setSignData($uid, $title = '', $number = 0, $balance = 0)
- {
- $add_time = time();
- return self::create(compact('uid', 'title', 'number', 'balance', 'add_time')) && UserBill::income($title, $uid, 'integral', 'sign', $number, 0, $balance, $title);
- }
-
- public static function getSignList($uid, $page, $limit)
- {
- if (!$limit) return [];
- $billModel = UserBill::where('a.category', 'integral')->where('a.type', 'sign')
- ->where('a.status', 1)->where('a.uid', $uid)->alias('a')
- ->join("user u", 'u.uid=a.uid')
- ->order('a.add_time desc')->field('FROM_UNIXTIME(a.add_time,"%Y-%m-%d") as add_time,a.title,a.number');
- if ($page) $billModel = $billModel->page((int)$page, (int)$limit);
- return $billModel->select();
- }
-
- public static function getSignSumDay($uid)
- {
- return self::where('uid', $uid)->count();
- }
-
- public static function getIsSign($uid, string $type = 'today')
- {
- return self::where('uid', $uid)->whereTime('add_time', $type)->count() ? true : false;
- }
-
- public static function getSignSystemList($key = 'sign_day_num')
- {
- return sys_data($key) ?: [];
- }
-
- public static function sign($uid)
- {
- $sign_list = self::getSignSystemList();
- if (!count($sign_list)) return self::setErrorInfo('请先配置签到天数');
- $user = User::where('uid', $uid)->find();
- $sign_num = 0;
-
- if (self::getIsSign($uid, 'yesterday')) {
- if ($user->sign_num > (count($sign_list) - 1)) $user->sign_num = 0;
- } else {
-
- $user->sign_num = 0;
- }
- foreach ($sign_list as $key => $item) {
- if ($key == $user->sign_num) {
- $sign_num = $item['sign_num'];
- break;
- }
- }
- $user->sign_num += 1;
- if ($user->sign_num == count($sign_list))
- $res1 = self::setSignData($uid, '连续签到奖励', $sign_num, bcadd($user->integral, $sign_num, 0));
- else
- $res1 = self::setSignData($uid, '签到奖励', $sign_num, bcadd($user->integral, $sign_num, 0));
- $res2 = User::bcInc($uid, 'integral', $sign_num, 'uid');
- $res3 = $user->save();
- $res = $res1 && $res2 && $res3 !== false;
- BaseModel::checkTrans($res);
- event('UserLevelAfter', [$user]);
- if ($res)
- return $sign_num;
- else
- return false;
- }
-
- public static function getSignMonthList($uid, $page = 1, $limit = 8)
- {
- if (!$limit) return [];
- if ($page) {
- $list = UserBill::where('uid', $uid)
- ->where('category', 'integral')
- ->where('type', 'sign')
- ->field('FROM_UNIXTIME(add_time,"%Y-%m") as time,group_concat(id SEPARATOR ",") ids')
- ->group('time')
- ->order('time DESC')
- ->page($page, $limit)
- ->select();
- } else {
- $list = UserBill::where('uid', $uid)
- ->where('category', 'integral')
- ->where('type', 'sign')
- ->field('FROM_UNIXTIME(add_time,"%Y-%m") as time,group_concat(id SEPARATOR ",") ids')
- ->group('time')
- ->order('time DESC')
- ->select();
- }
- $data = [];
- foreach ($list as $key => &$item) {
- $value['month'] = $item['time'];
- $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();
- array_push($data, $value);
- }
- return $data;
- }
- public static function checkUserSigned($uid)
- {
- return UserBill::be(['uid' => $uid, 'add_time' => ['>', strtotime('today')], 'category' => 'integral', 'type' => 'sign']);
- }
- public static function userSignedCount($uid)
- {
- return self::userSignBillWhere($uid)->count();
- }
-
- public static function userSignBillWhere($uid)
- {
- return UserBill::where('uid', $uid)->where('category', 'integral')->where('type', 'sign');
- }
- public static function signEbApi($userInfo)
- {
- $uid = $userInfo['uid'];
- $min = sys_config('sx_sign_min_int') ?: 0;
- $max = sys_config('sx_sign_max_int') ?: 5;
- $integral = rand($min, $max);
- BaseModel::beginTrans();
- $res1 = UserBill::income('用户签到', $uid, 'integral', 'sign', $integral, 0, $userInfo['integral'], '签到获得' . floatval($integral) . '积分');
- $res2 = User::bcInc($uid, 'integral', $integral, 'uid');
- $res = $res1 && $res2;
- BaseModel::checkTrans($res);
- if ($res)
- return $integral;
- else
- return false;
- }
- }
|