Info.php 11 KB

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