rabin 6 years ago
parent
commit
9ddbfc299b
2 changed files with 260 additions and 46 deletions
  1. 114 39
      database/info.php
  2. 146 7
      src/Api.php

+ 114 - 39
database/info.php

@@ -13,13 +13,15 @@ $cate = function()
 
 $type = array
 (
-	1 => '单行输入框',
-	2 => '多行输入框',
-	3 => '图文编辑器',
-	4 => '单图片上传',
-	5 => '多图片上传',
-	6 => '地区选择器',
-	//7 => '多级分类',
+	1 => '数字输入框',
+	2 => '单行输入框',
+	3 => '多行输入框',
+	4 => '图文编辑器',
+	5 => '单图片上传',
+	6 => '多图片上传',
+	7 => '地区选择器',
+	//8 => '多级分类',
+	//9 => '区间输入框',
 
 	10 => '单选框',
 	11 => '多选框',
@@ -39,12 +41,6 @@ $data_type = array
 	3 => '正则匹配',
 );
 
-$search = array
-(
-	1 => '可以作为搜索条件',
-	2 => '不作为搜索条件',
-);
-
 
 /*
 使用该组件,这么用
@@ -104,9 +100,9 @@ return array
 		'name'		=> array
 		(
 			'type' 		=> 'varchar-150',
-			'name' 		=> '中文名称',
+			'name' 		=> '名称',
 			'default' 	=> '',
-			'desc' 		=> '中文名称',
+			'desc' 		=> '名称',
 			'match' 	=> 'is_string',
 			'update'	=> 'text',
 			'search'	=> 'fulltext',
@@ -114,14 +110,24 @@ return array
 			'edit'		=> true,
 		),
 
+		'unit'		=> array
+		(
+			'type' 		=> 'varchar-150',
+			'name' 		=> '单位-填写属性单位,选填项',
+			'default' 	=> '',
+			'desc' 		=> '单位',
+			'match' 	=> 'option',
+			'update'	=> 'text',
+		),
+
 		'ename'		=> array
 		(
 			'type' 		=> 'varchar-150',
-			'name' 		=> '英文名称',
+			'name' 		=> '英文名称-设置之后,获取该属性的值时,将使用该值作为标识,请勿随意更改',
 			'default' 	=> '',
 			'desc' 		=> '英文名称',
 			'match' 	=> 'is_string',
-			//'update'	=> 'text',
+			//'update'	=> 'option',
 			//'search'	=> 'fulltext',
 			//'list'		=> true,
 			//'edit'		=> true,
@@ -141,7 +147,7 @@ return array
 
 		'type'	  => array
 		(
-			'type' 		=> 'tinyint-1',
+			'type' 		=> 'tinyint-11',
 			'name' 		=> '属性类型',
 			'default' 	=> '1',
 			'desc' 		=> '属性类型',
@@ -156,13 +162,13 @@ return array
 		'type_option'      => array
         (
             'type'      => 'varchar-800',
-            'name'      => '属性可选项-多个值用换行隔开',
+            'name'      => '属性可选项-多个值用换行隔开,如果是数字输入框,这里定义的是搜索时的选项,比如单价如果需要搜索,可以设置为"100万以下,{v}<=100",半角逗号之后的为表达式,{v}为当前值。',
             'default'   => '',
             'desc'      => '属性可选项',
             'match'     => 'is_string',
             'update'    => 'textarea',
             //'list'      => true,
-            'show'      => 'type=10,11,12',
+            'show'      => 'type=1,9,10,11,12',
         ),
 
         'option_type'	  => array
@@ -190,7 +196,7 @@ return array
 			'search'	=> 'select',
 			//'list'		=> true,
 			'control' 	=> 'data_type',
-			'show'      => 'type=1',
+			'show'      => 'type=2',
 		),
 
 		'match'		=> array
@@ -207,33 +213,49 @@ return array
 			'show'		=> array('data_type' => 3),
 		),
 
-		'search'	  => array
+		'list_reorder'	  => array
 		(
-			'type' 		=> 'tinyint-1',
-			'name' 		=> '是否作为搜索条件-如果选择作为搜索条件,将把该属性放入搜索引擎中,这里不宜选择过多,否则影响搜索速度。',
-			'default' 	=> '2',
+			'type' 		=> 'int-11',
+			'name' 		=> '列表页排序-设置该属性在列表页中的排序,数字越大越靠前,如果为负数或者0,则不加入到列表页中。',
+			'default' 	=> '-1',
 			'desc' 		=> '是否作为搜索条件',
 			'match' 	=> 'is_numeric',
-			'update'	=> 'radio',
-			'option'	=> $search,
-			'search'	=> 'select',
-			'show'      => 'type=1,6,10,11,12',
-			//'list'		=> true,
+			'update'	=> 'text',
+			'show'      => 'type=1,2,7,9,10,11,12',
+			'search'	=> 'order',
+			'order'		=> 'desc',
+			'edit'		=> true,
+			'list'		=> true,
 		),
 
-		'reorder'		=> array
+		'search_reorder'	  => array
 		(
 			'type' 		=> 'int-11',
-			'name' 		=> '排序(数值越大越靠前)',
-			'default' 	=> '1',
-			'desc' 		=> '请输入排序',
-			'match' 	=> 'option',
+			'name' 		=> '搜索条件排序-设置搜索条件中的排序,数字越大越靠前,如果为负数或者0,则不加入到搜索条件中。',
+			'default' 	=> '-1',
+			'desc' 		=> '是否作为搜索条件',
+			'match' 	=> 'is_numeric',
 			'update'	=> 'text',
+			'show'      => 'type=1,2,7,9,10,11,12',
 			'search'	=> 'order',
-			'list_name' => '排序',
+			'order'		=> 'desc',
+			'edit'		=> true,
 			'list'		=> true,
+		),
+
+		'view_reorder'	  => array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '详情页排序-设置该属性在详情页中的排序,数字越大越靠前,如果为负数或者0,则不加入到详情页中。',
+			'default' 	=> '-1',
+			'desc' 		=> '详情页排序',
+			'match' 	=> 'is_numeric',
+			'update'	=> 'text',
+			'show'      => 'type=1,2,7,9,10,11,12',
+			'search'	=> 'order',
 			'order'		=> 'desc',
 			'edit'		=> true,
+			'list'		=> true,
 		),
 		
 		'state'		=> array
@@ -253,8 +275,8 @@ return array
 			'desc' 		=> '',
 			# 只有insert时才生效
 			'insert'	=> true,
-			'search'	=> 'date',
-			'list'		=> 'date("Y-m-d H:i:s", {cdate})',
+			//'search'	=> 'date',
+			//'list'		=> 'date("Y-m-d H:i:s", {cdate})',
 		),
 	),
 
@@ -275,8 +297,61 @@ return array
 				'state' => 1,
 			),
 			'type' => 'all',
-			'order' => array('reorder' => 'desc', 'id' => 'desc'),
+			'order' => array('list_reorder`+`search_reorder`+`view_reorder' => 'desc', 'id' => 'desc'),
+			'col' => '*|id',
+		),
+
+		'getListByIds' => array
+		(
+			# 匹配的正则或函数 选填项
+			'option' => array
+			(
+				'ids' => array('yes-id', 'in'),
+				'state' => 1,
+			),
+			'type' => 'all',
+			'order' => array('list_reorder' => 'desc', 'id' => 'desc'),
+			'col' => '*|id',
+		),
+
+		'getSearchByIds' => array
+		(
+			# 匹配的正则或函数 选填项
+			'option' => array
+			(
+				'ids' => array('yes-id', 'in'),
+				'state' => 1,
+			),
+			'type' => 'all',
+			'order' => array('search_reorder' => 'desc', 'id' => 'desc'),
 			'col' => '*|id',
 		),
+
+		'getViewByIds' => array
+		(
+			# 匹配的正则或函数 选填项
+			'option' => array
+			(
+				'ids' => array('yes-id', 'in'),
+				'state' => 1,
+			),
+			'type' => 'all',
+			'order' => array('view_reorder' => 'desc', 'id' => 'desc'),
+			'col' => '*|id',
+		),
+
+		'getSearch' => array
+		(
+			# 匹配的正则或函数 选填项
+			'option' => array
+			(
+				'ids' => array('yes-id', 'in'),
+				'search_reorder' => array('yes', '>'),
+				'state' => 1,
+			),
+			'type' => 'all',
+			'order' => array('search_reorder' => 'desc', 'id' => 'desc'),
+			'col' => '*',
+		),
 	)
 );

+ 146 - 7
src/Api.php

@@ -69,38 +69,157 @@ class Api
 	}
 
 	/**
+     * 设置数据库的结构信息
+     *
+     * @return mixed
+     */
+    public function setDatabaseConfig($attr, &$config)
+    {
+        if ($attr) {
+			foreach ($attr as $k => $v) {
+				$k = 'attr_' . $v['id'];
+				$config['struct'][$k] = array();
+
+				$option = array();
+				if ($v['type_option']) {
+					$v['type_option'] = explode("\n", $v['type_option']);
+					foreach ($v['type_option'] as $k1 => $v1) {
+						$option[$k1+1] = $v1;
+					}
+				}
+
+				$config['struct'][$k]['name'] = $v['name'];
+				$config['struct'][$k]['default'] = '';
+				$config['struct'][$k]['desc'] = $v['name'];
+
+				switch ($v['type']) {
+					case 3:
+						$config['struct'][$k]['update'] = 'textarea';
+						$config['struct'][$k]['search'] = 'fulltext';
+						$config['struct'][$k]['type'] = 'varchar-800';
+						$config['struct'][$k]['match'] = 'is_string';
+						break;
+					case 4:
+						$config['struct'][$k]['type'] = 'text-255';
+						$config['struct'][$k]['update'] = 'editor';
+						$config['struct'][$k]['match'] = 'is_string';
+						$config['struct'][$k]['key'] = 1;
+					case 5:
+						$config['struct'][$k]['update'] = 'image';
+						$config['struct'][$k]['type'] = 'varchar-150';
+						$config['struct'][$k]['match'] = 'is_string';
+						$config['struct'][$k]['key'] = 1;
+						break;
+					case 6:
+						$config['struct'][$k]['update'] = 'images';
+						$config['struct'][$k]['type'] = 'text-255';
+						$config['struct'][$k]['match'] = 'is_string';
+						$config['struct'][$k]['key'] = 1;
+						break;
+					case 7:
+						$config['struct'][$k]['type'] = 'varchar-800';
+						$config['struct'][$k]['match'] = 'is_string';
+						$config['struct'][$k]['update'] = 'linkage';
+						$config['struct'][$k]['search'] = 'linkage';
+						$config['struct'][$k]['option'] = Dever::url('area/api.get');
+						//$config['struct'][$k]['list'] = 'Dever::load("area/api.string", "{area}")';
+						break;
+
+					case 10:
+						$config['struct'][$k]['update'] = 'radio';
+						$config['struct'][$k]['type'] = 'varchar-11';
+						$config['struct'][$k]['match'] = 'is_numeric';
+						$config['struct'][$k]['option'] = $option;
+						$config['struct'][$k]['search'] = 'select';
+						$config['struct'][$k]['default'] = '1';
+
+						break;
+					case 11:
+						$config['struct'][$k]['update'] = 'checkbox';
+						$config['struct'][$k]['type'] = 'varchar-150';
+						$config['struct'][$k]['match'] = 'is_string';
+						$config['struct'][$k]['option'] = $option;
+						$config['struct'][$k]['search'] = 'select';
+						$config['struct'][$k]['default'] = '1';
+						break;
+					case 12:
+						$config['struct'][$k]['update'] = 'select';
+						$config['struct'][$k]['type'] = 'varchar-11';
+						$config['struct'][$k]['match'] = 'is_numeric';
+						$config['struct'][$k]['option'] = $option;
+						$config['struct'][$k]['search'] = 'select';
+						$config['struct'][$k]['default'] = '1';
+						break;
+					
+					default:
+						$config['struct'][$k]['update'] = 'text';
+						$config['struct'][$k]['search'] = 'fulltext';
+						if ($v['data_type'] == 1 || $v['type'] == 1) {
+							$config['struct'][$k]['type'] = 'varchar-11';
+							$config['struct'][$k]['match'] = 'is_numeric';
+						} else {
+							$config['struct'][$k]['type'] = 'varchar-800';
+							$config['struct'][$k]['match'] = 'is_string';
+						}
+				}
+
+				if ($v['option_type'] && $v['option_type'] == 2) {
+					$config['struct'][$k]['match'] = 'option';
+				}
+
+				if ($v['value']) {
+					$config['struct'][$k]['default'] = $v['value'];
+				}
+
+				if ($v['state'] == 2) {
+					unset($config['struct'][$k]['update']);
+				}
+
+				# 还要根据搜索来建立索引,另外,如果数据结构改了怎么办。暂时用varchar
+			}
+		}
+    }
+
+    /**
      * 获取属性的值,无类别
      *
      * @return mixed
      */
