Info.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  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. # 根据商品条形码获取商品
  38. public function getGoodsByCode($type_id = false, $type = 1, $check = true)
  39. {
  40. $code = Dever::input('code');
  41. if (!$code) {
  42. Dever::alert('错误的条形码');
  43. }
  44. $where['code'] = $code;
  45. $sku = Dever::db('goods/info_sku')->find($where);
  46. if (!$sku) {
  47. Dever::alert('商品不存在');
  48. }
  49. $goods = Dever::db('goods/info')->one($sku['info_id']);
  50. if (!$goods) {
  51. Dever::alert('商品不存在');
  52. }
  53. if ($goods['status'] != 1) {
  54. Dever::alert('商品已下架');
  55. }
  56. if ($type_id > 0) {
  57. if ($type == 1) {
  58. $table = 'shop/goods_sku';
  59. $col = 'shop_id';
  60. $name = '门店';
  61. } elseif ($type == 2) {
  62. $table = 'store/goods_sku';
  63. $col = 'store_id';
  64. $name = '仓库';
  65. } elseif ($type == 3) {
  66. $table = 'factory/goods_sku';
  67. $col = 'factory_id';
  68. $name = '工厂';
  69. }
  70. $where = array();
  71. $where[$col] = $type_id;
  72. $where['goods_id'] = $goods['id'];
  73. $where['sku_id'] = $sku['id'];
  74. $data = Dever::db($table)->getOne($where);
  75. if (!$data) {
  76. if ($sku['key'] == -1) {
  77. $where['sku_id'] = -1;
  78. $data = Dever::db($table)->getOne($where);
  79. if (!$data) {
  80. Dever::alert($name . '商品不存在');
  81. }
  82. } else {
  83. Dever::alert($name . '商品不存在');
  84. }
  85. }
  86. if ($check && $data['total'] <= 0) {
  87. # 验证库存
  88. Dever::alert($name . '商品库存不足');
  89. }
  90. }
  91. if ($sku['key'] == -1) {
  92. $sku['id'] = -1;
  93. }
  94. $goods['sku'] = $sku;
  95. return $goods;
  96. }
  97. private function setAttr($cate, $order = 'list_reorder')
  98. {
  99. if ($order != 'list_reorder') {
  100. Dever::config('base')->attrOrder = $order;
  101. }
  102. Dever::setInput('update_category', $cate);
  103. $attr = Dever::db($this->table_attr, array(), $cate);
  104. return $attr;
  105. }
  106. public function getAttr($cate)
  107. {
  108. return Dever::load('attr/api')->getAttrByCate($cate, 'list');
  109. }
  110. # 根据分类获取需求列表数据
  111. public function getData($cate = false, $limit = '0,10', $page = false, $attr = true)
  112. {
  113. if (!$limit) {
  114. $limit = '0,10';
  115. }
  116. $where = $data = array();
  117. if (!$cate) {
  118. if ($this->top_category > 0) {
  119. $where['top_category_id'] = $this->top_category;
  120. }
  121. } elseif (strstr($cate, ',')) {
  122. $where['category'] = $this->top_category . ',' . $cate;
  123. } else {
  124. $where['second_category_id'] = $cate;
  125. }
  126. if ($page) {
  127. $method = 'getPageAll';
  128. } else {
  129. $where['limit'] = $limit;
  130. $method = 'getAll';
  131. }
  132. $name = Dever::input('name');
  133. if ($name) {
  134. $where['name'] = $name;
  135. }
  136. $data = Dever::db($this->table_info)->$method($where);
  137. if ($data && $attr) {
  138. foreach ($data as $k => $v) {
  139. $data[$k] = $this->info($v, 'list_reorder');
  140. }
  141. }
  142. return $data;
  143. }
  144. # 根据属性获取需求列表数据 这里带有检索属性的功能 此处还需优化
  145. public function getDataByAttr($cate = false, $where = array())
  146. {
  147. if (!$cate) {
  148. Dever::alert('错误的分类信息');
  149. }
  150. $where_sql = array();
  151. $name = Dever::input('name');
  152. if ($name) {
  153. //$where['name'] = $name;
  154. $where_sql[] = ' t_2.name = "'.$name.'"';
  155. }
  156. $attr = $this->setAttr($this->top_category . ',' . $cate);
  157. //print_r($attr->config['attr']);
  158. $where_sql = Dever::load('attr/api')->getSql($attr->config['attr'], $where, $where_sql);
  159. if (!$where_sql) {
  160. return array();
  161. }
  162. # 这里用pdo的数据绑定,字符串字段的区间查询就是有问题。。。只能用sql了
  163. //$data = $attr->getPageAll($where);
  164. $page['template'] = 'list';
  165. $page['num'] = 10;
  166. $where_sql = implode(' and ', $where_sql);
  167. $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';
  168. $data = $attr->fetchAll($sql, array(), $page);
  169. if ($data) {
  170. foreach ($data as $k => $v) {
  171. $data[$k] = $this->info($v, 'list_reorder');
  172. }
  173. }
  174. return $data;
  175. }
  176. # 获取列表页的搜索条件
  177. public function getSearch($cate = false)
  178. {
  179. # 获取搜索条件
  180. if (!$cate) {
  181. $search_cate = $cate = $this->top_category;
  182. } else {
  183. $search_cate = $cate;
  184. }
  185. # 1是当前用户所在的城市,暂时写死
  186. # 获取搜索条件
  187. $search = Dever::load('attr/api')->getSearch($search_cate, true, 1, false, 2);
  188. # 下级分类
  189. $search['cate'] = Dever::load('category/api')->getList($cate);
  190. # 上级分类
  191. $search['parent_cate'] = Dever::load('category/api')->getList($this->top_category);
  192. return $search;
  193. }
  194. # 获取支付信息
  195. public function getPayState($uid, $id, $category)
  196. {
  197. $pay_where['category'] = $category;
  198. $pay = Dever::db('goods/pay')->one($pay_where);
  199. $pay_state = 0;
  200. if ($pay && $pay['num'] > 0) {
  201. $pay_state = $pay['num'];
  202. } elseif (strstr($category, ',')) {
  203. $category = explode(',', $category);
  204. $category = array_pop($category);
  205. $pay_state = $this->getPayState($uid, $id, $category);
  206. }
  207. //$update['where_id'] = $id;
  208. //$update['poster_pay_type'] = $pay_state;
  209. //Dever::db($this->table_info)->update($update);
  210. return $pay_state;
  211. }
  212. # 更新需求
  213. public function update($top, $cate, $data, $id, $uid)
  214. {
  215. if ($cate) {
  216. $category = $top . ',' . $cate;
  217. } else {
  218. $category = $top;
  219. }
  220. $attr = $this->setAttr($category);
  221. $attr_update = Dever::preInput('attr_');
  222. if ($attr_update) {
  223. foreach ($attr_update as $k => $v) {
  224. if (strstr($k, '_s')) {
  225. $k = str_replace('_s', '', $k);
  226. $attr_update[$k] = $v;
  227. }
  228. if (strstr($k, '_e')) {
  229. $k = str_replace('_e', '', $k);
  230. $attr_update[$k] .= ',' . $v;
  231. }
  232. }
  233. }
  234. $update = array();
  235. $update['name'] = $data['name'];
  236. $update['category'] = $category;
  237. $update['pic'] = $data['pic'];
  238. //$update['pic_cover'] = $data['pic'];
  239. $update['poster_uid'] = $uid;
  240. if (isset($data['view_price']) && $data['view_price']) {
  241. $update['view_price'] = $data['view_price'];
  242. }
  243. if ($uid && $id) {
  244. # 此处要判断是用户自己的
  245. $info = Dever::db($this->table_info)->one($id);
  246. /*
  247. if ($uid == $info['poster_uid']) {
  248. Dever::alert('错误的用户参数');
  249. }
  250. */
  251. $update['where_id'] = $attr_update['where_id'] = $id;
  252. Dever::db($this->table_info)->update($update);
  253. Dever::db($this->table_attr)->update($attr_update);
  254. $pay = 0;
  255. } else {
  256. $id = Dever::db($this->table_info)->insert($update);
  257. $attr_update['category'] = $update['category'];
  258. $attr_update['info_id'] = $id;
  259. $attr_update['id'] = $id;
  260. Dever::db($this->table_attr)->insert($attr_update);
  261. # 验证是否需要支付,根据category判断
  262. $pay = false;
  263. //$pay = $this->getPayState($uid, $id, $category);
  264. $info = Dever::db($this->table_info)->one($id);
  265. $info = $this->getInfoLink($info);
  266. }
  267. return array('id' => $id, 'pay' => $pay, 'cate' => $category, 'top' => $top, 'info' => $info);
  268. }
  269. # 获取支付所需要的信息
  270. public function getPayInfo($id, $sku = 0, $type = 1, $value = '')
  271. {
  272. $info = is_array($id) ? $id : Dever::db($this->table_info)->one($id);
  273. if ($info) {
  274. if ($type == 2) {
  275. $info['price'] = $info['f_price'];
  276. unset($info['s_price']);
  277. } else {
  278. unset($info['f_price']);
  279. }
  280. $info['sku_name'] = '';
  281. $sku = $sku > 0 ? $sku : $info['sku_id'];
  282. if ($sku > 0) {
  283. $where['info_id'] = $info['id'];
  284. $where['id'] = $sku;
  285. $sku = Dever::db('goods/info_sku')->find($where);
  286. if ($sku) {
  287. if ($sku['type'] == 3) {
  288. $price = $sku['price'];
  289. $s_price = $sku['s_price'];
  290. $f_price = $sku['f_price'];
  291. $c_price = $sku['c_price'];
  292. $sku['price'] = 0;
  293. $sku['s_price'] = 0;
  294. $sku['f_price'] = 0;
  295. $sku['c_price'] = 0;
  296. # 计算价格
  297. $key = explode('_', $sku['key']);
  298. $attr = Dever::db('attr/info')->find($key[0]);
  299. if ($attr) {
  300. if ($value >= $attr['sell_value']) {
  301. $sku['price'] = $price;
  302. $sku['s_price'] = $s_price;
  303. $sku['f_price'] = $f_price;
  304. $sku['c_price'] = $c_price;
  305. }
  306. }
  307. }
  308. $info['sku_id'] = $sku['id'];
  309. if ($type == 2) {
  310. $info['price'] = $sku['f_price'];
  311. } else {
  312. $info['price'] = $sku['price'];
  313. $info['s_price'] = $sku['s_price'];
  314. $info['c_price'] = $sku['c_price'];
  315. }
  316. $info['min'] = $sku['min'];
  317. //$info['attr_json'] = $sku['attr'];
  318. $info['attr'] = Dever::json_decode($sku['attr']);
  319. if ($info['attr']) {
  320. $info['attr'] = Dever::load('attr/api')->getInfoByJson($info['attr']);
  321. $info['sku_name'] = $info['attr']['string'];
  322. }
  323. }
  324. }
  325. $this->getGroup($info);
  326. if (floor($info['min']) == $info['min']) {
  327. $info['min'] = intval($info['min']);
  328. }
  329. /*
  330. $info['freight_id'] = 0;
  331. $info['freight_price'] = 0;
  332. $freight = $this->freight($info, $info['price'], $user, $num);
  333. if ($freight) {
  334. $info['freight_id'] = $freight['id'];
  335. $info['freight_price'] = $freight['price'];
  336. }
  337. */
  338. # 佣金计算
  339. //$info['reward'] = $this->reward($info, $info['price']*$num);
  340. }
  341. if (isset($info['price'])) {
  342. $info['price'] = trim($info['price']);
  343. }
  344. if (isset($info['s_price'])) {
  345. $info['s_price'] = trim($info['s_price']);
  346. }
  347. if (isset($info['c_price'])) {
  348. $info['c_price'] = trim($info['c_price']);
  349. }
  350. if (isset($info['f_price'])) {
  351. $info['f_price'] = trim($info['f_price']);
  352. }
  353. return $info;
  354. }
  355. # 获取自提信息
  356. public function storeInfo($goods_id, $area_id = '')
  357. {
  358. $info = Dever::db($this->table_info)->one($goods_id);
  359. if (!$info) {
  360. return array();
  361. }
  362. $array = explode(',', $info['category']);
  363. $cate = $store = array();
  364. $total = count($array);
  365. $total -= 1;
  366. # 用户所在地理位置
  367. if ($area_id) {
  368. $where['area'] = $area_id;
  369. }
  370. foreach ($array as $k => $v) {
  371. $cate[$k] = $v;
  372. if ($k < $total) {
  373. $cate[] = '-1';
  374. }
  375. $where['category'] = $cate;
  376. $data = Dever::db('goods/store')->state($where);
  377. if ($data) {
  378. $store += $data;
  379. }
  380. }
  381. return $store;
  382. }
  383. # 获取运费信息
  384. public function getFreight($id, $goods_price, $user, $num)
  385. {
  386. $info = Dever::db($this->table_info)->one($id);
  387. return $this->freight($info, $goods_price, $user, $num);
  388. }
  389. # 根据已经选择的sku获取商品信息
  390. public function getInfoBySku($id, $sku_id)
  391. {
  392. $info = Dever::db($this->table_info)->one($id);
  393. if ($info) {
  394. if (!isset($info['unit'])) {
  395. $info['unit'] = '';
  396. }
  397. if (!isset($info['weight'])) {
  398. $info['weight'] = '';
  399. }
  400. if (!isset($info['code'])) {
  401. $info['code'] = '';
  402. }
  403. $info['sku_name'] = '';
  404. $info['cunhuo_code'] = '';
  405. if ($sku_id > 0) {
  406. $sku = Dever::db($this->table_sku)->find($sku_id);
  407. if ($sku && $sku['attr']) {
  408. $sku['attr'] = Dever::json_decode($sku['attr']);
  409. $info['sku'] = Dever::load('attr/api')->getInfoByJson($sku['attr']);
  410. $info['code'] = $sku['code'];
  411. $info['sku_name'] = $info['sku']['string'];
  412. $info['unit'] = $sku['unit'];
  413. $info['weight'] = $sku['weight'];
  414. $info['cunhuo_code'] = $sku['cunhuo_code'];
  415. }
  416. }
  417. $this->getGroup($info);
  418. }
  419. return $info;
  420. }
  421. # 获取基本信息
  422. public function getInfo($info, $attr = true, $other = false, $order = 'view_reorder', $user = array(), $reward = false)
  423. {
  424. if (!is_array($info)) {
  425. $info = Dever::db($this->table_info)->one($info);
  426. }
  427. if (!$info) {
  428. Dever::alert('商品不存在');
  429. }
  430. if ($info && $attr) {
  431. $info = $this->info($info, $order, $other, $user, $reward);
  432. } else {
  433. $this->getGroup($info);
  434. }
  435. if (!isset($info['goods_id'])) {
  436. $info['goods_id'] = $info['id'];
  437. }
  438. if ($info['pic']) {
  439. $info['pic'] = explode(',', $info['pic']);
  440. }
  441. if ($info['tag_id']) {
  442. $info['tag'] = Dever::db('goods/tag')->getAllByIds(array('ids' => $info['tag_id']));
  443. }
  444. return $info;
  445. }
  446. # 获取组合商品
  447. public function getGroup(&$info)
  448. {
  449. if ($info && isset($info['price_type']) && $info['price_type'] > 2 && $info['goods'] && is_string($info['goods'])) {
  450. # 获取套餐或者组合的商品
  451. $goods = Dever::array_decode($info['goods']);
  452. $goods_info = array();
  453. foreach ($goods as $k => $v) {
  454. $ginfo = Dever::db('goods/info')->one($v['goods_id']);
  455. if ($ginfo) {
  456. $goods_info[] = array
  457. (
  458. 'id' => $ginfo['id'],
  459. 'code' => $ginfo['code'],
  460. 'name' => $ginfo['name'],
  461. 'price' => $ginfo['price'],
  462. 'cover' => $ginfo['cover'],
  463. 'num' => $v['num'],
  464. 'unit' => $ginfo['unit'] ? $ginfo['unit'] : '个',
  465. );
  466. }
  467. }
  468. $info['goods'] = $goods_info;
  469. }
  470. }
  471. # 获取基本信息
  472. public function info($info, $key = 'view_reorder', $other = false, $user = array(), $reward = false)
  473. {
  474. $info['category_array'] = Dever::load('category/api')->string($info['category']);
  475. if ($info['price_type'] == 2) {
  476. $attr = $this->setAttr($info['category'], $key);
  477. $attr_data = $attr->one($info['id']);
  478. $info['attr'] = $info['group_attr'] = $info['single_attr'] = $info['input_attr'] = array();
  479. if ($attr->config['attr']) {
  480. $i = 0;
  481. foreach ($attr->config['attr'] as $k => $v) {
  482. if (isset($v[$key]) && $v[$key] > 0 && $v['state'] == 1) {
  483. if (isset($attr_data['attr_' . $v['id']])) {
  484. $v['value'] = $attr_data['attr_' . $v['id']];
  485. } else {
  486. $v['value'] = '';
  487. }
  488. if (!$v['value']) {
  489. continue;
  490. }
  491. $v['value_string'] = Dever::load('attr/api')->getValue($v);
  492. if ($v['ename'] == 'price') {
  493. $info['price'] = $v['value_string'];
  494. } else {
  495. if ($v['icon']) {
  496. $v['bg'] = 'background-image: url('.$v['icon'].')';
  497. $v['pic'] = '<img src="'.$v['icon'].'" width="18" />';
  498. }
  499. $info['attr'][$i] = $this->attr($i, $v, $info);
  500. $i++;
  501. }
  502. }
  503. }
  504. # 获取sku的价格
  505. $this->getPrice($info, $other);
  506. }
  507. if (!$info['attr']) {
  508. $info['price_type'] = 1;
  509. }
  510. } else {
  511. if ($other) {
  512. foreach ($other[1] as $k1 => $v1) {
  513. $v[$v1] = isset($other[0][-1][$v1]) ? $other[0][-1][$v1] : 0;
  514. if ($v1 == 'price_id' && $v[$v1] > 0) {
  515. $price_template = Dever::load('price/lib/data')->get($info['id'], -1, $v[$v1]);
  516. if ($price_template) {
  517. if (Dever::config('base')->buy && Dever::config('base')->buy == 1) {
  518. if ($price_template['price_num'] > 0) {
  519. $info['min'] = $price_template['price_num'];
  520. }
  521. if ($price_template['price_buy'] && $price_template['price_buy'] > 0) {
  522. $info['f_price'] = $price_template['price_buy'];
  523. }
  524. } else {
  525. if ($price_template['price_sell'] && $price_template['price_sell'] > 0) {
  526. $info['price'] = $price_template['price_sell'];
  527. }
  528. }
  529. }
  530. }
  531. }
  532. }
  533. }
  534. if (isset($info['cdate']) && $info['cdate']) {
  535. $info['cdate_string'] = Dever::mdate($info['cdate'], 2);
  536. }
  537. $info = $this->getInfoLink($info);
  538. $this->getGroup($info);
  539. if (Dever::config('base')->buy && Dever::config('base')->buy == 1) {
  540. $info['price'] = $info['f_price'];
  541. }
  542. //$info['freight'] = 0;
  543. //unset($info['goods']);
  544. unset($info['f_price']);
  545. unset($info['cdate']);
  546. unset($info['udate']);
  547. unset($info['category']);
  548. unset($info['top_category_id']);
  549. unset($info['second_category_id']);
  550. unset($info['category_id']);
  551. unset($info['area']);
  552. unset($info['mode']);
  553. unset($info['top']);
  554. unset($info['youhui']);
  555. unset($info['status']);
  556. unset($info['reorder']);
  557. unset($info['state']);
  558. return $info;
  559. }
  560. # 佣金计算
  561. private function reward($info, $price)
  562. {
  563. if ($info['reward_value'] && $info['reward_value'] > 0) {
  564. if ($info['reward_type'] == 2) {
  565. $info['reward_value'] = round(($info['reward_value']/100) * $price, 2);
  566. }
  567. return $info['reward_value'];
  568. }
  569. $array = explode(',', $info['category']);
  570. $cate = $reward = array();
  571. $total = count($array);
  572. $total -= 1;
  573. foreach ($array as $k => $v) {
  574. $cate[$k] = $v;
  575. if ($k < $total) {
  576. $cate[] = '-1';
  577. }
  578. $where['category'] = $cate;
  579. $data = Dever::db('goods/reward')->one($where);
  580. if ($data) {
  581. $reward = $data;
  582. }
  583. }
  584. if ($reward) {
  585. if ($reward['value'] && $reward['value'] > 0) {
  586. if ($reward['type'] == 2) {
  587. $reward['value'] = round(($reward['value']/100) * $price, 2);
  588. }
  589. return $reward['value'];
  590. }
  591. }
  592. return 0;
  593. }
  594. # 运费计算,暂时用省份代表地区
  595. private function freight($info, $goods_price, $user = array(), $num = 1)
  596. {
  597. $array = explode(',', $info['category']);
  598. $cate = $freight = array();
  599. $total = count($array);
  600. $total -= 1;
  601. if ($info && $info['goods_area']) {
  602. $where['goods_area'] = $info['goods_area'];
  603. }
  604. # 用户所在地理位置
  605. if ($user && isset($user['area_id']) && $user['area_id']) {
  606. $area = explode(',', $user['area_id']);
  607. $where['area'] = $area[0];
  608. }
  609. foreach ($array as $k => $v) {
  610. $cate[$k] = $v;
  611. if ($k < $total) {
  612. $cate[] = '-1';
  613. }
  614. $where['category'] = $cate;
  615. $data = Dever::db('goods/freight')->one($where);
  616. if ($data) {
  617. $freight = $data;
  618. }
  619. }
  620. if ($freight) {
  621. $price = $freight['first_price'];
  622. if ($num > $freight['first_num']) {
  623. $num = $num - $freight['first_num'];
  624. $price = $freight['first_price'] + ($freight['next_price']*$num);
  625. }
  626. if ($freight['type'] == 2) {
  627. $freight['price'] = round($goods_price*$num*($price/100), 2);
  628. } else {
  629. $freight['price'] = $price;
  630. }
  631. }
  632. return $freight;
  633. }
  634. private function attr($key, $data, &$info)
  635. {
  636. # 获取销售属性
  637. if ($data['option']) {
  638. $value = explode(',', $data['value']);
  639. $option = $data['option'];
  640. $data['option'] = array();
  641. foreach ($value as $k => $v) {
  642. if (isset($option[$v])) {
  643. $option[$v]['parent_name'] = $data['name'];
  644. $data['option'][] = $info['option'][$option[$v]['price_key']] = $option[$v];
  645. }
  646. }
  647. } else {
  648. unset($data['option']);
  649. }
  650. if ($data['is_sell'] == 2) {
  651. if ($data['sell_type'] == 1) {
  652. $info['group_attr'][] = $key;
  653. } elseif ($data['sell_type'] == 2) {
  654. $info['single_attr'][] = $key;
  655. } elseif ($data['sell_type'] == 3) {
  656. $info['input_attr'][] = $key;
  657. }
  658. }
  659. return $data;
  660. }
  661. private function getPrice(&$info, $other = false)
  662. {
  663. # 获取价格
  664. $where['info_id'] = $info['id'];
  665. $info['price_array'] = array();
  666. $list = Dever::db('goods/info_sku')->getData($where);
  667. $min = $max = array();
  668. if ($list) {
  669. foreach ($list as $k => $v) {
  670. if ($v['key']) {
  671. if (Dever::config('base')->buy && Dever::config('base')->buy == 1) {
  672. $v['price'] = $v['f_price'];
  673. }
  674. $v['name'] = array();
  675. $temp = explode('_', $v['key']);
  676. foreach ($temp as $k1 => $v1) {
  677. if (isset($info['option'][$v1])) {
  678. $v['name'][] = $info['option'][$v1]['name'];
  679. }
  680. }
  681. $v['name'] = implode(',', $v['name']);
  682. if ($other) {
  683. foreach ($other[1] as $k1 => $v1) {
  684. $v[$v1] = isset($other[0][$v['id']][$v1]) ? $other[0][$v['id']][$v1] : 0;
  685. if ($v1 == 'price_id' && $v[$v1] > 0) {
  686. $price_template = Dever::load('price/lib/data')->get($v['info_id'], $v['id'], $v[$v1]);
  687. if ($price_template) {
  688. if (Dever::config('base')->buy && Dever::config('base')->buy == 1) {
  689. if ($price_template['price_num'] > 0) {
  690. $v['min'] = $price_template['price_num'];
  691. }
  692. if ($price_template['price_buy'] && $price_template['price_buy'] > 0) {
  693. $v['price'] = $price_template['price_buy'];
  694. }
  695. } else {
  696. if ($price_template['price_sell'] && $price_template['price_sell'] > 0) {
  697. $v['price'] = $price_template['price_sell'];
  698. }
  699. }
  700. }
  701. }
  702. }
  703. }
  704. if ($v['type'] == 1) {
  705. if (!$min) {
  706. $min = $v;
  707. }
  708. if (!$max) {
  709. $max = $v;
  710. }
  711. if ($min['price'] > $v['price']) {
  712. $min = $v;
  713. }
  714. if ($max['price'] < $v['price']) {
  715. $max = $v;
  716. }
  717. }
  718. $info['price_array'][$v['key']] = $v;
  719. }
  720. }
  721. }
  722. $info['price_min'] = $min;
  723. $info['price_max'] = $max;
  724. if ($min) {
  725. $info['price'] = $min['price'];
  726. $info['s_price'] = $min['s_price'];
  727. $info['sku_id'] = $min['id'];
  728. }
  729. unset($info['option']);
  730. }
  731. public function getInfoLink($info)
  732. {
  733. if (isset($this->view_path)) {
  734. $info['view_path'] = $this->view_path . '?top=' . $info['top_category_id'] . '&id=' . $info['id'];
  735. $info['view_link'] = Dever::url($info['view_path']);
  736. }
  737. return $info;
  738. }
  739. public function setViewPath($path)
  740. {
  741. $this->view_path = $path;
  742. }
  743. # 根据分类获取商品
  744. public function getGoods_api()
  745. {
  746. $where['category_id'] = Dever::input('cate_id');
  747. $factory_id = Dever::input('id');
  748. $data = Dever::db('goods/info_category')->getData($where);
  749. return $data;
  750. }
  751. }