dever 6 years ago
commit
4c6cc029de
12 changed files with 1387 additions and 0 deletions
  1. 13 0
      README.md
  2. 77 0
      database/action.php
  3. 142 0
      database/action_log.php
  4. 114 0
      database/config.php
  5. 126 0
      database/level.php
  6. 167 0
      database/rule.php
  7. 127 0
      database/user.php
  8. 115 0
      database/user_level.php
  9. 214 0
      database/user_log.php
  10. 8 0
      index.php
  11. 240 0
      lib/Core.php
  12. 44 0
      lib/Manage.php

+ 13 - 0
README.md

@@ -0,0 +1,13 @@
+# score
+
+积分管理组件:非常方便使用的积分管理功能,需要依赖passport组件,也可以自行开发passport,只要能使用uid即可
+
+使用方法:
+
+```
+# 记录积分行为
+$uid = 1;//用户id
+$key = 'key';//积分行为的key
+$name = 'name';//积分行为的名
+Dever::score($uid, $key, $name);
+```

+ 77 - 0
database/action.php

@@ -0,0 +1,77 @@
+<?php
+
+return array
+(
+	# 表名
+	'name' => 'action',
+	# 显示给用户看的名称
+	'lang' => '用户行为配置',
+	'order' => 10,
+	'check' => 'key',
+	# 数据结构
+	'struct' => array
+	(
+		'id' 		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> 'ID',
+			'default' 	=> '',
+			'desc' 		=> '',
+			'match' 	=> 'is_numeric',
+		),
+		
+		'name'		=> array
+		(
+			'type' 		=> 'varchar-80',
+			'name' 		=> '行为名称',
+			'default' 	=> '',
+			'desc' 		=> '请输入行为名称',
+			'match' 	=> 'is_string',
+			'update'	=> 'text',
+			'search'	=> 'fulltext',
+			'list'		=> true,
+		),
+
+		'key'		=> array
+		(
+			'type' 		=> 'varchar-80',
+			'name' 		=> '行为标识-用于程序中统计积分总数',
+			'default' 	=> '',
+			'desc' 		=> '行为标识',
+			'match' 	=> 'is_string',
+			'update'	=> 'text',
+			'search'	=> 'fulltext',
+			'list'		=> true,
+		),
+
+		'state'		=> array
+		(
+			'type' 		=> 'tinyint-1',
+			'name' 		=> '状态',
+			'default' 	=> '1',
+			'desc' 		=> '请选择状态',
+			'match' 	=> 'is_numeric',
+		),
+		
+		'cdate'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '录入时间',
+			'match' 	=> array('is_numeric', time()),
+			'desc' 		=> '',
+			# 只有insert时才生效
+			'insert'	=> true,
+		),
+	),
+
+	'manage' => array
+	(
+		'delete' => false,
+		'edit' => false,
+		# 列表
+		'list_button' => array
+		(
+			'list' => array('行为日志', '"action_log&search_option_action_id={id}&oper_parent=action"'),
+		),
+	),
+);

+ 142 - 0
database/action_log.php

@@ -0,0 +1,142 @@
+<?php
+
+$action = function()
+{
+	$array = array();
+	$info = Dever::db('score/action')->state();
+	if($info)
+	{
+		$array += $info;
+	}
+	return $array;
+};
+
+$score_type = array
+(
+	1 => '未加积分',
+	2 => '已加积分',
+	3 => '达到积分上限',
+);
+
+return array
+(
+	# 表名
+	'name' => 'action_log',
+	# 显示给用户看的名称
+	'lang' => '积分行为日志',
+	'order' => 10,
+	'menu' => false,
+	# 数据结构
+	'struct' => array
+	(
+		'id' 		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> 'ID',
+			'default' 	=> '',
+			'desc' 		=> '',
+			'match' 	=> 'is_numeric',
+		),
+
+		'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}) : "匿名用户"',
+		),
+		
+		'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,
+		),
+
+		'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})',
+		),
+	),
+
+	'manage' => array
+	(
+		'delete' => false,
+		'edit' => false,
+		'insert' => false,
+	),
+);

+ 114 - 0
database/config.php

