123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- <?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, $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) {
- }
- }
- }
- }
- }
- }
|