log($uid, $key, $name); * * @return mixed */ public function log($uid, $action_key, $action_name, $content = '') { $action = Dever::db('score/action')->one(array('key' => $action_key)); if (!$action) { $action = array(); $action['id'] = Dever::db('score/action')->insert(array('key' => $action_key, 'name' => $action_name)); } if (isset($action['id'])) { $log_id = Dever::db('score/action_log')->insert(array('uid' => $uid, 'action_id' => $action['id'], 'content' => $content)); //Dever::deamon('lib/core.oper?log_id='.$log_id, 'score'); Dever::load('score/lib/core.oper?log_id='.$log_id); } } /** * 根据用户行为,增加积分 * * @return mixed */ public function oper() { $log_id = Dever::input('log_id'); $log = Dever::db('score/action_log')->one($log_id); $this->operAction($log); } private function rule($log, $info) { if ($info['num'] == 0) { return; } $uid = $log['uid']; $user = Dever::db('score/user')->one(array('uid' => $uid, 'config_id' => $info['config_id'])); if (!$user) { $user = array(); $user['id'] = Dever::db('score/user')->insert(array('uid' => $uid, 'config_id' => $info['config_id'], 'score' => 0, 'no_score' => 0)); $user['score'] = 0; } $num = $info['num']; if ($info['upper'] == 2 && $info['upper_limit'] >= 1) { $upper = $this->upper($uid, $info, $log); if (!$upper) { Dever::db('score/action_log')->update(array('where_id' => $log['id'], 'score_type' => 3, 'score' => '0')); return; } Dever::db('score/action_log')->update(array('where_id' => $log['id'], 'score_type' => 2, 'score' => $num)); } $update = array(); $update['where_id'] = $user['id']; $update['score'] = $user['score'] + $num; $insert['uid'] = $uid; $insert['config_id'] = $info['config_id']; $insert['action_id'] = $info['action_id']; $insert['status'] = 1; $insert['num'] = $num; $insert['total'] = $update['score']; Dever::db('score/user_log')->insert($insert); Dever::db('score/user')->update($update); } private function upper($uid, $info) { # 有上限限制 $limit = $info['upper_limit']; # 获取用户最新一次积分变化日志 $time = time(); $where = array(); $where['config_id'] = $info['config_id']; $where['action_id'] = $info['action_id']; $where['uid'] = $uid; $where['status'] = 1; $user_log = Dever::db('score/user_log')->getNew($where); if ($user_log) { if ($info['upper_type'] == 1) { # 按天 $where['start'] = Dever::maketime(date('Y-m-d 00:00:00', $time)); //$where['end'] = $end; } elseif ($info['upper_type'] == 2) { # 按小时 $where['start'] = $time - ($info['upper_time'] * 3600); //$where['end'] = $end; } elseif ($info['upper_type'] == 3) { # 永久 } else { return true; } $user_log_num = Dever::db('score/user_log')->getNewTotal($where); if ($user_log_num >= $info['upper_limit']) { return false; } } return true; } private function operAction($log) { if ($log && $log['score_type'] == 1) { $where = array(); $where['state'] = 1; $where['id'] = $log['action_id']; $action = Dever::db('score/action')->one($where); if ($action) { $rule = Dever::db('score/rule')->state(array('action_id' => $action['id'])); if ($rule) { foreach ($rule as $k => $v) { $this->rule($log, $v); } } } } } /** * 根据积分日志和规则,增加积分,这是一个定时任务,暂时不开启了 * * @return mixed */ public function cron() { $where['score_type'] = 1; $log = Dever::db('score/action_log')->state($where); if ($log) { $this->operAction($log['id']); } } /** * 根据积分算等级 * * @return mixed */ public function level() { $level = Dever::db('score/level')->state(); foreach ($level as $k => $v) { $config = Dever::array_decode($v['score']); if ($config) { foreach ($config as $k1 => $v1) { $score = $v1['config']; $num = $v1['num']; $user = Dever::db('score/user')->state(array('config_id' => $score, 'score' => $num)); if ($user) { } } } } } }