@@ -0,0 +1,114 @@
+<?php
+
+$type = array
+(
+	1 => '主积分',
+	2 => '普通积分',
+);
+
+return array
+(
+	# 表名
+	'name' => 'config',
+	# 显示给用户看的名称
+	'lang' => '积分名称设置',
+	'order' => 1,
+	'end' => array
+	(
+		'insert' => 'score/lib/manage.config',
+		'update' => 'score/lib/manage.config',
+	),
+	# 数据结构
+	'struct' => array
+	(
+		'id' 		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> 'ID',
+			'default' 	=> '',
+			'desc' 		=> '',
+			'match' 	=> 'is_numeric',
+		),
+		
+		'name'		=> array
+		(
+			'type' 		=> 'varchar-32',
+			'name' 		=> '积分名称',
+			'default' 	=> '',
+			'desc' 		=> '请输入积分名称',
+			'match' 	=> 'is_string',
+			'update'	=> 'text',
+			'search'	=> 'fulltext',
+			'list'		=> true,
+		),
+
+		'type'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '是否主积分-只能有一个主积分,主积分将作为用户主要积分显示在用户信息中',
+			'default' 	=> '2',
+			'desc' 		=> '是否主积分',
+			'match' 	=> 'is_numeric',
+			'update'	=> 'select',
+			'option'	=> $type,
+			'search'	=> 'select',
+			'list'		=> true,
+		),
+		
+		'reorder'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '排序(数值越大越靠前)',
+			'default' 	=> '1',
+			'desc' 		=> '请输入排序',
+			'match' 	=> 'option',
+			'update'	=> 'text',
+			'search'	=> 'order',
+			'list'		=> true,
+			'order'		=> 'desc',
+			'edit'		=> true,
+		),
+
+		'state'		=> array
+		(
+			'type' 		=> 'tinyint-1',
+			'name' 		=> '状态',
+			'default' 	=> '1',
+			'desc' 		=> '请选择状态',
+			'match' 	=> 'is_numeric',
+		),
+
+		'cdate'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '录入时间',
+			'match' 	=> array('is_numeric', time()),
+			'desc' 		=> '',
+			# 只有insert时才生效
+			'insert'	=> true,
+		),
+	),
+
+	'manage' => array
+	(
+		# 列表
+		'list_button' => array
+		(
+			'list' => array('规则设置', '"rule&search_option_config_id={id}&oper_parent=config"'),
+		),
+	),
+
+	'request' => array
+	(
+		# 更新
+		'set' => array
+		(
+			'type' => 'update',
+			'set' => array
+			(
+				'type' => '2',
+			),
+		),
+	),
+	
+);

+ 126 - 0
database/level.php

@@ -0,0 +1,126 @@
+<?php
+
+$config = function()
+{
+    $array = array();
+    $info = Dever::db('score/config')->state();
+    if($info)
+    {
+        $array += $info;
+    }
+    return $array;
+};
+
+return array
+(
+    # 表名
+    'name' => 'level',
+    # 显示给用户看的名称
+    'lang' => '积分等级设置',
+    # 后台菜单排序
+    'order' => 8,
+    'check' => 'level',
+    # 数据结构
+    'struct' => array
+    (
+        'id'        => array
+        (
+            'type'      => 'int-11',
+            'name'      => 'ID',
+            'default'   => '',
+            'desc'      => '',
+            'match'     => 'is_numeric',
+            'search'    => 'order',
+            //'list'      => true,
+            //'order'     => 'desc',
+        ),
+        
+        'name'      => array
+        (
+            'type'      => 'varchar-32',
+            'name'      => '等级名称',
+            'default'   => '',
+            'desc'      => '请输入名称',
+            'match'     => 'is_string',
+            'update'    => 'text',
+            'search'    => 'fulltext',
+            'list'      => true,
+        ),
+        
+        'score'       => array
+        (
+            'type'      => 'text-1000',
+            'name'      => '积分设置',
+            'default'   => '',
+            'desc'      => '积分设置',
+            'match'     => 'is_string',
+            'update'    => array
+            (
+                array
+                (
+                    'col'       => 'config',
+                    'name'      => '积分名称',
+                    'default'   => '',
+                    'desc'      => '积分名称',
+                    'match'     => 'is_string',
+                    'update'    => 'select',
+                    'option'    => $config,
+                ),
+
+                array
+                (
+                    'col'       => 'num',
+                    'name'      => '所需积分数',
+                    'default'   => '0',
+                    'desc'      => '所需积分数',
+                    'match'     => 'is_string',
+                    'update'    => 'text',
+                ),
+            ),
+        ),
+
+        'level'       => array
+        (
+            'type'      => 'int-11',
+            'name'      => '等级数字-请直接输入等级数字,每次升级按照该数字进行匹配,如果设置为1,则为lv1,不允许重复,用户升级',
+            'default'   => '1',
+            'desc'      => '等级数字',
+            'match'     => 'option',
+            'update'    => 'text',
+            'search'    => 'order',
+            'list'      => true,
+            'order'     => 'desc',
+            'edit'      => true,
+        ),
+
+        'state'     => array
+        (
+            'type'      => 'tinyint-1',
+            'name'      => '状态',
+            'default'   => '1',
+            'desc'      => '请选择状态',
+            'match'     => 'is_numeric',
+        ),
+        
+        'cdate'     => array
+        (
+            'type'      => 'int-11',
+            'name'      => '录入时间',
+            'match'     => array('is_numeric', time()),
+            'desc'      => '',
+            # 只有insert时才生效
+            'insert'    => true,
+            'list'      => 'date("Y-m-d H:i:s", {cdate})',
+        ),
+    ),
+
+    'manage' => array
+    (
+
+    ),
+
+    'request' => array
+    (
+        
+    ),
+);

