Info.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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. /**
  10. * instance
  11. *
  12. * @var string
  13. */
  14. protected static $instance;
  15. /**
  16. * init
  17. *
  18. * @return mixed
  19. */
  20. public static function init($top_category = false)
  21. {
  22. if (empty(self::$instance[$top_category])) {
  23. self::$instance[$top_category] = new self($top_category);
  24. }
  25. return self::$instance[$top_category];
  26. }
  27. public function __construct($top_category = false)
  28. {
  29. $this->top_category = $top_category;
  30. # 必有有分类才能进入到需求中
  31. if (!$this->top_category) {
  32. $this->top_category = -1;
  33. //Dever::alert('错误的分类信息');
  34. }
  35. }
  36. private function setAttr($cate, $order = 'list_reorder')
  37. {
  38. if ($order != 'list_reorder') {
  39. Dever::config('base')->attrOrder = $order;
  40. }
  41. Dever::setInput('search_option_category', $cate);
  42. $attr = Dever::db($this->table_attr, array(), $cate);
  43. return $attr;
  44. }
  45. public function getAttr($cate)
  46. {
  47. return Dever::load('attr/api')->getAttrByCate($cate, 'list');
  48. }
  49. # 根据分类获取需求列表数据
  50. public function getData($cate = false, $limit = '0,10', $page = false, $attr = true)
  51. {
  52. if (!$limit) {
  53. $limit = '0,10';
  54. }
  55. $where = $data = array();
  56. if (!$cate) {
  57. if ($this->top_category > 0) {
  58. $where['top_category_id'] = $this->top_category;
  59. }
  60. } elseif (strstr($cate, ',')) {
  61. $where['category'] = $this->top_category . ',' . $cate;
  62. } else {
  63. $where['second_category_id'] = $cate;
  64. }
  65. if ($page) {
  66. $method = 'getPageAll';
  67. } else {
  68. $where['limit'] = $limit;
  69. $method = 'getAll';
  70. }
  71. $name = Dever::input('name');
  72. if ($name) {
  73. $where['name'] = $name;
  74. }
  75. $data = Dever::db($this->table_info)->$method($where);
  76. if ($data && $attr) {
  77. foreach ($data as $k => $v) {
  78. $data[$k] = $this->info($v, 'list_reorder');
  79. }
  80. }
  81. return $data;
  82. }
  83. # 根据属性获取需求列表数据 这里带有检索属性的功能 此处还需优化
  84. public function getDataByAttr($cate = false, $where = array())
  85. {
  86. if (!$cate) {
  87. Dever::alert('错误的分类信息');
  88. }
  89. $where_sql = array();
  90. $name = Dever::input('name');
  91. if ($name) {
  92. //$where['name'] = $name;
  93. $where_sql[] = ' t_2.name = "'.$name.'"';
  94. }
  95. $attr = $this->setAttr($this->top_category . ',' . $cate);
  96. //print_r($attr->config['attr']);
  97. $where_sql = Dever::load('attr/api')->getSql($attr->config['attr'], $where, $where_sql);
  98. if (!$where_sql) {
  99. return array();
  100. }
  101. # 这里用pdo的数据绑定,字符串字段的区间查询就是有问题。。。只能用sql了
  102. //$data = $attr->getPageAll($where);
  103. $page['template'] = 'list';
  104. $page['num'] = 10;
  105. $where_sql = implode(' and ', $where_sql);
  106. $sql = 'SELECT t_2.* FROM `info_goods_'.$attr->config['name'].'` as t_1 Left Join `info_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';
  107. $data = $attr->fetchAll($sql, array(), $page);
  108. if ($data) {
  109. foreach ($data as $k => $v) {
  110. $data[$k] = $this->info($v, 'list_reorder');
  111. }
  112. }
  113. return $data;
  114. }
  115. # 获取列表页的搜索条件
  116. public function getSearch($cate = false)
  117. {
  118. # 获取搜索条件
  119. if (!$cate) {
  120. $search_cate = $cate = $this->top_category;
  121. } else {
  122. $search_cate = $cate;
  123. }
  124. # 1是当前用户所在的城市,暂时写死
  125. # 获取搜索条件
  126. $search = Dever::load('attr/api')->getSearch($search_cate, true, 1, false, 2);
  127. # 下级分类
  128. $search['cate'] = Dever::load('category/api')->getList($cate);
  129. # 上级分类
  130. $search['parent_cate'] = Dever::load('category/api')->getList($this->top_category);
  131. return $search;
  132. }
  133. # 获取支付信息
  134. public function getPayState($uid, $id, $category)
  135. {
  136. $pay_where['category'] = $category;
  137. $pay = Dever::db('goods/pay')->one($pay_where);
  138. $pay_state = 0;
  139. if ($pay && $pay['num'] > 0) {
  140. $pay_state = $pay['num'];
  141. } elseif (strstr($category, ',')) {
  142. $category = explode(',', $category);
  143. $category = array_pop($category);
  144. $pay_state = $this->getPayState($uid, $id, $category);
  145. }
  146. //$update['where_id'] = $id;
  147. //$update['poster_pay_type'] = $pay_state;
  148. //Dever::db($this->table_info)->update($update);
  149. return $pay_state;
  150. }
  151. # 更新需求
  152. public function update($top, $cate, $data, $id, $uid)
  153. {
  154. if ($cate) {
  155. $category = $top . ',' . $cate;
  156. } else {
  157. $category = $top;
  158. }
  159. $attr = $this->setAttr($category);
  160. $attr_update = Dever::preInput('attr_');
  161. if ($attr_update) {
  162. foreach ($attr_update as $k => $v) {
  163. if (strstr($k, '_s')) {
  164. $k = str_replace('_s', '', $k);
  165. $attr_update[$k] = $v;
  166. }
  167. if (strstr($k, '_e')) {
  168. $k = str_replace('_e', '', $k);
  169. $attr_update[$k] .= ',' . $v;
  170. }
  171. }
  172. }
  173. $update = array();
  174. $update['name'] = $data['name'];
  175. $update['category'] = $category;
  176. $update['pic'] = $data['pic'];
  177. //$update['pic_cover'] = $data['pic'];
  178. $update['poster_uid'] = $uid;
  179. if (isset($data['view_price']) && $data['view_price']) {
  180. $update['view_price'] = $data['view_price'];
  181. }
  182. if ($uid && $id) {
  183. # 此处要判断是用户自己的
  184. $info = Dever::db($this->table_info)->one($id);
  185. /*
  186. if ($uid == $info['poster_uid']) {
  187. Dever::alert('错误的用户参数');
  188. }
  189. */
  190. $update['where_id'] = $attr_update['where_id'] = $id;
  191. Dever::db($this->table_info)->update($update);
  192. Dever::db($this->table_attr)->update($attr_update);
  193. $pay = 0;
  194. } else {
  195. $id = Dever::db($this->table_info)->insert($update);
  196. $attr_update['category'] = $update['category'];
  197. $attr_update['info_id'] = $id;
  198. $attr_update['id'] = $id;
  199. Dever::db($this->table_attr)->insert($attr_update);
  200. # 验证是否需要支付,根据category判断
  201. $pay = false;
  202. //$pay = $this->getPayState($uid, $id, $category);
  203. $info = Dever::db($this->table_info)->one($id);
  204. $info = $this->getInfoLink($info);
  205. }
  206. return array('id' => $id, 'pay' => $pay, 'cate' => $category, 'top' => $top, 'info' => $info);
  207. }
  208. # 获取支付所需要的信息
  209. public function getPayInfo($id, $sku)
  210. {
  211. $info = Dever::db($this->table_info)->getOne($id);
  212. if ($info && $sku > 0) {
  213. $where['info_id'] = $info['id'];
  214. $where['id'] = $sku;
  215. $sku = Dever::db('goods/info_sku')->one($where);
  216. if ($sku) {
  217. $info['price'] = $sku['price'];
  218. }
  219. }
  220. return $info;
  221. }
  222. # 获取基本信息
  223. public function getInfo($id, $attr = true, $order = 'view_reorder')
  224. {
  225. $info = Dever::db($this->table_info)->getOne($id);
  226. if ($info && $attr) {
  227. $info = $this->info($info, $order);
  228. }
  229. if ($info['pic']) {
  230. $info['pic'] = explode(',', $info['pic']);
  231. }
  232. return $info;
  233. }
  234. # 获取基本信息
  235. public function info($info, $key = 'list_reorder', $is_sell = false)
  236. {
  237. $info['freight'] = 0;
  238. if ($info['type'] == 1) {
  239. # 获取运费
  240. $info['freight'] = $this->freight($info);
  241. }
  242. if ($info['price_type'] == 2) {
  243. $attr = $this->setAttr($info['category'], $key);
  244. $attr_data = $attr->one($info['id']);
  245. $info['category_array'] = Dever::load('category/api')->string($info['category']);
  246. $info['attr'] = $info['sell_attr'] = array();
  247. if ($attr->config['attr']) {
  248. foreach ($attr->config['attr'] as $k => $v) {
  249. if (isset($v[$key]) && $v[$key] > 0) {
  250. if (isset($attr_data['attr_' . $v['id']])) {
  251. $v['value'] = $attr_data['attr_' . $v['id']];
  252. } else {
  253. continue;
  254. $v['value'] = '';
  255. }
  256. $v['value_string'] = Dever::load('attr/api')->getValue($v);
  257. if ($v['ename'] == 'price') {
  258. $info['price'] = $v['value_string'];
  259. } else {
  260. $v['bg'] = 'background-image: url('.$v['icon'].')';
  261. $v['pic'] = '<img src="'.$v['icon'].'" width="18" />';
  262. if ($is_sell > 0) {
  263. if ($is_sell == $v['is_sell']) {
  264. $info['attr'][] = $v;
  265. }
  266. } else {
  267. $info['attr'][] = $v;
  268. # 设置sku
  269. $this->sku($v, $info);
  270. }
  271. }
  272. }
  273. }
  274. }
  275. } else {
  276. $price = $info['price'];
  277. $s_price = $info['s_price'];
  278. $info['price'] = array();
  279. $info['price']['list'] = array();
  280. $info['price']['min'] = array
  281. (
  282. 'price' => $price,
  283. 's_price' => $s_price
  284. );
  285. $info['price']['max'] = array();
  286. }
  287. $info['type_name'] = Dever::db('goods/info')->config['config_type'][$info['type']];
  288. if (isset($info['cdate']) && $info['cdate']) {
  289. $info['cdate_string'] = Dever::mdate($info['cdate'], 2);
  290. }
  291. $info = $this->getInfoLink($info);
  292. return $info;
  293. }
  294. private function freight($info)
  295. {
  296. # 之后要加入当前用户的地理位置
  297. $array = explode(',', $info['category']);
  298. $cate = $freight = array();
  299. $total = count($array);
  300. $total -= 1;
  301. foreach ($array as $k => $v) {
  302. $cate[$k] = $v;
  303. if ($k < $total) {
  304. $cate[] = '-1';
  305. }
  306. $where['category'] = $cate;
  307. $data = Dever::db('goods/freight')->one($where);
  308. if ($data) {
  309. $freight = $data;
  310. }
  311. }
  312. return $freight;
  313. }
  314. private function sku($data, &$info)
  315. {
  316. # 获取销售属性
  317. if ($data['is_sell'] == 2) {
  318. if ($data['option']) {
  319. $value = explode(',', $data['value']);
  320. foreach($data['option'] as $k => $v) {
  321. if (in_array($v['id'], $value)) {
  322. $data['option_sku'][] = array
  323. (
  324. 'id' => $v['id'],
  325. 'info_id' => $v['info_id'],
  326. 'name' => $v['name'],
  327. 'icon' => $v['icon'],
  328. 'value' => $v['info_id'] . '-' . $v['id'],
  329. );
  330. }
  331. }
  332. unset($data['option']);
  333. }
  334. $info['sell_attr'][] = $data;
  335. # 获取最便宜的价格
  336. $where['info_id'] = $info['id'];
  337. $info['price'] = array();
  338. $info['price']['list'] = Dever::db('goods/info_sku')->getData($where);
  339. $info['price']['min'] = Dever::db('goods/info_sku')->getMinOne($where);
  340. $info['price']['max'] = Dever::db('goods/info_sku')->getMaxOne($where);
  341. }
  342. }
  343. public function getInfoLink($info)
  344. {
  345. if (isset($this->view_path)) {
  346. $info['view_path'] = $this->view_path . '?top=' . $info['top_category_id'] . '&id=' . $info['id'];
  347. $info['view_link'] = Dever::url($info['view_path']);
  348. }
  349. return $info;
  350. }
  351. public function setViewPath($path)
  352. {
  353. $this->view_path = $path;
  354. }
  355. }