dever 5 роки тому
батько
коміт
ac46242d60
6 змінених файлів з 447 додано та 27 видалено
  1. 22 0
      database/action_log.php
  2. 214 0
      database/action_log_finish.php
  3. 76 0
      database/rule.php
  4. 1 1
      database/user.php
  5. 10 0
      database/user_log.php
  6. 124 26
      lib/Core.php

+ 22 - 0
database/action_log.php

@@ -63,6 +63,16 @@ return array
             ),
 			'list'		=> '{uid} > 0 ? Dever::load("passport/user-one#username", {uid}) : "匿名用户"',
 		),
+
+		'parent_uid'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '上级用户',
+			'default' 	=> '0',
+			'desc' 		=> '请选择上级用户',
+			'match' 	=> 'is_numeric',
+			'list'		=> true,
+		),
 		
 		'action_id'		=> array
 		(
@@ -154,6 +164,18 @@ return array
 		),
 	),
 
+	# 索引
+	'index' => array
+	(
+		1 => array
+		(
+			'search' => 'uid,action_id,cron_type',
+		),
+		
+		# 版本号 更改版本号会更新当前表的索引
+		'version' => 1,
+	),
+
 	'manage' => array
 	(
 		'delete' => false,

+ 214 - 0
database/action_log_finish.php

@@ -0,0 +1,214 @@
+<?php
+
+$action = function()
+{
+	$array = array();
+	$info = Dever::db('score/action')->state();
+	if($info)
+	{
+		$array += $info;
+	}
+	return $array;
+};
+
+$score_type = array
+(
+	1 => '未加积分',
+	2 => '已加积分',
+	3 => '达到积分上限',
+);
+
+$cron_type = array
+(
+	1 => '未开始',
+	2 => '进行中',
+	3 => '已完成',
+);
+
+return array
+(
+	# 表名
+	'name' => 'action_log_finish',
+	# 显示给用户看的名称
+	'lang' => '积分行为日志-已完成',
+	'order' => 10,
+	'menu' => false,
+	# 数据结构
+	'struct' => array
+	(
+		'id' 		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> 'ID',
+			'default' 	=> '',
+			'desc' 		=> '',
+			'match' 	=> 'is_numeric',
+			//'list'		=> true,
+		),
+
+		'log_id' 		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> 'ID',
+			'default' 	=> '',
+			'desc' 		=> '',
+			'match' 	=> 'is_numeric',
+			'list'		=> true,
+		),
+
+		'uid'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '用户名',
+			'default' 	=> '0',
+			'desc' 		=> '请选择用户',
+			'match' 	=> 'is_numeric',
+			//'update'	=> 'select',
+			//'search'	=> 'select',
+			'search'    => array
+            (
+                'api' => 'passport/user-all',
+                'col' => 'username',
+                'result' => 'id',
+            ),
+			'list'		=> '{uid} > 0 ? Dever::load("passport/user-one#username", {uid}) : "匿名用户"',
+		),
+
+		'parent_uid'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '上级用户',
+			'default' 	=> '0',
+			'desc' 		=> '请选择上级用户',
+			'match' 	=> 'is_numeric',
+			'list'		=> true,
+		),
+		
+		'action_id'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '所属行为',
+			'default' 	=> '1',
+			'desc' 		=> '所属行为',
+			'match' 	=> 'is_numeric',
+			'update'	=> 'select',
+			'option'	=> $action,
+			'search'	=> 'select',
+			'list'		=> true,
+		),
+
+		'score_type'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '是否增加积分',
+			'default' 	=> '1',
+			'desc' 		=> '是否增加积分',
+			'match' 	=> 'is_numeric',
+			'update'	=> 'select',
+			'option'	=> $score_type,
+			'search'	=> 'select',
+			'list'		=> true,
+		),
+
+		'cron_type'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '是否增加积分',
+			'default' 	=> '3',
+			'desc' 		=> '是否增加积分',
+			'match' 	=> 'is_numeric',
+			'update'	=> 'select',
+			'option'	=> $cron_type,
+			'search'	=> 'select',
+			'list'		=> true,
+		),
+
+		'score'		=> array
+		(
+			'type' 		=> 'varchar-30',
+			'name' 		=> '增加积分数',
+			'default' 	=> '0',
+			'desc' 		=> '积分数',
+			'match' 	=> 'is_string',
+			'list'		=> true,
+		),
+
+		'num'		=> array
+		(
+			'type' 		=> 'varchar-30',
+			'name' 		=> '增加的积分将乘以该数量',
+			'default' 	=> '0',
+			'desc' 		=> '积分数',
+			'match' 	=> 'is_string',
+		),
+
+		'mscore'		=> array
+		(
+			'type' 		=> 'varchar-30',
+			'name' 		=> '手动增加积分数',
+			'default' 	=> '0',
+			'desc' 		=> '积分数',
+			'match' 	=> 'is_string',
+		),
+
+		'callback'		=> array
+		(
+			'type' 		=> 'varchar-800',
+			'name' 		=> '行为回调方法',
+			'default' 	=> '',
+			'desc' 		=> '行为回调方法',
+			'match' 	=> 'option',
+			'update'	=> 'textarea',
+			//'list'		=> true,
+		),
+
+		'cdate'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '录入时间',
+			'match' 	=> array('is_numeric', time()),
+			'desc' 		=> '',
+			# 只有insert时才生效
+			'insert'	=> true,
+			'list'      => 'date("Y-m-d H:i:s", {cdate})',
+		),
+	),
+
+	# 索引
+	'index' => array
+	(
+		1 => array
+		(
+			'search' => 'uid,action_id,cron_type',
+		),
+		
+		# 版本号 更改版本号会更新当前表的索引
+		'version' => 1,
+	),
+
+	'manage' => array
+	(
+		'delete' => false,
+		'edit' => false,
+		'insert' => false,
+	),
+
+	# request 请求接口定义
+	'request' => array
+	(
+		'getDataByDate' => array
+		(
+			# 匹配的正则或函数 选填项
+			'option' => array
+			(
+				'start' => array('yes-cdate', '>='),
+				'end' => array('yes-cdate', '<='),
+				'score_type' => 'yes',
+				'action_id' => 'yes',
+			),
+			'type' => 'all',
+			'order' => array('id' => 'desc'),
+			'col' => 'id',
+		),
+	)
+);

