123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- <?php
- namespace Score\Lib;
- use Dever;
- class Core
- {
- /**
- * 增加日志 Dever::load('score/lib/core')->log($uid, $key, $name);
- *
- * @return mixed
- */
- public function log($uid, $action_key, $action_name, $callback = '', $score = false, $num = false)
- {
- $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']) && $action['id'] > 0) {
- $insert = array();
- $insert['uid'] = $uid;
- $insert['action_id'] = $action['id'];
- $insert['callback'] = $callback;
- if ($score > 0) {
- $insert['mscore'] = $score;
- }
- if ($num > 0) {
- $insert['num'] = $num;
- }
- $log_id = Dever::db('score/action_log')->insert($insert);
- Dever::daemon('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 (isset($log['mscore']) && $log['mscore'] > 0) {
- $info['num'] = $log['mscore'];
- }
- # 积分乘以倍数
- if (isset($log['num']) && $log['num'] > 0) {
- $info['num'] = $info['num'] * $log['num'];
- }
- 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'];
- $user_log_id = Dever::db('score/user_log')->insert($insert);
- Dever::db('score/user')->update($update);
- # 提升等级
- $this->setLevel($uid);
- if ($log['callback']) {
- if (strstr($log['callback'], '?')) {
- Dever::load($log['callback'] . '&user_log_id=' . $user_log_id);
- } else {
- Dever::load($log['callback'], $user_log_id);
- }
- }
- }
- 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')->all($where);
- if ($log) {
- foreach ($log as $k => $v) {
- $this->operAction($v);
- }
- }
- }
- /**
- * 根据积分算等级
- *
- * @return mixed
- */
- public function setLevel($uid)
- {
- # 获取当前用户的等级
- $where['uid'] = $uid;
- $user_level = Dever::db('score/user_level')->one($where);
- if ($user_level && $user_level['type'] == 2) {
- return;
- } elseif ($user_level && $user_level['type'] == 1) {
- $config = Dever::db('score/level')->one($user_level['level_id']);
- $level = $config['level'] + 1;
- } else {
- $level = 1;
- }
- $config = Dever::db('score/level')->one(array('level' => $level, 'state' => 1));
- if ($config) {
- # 是否能升级
- $yes = false;
- $rule = Dever::array_decode($config['score']);
- if ($rule) {
- $user_score = Dever::db('score/user')->getScoreByConfig($where);
- if ($user_score) {
- foreach ($rule as $k => $v) {
- $score = $v['config'];
- $num = $v['num'];
- if (isset($user_score[$score]) && $user_score[$score]['score'] && $user_score[$score]['score'] >= $num) {
- $yes = $config['id'];
- } else {
- $yes = false;
- }
- }
- }
- }
- if ($yes) {
- # 可以升级
- if ($user_level) {
- Dever::db('score/user_level')->update(array('where_id' => $user_level['id'], 'level_id' => $yes));
- } else {
- Dever::db('score/user_level')->insert(array('uid' => $uid,'level_id' => $yes, 'type' => 1));
- }
- }
- }
- }
- }
|