-    public function getValue($ids, $value)
+    public function getValue($ids, $value, $order = 'all')
     {
         $result = array();
 		if ($ids) {
 			$where['ids'] = $ids;
-			$data = Dever::db('attr/info')->getAllByIds($where);
+			$method = 'getAllByIds';
+			if ($order == 'list') {
+				$method = 'getListByIds';
+			} elseif ($order == 'search') {
+				$method = 'getSearchByIds';
+			} elseif ($order == 'view') {
+				$method = 'getViewByIds';
+			}
+			$data = Dever::db('attr/info')->$method($where);
 
 			$ids = explode(',', $ids);
 			$value = explode(',', $value);
 			if ($data) {
 				foreach ($ids as $k => $v) {
 					if (isset($data[$v])) {
-						$result[$v] = $data[$v];
 						if (isset($value) && $value[$k]) {
-							$result[$v]['value'] = $value[$k];
+							$data[$v]['value'] = $value[$k];
 						} else {
-							$result[$v]['value'] = '';
+							$data[$v]['value'] = '';
 						}
 					}
 				}
 			}
 		}
 
-		return $result;
+		return $data;
     }
 
     /**
-     * 获取属性的值,无类别
+     * 获取属性的值,字符类型
      *
      * @return mixed
      */
@@ -118,4 +237,24 @@ class Api
 
 		return Dever::table($table);
     }
+
+    /**
+     * 获取某个分类下的搜索列表
+     *
+     * @return mixed
+     */
+    public function search($ids)
+    {
+        $data = Dever::db('attr/info')->($where);
+
+        $table = array();
+        foreach ($result as $k => $v) {
+        	$table[$v['name']] = '';
+        	foreach ($v['attr'] as $k1 => $v1) {
+        		$table[$v['name']] .= $v1['name'] . "&nbsp;&nbsp;";
+        	}
+        }
+
+		return Dever::table($table);
+    }
 }