+ 76 - 0
database/rule.php

@@ -35,6 +35,31 @@ $upper_type = array
 	3 => '永久',
 );
 
+$rebate_type = array
+(
+    1 => '无返利',
+    2 => '百分比',
+    3 => '数值',
+);
+
+$rebate_config = function()
+{
+	$array = array
+	(
+		-1 => array
+		(
+			'id' => -1,
+			'name' => '当前积分',
+		),
+	);
+	$info = Dever::db('score/config')->state();
+	if($info)
+	{
+		$array += $info;
+	}
+	return $array;
+};
+
 return array
 (
 	# 表名
@@ -92,6 +117,17 @@ return array
 			'list'		=> true,
 		),
 
+		'num'		=> array
+		(
+			'type' 		=> 'varchar-80',
+			'name' 		=> '增加积分数-如果为负数,则是减少积分',
+			'default' 	=> '',
+			'desc' 		=> '增加积分数',
+			'match' 	=> 'option',
+			'update'	=> 'textarea',
+			'list'		=> true,
+		),
+
 		'upper'		=> array
 		(
 			'type' 		=> 'int-11',
@@ -140,6 +176,46 @@ return array
 			'show'		=> 'upper=2'
 		),
 
+		'rebate_type'      => array
+        (
+            'type'      => 'tinyint-1',
+            'name'      => '返利设置',
+            'default'   => '1',
+            'desc'      => '返利设置',
+            'match'     => 'option',
+            'update'    => 'radio',
+            'option'    => $rebate_type,
+            'list'      => true,
+            'search'    => 'select',
+            'control'	=> 'rebate_type',
+        ),
+
+        'rebate_config_id'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '返利积分名称',
+			'default' 	=> '-1',
+			'desc' 		=> '返利积分名称',
+			'match' 	=> 'is_numeric',
+			'update'	=> 'select',
+			'option'	=> $rebate_config,
+			'search'	=> 'select',
+			'list'		=> true,
+			'show'		=> 'rebate_type=2,3',
+		),
+        
+        'rebate_value'     => array
+        (
+            'type'      => 'varchar-500',
+            'name'      => '返利积分数-如果是百分比类型,这里如果填的是10,那就是当前获得积分数的10%。直接填写数字就是当前用户的上级(1级)返利,支持多层层级设置,1:20,2:30 就是1级202级30,同时也支持0级',
+            'default'   => '',
+            'desc'      => '返利积分数',
+            'match'     => 'is_string',
+            'update'    => 'textarea',
+            'list'      => true,
+            'show'		=> 'rebate_type=2,3',
+        ),
+
 		'state'		=> array
 		(
 			'type' 		=> 'tinyint-1',

+ 1 - 1
database/user.php

@@ -107,7 +107,7 @@ return array
 		# 列表
 		'list_button' => array
 		(
-			'list' => array('积分日志', '"user_log&search_option_uid={uid}&oper_parent=user"'),
+			'list' => array('积分日志', '"user_log&&project=score&search_option_uid={uid}&oper_parent=user&oper_project=score&top_project=passport&top_parent=user"'),
 		),
 	),
 

