123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591 |
- <?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, $type = false, $type_id = false)
- {
- if (strstr($action_key, '.')) {
- $temp = explode('.', $action_key);
- $action_key = $temp[0];
- $type = $temp[1];
- $type_id = $temp[2];
- }
- $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();
- $parent_uid = 0;
- if (strstr($uid, '_')) {
- $temp = explode('_', $uid);
- $uid = $temp[1];
- $parent_uid = $temp[0];
- }
- $insert['uid'] = $uid;
- $insert['action_id'] = $action['id'];
- $insert['callback'] = $callback;
- if ($score > 0) {
- $insert['mscore'] = $score;
- }
- if ($num > 0) {
- $insert['num'] = $num;
- }
- if ($parent_uid > 0) {
- $insert['parent_uid'] = $parent_uid;
- }
- if ($type) {
- $insert['type'] = $type;
- }
- if ($type_id) {
- $insert['type_id'] = $type_id;
- }
- $insert['cron_type'] = 1;
- $log_id = Dever::db('score/action_log')->insert($insert);
- Dever::load('score/lib/core.oper_commit?log_id='.$log_id);
- return;
- $sync = Dever::config('base', 'project')->score['sync'];
- if ($sync) {
- Dever::daemon('lib/core.oper?log_id='.$log_id, 'score');
- }
- //Dever::daemon('lib/core.oper?log_id='.$log_id, 'score');
- //Dever::load('score/lib/core.oper?log_id='.$log_id);
- }
- }
- # 获取积分列表
- public function getScore()
- {
- $data = Dever::db('score/config')->state();
- return $data;
- }
- # 获取某一个用户的积分
- public function getUserScore($uid, $config_id = false, $type = false, $type_id = false)
- {
- $score = array();
- if ($config_id && $type && $type_id) {
- $where['config_id'] = $config_id;
- $where['type'] = $type;
- $where['type_id'] = $type_id;
- $where['state'] = 1;
- $child = Dever::db('score/config_child')->one($where);
- if ($child) {
- $where = array();
- $where['uid'] = $uid;
- $where['config_child_id'] = $child['id'];
- $score = Dever::db('score/user_child')->one($where);
- if ($score) {
- return array('name' => $child['name'], 'score' => $score['score'] ? $score['score'] : 0);
- } else {
- return array();
- }
- }
- }
- if (!$config_id) {
- $config = Dever::db('score/config')->one(array('type' => 1));
- } else {
- $config = Dever::db('score/config')->one($config_id);
- }
-
- if ($config) {
- $where = array();
- $where['uid'] = $uid;
- $where['config_id'] = $config['id'];
- $score = Dever::db('score/user')->one($where);
- if ($score) {
- return array('name' => $config['name'], 'score' => $score['score'] ? $score['score'] : 0);
- } else {
- return array();
- }
- }
- return array();
- }
- # 获取用户积分日志列表
- public function getUserScoreList($uid, $config_id = false, $type = false, $type_id = false)
- {
- $result = array();
- if ($config_id && $type && $type_id) {
- $where['config_id'] = $config_id;
- $where['type'] = $type;
- $where['type_id'] = $type_id;
- $where['state'] = 1;
- $child = Dever::db('score/config_child')->one($where);
- if ($child) {
- $where = array();
- $where['uid'] = $uid;
- $where['config_child_id'] = $child['id'];
- $result = Dever::db('score/user_child_log')->getList($where);
- }
- }
- if (!$result) {
- if (!$config_id) {
- $config = Dever::db('score/config')->one(array('type' => 1));
- } else {
- $config = Dever::db('score/config')->one($config_id);
- }
-
- if ($config) {
- $where = array();
- $where['uid'] = $uid;
- $where['config_id'] = $config['id'];
- $result = Dever::db('score/user_log')->getList($where);
- }
- }
- if ($result) {
- $action = Dever::db('score/action')->getData();
- foreach ($result as $k => $v) {
- $result[$k]['cdate'] = date('Y-m-d H:i:s', $v['cdate']);
- if (isset($action[$v['action_id']])) {
- $result[$k]['title'] = $action[$v['action_id']]['name'];
- } else {
- $result[$k]['title'] = '未知';
- }
- }
- }
-
- return $result;
- }
- # 设置子积分
- public function setScoreChild($config_type, $config_id, $type, $type_id, $name)
- {
- $where['config_id'] = $config_id;
- $where['type'] = $type;
- $where['type_id'] = $type_id;
- $info = Dever::db('score/config_child')->one($where);
- if ($info && $config_type == 1) {
- $update['where_id'] = $info['id'];
- $update['state'] = 2;
- Dever::db('score/config_child')->update($update);
- } elseif ($config_type == 2) {
- if (!$name) {
- $config = Dever::db('score/config')->one($config_id);
- $name = $config['name'];
- }
- if ($info) {
- $update['where_id'] = $info['id'];
- $update['name'] = $name;
- $update['state'] = 1;
- Dever::db('score/config_child')->update($update);
- } else {
- $insert = $where;
- $insert['name'] = $name;
- Dever::db('score/config_child')->insert($insert);
- }
- }
-
- return true;
- }
- public function oper_commit_api()
- {
-
- }
- /**
- * 根据用户行为,增加积分
- *
- * @return mixed
- */
- public function oper_commit()
- {
- $log_id = Dever::input('log_id');
- $log = Dever::db('score/action_log')->one($log_id);
- $this->operAction($log);
- return $log;
- }
- private function rule($log, $info)
- {
- $config = Dever::db('score/config')->one($info['config_id']);
- if (!$config) {
- return;
- }
- if ($config && $config['state'] == 2) {
- return;
- }
- # 手动增加积分
- 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 $this->finish($log);
- }
- $uid = $log['uid'];
- $num = $info['num'];
- $this->user = 'score/user';
- $this->user_log = 'score/user_log';
- $info['config_child_id'] = false;
- # 验证是否有子积分设置
- if ($log['type'] && $log['type_id']) {
- $child = Dever::db('score/config_child')->one(array(
- 'config_id' => $info['config_id'], 'type' => $log['type'], 'type_id' => $log['type_id'], 'state' => 1));
- if ($child) {
- $this->user = 'score/user_child';
- $this->user_log = 'score/user_child_log';
- $info['config_child_id'] = $child['id'];
- }
- }
- if ($info['upper'] == 2 && $info['upper_limit'] >= 1) {
- $upper = $this->upper($uid, $info, $log);
- if (!$upper) {
- # 达到积分上限
- return $this->finish($log, 3, '0');
- }
- }
- # 完成
- $this->finish($log, 2, $num);
- $user_log = $this->add($uid, $num, $info, $log);
- if (!$user_log['id']) {
- return;
- }
- $user_log['type'] = $log['type'];
- $user_log['type_id'] = $log['type_id'];
- # 增加返利
- if ($info['rebate_type'] > 1) {
- $this->rebate($uid, $num, $info, $log);
- }
- if ($log['callback']) {
- Dever::load($log['callback'], $user_log);
- /*
- if (strstr($log['callback'], '?')) {
- Dever::load($log['callback'] . '&user_log_id=' . $user_log_id);
- } else {
- Dever::load($log['callback'], $user_log_id);
- }
- */
- } elseif ($config['callback_type'] > 1 && $config['callback_value']) {
- if ($config['callback_type'] == 2) {
- Dever::load($config['callback_value'], $user_log);
- } elseif ($config['callback_type'] == 3) {
- Dever::curl($config['callback_value'], $user_log, 'post');
- } elseif ($config['callback_type'] == 4) {
- $temp = explode('/', $config['callback_value']);
- $config['callback_value'] = str_replace($temp[0] . '/', '', $config['callback_value']);
- Dever::daemon($config['callback_value'] . '?log_id='.$user_log['id'], $temp[0]);
- }
- }
- }
- # 增加积分
- private function add($uid, $num, $info, $log, $rebate_uid = false)
- {
- $where = array();
- $where['uid'] = $uid;
- $where['config_id'] = $info['config_id'];
- if ($info['config_child_id']) {
- $where['config_child_id'] = $info['config_child_id'];
- }
- $user = Dever::db($this->user)->one($where);
- if (!$user) {
- $user = array();
- $user['id'] = Dever::db($this->user)->insert($where);
- $user['score'] = 0;
- }
- $update = array();
- $update['where_id'] = $user['id'];
- $update['score'] = $num;
- $insert = $where;
- $insert['action_log_id'] = $log['id'];
- $insert['action_id'] = $info['action_id'];
- if ($rebate_uid) {
- $insert['rebate_uid'] = $rebate_uid;
- }
-
- $insert['status'] = 1;
- $insert['num'] = $num;
- $insert['total'] = $update['score'];
- $insert['cdate'] = time();
- $insert['id'] = Dever::db($this->user_log)->insert($insert);
- if ($insert['id']) {
- Dever::db($this->user)->inc($update);
- # 提升等级
- $level_type = Dever::db('score/level_type')->state();
- if ($level_type) {
- foreach ($level_type as $k => $v) {
- $this->setLevel($uid, $v);
- }
- }
- }
- return $insert;
- }
- # 返利
- private function rebate($uid, $num, $info, $log)
- {
- if (!strstr($info['rebate_value'], ':')) {
- $info['rebate_value'] = '1:' . $info['rebate_value'];
- }
- $temp = explode(',', $info['rebate_value']);
- $invite = Dever::project('invite');
- $parent_uid = false;
- foreach ($temp as $k => $v) {
- $config = explode(':', $v);
- $value = $config[1];
- if ($info['rebate_type'] == 2) {
- # 百分比
- $value = round($num*($value/100), 2);
- }
- # 直属上级
- if ($config[0] == 1) {
- if ($log['parent_uid'] > 0) {
- $parent_uid = $log['parent_uid'];
- } elseif ($invite) {
- $parent_uid = Dever::load('invite/api')->getParent($uid, $config[0]);
- }
-
- if ($parent_uid > 0) {
- $this->add($parent_uid, $value, $info, $log, $uid);
- }
- } elseif ($invite) {
- # 上级的上级
- if ($parent_uid > 0) {
- $rebate_uid = $parent_uid;
- $level = $config[0]-1;
- } else {
- $rebate_uid = $uid;
- $level = $config[0];
- }
- $other_parent_uid = Dever::load('invite/api')->getParent($rebate_uid, $level);
- if ($other_parent_uid > 0) {
- $this->add($other_parent_uid, $value, $info, $log, $rebate_uid);
- }
- }
- }
- }
- # 迁移完成的数据
- private function finish($log, $score_type = 1, $score = 'no')
- {
- $id = $log['id'];
- if ($score_type == -1) {
- Dever::db('score/action_log')->update(array('where_id' => $id, 'cron_type' => 3));
- } else {
- unset($log['id']);
- $log['log_id'] = $id;
- $log['cron_type'] = 3;
- $log['score_type'] = $score_type;
- if ($score != 'no') {
- $log['score'] = $score;
- }
- Dever::db('score/action_log_finish')->insert($log);
- Dever::db('score/action_log')->delete($id);
- }
- return true;
- }
- # 设置上限限制
- private function upper($uid, $info, $log)
- {
- # 有上限限制
- $limit = $info['upper_limit'];
- # 获取用户最新一次积分变化日志
- $time = time();
- $where = array();
- if ($info['config_child_id']) {
- $where['config_child_id'] = $info['config_child_id'];
- }
- $where['config_id'] = $info['config_id'];
- $where['action_id'] = $info['action_id'];
- $where['uid'] = $uid;
- $where['status'] = 1;
- $user_log = Dever::db($this->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($this->user_log)->getNewTotal($where);
- if ($user_log_num >= $info['upper_limit']) {
- return false;
- }
- }
- return true;
- }
- private function operAction($log)
- {
- if ($log && $log['cron_type']) {
- Dever::db('score/action_log')->update(array('where_id' => $log['id'], 'cron_type' => 2));
- $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['cron_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, $level_type)
- {
- # 获取当前用户的等级
- $where['uid'] = $uid;
- $where['level_type_id'] = $level_type['id'];
- $user_level = Dever::db('score/user_level')->one($where);
- /*
- if ($user_level && $user_level['type'] == 2) {
- return;
- } else
- */
- $update = array();
- if ($user_level) {
- $config = Dever::db('score/level')->one($user_level['level_id']);
- $level = $config['level'] + 1;
- $method = 'update';
- $update['where_id'] = $user_level['id'];
- } else {
- $level = 1;
- $method = 'insert';
- }
- $config = Dever::db('score/level')->one(array('level' => $level, 'level_type_id' => $level_type['id'], 'state' => 1));
- if ($config) {
- # 是否能升级
- $yes = false;
- if ($config['total_type'] == 1 && $config['total'] > 0) {
- # 查看是否超过总数
- $total = Dever::db('score/user_level')->total(array('level_id' => $config['id']));
- if ($total >= $config['total']) {
- return $yes;
- }
- }
-
- $rule = Dever::array_decode($config['score']);
- if ($rule) {
- $user_score = Dever::db($this->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'];
- # 是否清空积分 当type为自动并且score为2时
- if ($level_type['score'] == 2 && $level_type['type'] == 1) {
- $user_score_update['where_id'] = $user_score[$score]['id'];
- $user_score_update['score'] = $user_score[$score]['score'] - $num;
- Dever::db($this->user)->update($user_score_update);
- }
- } else {
- $yes = false;
- }
- }
- }
- }
- # 可以升级
- if ($yes) {
- if ($level_type['type'] == 2) {
- $update['next_level_id'] = $yes;
- $update['level_status'] = 1;
- } else {
- $update['level_id'] = $update['next_level_id'] = $yes;
- $update['level_status'] = 2;
- }
- $update['uid'] = $uid;
- $update['level_type_id'] = $level_type['id'];
- Dever::db('score/user_level')->$method($update);
- }
- }
- }
- }
|