+ 167 - 0
database/rule.php

@@ -0,0 +1,167 @@
+<?php
+
+$config = function()
+{
+	$array = array();
+	$info = Dever::db('score/config')->state();
+	if($info)
+	{
+		$array += $info;
+	}
+	return $array;
+};
+
+$action = function()
+{
+	$array = array();
+	$info = Dever::db('score/action')->state();
+	if($info)
+	{
+		$array += $info;
+	}
+	return $array;
+};
+
+$upper = array
+(
+	1 => '无上限',
+	2 => '有上限',
+);
+
+$upper_type = array
+(
+	1 => '按自然天-每天0点重置',
+	2 => '按小时',
+	3 => '永久',
+);
+
+return array
+(
+	# 表名
+	'name' => 'rule',
+	# 显示给用户看的名称
+	'lang' => '积分规则设置',
+	'order' => 10,
+	'menu' => false,
+	# 数据结构
+	'struct' => array
+	(
+		'id' 		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> 'ID',
+			'default' 	=> '',
+			'desc' 		=> '',
+			'match' 	=> 'is_numeric',
+		),
+		
+		'config_id'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '积分名称',
+			'default' 	=> '1',
+			'desc' 		=> '积分名称',
+			'match' 	=> 'is_numeric',
+			'update'	=> 'select',
+			'option'	=> $config,
+			'search'	=> 'select',
+			'list'		=> true,
+		),
+
+		'action_id'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '所属行为',
+			'default' 	=> '1',
+			'desc' 		=> '所属行为',
+			'match' 	=> 'is_numeric',
+			'update'	=> 'select',
+			'option'	=> $action,
+			'search'	=> 'select',
+			'list'		=> true,
+		),
+
+		'num'		=> array
+		(
+			'type' 		=> 'varchar-80',
+			'name' 		=> '增加积分数-如果为负数,则是减少积分',
+			'default' 	=> '',
+			'desc' 		=> '增加积分数',
+			'match' 	=> 'option',
+			'update'	=> 'textarea',
+			'list'		=> true,
+		),
+
+		'upper'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '是否有上限限制',
+			'default' 	=> '1',
+			'desc' 		=> '是否有上限限制',
+			'match' 	=> 'is_numeric',
+			'update'	=> 'select',
+			'option'	=> $upper,
+			'search'	=> 'select',
+			'control'	=> 'upper',
+		),
+
+		'upper_type'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '上限限制类型',
+			'default' 	=> '1',
+			'desc' 		=> '上限限制类型',
+			'match' 	=> 'is_numeric',
+			'update'	=> 'select',
+			'option'	=> $upper_type,
+			'show'		=> 'upper=2',
+			'control'	=> 'upper_type',
+		),
+
+		'upper_time'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '间隔小时数',
+			'default' 	=> '12',
+			'desc' 		=> '间隔小时数',
+			'match' 	=> 'option',
+			'update'	=> 'text',
+			'show'		=> 'upper_type=2'
+		),
+
+		'upper_limit'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '上限限制次数-小于1则不限制',
+			'default' 	=> '2',
+			'desc' 		=> '上限限制次数',
+			'match' 	=> 'option',
+			'update'	=> 'text',
+			'show'		=> 'upper=2'
+		),
+
+		'state'		=> array
+		(
+			'type' 		=> 'tinyint-1',
+			'name' 		=> '状态',
+			'default' 	=> '1',
+			'desc' 		=> '请选择状态',
+			'match' 	=> 'is_numeric',
+		),
+
+		'cdate'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '录入时间',
+			'match' 	=> array('is_numeric', time()),
+			'desc' 		=> '',
+			# 只有insert时才生效
+			'insert'	=> true,
+		),
+	),
+
+	'manage' => array
+	(
+		
+	),
+);