+ 10 - 0
database/user_log.php

@@ -68,6 +68,16 @@ return array
 			'list'		=> '{uid} > 0 ? Dever::load("passport/user-one#username", {uid}) : "匿名用户"',
 		),
 
+		'rebate_uid'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '下级用户',
+			'default' 	=> '0',
+			'desc' 		=> '下级用户',
+			'match' 	=> 'is_numeric',
+			'list'		=> '{rebate_uid} > 0 ? Dever::load("passport/user-one#username", {rebate_uid}) : "无"',
+		),
+
 		'config_id'		=> array
 		(
 			'type' 		=> 'int-11',

+ 124 - 26
lib/Core.php

@@ -22,6 +22,12 @@ class Core
 
 		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;
@@ -31,9 +37,14 @@ class Core
 			if ($num > 0) {
 				$insert['num'] = $num;
 			}
+			if ($parent_uid > 0) {
+				$insert['parent_uid'] = $parent_uid;
+			}
 			$insert['cron_type'] = 1;
 			$log_id = Dever::db('score/action_log')->insert($insert);
 
+			Dever::load('score/lib/core.oper?log_id='.$log_id);
+			return;
 			$sync = Dever::config('base', 'score')->sync;
 			if ($sync) {
 				Dever::daemon('lib/core.oper?log_id='.$log_id, 'score');
@@ -83,46 +94,31 @@ class Core
 			$info['num'] = $info['num'] * $log['num'];
 		}
 		if ($info['num'] == 0) {
-			Dever::db('score/action_log')->update(array('where_id' => $log['id'], 'cron_type' => 3));
-			return;
+			# 未加数据
+			return $this->finish($log);
 		}
 
 		$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, 'cron_type' => 3, 'score' => '0'));
-				return;
+				# 达到积分上限
+				return $this->finish($log, 3, '0');
 			}
 		}
-		Dever::db('score/action_log')->update(array('where_id' => $log['id'], 'score_type' => 2, 'cron_type' => 3, 'score' => $num));
 
-		$update = array();
-		$update['where_id'] = $user['id'];
-		$update['score'] = $user['score'] + $num;
+		# 完成
+		$this->finish($log, 2, $num);
 
-		$insert['uid'] = $uid;
-		$insert['config_id'] = $info['config_id'];
-		$insert['action_log_id'] = $log['id'];
-		$insert['action_id'] = $info['action_id'];
-		$insert['status'] = 1;
-		$insert['num'] = $num;
-		$insert['total'] = $update['score'];
-		$insert['cdate'] = time();
-		$user_log_id = Dever::db('score/user_log')->insert($insert);
+		$user_log_id = $this->add($uid, $num, $info['config_id'], $info['action_id'], $log['id']);
 
-		Dever::db('score/user')->update($update);
+		# 增加返利
+		if ($info['rebate_type'] > 1) {
+			$this->rebate($uid, $num, $info, $log);
+		}
 
 		# 提升等级
 		$level_type = Dever::db('score/level_type')->state();
@@ -141,6 +137,108 @@ class Core
 		}
 	}
 
+	# 增加积分
+	private function add($uid, $num, $config_id, $action_id, $log_id, $rebate_uid = false)
+	{
+		$user = Dever::db('score/user')->one(array('uid' => $uid, 'config_id' => $config_id));
+
+		if (!$user) {
+			$user = array();
+			$user['id'] = Dever::db('score/user')->insert(array('uid' => $uid, 'config_id' => $config_id, 'score' => 0, 'no_score' => 0));
+			$user['score'] = 0;
+		}
+
+		$update = array();
+		$update['where_id'] = $user['id'];
+		$update['score'] = $user['score'] + $num;
+
+		$insert['uid'] = $uid;
+		$insert['config_id'] = $config_id;
+		$insert['action_log_id'] = $log_id;
+		$insert['action_id'] = $action_id;
+		if ($rebate_uid) {
+			$insert['rebate_uid'] = $rebate_uid;
+		}
+		$insert['status'] = 1;
+		$insert['num'] = $num;
+		$insert['total'] = $update['score'];
+		$insert['cdate'] = time();
+		$user_log_id = Dever::db('score/user_log')->insert($insert);
+
+		Dever::db('score/user')->update($update);
+
+		return $user_log_id;
+	}
+
+	# 返利
+	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['config_id'], $info['action_id'], $log['id'], $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['config_id'], $info['action_id'], $log['id'], $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)
 	{
 		# 有上限限制