123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use think\Db;
- use think\Exception;
- /**
- * 签到
- */
- class Usersign extends Api
- {
- protected $noNeedLogin = [];
- protected $noNeedRight = ['*'];
- /**
- * 配置
- */
- public function config()
- {
- $list = Db::name('signin')->order('id asc')->select();
- //连续签到次数
- $user_sign = Db::name('user_sign')->where(['uid' => $this->auth->id])->order('id desc')->find();
- $yesterday_time = strtotime('yesterday'); //昨天0点时间戳
- $today_time = $yesterday_time + 86400; //今日0点时间戳
- if ($user_sign) {
- if ($user_sign['createtime'] >= $yesterday_time) {
- $data['sign_times'] = $user_sign['times']; //连续签到天数
- } else {
- $data['sign_times'] = 0;
- }
- if ($user_sign['createtime'] >= $today_time) {
- $data['is_sign'] = 1; //今日是否签到: 1已签到 0未签到
- } else {
- $data['is_sign'] = 0; //今日是否签到: 1已签到 0未签到
- }
- } else {
- $data['sign_times'] = 0; //连续签到天数
- $data['is_sign'] = 0; //今日是否签到: 1已签到 0未签到
- }
- $data['sign_round_times'] = $data['sign_times'] % 7;
- $data['list'] = $list;
- $this->success('success',$data);
- }
- //签到
- public function signin(){
- Db::startTrans();
- try {
- $uid = $this->auth->id;
- //记录日志
- $data = [
- 'uid' => $uid,
- 'type' => 1,
- 'times' => 1,
- 'goldnum' => 0,
- 'createtime' => time(),
- ];
- $yesterday_time = strtotime('yesterday'); //昨天0点时间戳
- $today_time = $yesterday_time + 86400; //今日0点时间戳
- //修正当前次数
- $check = Db::name('user_sign')->where('uid',$uid)->order('id desc')->find();
- if($check){
- //今天只能签一次
- if($check['createtime'] >= $today_time){
- throw new Exception('今天已经签过了');
- }
- if ($check['createtime'] >= $yesterday_time) {
- $data['times'] = $check['times'] + 1; //连续签到次数
- }
- }
- /*$signin_id = $data['times'] % 7;
- if ($signin_id == 0) {
- $signin_id = 7;
- }
- //匹配对应金币数
- $list = Db::name('signin')->order('id asc')->column('id,goldnum');
- $data['goldnum'] = isset($list[$signin_id]) ? $list[$signin_id] : 0 ;
- Db::startTrans();
- //记录日志
- $log_id = Db::name('user_sign')->insertGetId($data);
- if(!$log_id){
- Db::rollback();
- $this->error('签到失败');
- }*/
- //加经验
- /*$userWhere['id'] = $uid;
- $user = model('User')->where($userWhere)->lock(true)->find();
- $userNewEmpirical = $user['empirical'] + $data['goldnum'];
- $userUpdate['empirical'] = $userNewEmpirical;
- // 获取用户当前经验值对应等级
- $userconfigModel = new \app\common\model\UserLevelConfig();
- $where = [];
- $where["empirical"] = ["elt",$userNewEmpirical];
- $userexplainstart = $userconfigModel->where($where)->order("empirical","desc")->find();
- if (!empty($userexplainstart)) {
- if ($userexplainstart['level'] != $user['level']) {
- $userUpdate['level'] = $userexplainstart['level'];
- }
- }
- $res = $user->update($userUpdate,$userWhere);
- if($res === false){
- Db::rollback();
- $this->error('经验更新失败');
- }*/
- //第七天赠送礼物
- $gift = [];
- $sendGift = config('site.sign_gift');
- $userSignGift = model('UserSignGift')->with(['gift'])->find();
- if ($data['times'] == 7 && $sendGift == '1' && !empty($userSignGift)) {
- $giftData = isset($userSignGift['gift']) ? $userSignGift['gift'] : [];
- if (!empty($giftData)) {
- $gift = [
- 'image' => $giftData['image'],
- 'num' => $userSignGift['num'],
- ];
- }
- $giftName = isset($giftData['name']) ? $giftData['name'] : '';
- $data['goldnum'] = $gift['num'];
- $data['type'] = 2;
- $data['remark'] = '签到奖励'.$giftName.$gift['num'];
- } else {//随机获取钻石
- $signJewel = config('site.sign_jewel');
- $signJewelArr = [];
- if (!empty($signJewel)) {
- $signJewelArr = explode('-', $signJewel);
- }
- $min = isset($signJewelArr[0]) ? $signJewelArr[0] : 1;
- $max = isset($signJewelArr[1]) ? $signJewelArr[1] : 1;
- $gift['num'] = rand($min,$max);
- if ($gift['num'] < 1) {
- throw new Exception('签到功能已被关闭');
- }
- $data['goldnum'] = $gift['num'];
- $data['remark'] = '签到钻石'.$gift['num'];
- $user = model('User')->find($uid);
- $userJewel = isset($user['jewel']) ? $user['jewel'] : 0;
- $rs_wallet = model('wallet')->lockChangeAccountRemain($uid,$gift['num'],'+',$userJewel,$data['remark'],16,'jewel');
- if (!$rs_wallet['status']) {
- throw new Exception($rs_wallet['msg']);
- }
- }
- //记录日志
- $log_id = Db::name('user_sign')->insertGetId($data);
- if(!$log_id){
- throw new Exception('签到失败');
- }
- //系统消息
- $msg_id = \app\common\model\Message::addMessage($this->auth->id,'签到奖励','签到成功,获得奖励');
- $signImg = config('site.sign_img');
- $result = [
- 'image' => isset($gift['image']) ? $gift['image'] : cdnurl($signImg),
- 'goldnum' => isset($gift['num']) ? $gift['num'] : $data['goldnum'],
- ];
- Db::commit();
- $this->success('签到成功', $result);
- } catch (Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- }
- //第七天赠送vip
- //找出最近七天的次数
- private function gift_vip($user_sign_gift_vipdays){
- $start = strtotime(date('Y-m-d')) - 518400;
- $map = [
- 'uid' => $this->auth->id,
- 'createtime' => ['gt',$start],
- ];
- $count = Db::name('user_sign')->where($map)->count();
- if($count >= 7){
- //赠送vip三天
- $add_time = 86400 * $user_sign_gift_vipdays;
- //有vip的续费,没有的从现在开始
- $vip_endtime = model('wallet')->getWallet($this->auth->id,'vip_endtime');
- if($vip_endtime <= time()){
- $new_vip_endtime = time() + $add_time;
- }else{
- $new_vip_endtime = $vip_endtime + $add_time;
- }
- $rs = Db::name('user_wallet')->where('user_id',$this->auth->id)->update(['vip_endtime'=>$new_vip_endtime]);
- if($rs === false){
- Db::rollback();
- $this->error('连续签到赠送vip失败');
- }
- return true;
- }
- return false;
- }
- }
|