+ 127 - 0
database/user.php

@@ -0,0 +1,127 @@
+<?php
+$config = function()
+{
+	$array = array();
+	$info = Dever::db('score/config')->state();
+	if($info)
+	{
+		$array += $info;
+	}
+	return $array;
+};
+
+return array
+(
+	# 表名
+	'name' => 'user',
+	# 显示给用户看的名称
+	'lang' => '用户积分列表',
+	'order' => 100,
+	'menu' => 'passport',
+	# 数据结构
+	'struct' => array
+	(
+		'id' 		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> 'ID',
+			'default' 	=> '',
+			'desc' 		=> '',
+			'match' 	=> 'is_numeric',
+		),
+
+		'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}) : "匿名用户"',
+		),
+
+		'config_id'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '积分名称',
+			'default' 	=> '1',
+			'desc' 		=> '积分名称',
+			'match' 	=> 'is_numeric',
+			'update'	=> 'select',
+			'option'	=> $config,
+			'search'	=> 'select',
+			'list'		=> true,
+		),
+
+		'score'		=> array
+		(
+			'type' 		=> 'varchar-80',
+			'name' 		=> '可用数量',
+			'default' 	=> '0',
+			'desc' 		=> '可用数量',
+			'match' 	=> 'option',
+			'update'	=> 'textarea',
+			'search'	=> 'order',
+			'list'		=> true,
+		),
+
+		'no_score'		=> array
+		(
+			'type' 		=> 'varchar-80',
+			'name' 		=> '冻结数量',
+			'default' 	=> '0',
+			'desc' 		=> '冻结数量',
+			'match' 	=> 'option',
+			'update'	=> 'textarea',
+			'search'	=> 'order',
+			'list'		=> true,
+		),
+
+		'cdate'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '录入时间',
+			'match' 	=> array('is_numeric', time()),
+			'desc' 		=> '',
+			# 只有insert时才生效
+			'insert'	=> true,
+		),
+	),
+
+	'manage' => array
+	(
+		'delete' => false,
+		'edit' => false,
+		'insert' => false,
+
+		# 列表
+		'list_button' => array
+		(
+			'list' => array('积分日志', '"user_log&search_option_uid={uid}&oper_parent=user"'),
+		),
+	),
+
+	# request 请求接口定义
+	'request' => array
+	(
+		'getScoreByConfig' => array
+		(
+			# 匹配的正则或函数 选填项
+			'option' => array
+			(
+				'uid' => 'yes',
+			),
+			'type' => 'all',
+			'order' => array('id' => 'desc'),
+			'col' => 'uid,config_id,score|config_id',
+		),
+	)
+);

+ 115 - 0
database/user_level.php

