Api.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. <?php
  2. namespace Attr\Src;
  3. use Dever;
  4. class Api
  5. {
  6. /**
  7. * 获取属性配置
  8. *
  9. * @return mixed
  10. */
  11. public function get()
  12. {
  13. $cate = Dever::db('attr/cate')->state();
  14. if ($cate) {
  15. foreach ($cate as $k => $v) {
  16. $cate[$k]['child'] = Dever::db('attr/info')->state(array('cate_id' => $v['id']));
  17. }
  18. }
  19. $cate['state'] = 1;
  20. return $cate;
  21. }
  22. /**
  23. * 根据json格式获取信息
  24. *
  25. * @return mixed
  26. */
  27. public function getInfoByJson($data)
  28. {
  29. $result = array();
  30. $string = array();
  31. foreach ($data as $k => $v) {
  32. $info = Dever::db('attr/info')->find($v['attr_id']);
  33. $result[$k]['id'] = $info['id'];
  34. $result[$k]['name'] = $info['name'];
  35. if ($v['id']) {
  36. $attr = Dever::db('attr/value')->find($v['id']);
  37. if ($attr) {
  38. $result[$k]['value_id'] = $attr['id'];
  39. $result[$k]['value_name'] = $attr['name'];
  40. $string[] = $result[$k]['value_name'];
  41. }
  42. }
  43. }
  44. return array('data' => $result, 'string' => implode(';', $string));
  45. }
  46. /**
  47. * 获取属性详细信息,有类别
  48. *
  49. * @return mixed
  50. */
  51. public function getInfo($ids, $value)
  52. {
  53. $result = array();
  54. if ($ids) {
  55. $where['ids'] = $ids;
  56. $cate = Dever::db('attr/cate')->state();
  57. $data = Dever::db('attr/info')->getAllByIds($where);
  58. $ids = explode(',', $ids);
  59. $value = explode(',', $value);
  60. if ($data) {
  61. foreach ($ids as $k => $v) {
  62. if (isset($data[$v])) {
  63. $cate_id = $data[$v]['cate_id'];
  64. if (!isset($result[$cate_id])) {
  65. $result[$cate_id] = array
  66. (
  67. 'name' => $cate[$cate_id]['name'],
  68. 'id' => $cate_id
  69. );
  70. }
  71. $result[$cate_id]['attr'][$v] = array
  72. (
  73. 'name' => $data[$v]['name'],
  74. 'value' => $value[$k],
  75. 'id' => $v
  76. );
  77. }
  78. }
  79. }
  80. }
  81. return $result;
  82. }
  83. public function getInfoAttr($category)
  84. {
  85. //$all = Dever::db('category/attr')->all();
  86. $table = '';
  87. $attr = array();
  88. $create = Dever::config('database')->create;
  89. if ($category) {
  90. if (is_array($category)) {
  91. $category = implode(',', $category);
  92. }
  93. $table = '_' . str_replace(',', '_', $category);
  94. # 自动建表开启并且自动添加字段
  95. $create = false;
  96. $attr = $this->getAttrByCate($category);
  97. $table = str_replace('_-1', '', $table);
  98. }
  99. return array($category, $attr, $table, $create);
  100. }
  101. /**
  102. * 设置数据库的结构信息
  103. *
  104. * @return mixed
  105. */
  106. public function setDatabaseConfig($attr, &$config)
  107. {
  108. if ($attr) {
  109. foreach ($attr as $k => $v) {
  110. if ($v['type'] == 9) {
  111. $k = 'attr_' . $v['id'] . '_s';
  112. $config['struct'][$k]['name'] = $v['name'];
  113. $config['struct'][$k]['default'] = '';
  114. $config['struct'][$k]['desc'] = $v['name'];
  115. $config['struct'][$k]['update'] = 'hidden';
  116. $config['struct'][$k]['type'] = 'varchar-11';
  117. $config['struct'][$k]['match'] = 'is_numeric';
  118. $k = 'attr_' . $v['id'] . '_e';
  119. $config['struct'][$k]['name'] = $v['name'];
  120. $config['struct'][$k]['default'] = '';
  121. $config['struct'][$k]['desc'] = $v['name'];
  122. $config['struct'][$k]['update'] = 'hidden';
  123. $config['struct'][$k]['type'] = 'varchar-11';
  124. $config['struct'][$k]['match'] = 'is_numeric';
  125. }
  126. $k = 'attr_' . $v['id'];
  127. $config['struct'][$k] = array();
  128. /*
  129. $option = array();
  130. if ($v['type_option']) {
  131. $v['type_option'] = explode("\n", $v['type_option']);
  132. foreach ($v['type_option'] as $k1 => $v1) {
  133. $option[$k1+1] = $v1;
  134. }
  135. }*/
  136. $option = Dever::db('attr/value')->getData(array('info_id' => $v['id']));
  137. $config['struct'][$k]['name'] = $v['name'];
  138. $config['struct'][$k]['default'] = '';
  139. $config['struct'][$k]['desc'] = $v['name'];
  140. switch ($v['type']) {
  141. case 3:
  142. $config['struct'][$k]['update'] = 'textarea';
  143. $config['struct'][$k]['search'] = 'fulltext';
  144. $config['struct'][$k]['type'] = 'varchar-800';
  145. $config['struct'][$k]['match'] = 'is_string';
  146. break;
  147. case 4:
  148. $config['struct'][$k]['type'] = 'text-255';
  149. $config['struct'][$k]['update'] = 'editor';
  150. $config['struct'][$k]['match'] = 'is_string';
  151. $config['struct'][$k]['key'] = 1;
  152. case 5:
  153. $config['struct'][$k]['update'] = 'image';
  154. $config['struct'][$k]['type'] = 'varchar-150';
  155. $config['struct'][$k]['match'] = 'is_string';
  156. $config['struct'][$k]['key'] = 1;
  157. break;
  158. case 6:
  159. $config['struct'][$k]['update'] = 'images';
  160. $config['struct'][$k]['type'] = 'text-255';
  161. $config['struct'][$k]['match'] = 'is_string';
  162. $config['struct'][$k]['key'] = 1;
  163. break;
  164. case 7:
  165. $config['struct'][$k]['type'] = 'varchar-800';
  166. $config['struct'][$k]['match'] = 'is_string';
  167. $config['struct'][$k]['update'] = 'linkage';
  168. $config['struct'][$k]['search'] = 'linkage';
  169. $config['struct'][$k]['option'] = Dever::url('area/api.get');
  170. //$config['struct'][$k]['list'] = 'Dever::load("area/api.string", "{area}")';
  171. break;
  172. case 10:
  173. $config['struct'][$k]['update'] = 'radio';
  174. $config['struct'][$k]['type'] = 'varchar-11';
  175. $config['struct'][$k]['match'] = 'is_numeric';
  176. $config['struct'][$k]['option'] = $option;
  177. $config['struct'][$k]['search'] = 'select';
  178. $config['struct'][$k]['default'] = '';
  179. break;
  180. case 11:
  181. $config['struct'][$k]['update'] = 'checkbox';
  182. $config['struct'][$k]['type'] = 'varchar-150';
  183. $config['struct'][$k]['match'] = 'is_string';
  184. $config['struct'][$k]['option'] = $option;
  185. $config['struct'][$k]['search'] = 'select';
  186. $config['struct'][$k]['default'] = '';
  187. break;
  188. case 12:
  189. $config['struct'][$k]['update'] = 'select';
  190. $config['struct'][$k]['type'] = 'varchar-11';
  191. $config['struct'][$k]['match'] = 'is_numeric';
  192. $config['struct'][$k]['option'] = $option;
  193. $config['struct'][$k]['search'] = 'select';
  194. $config['struct'][$k]['default'] = '';
  195. break;
  196. default:
  197. $config['struct'][$k]['update'] = 'text';
  198. $config['struct'][$k]['search'] = 'fulltext';
  199. if ($v['type'] == 9) {
  200. $config['struct'][$k]['name'] .= '-这是一个区间数值,后台填写请用半角逗号,隔开';
  201. $config['struct'][$k]['type'] = 'varchar-100';
  202. $config['struct'][$k]['match'] = 'is_string';
  203. } elseif ($v['data_type'] == 1 || $v['type'] == 1) {
  204. $config['struct'][$k]['type'] = 'varchar-11';
  205. $config['struct'][$k]['match'] = 'is_numeric';
  206. } else {
  207. $config['struct'][$k]['type'] = 'varchar-800';
  208. $config['struct'][$k]['match'] = 'is_string';
  209. }
  210. }
  211. if ($v['is_must'] && $v['is_must'] == 2) {
  212. $config['struct'][$k]['match'] = 'option';
  213. }
  214. if ($v['value']) {
  215. //$config['struct'][$k]['default'] = $v['value'];
  216. }
  217. if ($v['state'] == 2) {
  218. unset($config['struct'][$k]['update']);
  219. }
  220. # 还要根据搜索来建立索引,另外,如果数据结构改了怎么办。暂时用varchar
  221. }
  222. }
  223. }
  224. /**
  225. * 根据属性id,获取属性列表
  226. *
  227. * @return mixed
  228. */
  229. public function getList($ids, $value, $order = 'list')
  230. {
  231. $result = array();
  232. if ($ids) {
  233. $where['ids'] = $ids;
  234. $method = 'getAllByIds';
  235. if ($order == 'list') {
  236. $method = 'getListByIds';
  237. } elseif ($order == 'search') {
  238. $method = 'getSearchByIds';
  239. } elseif ($order == 'search_reorder') {
  240. $method = 'getSearchByIds';
  241. $where['search_reorder'] = 1;
  242. } elseif ($order == 'view') {
  243. $method = 'getViewByIds';
  244. }
  245. $data = Dever::db('attr/info')->$method($where);
  246. $ids = explode(',', $ids);
  247. $value = explode(',', $value);
  248. if ($data) {
  249. foreach ($ids as $k => $v) {
  250. if (isset($data[$v])) {
  251. if (isset($value) && $value[$k]) {
  252. $data[$v]['value'] = $value[$k];
  253. } else {
  254. $data[$v]['value'] = '';
  255. }
  256. }
  257. }
  258. }
  259. }
  260. return $data;
  261. }
  262. /**
  263. * 获取属性的值
  264. *
  265. * @return mixed
  266. */
  267. public function getValue($info)
  268. {
  269. # 地区
  270. if ($info['type'] == 7) {
  271. $info['value'] = Dever::load("area/api.string", $info['value']);
  272. /*
  273. $info['value'] = explode(',', $info['value']);
  274. $temp = end($info['value']);
  275. $info['value'] = $temp;
  276. */
  277. } elseif ($info['type'] == 9 && $info['value']) {
  278. $info['value'] = explode(',', $info['value']);
  279. $info['value'] = $info['value'][0] . $info['unit'];
  280. } elseif ($info['type'] > 9 && $info['value']) {
  281. $value = Dever::db('attr/value')->getData(array('ids' => $info['value']));
  282. if ($value) {
  283. $name = array();
  284. foreach ($value as $k => $v) {
  285. $name[] = $v['name'] . $info['unit'];
  286. }
  287. $info['value'] = implode(',', $name);
  288. }
  289. } elseif ($info['unit']) {
  290. $info['value'] = $info['value'] . $info['unit'];
  291. }
  292. return $info['value'];
  293. }
  294. /**
  295. * 获取属性的名称,字符类型
  296. *
  297. * @return mixed
  298. */
  299. public function string($ids, $value = '')
  300. {
  301. $result = $this->getInfo($ids, $value);
  302. $table = array();
  303. foreach ($result as $k => $v) {
  304. $table[$v['name']] = '';
  305. foreach ($v['attr'] as $k1 => $v1) {
  306. $table[$v['name']] .= $v1['name'] . "&nbsp;&nbsp;";
  307. }
  308. }
  309. return Dever::table($table);
  310. }
  311. private function attrSort(&$attr, $reorder = 'list_reorder')
  312. {
  313. if ($reorder == 'list') {
  314. $reorder = 'list_reorder';
  315. }
  316. $order = Dever::config('base')->attrOrder;
  317. if ($order && $order != $reorder) {
  318. $reorder = $order;
  319. }
  320. if ($attr) {
  321. foreach ($attr as $k => $v) {
  322. $sort[$k] = $v[$reorder];
  323. $attr[$k]['option'] = Dever::db('attr/value')->getData(array('info_id' => $v['id']));
  324. /*
  325. if ($v['type_option']) {
  326. $attr[$k]['option'] = explode("\n", $v['type_option']);
  327. }
  328. */
  329. }
  330. array_multisort($sort, SORT_DESC, $attr);
  331. }
  332. }
  333. public function getAttrByCate($category, $order = 'list_reorder', $total = false)
  334. {
  335. $data = array();
  336. //print_r(Dever::db('category/attr')->all());
  337. if ($category && Dever::project('category')) {
  338. $array = explode(',', $category);
  339. $cate = array();
  340. $total = $total ? $total : count($array);
  341. $total -= 1;
  342. foreach ($array as $k => $v) {
  343. $cate[$k] = $v;
  344. if ($k < $total) {
  345. $cate[] = '-1';
  346. }
  347. $where['category'] = $cate;
  348. $info = Dever::db('category/attr')->one($where);
  349. if ($info) {
  350. $data += Dever::load('attr/api')->getList($info['attr'], $info['attr_input'], $order);
  351. }
  352. }
  353. $this->attrSort($data, $order);
  354. }
  355. return $data;
  356. }
  357. /**
  358. * 获取某个分类下的搜索列表
  359. *
  360. * @return mixed
  361. */
  362. public function getSearch($ids, $cate = true, $city = false, $search_value = false, $total = 3)
  363. {
  364. $value = array();
  365. $search_value = Dever::preInput('attr_', array(), $search_value);
  366. if ($cate && $ids && Dever::project('category')) {
  367. $data = $this->getAttrByCate($ids, 'search_reorder', $total);
  368. if (!$data) {
  369. return array('search' => array(), 'value' => $value);
  370. }
  371. } else {
  372. $where['ids'] = $ids;
  373. $where['search_reorder'] = 1;
  374. $data = Dever::db('attr/info')->getSearchByIds($where);
  375. }
  376. if ($data) {
  377. foreach ($data as $k => $v) {
  378. if (isset($ids_input[$v['id']]) && $ids_input[$v['id']]) {
  379. $data[$k]['name'] = $ids_input[$v['id']];
  380. }
  381. $data[$k]['key'] = 'attr_' . $v['id'];
  382. if ($v['type'] == 7 && $city > 0) {
  383. # 获取地区,获取最后一个级别的地区
  384. $data[$k]['option'] = Dever::db('area/county')->state(array('city_id' => $city));
  385. } elseif ($v['type'] == 1 || $v['type'] == 9) {
  386. $data[$k]['option'] = Dever::db('attr/search')->getData(array('info_id' => $v['id']));
  387. } else {
  388. $data[$k]['option'] = Dever::db('attr/value')->getData(array('info_id' => $v['id']));
  389. }
  390. /*
  391. elseif ($v['type_option']) {
  392. $temp = explode("\n", $v['type_option']);
  393. foreach ($temp as $k1 => $v1) {
  394. $v1 = explode(',', $v1);
  395. $k1 = $k1+1;
  396. $data[$k]['option'][$k1] = array
  397. (
  398. 'id' => $k1,
  399. 'name' => $v1[0],
  400. );
  401. if (isset($v1[1])) {
  402. $data[$k]['option_match'][$k1] = $v1[1];
  403. }
  404. }
  405. }
  406. */
  407. if (isset($search_value[$data[$k]['key']])) {
  408. $data[$k]['option_value'] = $search_value[$data[$k]['key']];
  409. $value[$data[$k]['key']] = $data[$k]['option_value'];
  410. }
  411. }
  412. }
  413. return array('search' => $data, 'value' => $value);
  414. }
  415. /**
  416. * 获取搜索sql
  417. *
  418. * @return mixed
  419. */
  420. public function getSql($attr, $where, $sql = array(), $tname = 't_1')
  421. {
  422. if (!$attr) {
  423. return $sql;
  424. }
  425. foreach ($attr as $k => $v) {
  426. $key = 'attr_' . $v['id'];
  427. if (isset($where[$key]) && $where[$key]) {
  428. //$where['option'][$key] = array('yes', '=');
  429. if ($v['type'] == 7) {
  430. # 地区有点复杂,暂时先这样,后续优化一下
  431. $county = Dever::db('area/county')->one($where[$key]);
  432. if ($county) {
  433. $city = Dever::db('area/city')->one($county['city_id']);
  434. if ($city) {
  435. $province = Dever::db('area/province')->one($city['province_id']);
  436. if ($province) {
  437. $value = $province['id'] . ',' . $city['id'] . ',' . $county['id'];
  438. //$where[$key] = $value;
  439. $sql[] = ' '.$tname.'.'.$key.' = "'.$value.'"';
  440. }
  441. }
  442. }
  443. //} elseif ($v['type'] == 1 && $v['type_option']) {
  444. } elseif ($v['type'] == 1) {
  445. $search = Dever::db('attr/search')->getData(array('info_id' => $v['id']));
  446. //print_r($where[$key]);die;
  447. /*
  448. $temp = explode("\n", $v['type_option']);
  449. if (isset($temp[$where[$key]-1])) {
  450. $temp = explode(',', $temp[$where[$key]-1]);
  451. $match = $temp[1];
  452. //$where['option'][$key] = array('yes', '<=');
  453. //$where[$key] = 100;
  454. $match = str_replace('{v}', $tname . '.' . $key, $match);
  455. $sql[] = $match;
  456. }
  457. */
  458. //} elseif ($v['type'] == 9 && $v['type_option']) {
  459. } elseif ($v['type'] == 9 && $v['type_option']) {
  460. $search = Dever::db('attr/search')->getData(array('info_id' => $v['id']));
  461. //print_r($where[$key]);die;
  462. /*
  463. $temp = explode("\n", $v['type_option']);
  464. if (isset($temp[$where[$key]-1])) {
  465. $temp = explode(',', $temp[$where[$key]-1]);
  466. $match = $temp[1];
  467. //$where['option'][$key] = array('yes', '<=');
  468. //$where[$key] = 100;
  469. $match = str_replace('{s}', $tname . '.' . $key . '_s', $match);
  470. $match = str_replace('{e}', $tname . '.' . $key . '_e', $match);
  471. $match = str_replace('{v}', $tname . '.' . $key . '_e', $match);
  472. $sql[] = $match;
  473. }
  474. */
  475. } else {
  476. $sql[] = ' '.$tname.'.'.$key.' = "'.$where[$key].'"';
  477. }
  478. }
  479. }
  480. return $sql;
  481. }
  482. }