Api.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. <?php
  2. namespace Collection\Src;
  3. use Dever;
  4. use Collection\Lib\Core;
  5. class Api extends Core
  6. {
  7. public function __construct()
  8. {
  9. parent::__construct();
  10. if (!$this->id) {
  11. Dever::alert('错误的参数');
  12. }
  13. }
  14. public function data()
  15. {
  16. return Dever::db('collection/info')->state();
  17. }
  18. # 获取用户信息
  19. public function getUser()
  20. {
  21. $data['user'] = $this->user;
  22. $data['num'] = array
  23. (
  24. array('¥199', '', '钱包'),
  25. array('200', '', '金币'),
  26. array('10', '', '好友'),
  27. array('30', '', '动态'),
  28. );
  29. return $data;
  30. }
  31. public function qrcode($url, $content_id)
  32. {
  33. $file = 'shareDv_' . $this->uid . '_' . $this->id . '_' . $content_id;
  34. $file = Dever::load('qrcode/api')->qrcode($url, $file, $this->user['avatar']);
  35. return $file['url'];
  36. }
  37. # 获取分享的内容
  38. public function getShare()
  39. {
  40. $url = Dever::input('url');
  41. $type = Dever::input('type');
  42. $index = Dever::input('index');
  43. $content_id = Dever::input('content_id');
  44. $data = array();
  45. $info = Dever::db('collection/info')->one($this->id);
  46. $data['info'] = $data['pic'] = $data['text'] = $data['share'] = array();
  47. $data['info']['id'] = $content_id;
  48. $data['info']['platform_name'] = $info['name'];
  49. $data['info']['qrcode'] = $data['info']['name'] = $data['info']['price'] = $data['info']['s_price'] = '';
  50. $code = $this->getCode($this->id, $this->parent_page_id, $this->page_id, $index, $this->times, $this->uid);
  51. $url .= '?code=' . $code;
  52. $data['info']['qrcode'] = $this->qrcode($url, $content_id);
  53. $background = array
  54. (
  55. '#ffaa00',
  56. '#bababa'
  57. );
  58. if ($type == -1) {
  59. $page = Dever::db('collection/page')->one($content_id);
  60. $data['info']['name'] = $page['name'];
  61. $data['pic'] = explode(',', $page['pic']);
  62. $data['text'] = array(
  63. array
  64. (
  65. 'background' => $background[0],
  66. 'content' => $page['desc'],
  67. )
  68. );
  69. $data['share'] = array
  70. (
  71. 'title' => $page['name'],
  72. 'content' => $page['desc'],
  73. 'pic' => $page['pic'],
  74. );
  75. } else {
  76. $content = Dever::db('collection/content')->one($content_id);
  77. $data['info']['name'] = $content['share_title'] ? $content['share_title'] : $content['name'];
  78. if ($content['share_pic']) {
  79. $data['pic'] = explode(',', $content['share_pic']);
  80. } else {
  81. $data['pic'] = explode(',', $content['pic']);
  82. }
  83. $content['share_content'] = $content['share_content'] ? $content['share_content'] : $content['name'];
  84. if ($content['type'] == 70) {
  85. $content['data'] = Dever::db('content/product')->one(array('content_id' => $content['id'], 'info_id' => $content['info_id']));
  86. if (isset($content['data']['goods_id']) && $content['data']['goods_id'] > 0) {
  87. $goods = Dever::load('goods/lib/info')->getInfo($content['data']['goods_id']);
  88. if ($goods && isset($goods['price']['min']['price'])) {
  89. $data['info']['price'] = '¥' . $goods['price']['min']['price'];
  90. $data['info']['s_price'] = '¥' . $goods['price']['min']['s_price'];
  91. }
  92. }
  93. }
  94. if ($content['copy_text']) {
  95. $text = Dever::array_decode($content['copy_text']);
  96. $i = 0;
  97. foreach ($text as $k => $v) {
  98. if ($v['name']) {
  99. $back = $background[1];
  100. if ($i == 0) {
  101. $back = $background[0];
  102. }
  103. $data['text'][$i] = array
  104. (
  105. 'background' => $back,
  106. 'content' => $v['name'],
  107. );
  108. $i++;
  109. }
  110. }
  111. }
  112. if (!$data['text']) {
  113. $data['text'] = array(
  114. array
  115. (
  116. 'background' => $background[0],
  117. 'content' => $content['share_content'],
  118. )
  119. );
  120. }
  121. $data['share'] = array
  122. (
  123. 'title' => $data['info']['name'],
  124. 'content' => $content['share_content'],
  125. 'pic' => $data['pic'][0],
  126. );
  127. }
  128. return $data;
  129. }
  130. # 获取合集内容
  131. public function getInfo()
  132. {
  133. # 是否已经购买
  134. $data['is_buy'] = false;
  135. $data['button'] = array(
  136. 'bgcolor' => 'background: linear-gradient(to right, #000000,#000000) !important;color:#fff;',
  137. 'color' => 'color:#fff',
  138. 'name' => array('入口', '排行榜')
  139. );
  140. $data['info'] = Dever::db('collection/info')->one($this->id);
  141. if ($data['info']) {
  142. $data['info']['user'] = array();
  143. if ($data['info']['uid'] > 0) {
  144. $data['info']['user'] = Dever::load('passport/api')->info($data['info']['uid']);
  145. }
  146. $data['info']['pic_bg'] = explode(',', $data['info']['pic_bg']);
  147. if ($data['info']['buy'] == 1) {
  148. # 收费 从订单中读取是否已经购买
  149. $data['is_buy'] = false;
  150. } else {
  151. # 免费
  152. $data['is_buy'] = true;
  153. }
  154. $data['button']['name'] = explode(',', $data['info']['button']);
  155. if ($data['info']['button_bgcolor'] && $data['info']['button_bgjcolor']) {
  156. $data['button']['bgcolor'] = 'background: linear-gradient(to right, '.$data['info']['button_bgcolor'].','.$data['info']['button_bgjcolor'].') !important;';
  157. }
  158. if ($data['info']['button_ftcolor']) {
  159. $data['button']['color'] = 'color:' . $data['info']['button_ftcolor'];
  160. $data['button']['bgcolor'] .= $data['button']['color'];
  161. }
  162. # 获取最新的用户记录
  163. $where['info_id'] = $this->id;
  164. $where['uid'] = $this->uid;
  165. $record = Dever::db('collection/user_record')->one($where);
  166. if ($record) {
  167. $parent_page_id = $record['parent_page_id'];
  168. $page_id = $record['page_id'];
  169. $times = $record['times_id'];
  170. $index = $record['index'];
  171. } else {
  172. # 获取最新的章节页id
  173. $page_where['info_id'] = $data['info']['id'];
  174. $page_where = $this->getTimesId($page_where);
  175. $page = Dever::db('collection/page')->child($page_where);
  176. $parent_page_id = $page_id = 0;
  177. if ($page) {
  178. $parent_page_id = $page[0]['page_id'];
  179. $page_id = $page[0]['id'];
  180. } else {
  181. Dever::alret('内容还没有准备好');
  182. }
  183. $times = 0;
  184. if ($this->times) {
  185. $times = $this->times;
  186. }
  187. $index = 0;
  188. }
  189. # 获取code
  190. $data['code'] = $this->getCode($data['info']['id'], $parent_page_id, $page_id, $index, $times);
  191. }
  192. return $data;
  193. }
  194. # 获取合集下的内容
  195. public function getContent()
  196. {
  197. $data['info_id'] = $this->id;
  198. $data['page_id'] = $this->page_id;
  199. $data['parent_page_id'] = $this->parent_page_id;
  200. $data['index'] = $this->index;
  201. $data['times_id'] = $this->times;
  202. # 获取用户信息
  203. $data['user'] = $this->user;
  204. # 获取每个模块
  205. $data['items'] = Dever::load('collection/lib/content')->getList($this->id, $this->page_id);
  206. # 滑动到多少时,加载下一页
  207. $data['total'] = count($data['items']) - 2;
  208. $data['func'] = $this->getFunc();
  209. return $data;
  210. }
  211. # 获取合集下的购买内容
  212. public function getBuy()
  213. {
  214. $data['info'] = Dever::db('collection/info')->one($this->id);
  215. $where['info_id'] = $this->id;
  216. $data['buy'] = Dever::db('collection/buy')->getAll($where);
  217. return $data;
  218. }
  219. # 获取合集下的榜单列表
  220. public function getRanking()
  221. {
  222. $where['info_id'] = $this->id;
  223. $data['ranking'] = Dever::db('collection/ranking')->getAll($where);
  224. return $data;
  225. }
  226. # 获取合集下的商品
  227. public function getProduct()
  228. {
  229. $where['info_id'] = $this->id;
  230. $data['product'] = Dever::db('collection/product')->getAll($where);
  231. return $data;
  232. }
  233. # 获取某一个商品详情
  234. public function getProductOne()
  235. {
  236. $where['id'] = Dever::input('product_id');
  237. $table = Dever::input('table');
  238. $data['product'] = Dever::db($table)->one($where);
  239. if ($data['product'] && isset($data['product']['goods_id']) && $data['product']['goods_id'] > 0) {
  240. $data['product']['goods'] = Dever::load('goods/lib/info')->getInfo($data['product']['goods_id']);
  241. $data['product']['goods'] = Dever::load('collection/lib/content')->getContent($data['product']['goods']);
  242. }
  243. return $data;
  244. }
  245. # 获取榜单数据
  246. public function getRankingData()
  247. {
  248. $where['info_id'] = $this->id;
  249. $where['ranking_id'] = Dever::input('ranking_id');
  250. # 获取所有期数
  251. $data['ranking_periods'] = Dever::db('collection/ranking_periods')->getAll($where);
  252. if ($data['ranking_periods']) {
  253. $where['periods_id'] = Dever::input('ranking_periods', $data['ranking_periods'][0]['id']);
  254. $data['ranking_data'] = Dever::db('collection/ranking_data')->getAll($where);
  255. if ($data['ranking_data']) {
  256. foreach ($data['ranking_data'] as $k => $v) {
  257. $data['ranking_data'][$k]['user'] = Dever::load('passport/api')->info($v['uid']);
  258. }
  259. }
  260. }
  261. return $data;
  262. }
  263. # 获取合集下的分类
  264. public function getCategory()
  265. {
  266. $parent_page_id = $this->parent_page_id;
  267. $page_id = $this->page_id;
  268. $where['info_id'] = $this->id;
  269. $data['cate'] = Dever::db('collection/page')->main($where);
  270. if ($data['cate']) {
  271. $content = Dever::db('collection/content')->child($where);
  272. $page = Dever::db('collection/page')->one($page_id);
  273. # 步长
  274. $data['num'] = 0;
  275. if ($page && $page['desc']) {
  276. $data['num'] = 1;
  277. }
  278. $where = $this->getTimesId($where);
  279. $data['child'] = Dever::db('collection/page')->child($where);
  280. $data['show'] = array();
  281. foreach ($data['cate'] as $k => $v) {
  282. if ($v['id'] == $parent_page_id) {
  283. $data['show'][$v['id']] = 1;
  284. } else {
  285. $data['show'][$v['id']] = 2;
  286. }
  287. }
  288. foreach ($data['child'] as $k => $v) {
  289. if (isset($content[$v['id']])) {
  290. $data['child'][$k]['content'] = $content[$v['id']];
  291. }
  292. }
  293. }
  294. return $data;
  295. }
  296. # 获取合集下的时光
  297. public function getTimes()
  298. {
  299. $where['info_id'] = $this->id;
  300. $data['cur'] = $this->times;
  301. $data['times'] = Dever::db('collection/times')->main($where);
  302. if ($data['times']) {
  303. foreach ($data['times'] as $k => $v) {
  304. $where['times_id'] = $v['id'];
  305. $data['times'][$k]['child'] = Dever::db('collection/times')->child($where);
  306. }
  307. }
  308. return $data;
  309. }
  310. # 根据时光id获取pageid
  311. public function getPageId()
  312. {
  313. $where['info_id'] = $this->id;
  314. $where['times_id'] = Dever::input('times_id');
  315. $page = Dever::db('collection/page')->getOne($where);
  316. if ($page) {
  317. # 获取code
  318. $data['code'] = $this->getCode($this->id, $page['page_id'], $page['id'], 0, $page['times_id']);
  319. return $data;
  320. }
  321. Dever::alert('error');
  322. }
  323. # 记录用户操作
  324. public function record()
  325. {
  326. $where['info_id'] = $this->id;
  327. $where['uid'] = $this->uid;
  328. $record = Dever::db('collection/user_record')->one($where);
  329. $where['parent_page_id'] = $this->parent_page_id;
  330. $where['page_id'] = $this->page_id;
  331. $where['times_id'] = $this->times;
  332. $where['content_id'] = Dever::input('content_id');
  333. $where['index'] = Dever::input('index', 0);
  334. if ($record) {
  335. $where['where_id'] = $record['id'];
  336. Dever::db('collection/user_record')->update($where);
  337. } else {
  338. Dever::db('collection/user_record')->insert($where);
  339. }
  340. return 'yes';
  341. }
  342. public function ding($data)
  343. {
  344. if (isset($data['num_add_buy']) && isset($data['num_ratio_buy'])) {
  345. if ($data['buy'] == 1) {
  346. /*
  347. if (!$data['num_buy']) {
  348. $stat = Dever::db('stat/collection')->getNumBycollection(array('collection_id' => $data['id']));
  349. if (!$stat) {
  350. $stat['buy_num'] = 0;
  351. }
  352. if (!$stat['buy_num']) {
  353. $stat['buy_num'] = 0;
  354. }
  355. $data['num_buy'] = $stat['buy_num'];
  356. }
  357. */
  358. $data['num_up'] = 0;
  359. } else {
  360. $data['num_up'] = 0;
  361. }
  362. $data['num_buy'] = $data['num_buy'] + $data['num_up'];
  363. $data['buy_num'] = $data['num_buy'];
  364. $data['num_buy'] = round(($data['num_buy']+$data['num_add_buy'])*$data['num_ratio_buy']);
  365. }
  366. return $data;
  367. }
  368. # 获取功能
  369. private function getFunc()
  370. {
  371. $where['info_id'] = $this->id;
  372. $func = Dever::db('collection/function')->getAll($where);
  373. $result = array();
  374. if ($func) {
  375. foreach ($func as $k => $v) {
  376. $v['direction'] = strtolower($v['direction']);
  377. if ($v['direction'] == 'left') {
  378. $v['width'] = '86';
  379. } else {
  380. $v['width'] = '100';
  381. }
  382. $result[$v['type']] = array
  383. (
  384. 'show' => false,
  385. 'index' => 5,
  386. 'top' => 0,
  387. 'padding' => '0rpx',
  388. 'width' => $v['width'] . '%',
  389. 'direction' => strtolower($v['direction']),
  390. 'param' => array(),
  391. 'button' => array
  392. (
  393. 'bgColor' => $v['bgcolor'],
  394. 'text' => $v['name'],
  395. 'fontSize' => 28,
  396. 'color' => $v['color'],
  397. ),
  398. );
  399. }
  400. }
  401. return $result;
  402. }
  403. # 获取时光id
  404. private function getTimesId($where)
  405. {
  406. # 获取当前年份的最新的时光
  407. if (!$this->times) {
  408. $where['year'] = date('Y');
  409. $times = Dever::db('collection/times')->getNew($where);
  410. if ($times) {
  411. $this->times = $times['id'];
  412. }
  413. }
  414. if ($this->times) {
  415. $where['times_id'] = $this->times;
  416. }
  417. return $where;
  418. }
  419. }