@@ -0,0 +1,115 @@
+<?php
+$level = function()
+{
+	$array = array();
+	$info = Dever::db('score/level')->state();
+	if($info)
+	{
+		$array += $info;
+	}
+	return $array;
+};
+
+$type = array
+(
+	1 => '自动升级',
+	2 => '手动升级',
+);
+
+return array
+(
+	# 表名
+	'name' => 'user_level',
+	# 显示给用户看的名称
+	'lang' => '用户积分等级',
+	'order' => 99,
+	'menu' => 'passport',
+	# 数据结构
+	'struct' => array
+	(
+		'id' 		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> 'ID',
+			'default' 	=> '',
+			'desc' 		=> '',
+			'match' 	=> 'is_numeric',
+		),
+
+		'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}) : "匿名用户"',
+		),
+
+		'level_id'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '等级名称',
+			'default' 	=> '1',
+			'desc' 		=> '等级名称',
+			'match' 	=> 'is_numeric',
+			'update'	=> 'select',
+			'option'	=> $level,
+			'search'	=> 'select',
+			'list'		=> true,
+			'edit'		=> true,
+		),
+
+		'type'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '升级方式',
+			'default' 	=> '1',
+			'desc' 		=> '升级方式',
+			'match' 	=> 'is_numeric',
+			'update'	=> 'select',
+			'option'	=> $type,
+			'search'	=> 'select',
+			'list'		=> true,
+			'edit'		=> true,
+		),
+
+		'info'      => array
+        (
+            'type'      => 'varchar-300',
+            'name'      => '备注',
+            'default'   => '',
+            'desc'      => '备注',
+            'match'     => 'option',
+            'update'    => 'textarea',
+            //'show'        => 'status=5'
+            'list'      => '"{info}" ? "{info}" : "双击添加备注"',
+            'edit'      => true,
+        ),
+
+		'cdate'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '录入时间',
+			'match' 	=> array('is_numeric', time()),
+			'desc' 		=> '',
+			# 只有insert时才生效
+			'insert'	=> true,
+		),
+	),
+
+	'manage' => array
+	(
+		'delete' => false,
+		'edit' => false,
+		'insert' => false,
+	),
+);

+ 214 - 0
database/user_log.php

