Info.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  1. <?php
  2. # 核心类,数据获取类
  3. namespace Goods\Lib;
  4. use Dever;
  5. class Info
  6. {
  7. private $table_info = 'goods/info';
  8. private $table_attr = 'goods/info_attr';
  9. private $table_sku = 'goods/info_sku';
  10. /**
  11. * instance
  12. *
  13. * @var string
  14. */
  15. protected static $instance;
  16. /**
  17. * init
  18. *
  19. * @return mixed
  20. */
  21. public static function init($top_category = false)
  22. {
  23. if (empty(self::$instance[$top_category])) {
  24. self::$instance[$top_category] = new self($top_category);
  25. }
  26. return self::$instance[$top_category];
  27. }
  28. public function __construct($top_category = false)
  29. {
  30. $this->top_category = $top_category;
  31. # 必有有分类才能进入到需求中
  32. if (!$this->top_category) {
  33. $this->top_category = -1;
  34. //Dever::alert('错误的分类信息');
  35. }
  36. }
  37. private function setAttr($cate, $order = 'list_reorder')
  38. {
  39. if ($order != 'list_reorder') {
  40. Dever::config('base')->attrOrder = $order;
  41. }
  42. Dever::setInput('update_category', $cate);
  43. $attr = Dever::db($this->table_attr, array(), $cate);
  44. return $attr;
  45. }
  46. public function getAttr($cate)
  47. {
  48. return Dever::load('attr/api')->getAttrByCate($cate, 'list');
  49. }
  50. # 根据分类获取需求列表数据
  51. public function getData($cate = false, $limit = '0,10', $page = false, $attr = true)
  52. {
  53. if (!$limit) {
  54. $limit = '0,10';
  55. }
  56. $where = $data = array();
  57. if (!$cate) {
  58. if ($this->top_category > 0) {
  59. $where['top_category_id'] = $this->top_category;
  60. }
  61. } elseif (strstr($cate, ',')) {
  62. $where['category'] = $this->top_category . ',' . $cate;
  63. } else {
  64. $where['second_category_id'] = $cate;
  65. }
  66. if ($page) {
  67. $method = 'getPageAll';
  68. } else {
  69. $where['limit'] = $limit;
  70. $method = 'getAll';
  71. }
  72. $name = Dever::input('name');
  73. if ($name) {
  74. $where['name'] = $name;
  75. }
  76. $data = Dever::db($this->table_info)->$method($where);
  77. if ($data && $attr) {
  78. foreach ($data as $k => $v) {
  79. $data[$k] = $this->info($v, 'list_reorder');
  80. }
  81. }
  82. return $data;
  83. }
  84. # 根据属性获取需求列表数据 这里带有检索属性的功能 此处还需优化
  85. public function getDataByAttr($cate = false, $where = array())
  86. {
  87. if (!$cate) {
  88. Dever::alert('错误的分类信息');
  89. }
  90. $where_sql = array();
  91. $name = Dever::input('name');
  92. if ($name) {
  93. //$where['name'] = $name;
  94. $where_sql[] = ' t_2.name = "'.$name.'"';
  95. }
  96. $attr = $this->setAttr($this->top_category . ',' . $cate);
  97. //print_r($attr->config['attr']);
  98. $where_sql = Dever::load('attr/api')->getSql($attr->config['attr'], $where, $where_sql);
  99. if (!$where_sql) {
  100. return array();
  101. }
  102. # 这里用pdo的数据绑定,字符串字段的区间查询就是有问题。。。只能用sql了
  103. //$data = $attr->getPageAll($where);
  104. $page['template'] = 'list';
  105. $page['num'] = 10;
  106. $where_sql = implode(' and ', $where_sql);
  107. $sql = 'SELECT t_2.* FROM `{project}_goods_'.$attr->config['name'].'` as t_1 Left Join `{project}_goods_info` AS t_2 ON t_1.`info_id`=t_2.`id` WHERE '.$where_sql.' order by t_2.`reorder` desc,t_2.`id` desc';
  108. $data = $attr->fetchAll($sql, array(), $page);
  109. if ($data) {
  110. foreach ($data as $k => $v) {
  111. $data[$k] = $this->info($v, 'list_reorder');
  112. }
  113. }
  114. return $data;
  115. }
  116. # 获取列表页的搜索条件
  117. public function getSearch($cate = false)
  118. {
  119. # 获取搜索条件
  120. if (!$cate) {
  121. $search_cate = $cate = $this->top_category;
  122. } else {
  123. $search_cate = $cate;
  124. }
  125. # 1是当前用户所在的城市,暂时写死
  126. # 获取搜索条件
  127. $search = Dever::load('attr/api')->getSearch($search_cate, true, 1, false, 2);
  128. # 下级分类
  129. $search['cate'] = Dever::load('category/api')->getList($cate);
  130. # 上级分类
  131. $search['parent_cate'] = Dever::load('category/api')->getList($this->top_category);
  132. return $search;
  133. }
  134. # 获取支付信息
  135. public function getPayState($uid, $id, $category)
  136. {
  137. $pay_where['category'] = $category;
  138. $pay = Dever::db('goods/pay')->one($pay_where);
  139. $pay_state = 0;
  140. if ($pay && $pay['num'] > 0) {
  141. $pay_state = $pay['num'];
  142. } elseif (strstr($category, ',')) {
  143. $category = explode(',', $category);
  144. $category = array_pop($category);
  145. $pay_state = $this->getPayState($uid, $id, $category);
  146. }
  147. //$update['where_id'] = $id;
  148. //$update['poster_pay_type'] = $pay_state;
  149. //Dever::db($this->table_info)->update($update);
  150. return $pay_state;
  151. }
  152. # 更新需求
  153. public function update($top, $cate, $data, $id, $uid)
  154. {
  155. if ($cate) {
  156. $category = $top . ',' . $cate;
  157. } else {
  158. $category = $top;
  159. }
  160. $attr = $this->setAttr($category);
  161. $attr_update = Dever::preInput('attr_');
  162. if ($attr_update) {
  163. foreach ($attr_update as $k => $v) {
  164. if (strstr($k, '_s')) {
  165. $k = str_replace('_s', '', $k);
  166. $attr_update[$k] = $v;
  167. }
  168. if (strstr($k, '_e')) {
  169. $k = str_replace('_e', '', $k);
  170. $attr_update[$k] .= ',' . $v;
  171. }
  172. }
  173. }
  174. $update = array();
  175. $update['name'] = $data['name'];
  176. $update['category'] = $category;
  177. $update['pic'] = $data['pic'];
  178. //$update['pic_cover'] = $data['pic'];
  179. $update['poster_uid'] = $uid;
  180. if (isset($data['view_price']) && $data['view_price']) {
  181. $update['view_price'] = $data['view_price'];
  182. }
  183. if ($uid && $id) {
  184. # 此处要判断是用户自己的
  185. $info = Dever::db($this->table_info)->one($id);
  186. /*
  187. if ($uid == $info['poster_uid']) {
  188. Dever::alert('错误的用户参数');
  189. }
  190. */
  191. $update['where_id'] = $attr_update['where_id'] = $id;
  192. Dever::db($this->table_info)->update($update);
  193. Dever::db($this->table_attr)->update($attr_update);
  194. $pay = 0;
  195. } else {
  196. $id = Dever::db($this->table_info)->insert($update);
  197. $attr_update['category'] = $update['category'];
  198. $attr_update['info_id'] = $id;
  199. $attr_update['id'] = $id;
  200. Dever::db($this->table_attr)->insert($attr_update);
  201. # 验证是否需要支付,根据category判断
  202. $pay = false;
  203. //$pay = $this->getPayState($uid, $id, $category);
  204. $info = Dever::db($this->table_info)->one($id);
  205. $info = $this->getInfoLink($info);
  206. }
  207. return array('id' => $id, 'pay' => $pay, 'cate' => $category, 'top' => $top, 'info' => $info);
  208. }
  209. # 获取支付所需要的信息
  210. public function getPayInfo($id, $sku = 0, $type = 1)
  211. {
  212. $info = is_array($id) ? $id : Dever::db($this->table_info)->find($id);
  213. if ($info) {
  214. if ($type == 2) {
  215. $info['price'] = $info['f_price'];
  216. unset($info['s_price']);
  217. } else {
  218. unset($info['f_price']);
  219. }
  220. $sku = $sku > 0 ? $sku : $info['sku_id'];
  221. if ($sku > 0) {
  222. $where['info_id'] = $info['id'];
  223. $where['id'] = $sku;
  224. $where['type'] = 2;
  225. $sku = Dever::db('goods/info_sku')->find($where);
  226. if ($sku) {
  227. $info['sku_id'] = $sku['id'];
  228. if ($type == 2) {
  229. $info['price'] = $sku['f_price'];
  230. } else {
  231. $info['price'] = $sku['price'];
  232. $info['s_price'] = $sku['s_price'];
  233. }
  234. //$info['num'] = $sku['num'];
  235. //$info['attr_json'] = $sku['attr'];
  236. $info['attr'] = Dever::json_decode($sku['attr']);
  237. if ($info['attr']) {
  238. $info['attr'] = Dever::load('attr/api')->getInfoByJson($info['attr']);
  239. $info['sku_name'] = $info['attr']['string'];
  240. }
  241. }
  242. }
  243. /*
  244. $info['freight_id'] = 0;
  245. $info['freight_price'] = 0;
  246. $freight = $this->freight($info, $info['price'], $user, $num);
  247. if ($freight) {
  248. $info['freight_id'] = $freight['id'];
  249. $info['freight_price'] = $freight['price'];
  250. }
  251. */
  252. # 佣金计算
  253. //$info['reward'] = $this->reward($info, $info['price']*$num);
  254. }
  255. return $info;
  256. }
  257. # 获取自提信息
  258. public function storeInfo($goods_id, $area_id = '')
  259. {
  260. $info = Dever::db($this->table_info)->find($goods_id);
  261. if (!$info) {
  262. return array();
  263. }
  264. $array = explode(',', $info['category']);
  265. $cate = $store = array();
  266. $total = count($array);
  267. $total -= 1;
  268. # 用户所在地理位置
  269. if ($area_id) {
  270. $where['area'] = $area_id;
  271. }
  272. foreach ($array as $k => $v) {
  273. $cate[$k] = $v;
  274. if ($k < $total) {
  275. $cate[] = '-1';
  276. }
  277. $where['category'] = $cate;
  278. $data = Dever::db('goods/store')->state($where);
  279. if ($data) {
  280. $store += $data;
  281. }
  282. }
  283. return $store;
  284. }
  285. # 获取运费信息
  286. public function getFreight($id, $goods_price, $user, $num)
  287. {
  288. $info = Dever::db($this->table_info)->find($id);
  289. return $this->freight($info, $goods_price, $user, $num);
  290. }
  291. # 根据已经选择的sku获取商品信息
  292. public function getInfoBySku($id, $sku_id)
  293. {
  294. $info = Dever::db($this->table_info)->find($id);
  295. if ($info && $sku_id > 0) {
  296. $sku = Dever::db($this->table_sku)->find($sku_id);
  297. if ($sku && $sku['attr']) {
  298. $sku['attr'] = Dever::json_decode($sku['attr']);
  299. $info['sku'] = Dever::load('attr/api')->getInfoByJson($sku['attr']);
  300. }
  301. }
  302. return $info;
  303. }
  304. # 获取基本信息
  305. public function getInfo($info, $attr = true, $other = false, $order = 'view_reorder', $user = array(), $reward = false)
  306. {
  307. if (!is_array($info)) {
  308. $info = Dever::db($this->table_info)->find($info);
  309. }
  310. if ($info && $attr) {
  311. $info = $this->info($info, $order, $other, false, $user, $reward);
  312. } else {
  313. $this->getGroup($info);
  314. }
  315. if (!isset($info['goods_id'])) {
  316. $info['goods_id'] = $info['id'];
  317. }
  318. if ($info['pic']) {
  319. $info['pic'] = explode(',', $info['pic']);
  320. }
  321. if ($info['tag_id']) {
  322. $info['tag'] = Dever::db('goods/tag')->getAllByIds($info['tag_id']);
  323. }
  324. return $info;
  325. }
  326. # 获取组合商品
  327. public function getGroup(&$info)
  328. {
  329. if ($info && $info['price_type'] > 2 && $info['goods'] && is_string($info['goods'])) {
  330. # 获取套餐或者组合的商品
  331. $goods = Dever::array_decode($info['goods']);
  332. $goods_info = array();
  333. foreach ($goods as $k => $v) {
  334. $ginfo = Dever::db('goods/info')->one($v['goods_id']);
  335. if ($ginfo) {
  336. $goods_info[] = array
  337. (
  338. 'id' => $ginfo['id'],
  339. 'name' => $ginfo['name'],
  340. 'price' => $ginfo['price'],
  341. 'cover' => $ginfo['cover'],
  342. 'num' => $v['num'],
  343. );
  344. }
  345. }
  346. $info['goods'] = $goods_info;
  347. }
  348. }
  349. # 获取基本信息
  350. public function info($info, $key = 'view_reorder', $other = false, $is_sell = false, $user = array(), $reward = false)
  351. {
  352. $info['category_array'] = Dever::load('category/api')->string($info['category']);
  353. if ($info['price_type'] == 2) {
  354. $attr = $this->setAttr($info['category'], $key);
  355. $attr_data = $attr->one($info['id']);
  356. $info['attr'] = $info['sell_attr'] = $info['option_attr'] = array();
  357. if ($attr->config['attr']) {
  358. foreach ($attr->config['attr'] as $k => $v) {
  359. if (isset($v[$key]) && $v[$key] > 0) {
  360. if (isset($attr_data['attr_' . $v['id']])) {
  361. $v['value'] = $attr_data['attr_' . $v['id']];
  362. } else {
  363. continue;
  364. $v['value'] = '';
  365. }
  366. $v['value_string'] = Dever::load('attr/api')->getValue($v);
  367. if ($v['ename'] == 'price') {
  368. $info['price_array'] = $v['value_string'];
  369. } else {
  370. $v['bg'] = 'background-image: url('.$v['icon'].')';
  371. $v['pic'] = '<img src="'.$v['icon'].'" width="18" />';
  372. if ($is_sell > 0) {
  373. if ($is_sell == $v['is_sell']) {
  374. $info['attr'][] = $v;
  375. }
  376. } else {
  377. $info['attr'][] = $v;
  378. # 设置sku
  379. $this->sku($v, $info);
  380. }
  381. }
  382. }
  383. }
  384. # 获取sku的价格
  385. $this->getPrice($info, $other);
  386. }
  387. if (!$info['attr']) {
  388. $info['price_type'] = 1;
  389. }
  390. }
  391. if (isset($info['cdate']) && $info['cdate']) {
  392. $info['cdate_string'] = Dever::mdate($info['cdate'], 2);
  393. }
  394. $info = $this->getInfoLink($info);
  395. $this->getGroup($info);
  396. //$info['freight'] = 0;
  397. unset($info['f_price']);
  398. unset($info['cdate']);
  399. unset($info['udate']);
  400. unset($info['category']);
  401. unset($info['top_category_id']);
  402. unset($info['second_category_id']);
  403. unset($info['category_id']);
  404. unset($info['area']);
  405. unset($info['mode']);
  406. unset($info['top']);
  407. unset($info['youhui']);
  408. unset($info['status']);
  409. unset($info['reorder']);
  410. unset($info['state']);
  411. return $info;
  412. }
  413. # 佣金计算
  414. private function reward($info, $price)
  415. {
  416. if ($info['reward_value'] && $info['reward_value'] > 0) {
  417. if ($info['reward_type'] == 2) {
  418. $info['reward_value'] = round(($info['reward_value']/100) * $price, 2);
  419. }
  420. return $info['reward_value'];
  421. }
  422. $array = explode(',', $info['category']);
  423. $cate = $reward = array();
  424. $total = count($array);
  425. $total -= 1;
  426. foreach ($array as $k => $v) {
  427. $cate[$k] = $v;
  428. if ($k < $total) {
  429. $cate[] = '-1';
  430. }
  431. $where['category'] = $cate;
  432. $data = Dever::db('goods/reward')->one($where);
  433. if ($data) {
  434. $reward = $data;
  435. }
  436. }
  437. if ($reward) {
  438. if ($reward['value'] && $reward['value'] > 0) {
  439. if ($reward['type'] == 2) {
  440. $reward['value'] = round(($reward['value']/100) * $price, 2);
  441. }
  442. return $reward['value'];
  443. }
  444. }
  445. return 0;
  446. }
  447. # 运费计算,暂时用省份代表地区
  448. private function freight($info, $goods_price, $user = array(), $num = 1)
  449. {
  450. $array = explode(',', $info['category']);
  451. $cate = $freight = array();
  452. $total = count($array);
  453. $total -= 1;
  454. if ($info && $info['goods_area']) {
  455. $where['goods_area'] = $info['goods_area'];
  456. }
  457. # 用户所在地理位置
  458. if ($user && isset($user['area_id']) && $user['area_id']) {
  459. $area = explode(',', $user['area_id']);
  460. $where['area'] = $area[0];
  461. }
  462. foreach ($array as $k => $v) {
  463. $cate[$k] = $v;
  464. if ($k < $total) {
  465. $cate[] = '-1';
  466. }
  467. $where['category'] = $cate;
  468. $data = Dever::db('goods/freight')->one($where);
  469. if ($data) {
  470. $freight = $data;
  471. }
  472. }
  473. if ($freight) {
  474. $price = $freight['first_price'];
  475. if ($num > $freight['first_num']) {
  476. $num = $num - $freight['first_num'];
  477. $price = $freight['first_price'] + ($freight['next_price']*$num);
  478. }
  479. if ($freight['type'] == 2) {
  480. $freight['price'] = round($goods_price*$num*($price/100), 2);
  481. } else {
  482. $freight['price'] = $price;
  483. }
  484. }
  485. return $freight;
  486. }
  487. private function sku($data, &$info)
  488. {
  489. # 获取销售属性
  490. if ($data['is_sell'] > 1) {
  491. if ($data['option']) {
  492. $value = explode(',', $data['value']);
  493. foreach($data['option'] as $k => $v) {
  494. if (in_array($v['id'], $value)) {
  495. $key = $v['info_id'] . '-' . $v['id'];
  496. $info['option_sku'][$key] = $data['option_sku'][] = array
  497. (
  498. 'id' => $v['id'],
  499. 'info_id' => $v['info_id'],
  500. 'name' => $v['name'],
  501. 'parent_name' => $data['name'],
  502. 'icon' => $v['icon'],
  503. 'value' => $key,
  504. );
  505. }
  506. }
  507. unset($data['option']);
  508. }
  509. if ($data['is_sell'] == 2) {
  510. $info['sell_attr'][] = $data;
  511. } elseif ($data['is_sell'] == 3) {
  512. $info['option_attr'][] = $data;
  513. }
  514. }
  515. }
  516. private function getPrice(&$info, $other = false)
  517. {
  518. # 获取价格
  519. $where['info_id'] = $info['id'];
  520. $info['price_array'] = array();
  521. $list = Dever::db('goods/info_sku')->getData($where);
  522. $info['price_array']['sku'] = $info['price_array']['option'] = $info['price_array']['main'] = array();
  523. $min = $max = array();
  524. if ($list) {
  525. foreach ($list as $k => $v) {
  526. if ($v['key']) {
  527. if ($v['key'] == -1) {
  528. $info['price_array']['main'] = $v;
  529. } elseif ($v['type'] == 2) {
  530. $v['name'] = array();
  531. $temp = explode('_', $v['key']);
  532. foreach ($temp as $k1 => $v1) {
  533. if (isset($info['option_sku'][$v1])) {
  534. $v['name'][] = $info['option_sku'][$v1]['name'];
  535. }
  536. }
  537. $v['name'] = implode(';', $v['name']);
  538. if ($other) {
  539. foreach ($other[1] as $k1 => $v1) {
  540. $v[$v1] = isset($other[0][$v['id']][$v1]) ? $other[0][$v['id']][$v1] : 0;
  541. }
  542. }
  543. $info['price_array']['sku'][$v['key']] = $v;
  544. if (!$min) {
  545. $min = $v;
  546. }
  547. if (!$max) {
  548. $max = $v;
  549. }
  550. if ($min['price'] > $v['price']) {
  551. $min = $v;
  552. }
  553. if ($max['price'] < $v['price']) {
  554. $max = $v;
  555. }
  556. } elseif ($v['type'] == 3) {
  557. $info['price_array']['option'][$v['key']] = $v;
  558. }
  559. }
  560. }
  561. }
  562. $info['price_array']['sku_min'] = $min;
  563. $info['price_array']['sku_max'] = $max;
  564. if ($min) {
  565. $info['price'] = $min['price'];
  566. $info['s_price'] = $min['s_price'];
  567. $info['sku_id'] = $min['id'];
  568. }
  569. unset($info['option_sku']);
  570. }
  571. public function getInfoLink($info)
  572. {
  573. if (isset($this->view_path)) {
  574. $info['view_path'] = $this->view_path . '?top=' . $info['top_category_id'] . '&id=' . $info['id'];
  575. $info['view_link'] = Dever::url($info['view_path']);
  576. }
  577. return $info;
  578. }
  579. public function setViewPath($path)
  580. {
  581. $this->view_path = $path;
  582. }
  583. # 根据分类获取商品
  584. public function getGoods_api()
  585. {
  586. $where['category_id'] = Dever::input('cate_id');
  587. $factory_id = Dever::input('id');
  588. $data = Dever::db('goods/info_category')->getData($where);
  589. return $data;
  590. }
  591. }