where($map)->find(); if(empty($type)){ return false; } return $type; } /** * 获取用户签到信息 * @param integer $user_id 用户编号 * @param integer $type_id 积分类型编号 * @return array * @author 鹿文学 */ public function get_sign_in_info($user_id,$type_id){ $map['type_id'] = $type_id; $map['user_id'] = $user_id; $record = M(self::model_point_record,self::model_prefix)->where($map)->order("create_time desc")->find(); return $record; } /** * 操作用户积分 * @param integer $user_id 用户编号 * @param integer $point 积分 * @param integer $type 1 增加 2减少 * @return bool * author: 鹿文学 */ public function operation_point($user_id,$point,$type){ $usermodel = M(self::model_user,self::model_prefix); $user = $usermodel->find($user_id); if($type == 1){ $user['point'] += $point; }else{ if($user['point'] >= $point){ $user['point'] -= $point; }else{ return false; } } return $usermodel->save($user); } /** * 用户签到 * @param integer $user_id 用户编号 * @author 鹿文学 */ public function sign_in($user_id) { $day_count = 7; $type = $this->get_point_type("sign_in"); if(!$type){/* 此奖励不存在或被禁用 */ return ['mark'=>-1]; } $base_point = $type['point']; $increase_point = $type['time_of_day']; $record = $this->get_sign_in_info($user_id,$type['id']); //计算间隔时间 $today = strtotime(date("Y-m-d")); $time = strtotime(date("Y-m-d",$record['create_time'])); $day = ($today-$time)/86400; if($day == 0){/* 今日已签过 */ $point = M('user','tab_')->field('point')->where(['id'=>$user_id])->find(); return [ 'mark'=>-2, 'sign_day'=>$record['day'], /* 签到天数 */ 'day_count'=>$day_count, /* 循环周期的总数 */ 'base_point'=>$type['point'], 'increase_point'=>$type['time_of_day'], 'point'=>!empty($point)?$point['point']:0,/* 用户积分 */ ]; }elseif($day == 1){//昨日积分 + 递增积分 $add_point = $record['point'] + $increase_point; //超过7天 重新计算 if ($record['day'] >= $day_count) { $add_point = $base_point; $sign_day = 1; }else{ $sign_day = $record['day'] + 1; } $data['point'] = $add_point; }else{//基础积分 $data['point'] = $base_point; $sign_day = 1; } $data['user_id'] = $user_id; $data['type_id'] = $type['id']; $data['create_time'] = time(); $data['day'] = $sign_day; $data['type'] = 1; $pointrecordmodel = M(self::model_point_record,self::model_prefix); $pointrecordmodel->startTrans(); $record_result = $pointrecordmodel->add($data); $user_result = $this->operation_point($user_id,$data['point'],1); if($user_result !== false && $record_result !== false){ $pointrecordmodel->commit(); $point = M('user','tab_')->field('point')->where(['id'=>$user_id])->find(); return array( 'mark'=>1, 'sign_day'=>$sign_day, /* 签到天数 */ 'day_count'=>$day_count, /* 循环周期的总数 */ 'base_point'=>$type['point'], 'increase_point'=>$type['time_of_day'], 'point'=>!empty($point)?$point['point']:0,/* 用户积分 */ ); }else{ $pointrecordmodel->rollback(); $point = M('user','tab_')->field('point')->where(['id'=>$user_id])->find(); return [ 'mark'=>-3, 'sign_day'=>$record['day']?$record['day']:0, /* 签到天数 */ 'day_count'=>$day_count, /* 循环周期的总数 */ 'base_point'=>$type['point'], 'increase_point'=>$type['time_of_day'], 'point'=>!empty($point)?$point['point']:0 ]; } } }