@@ -0,0 +1,214 @@
+<?php
+
+$config = function()
+{
+	$array = array();
+	$info = Dever::db('score/config')->state();
+	if($info)
+	{
+		$array += $info;
+	}
+	return $array;
+};
+
+$action = function()
+{
+	$array = array();
+	$info = Dever::db('score/action')->state();
+	if($info)
+	{
+		$array += $info;
+	}
+	return $array;
+};
+
+
+$status = array
+(
+    1 => '收入',
+    2 => '支出',
+);
+
+
+return array
+(
+	# 表名
+	'name' => 'user_log',
+	# 显示给用户看的名称
+	'lang' => '用户积分日志',
+	'order' => 10,
+	'menu' => false,
+	# 数据结构
+	'struct' => array
+	(
+		'id' 		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> 'ID',
+			'default' 	=> '',
+			'desc' 		=> '',
+			'match' 	=> 'is_numeric',
+		),
+
+		'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}) : "匿名用户"',
+		),
+
+		'config_id'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '积分名称',
+			'default' 	=> '1',
+			'desc' 		=> '积分名称',
+			'match' 	=> 'is_numeric',
+			'update'	=> 'select',
+			'option'	=> $config,
+			'search'	=> 'select',
+			'list'		=> true,
+		),
+		
+		'action_id'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '所属行为',
+			'default' 	=> '1',
+			'desc' 		=> '所属行为',
+			'match' 	=> 'is_numeric',
+			'update'	=> 'select',
+			'option'	=> $action,
+			'search'	=> 'select',
+			'list'		=> true,
+		),
+
+		'status'        => array
+        (
+            'type'      => 'tinyint-1',
+            'name'      => '收入支出状态',
+            'default'   => '1',
+            'desc'      => '收入支出状态',
+            'match'     => 'is_numeric',
+            'update'    => 'select',
+            'option'    => $status,
+            //'search'  => 'select',
+            'list'      => true,
+            //'edit'        => true,
+        ),
+
+		'num'		=> array
+		(
+			'type' 		=> 'varchar-80',
+			'name' 		=> '增加积分数-如果为负数,则是减少积分',
+			'default' 	=> '',
+			'desc' 		=> '增加积分数',
+			'match' 	=> 'option',
+			'update'	=> 'textarea',
+			'list'		=> true,
+		),
+
+		'total'		=> array
+		(
+			'type' 		=> 'varchar-80',
+			'name' 		=> '总积分数',
+			'default' 	=> '',
+			'desc' 		=> '总积分数',
+			'match' 	=> 'option',
+			'update'	=> 'textarea',
+			'list'		=> true,
+		),
+
+		'desc'     => array
+        (
+            'type'      => 'varchar-300',
+            'name'      => '描述',
+            'default'   => '',
+            'desc'      => '描述',
+            'match'     => 'option',
+            'update'    => 'textarea',
+        ),
+
+        'state'		=> array
+		(
+			'type' 		=> 'tinyint-1',
+			'name' 		=> '状态',
+			'default' 	=> '1',
+			'desc' 		=> '请选择状态',
+			'match' 	=> 'is_numeric',
+		),
+
+		'cdate'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '录入时间',
+			'match' 	=> array('is_numeric', time()),
+			'desc' 		=> '',
+			# 只有insert时才生效
+			'insert'	=> true,
+			'list'      => 'date("Y-m-d H:i:s", {cdate})',
+		),
+	),
+
+	'manage' => array
+	(
+		'delete' => false,
+		'edit' => false,
+		'insert' => false,
+
+		# 列表
+		'list_button' => array
+		(
+			//'list' => array('行为日志', '"action_log&search_option_action_id={action_id}&search_option_uid={uid}&oper_parent=user_log"'),
+		),
+	),
+
+	# request 请求接口定义
+	'request' => array
+	(
+		'getNew' => array
+		(
+			# 匹配的正则或函数 选填项
+			'option' => array
+			(
+				'config_id' => 'yes',
+				'action_id' => 'yes',
+				'uid' => 'yes',
+				'status' => 'yes',
+				'state' => 1,
+			),
+			'type' => 'one',
+			'order' => array('cdate' => 'desc'),
+			'col' => '*',
+		),
+
+		'getNewTotal' => array
+		(
+			# 匹配的正则或函数 选填项
+			'option' => array
+			(
+				'config_id' => 'yes',
+				'action_id' => 'yes',
+				'uid' => 'yes',
+				'status' => 'yes',
+				'state' => 1,
+				'start' => array('yes-cdate', '>='),
+				'end' => array('yes-cdate', '<='),
+			),
+			'type' => 'count',
+			'order' => array('cdate' => 'desc'),
+			'col' => 'count(1) as total',
+		),
+	)
+);

+ 8 - 0
index.php

@@ -0,0 +1,8 @@
+<?php
+
+define('DEVER_APP_NAME', 'score');
+define('DEVER_APP_LANG', '积分规则配置');
+define('DEVER_APP_PATH', dirname(__FILE__) . DIRECTORY_SEPARATOR);
+define('DEVER_MANAGE_ORDER', 1);
+define('DEVER_MANAGE_ICON', 'glyphicon glyphicon-credit-card layui-icon-chat');
+include(DEVER_APP_PATH . '../boot.php');

+ 240 - 0
lib/Core.php

@@ -0,0 +1,240 @@
+<?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));
+				}
+			}
+		}
+	}
+}

+ 44 - 0
lib/Manage.php

@@ -0,0 +1,44 @@
+<?php
+
+namespace Score\Lib;
+
+use Dever;
+
+class Manage
+{
+	/**
+	 * 显示用户信息
+	 *
+	 * @return mixed
+	 */
+	public function showUserInfo($uid, $ldate = false)
+	{
+		if ($uid) {
+			$user = Dever::load('passport/user.info', $uid);
+			if ($user) {
+				$table = array();
+				$table['用户名'] = $user['username'];
+				$table['手机号'] = $user['mobile'];
+				$table['领取时间'] = date('Y-m-d H:i:s', $ldate);
+				return Dever::table($table);
+			}
+		} else {
+			return '';
+		}
+	}
+
+    /**
+	 * 设置主积分
+	 *
+	 * @return mixed
+	 */
+	public function config($id, $name, $param)
+	{
+		$type = Dever::param('type', $param);
+
+		if ($type == 1) {
+			Dever::db('score/config')->set(array('type' => 2));
+			Dever::db('score/config')->update(array('where_id' => $id, 'type' => 1));
+		}
+	}
+}