Info.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. Dever::alert('错误的分类信息');
  33. }
  34. }
  35. private function setAttr($cate, $order = 'list_reorder')
  36. {
  37. if ($order != 'list_reorder') {
  38. Dever::config('base')->attrOrder = $order;
  39. }
  40. Dever::setInput('search_option_category', $cate);
  41. $attr = Dever::db($this->table_attr, array(), $cate);
  42. return $attr;
  43. }
  44. public function getAttr($cate)
  45. {
  46. return Dever::load('attr/api')->getAttrByCate($cate, 'list');
  47. }
  48. # 根据分类获取需求列表数据
  49. public function getData($cate = false, $limit = '0,10', $page = false, $attr = true)
  50. {
  51. if (!$limit) {
  52. $limit = '0,10';
  53. }
  54. $where = $data = array();
  55. if (!$cate) {
  56. if ($this->top_category > 0) {
  57. $where['top_category_id'] = $this->top_category;
  58. }
  59. } elseif (strstr($cate, ',')) {
  60. $where['category'] = $this->top_category . ',' . $cate;
  61. } else {
  62. $where['second_category_id'] = $cate;
  63. }
  64. if ($page) {
  65. $method = 'getPageAll';
  66. } else {
  67. $where['limit'] = $limit;
  68. $method = 'getAll';
  69. }
  70. $name = Dever::input('name');
  71. if ($name) {
  72. $where['name'] = $name;
  73. }
  74. $data = Dever::db($this->table_info)->$method($where);
  75. if ($data && $attr) {
  76. foreach ($data as $k => $v) {
  77. $data[$k] = $this->info($v, 'list_reorder');
  78. }
  79. }
  80. return $data;
  81. }
  82. # 根据属性获取需求列表数据 这里带有检索属性的功能 此处还需优化
  83. public function getDataByAttr($cate = false, $where = array())
  84. {
  85. if (!$cate) {
  86. Dever::alert('错误的分类信息');
  87. }
  88. $where_sql = array();
  89. $name = Dever::input('name');
  90. if ($name) {
  91. //$where['name'] = $name;
  92. $where_sql[] = ' t_2.name = "'.$name.'"';
  93. }
  94. $attr = $this->setAttr($this->top_category . ',' . $cate);
  95. //print_r($attr->config['attr']);
  96. $where_sql = Dever::load('attr/api')->getSql($attr->config['attr'], $where, $where_sql);
  97. if (!$where_sql) {
  98. return array();
  99. }
  100. # 这里用pdo的数据绑定,字符串字段的区间查询就是有问题。。。只能用sql了
  101. //$data = $attr->getPageAll($where);
  102. $page['template'] = 'list';
  103. $page['num'] = 10;
  104. $where_sql = implode(' and ', $where_sql);
  105. $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';
  106. $data = $attr->fetchAll($sql, array(), $page);
  107. if ($data) {
  108. foreach ($data as $k => $v) {
  109. $data[$k] = $this->info($v, 'list_reorder');
  110. }
  111. }
  112. return $data;
  113. }
  114. # 获取列表页的搜索条件
  115. public function getSearch($cate = false)
  116. {
  117. # 获取搜索条件
  118. if (!$cate) {
  119. $search_cate = $cate = $this->top_category;
  120. } else {
  121. $search_cate = $cate;
  122. }
  123. # 1是当前用户所在的城市,暂时写死
  124. # 获取搜索条件
  125. $search = Dever::load('attr/api')->getSearch($search_cate, true, 1, false, 2);
  126. # 下级分类
  127. $search['cate'] = Dever::load('category/api')->getList($cate);
  128. # 上级分类
  129. $search['parent_cate'] = Dever::load('category/api')->getList($this->top_category);
  130. return $search;
  131. }
  132. public function getPayState($uid, $id, $category)
  133. {
  134. $pay_where['category'] = $category;
  135. $pay = Dever::db('goods/pay')->one($pay_where);
  136. $pay_state = 0;
  137. if ($pay && $pay['num'] > 0) {
  138. $pay_state = $pay['num'];
  139. } elseif (strstr($category, ',')) {
  140. $category = explode(',', $category);
  141. $category = array_pop($category);
  142. $pay_state = $this->getPayState($uid, $id, $category);
  143. }
  144. //$update['where_id'] = $id;
  145. //$update['poster_pay_type'] = $pay_state;
  146. //Dever::db($this->table_info)->update($update);
  147. return $pay_state;
  148. }
  149. # 更新需求
  150. public function update($top, $cate, $data, $id, $uid)
  151. {
  152. if ($cate) {
  153. $category = $top . ',' . $cate;
  154. } else {
  155. $category = $top;
  156. }
  157. $attr = $this->setAttr($category);
  158. $attr_update = Dever::preInput('attr_');
  159. if ($attr_update) {
  160. foreach ($attr_update as $k => $v) {
  161. if (strstr($k, '_s')) {
  162. $k = str_replace('_s', '', $k);
  163. $attr_update[$k] = $v;
  164. }
  165. if (strstr($k, '_e')) {
  166. $k = str_replace('_e', '', $k);
  167. $attr_update[$k] .= ',' . $v;
  168. }
  169. }
  170. }
  171. $update = array();
  172. $update['name'] = $data['name'];
  173. $update['category'] = $category;
  174. $update['pic'] = $data['pic'];
  175. //$update['pic_cover'] = $data['pic'];
  176. $update['poster_uid'] = $uid;
  177. if (isset($data['view_price']) && $data['view_price']) {
  178. $update['view_price'] = $data['view_price'];
  179. }
  180. if ($uid && $id) {
  181. # 此处要判断是用户自己的
  182. $info = Dever::db($this->table_info)->one($id);
  183. /*
  184. if ($uid == $info['poster_uid']) {
  185. Dever::alert('错误的用户参数');
  186. }
  187. */
  188. $update['where_id'] = $attr_update['where_id'] = $id;
  189. Dever::db($this->table_info)->update($update);
  190. Dever::db($this->table_attr)->update($attr_update);
  191. $pay = 0;
  192. } else {
  193. $id = Dever::db($this->table_info)->insert($update);
  194. $attr_update['category'] = $update['category'];
  195. $attr_update['info_id'] = $id;
  196. $attr_update['id'] = $id;
  197. Dever::db($this->table_attr)->insert($attr_update);
  198. # 验证是否需要支付,根据category判断
  199. $pay = false;
  200. //$pay = $this->getPayState($uid, $id, $category);
  201. $info = Dever::db($this->table_info)->one($id);
  202. $info = $this->getInfoLink($info);
  203. }
  204. return array('id' => $id, 'pay' => $pay, 'cate' => $category, 'top' => $top, 'info' => $info);
  205. }
  206. # 获取需求基本信息
  207. public function getInfo($id, $attr = true, $order = 'view_reorder')
  208. {
  209. $info = Dever::db($this->table_info)->one($id);
  210. if ($info && $attr) {
  211. $info = $this->info($info, $order);
  212. }
  213. if ($info['poster_uid'] < 0) {
  214. $info['poster_uid'] = 1;
  215. }
  216. if ($info['poster_uid'] > 0) {
  217. $info['user'] = Dever::db('passport/user')->one($info['poster_uid']);
  218. $sex = Dever::db('passport/user')->config['sex'];
  219. if ($info['user']['area']) {
  220. $info['user']['area'] = str_replace(',', '-', $info['user']['area']);
  221. $info['user']['area'] .= '-';
  222. }
  223. $info['user']['text'] = $info['user']['area'] . $sex[$info['user']['sex']];
  224. if (!$info['user']['avatar']) {
  225. $config = Dever::db('main/config')->one();
  226. $info['user']['avatar'] = $config['avatar'];
  227. }
  228. }
  229. $info['num_view_text'] = $info['num_view'] + $info['num_add_view'];
  230. $info['num_view_text'] .= '人浏览';
  231. if ($info['pic']) {
  232. $info['pic'] = explode(',', $info['pic']);
  233. }
  234. return $info;
  235. }
  236. # 获取需求基本信息
  237. public function info($info, $key = 'list_reorder')
  238. {
  239. $attr = $this->setAttr($info['category'], $key);
  240. $attr_data = $attr->one($info['id']);
  241. $info['category_array'] = Dever::load('category/api')->string($info['category']);
  242. $info['attr'] = array();
  243. if ($attr->config['attr']) {
  244. foreach ($attr->config['attr'] as $k => $v) {
  245. if (isset($v[$key]) && $v[$key] > 0) {
  246. if (isset($attr_data['attr_' . $v['id']])) {
  247. $v['value'] = $attr_data['attr_' . $v['id']];
  248. } else {
  249. continue;
  250. $v['value'] = '';
  251. }
  252. $v['value_string'] = Dever::load('attr/api')->getValue($v);
  253. if ($v['ename'] == 'price') {
  254. $info['price'] = $v['value_string'];
  255. } else {
  256. $v['bg'] = 'background-image: url('.$v['icon'].')';
  257. $v['pic'] = '<img src="'.$v['icon'].'" width="18" />';
  258. $info['attr'][] = $v;
  259. }
  260. }
  261. }
  262. }
  263. if (isset($info['cdate']) && $info['cdate']) {
  264. $info['cdate_string'] = Dever::mdate($info['cdate'], 2);
  265. }
  266. $info = $this->getInfoLink($info);
  267. return $info;
  268. }
  269. public function getInfoLink($info)
  270. {
  271. if (isset($this->view_path)) {
  272. $info['view_path'] = $this->view_path . '?top=' . $info['top_category_id'] . '&id=' . $info['id'];
  273. $info['view_link'] = Dever::url($info['view_path']);
  274. }
  275. return $info;
  276. }
  277. public function setViewPath($path)
  278. {
  279. $this->view_path = $path;
  280. }
  281. }