Data.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. <?php
  2. # 需求接口类
  3. namespace Res\Src;
  4. use Dever;
  5. use Res\lib\Info;
  6. use Main\Lib\Core;
  7. use Dever\Routing\Uri;
  8. class Data extends Core
  9. {
  10. protected $checkUser = true;
  11. # 列表页path
  12. private $list_path = 'main/list';//'info/data.getList';//main/list
  13. # 详情页path
  14. private $view_path = 'main/view';//'info/data.getInfo';//main/view
  15. private $info = false;
  16. public function __construct()
  17. {
  18. # 当没有top时,就是首页
  19. $this->top = Dever::input('top', -1);
  20. if ($this->top) {
  21. $this->info = Info::init($this->top);
  22. }
  23. # 当没有cate,但有top时就是顶级分类列表页,如果有cate,就是二级分类列表页
  24. $this->cate = Dever::input('cate');
  25. # 当有id时,就是详情页
  26. $this->id = Dever::input('id');
  27. }
  28. # 获取顶级分类列表,首页用到
  29. public function top()
  30. {
  31. $top = Dever::load('category/api')->getTop(Dever::config('base')->category);
  32. $result = array();
  33. if ($top) {
  34. $cates = array_keys($top);
  35. $child = Dever::load('category/api')->getChild($cates);
  36. $i = 0;
  37. $path = $this->list_path;
  38. if (Uri::$value == 'submit') {
  39. $path = 'submit_next';
  40. }
  41. foreach ($top as $k => $v) {
  42. $result[$i] = $v;
  43. $result[$i]['path'] = $path . '?top=' . $v['id'];
  44. if (isset($child[$v['id']]) && $child[$v['id']][0]['level'] != -1) {
  45. $result[$i]['child'] = $child[$v['id']];
  46. if (Uri::$value == 'submit') {
  47. $result[$i]['path'] = 'submit_cate?top=' . $v['id'];
  48. } else {
  49. $result[$i]['path'] .= '&cate=' . $child[$v['id']][0]['id'];
  50. }
  51. }
  52. if ($v['status'] == 1) {
  53. $result[$i]['link'] = Dever::url($result[$i]['path']);
  54. } else {
  55. $result[$i]['link'] = 'javascript:hui.toast(\'暂未开放\');';
  56. }
  57. $i++;
  58. }
  59. }
  60. return $result;
  61. }
  62. # 获取首页的分类和需求数据
  63. # 接口:http://192.168.33.10/info/info/?l=data.getIndex
  64. public function getIndex()
  65. {
  66. $top = Dever::load('res/data.top');
  67. if ($top) {
  68. $this->cate = false;
  69. foreach ($top as $k => $v) {
  70. $this->info = Info::init($v['id']);
  71. # 首页显示多少条需求数据,暂定10条
  72. $top[$k]['data'] = $this->getList('0,10', $v);
  73. }
  74. }
  75. return $top;
  76. }
  77. # 获取最新需求数据
  78. # 接口:http://192.168.33.10/info/demand/?l=data.getNew
  79. public function getNew()
  80. {
  81. $info = Info::init(-1);
  82. $data = $info->getData($this->cate, '0,10', false, false);
  83. # 这里需要有用户信息
  84. if ($data) {
  85. $info->setViewPath($this->view_path);
  86. foreach ($data as $k => $v) {
  87. if ($v['poster_uid'] < 0) {
  88. $v['poster_uid'] = 1;
  89. }
  90. $data[$k] = $info->getInfoLink($v);
  91. $link = $data[$k]['view_link'];
  92. $data[$k]['user'] = Dever::db('passport/user')->one($v['poster_uid']);
  93. $data[$k]['text'] = '<span class="cuIcon-notifcation"></span><a href="'.$link.'"><span class="text-blue">'.$data[$k]['user']['username'].'刚刚发布:</span>' . Dever::cut($v['name'], 10) . '</a>';
  94. }
  95. }
  96. return $data;
  97. }
  98. # 获取搜索条件列表 分类列表页用到
  99. # 接口:http://192.168.33.10/info/demand/?l=data.getSearch&top=1&cate=3,7&json=1
  100. public function getSearch()
  101. {
  102. $this->checkInfo();
  103. return $this->info->getSearch($this->cate);
  104. }
  105. # 获取搜索条件列表 分类列表页用到 只用到了搜索条件
  106. # 接口:http://192.168.33.10/info/demand/?l=data.getSearchList&top=1&cate=3,7&json=1
  107. public function getSearchList()
  108. {
  109. $data = $this->getSearch();
  110. $result = array();
  111. if ($data && $data['cate']) {
  112. $result[0]['id'] = -1;
  113. $result[0]['name'] = '分类';
  114. $result[0]['option'] = $data['cate'];
  115. $result = array_merge($result, $data['search']);
  116. }
  117. return $result;
  118. }
  119. public function getCateInfo()
  120. {
  121. $cate = $this->cate ? $this->cate : $this->top;
  122. $data = Dever::db('category/info')->one($cate);
  123. $name = Dever::input('name');
  124. if ($name) {
  125. $data['name'] = urldecode($name);
  126. }
  127. return $data;
  128. }
  129. # 获取分类子信息
  130. public function getCateChild()
  131. {
  132. $data = Dever::load('category/api')->getChild($this->top);
  133. if ($data) {
  134. $data = $data[$this->top];
  135. foreach ($data as $k => $v) {
  136. $data[$k]['name'] = '我要' . $v['name'];
  137. $path = 'submit_next?top=' . $this->top . '&cate=' . $v['id'] . '&name=' . urlencode($data[$k]['name']);
  138. $data[$k]['link'] = Dever::url('main/' . $path);
  139. }
  140. }
  141. return $data;
  142. }
  143. # 根据顶级分类或者二级分类,获取需求列表 分类列表页用到
  144. # 接口:http://192.168.33.10/info/demand/?l=data.getList&top=1&cate=3,7&json=1&&attr_5=1&attr_2=9&attr_4=1
  145. public function getList($limit, $cate = array())
  146. {
  147. $this->checkInfo();
  148. $page = false;
  149. if (!$limit) {
  150. $page = true;
  151. }
  152. $where = Dever::preInput('attr_');
  153. $this->info->setViewPath($this->view_path);
  154. if ($where) {
  155. $data = $this->info->getDataByAttr($this->cate, $where);
  156. } else {
  157. $data = $this->info->getData($this->cate, $limit, $page);
  158. }
  159. if (Dever::input('test') == 1) {
  160. return $data;
  161. }
  162. if (!$cate) {
  163. $cate = Dever::db('category/info')->one($this->top);
  164. }
  165. $data = array('data' => $data, 'cate' => $cate, 'top' => $this->top);
  166. $data['show'] = false;
  167. if ($cate && $this->top < 0) {
  168. $data['show'] = true;
  169. }
  170. return Dever::render('render/list', $data, 'main');
  171. }
  172. # 获取当前用户的需求列表
  173. # 接口:http://192.168.33.10/info/demand/?l=data.getUserList&top=-1
  174. public function getUserList($limit)
  175. {
  176. $this->checkInfo();
  177. $data = $this->info->getDataByUid($this->data['uid']);
  178. return $data;
  179. }
  180. # 获取需求详情
  181. # 接口:http://192.168.33.10/info/demand/?l=data.getInfo&top=1&json=1&id=10000000
  182. public function getInfo()
  183. {
  184. if (!$this->id) {
  185. Dever::alert('错误的参数,请重试');
  186. }
  187. $this->checkInfo();
  188. $data = $this->info->getInfo($this->id);
  189. $data['like'] = 2;
  190. $data['tip'] = 2;
  191. # 检查当前用户是否已收藏、已举报
  192. if ($this->data['uid'] && $this->data['uid'] > 0) {
  193. $data['like'] = Dever::load('service/lib/like')->get($this->data['uid'], $this->id, 1);
  194. }
  195. return $data;
  196. }
  197. # 获取需求详情
  198. # 接口:http://192.168.33.10/info/demand/?l=data.getInfo&top=1&json=1&id=10000000
  199. public function getInfoPic()
  200. {
  201. $data = Dever::load('res/data.getInfo');
  202. if ($data['pic']) {
  203. return $data['pic'];
  204. }
  205. return false;
  206. }
  207. # 获取需求详情
  208. # 接口:http://192.168.33.10/info/demand/?l=data.getInfo&top=1&json=1&id=10000000
  209. public function getInfoAttr()
  210. {
  211. $data = Dever::load('res/data.getInfo');
  212. if ($data['attr']) {
  213. return $data['attr'];
  214. }
  215. return array();
  216. }
  217. # 获取更新需求时的字段
  218. public function getUpdate()
  219. {
  220. $this->checkInfo();
  221. if ($this->cate) {
  222. $cate = $this->cate;
  223. $category = $this->top . ',' . $cate;
  224. } else {
  225. $cate = $this->top;
  226. $category = $this->top;
  227. }
  228. $cate = $this->cate ? $this->cate : $this->top;
  229. $data = Dever::load('category/api')->getChild($cate);
  230. if ($data) {
  231. $data = $data[$cate];
  232. }
  233. $data = array('cate' => $data, 'category' => $category);
  234. # 获取当前分类的属性
  235. $attr = $this->info->getAttr($category);
  236. $data['attr'] = '';
  237. $data['attr_id'] = array();
  238. if ($attr) {
  239. foreach ($attr as $k => $v) {
  240. $data['attr_id'][] = $v['id'];
  241. }
  242. $data['attr_id'] = implode(',', $data['attr_id']);
  243. $data['attr'] = Dever::render('render/submit_attr', array('attr' => $attr), 'main');
  244. }
  245. return Dever::render('render/submit', $data, 'main');
  246. }
  247. # 获取分类下的属性
  248. public function getAttr()
  249. {
  250. $category = Dever::input('category');
  251. $attr_id = Dever::input('attr_id');
  252. $attr = $this->info->getAttr($category);
  253. if ($attr) {
  254. $attr_id = explode(',', $attr_id);
  255. foreach ($attr as $k => $v) {
  256. if (in_array($v['id'], $attr_id)) {
  257. unset($attr[$k]);
  258. }
  259. }
  260. if ($attr) {
  261. return Dever::render('render/submit_attr', array('attr' => $attr), 'main');
  262. }
  263. }
  264. echo '';die;
  265. }
  266. # 更新或者新增需求
  267. # 接口:http://192.168.33.10/info/demand/?l=data.update&top=1&json=1&id=10000000&name=test&cate=1,3,9&attr_2=1
  268. public function update()
  269. {
  270. $this->checkInfo();
  271. if ($this->cate) {
  272. $cate = $this->top . ',' . $this->cate;
  273. } else {
  274. $cate = $this->top;
  275. }
  276. $data['name'] = Dever::input('name');
  277. if (!$data['name']) {
  278. Dever::alert('请输入标题');
  279. }
  280. $data['pic'] = Dever::input('pic');
  281. $data['view_price'] = Dever::input('view_price');
  282. $this->info->setViewPath($this->view_path);
  283. $data = $this->info->update($this->top, $this->cate, $data, $this->id, $this->data['uid']);
  284. # 提交信息之后,验证是否需要支付
  285. $url = $data['info']['view_link'];
  286. return $url;
  287. }
  288. private function checkInfo()
  289. {
  290. if (!$this->info) {
  291. Dever::alert('错误的参数,请重试');
  292. }
  293. }
  294. }