Core.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <?php
  2. namespace Collection\Lib;
  3. use Dever;
  4. class Core
  5. {
  6. protected $token;
  7. protected $uid;
  8. protected $id;
  9. protected $id_code;
  10. protected $share_uid = 0;
  11. protected $ticket_id = 0;
  12. protected $user;
  13. protected $year;
  14. protected $times;
  15. protected $day = 0;
  16. protected $parent_page_id;
  17. protected $page_id;
  18. protected $info;
  19. /*
  20. # 定义全局的系统类型
  21. 'system_source' => array
  22. (
  23. 1 => 'H5',
  24. 2 => '安卓',
  25. 3 => 'ios',
  26. 4 => 'pc',
  27. 5 => '微信小程序',
  28. 6 => '微信公众号',
  29. ),
  30. */
  31. protected $system_source = 1;
  32. public function __construct()
  33. {
  34. # 获取当前的用户信息
  35. $this->token = Dever::input('token');
  36. $this->uid = Dever::load('passport/user')->check(false);
  37. $this->checkCode();
  38. $this->user = $this->getUser($this->uid, $this->id);
  39. $this->user['id'] = $this->uid;
  40. $this->system_source = Dever::input('system_source', 1);
  41. }
  42. # 获取用户信息
  43. protected function getUser($uid, $collection_id)
  44. {
  45. return Dever::load('user/lib/info')->get($uid, $collection_id, true);
  46. }
  47. # 检查内容
  48. protected function checkInfo()
  49. {
  50. $this->info = $this->getOne($this->id);
  51. if (!$this->info) {
  52. Dever::alert('内容还没有准备好');
  53. }
  54. return $this->info;
  55. }
  56. # 获取内容
  57. protected function getOne($id)
  58. {
  59. $info = Dever::load('collection/lib/info')->num($id);
  60. if ($info) {
  61. $this->handle($info);
  62. }
  63. return $info;
  64. }
  65. protected function handle(&$info)
  66. {
  67. # 过滤一些不用的信息
  68. unset($info['num_buy']);
  69. unset($info['num_ratio_buy']);
  70. unset($info['num_add_buy']);
  71. unset($info['num_add_view']);
  72. unset($info['num_view']);
  73. unset($info['buy_price']);
  74. unset($info['audit']);
  75. unset($info['status']);
  76. unset($info['reorder']);
  77. unset($info['admin_founder']);
  78. unset($info['admin_editor']);
  79. unset($info['udate']);
  80. unset($info['state']);
  81. unset($info['cdate']);
  82. unset($info['score_type']);
  83. unset($info['score_name']);
  84. $this->id_code = $info['id_code'] = Dever::load('collection/lib/common')->encode($info['id']);
  85. }
  86. # 检查是否可以查看
  87. protected function checkView()
  88. {
  89. if ($this->uid < 1) {
  90. return false;
  91. }
  92. if (!$this->info) {
  93. $this->checkInfo();
  94. }
  95. if ($this->info['buy'] == 1) {
  96. # 收费 获取是否已经拥有
  97. $this->info['buy_data'] = Dever::load('user/lib/collection')->check($this->uid, $this->info['id']);
  98. return $this->info['buy_data'] ? true : false;
  99. } else {
  100. # 免费
  101. return true;
  102. }
  103. }
  104. # 检测邀请码
  105. protected function checkCode()
  106. {
  107. $this->code = Dever::input('code');
  108. if ($this->code) {
  109. $code = Dever::load('collection/lib/common')->decode($this->code);
  110. $code = explode('_', $code);
  111. $this->id = $code[1];
  112. $this->parent_page_id = $code[2];
  113. $this->page_id = $code[3];
  114. $this->index = $code[4] ? $code[4] : 0;
  115. $this->times = $code[5];
  116. if (isset($code[6]) && $code[6]) {
  117. # 用户设置时间
  118. $this->day = $code[6];
  119. } else {
  120. $this->day = 0;
  121. }
  122. if (isset($code[7]) && $code[7] && $code[7] != $this->uid) {
  123. $this->share_uid = $code[7];
  124. if (isset($code[8]) && $code[8]) {
  125. $this->ticket_id = $code[8];
  126. }
  127. }
  128. } else {
  129. $this->id_code = Dever::input('id');
  130. if ($this->id_code == -1) {
  131. $this->id = $this->id_code;
  132. } else {
  133. $this->id = Dever::load('collection/lib/common')->decode($this->id_code);
  134. }
  135. }
  136. if ($this->share_uid == $this->uid) {
  137. $this->share_uid = 0;
  138. }
  139. }
  140. # 获取邀请码
  141. protected function getCode($id, $parent_page_id, $page_id, $index, $times_id, $day = 0, $uid = 0, $ticket_id = 0, $encode = true)
  142. {
  143. if (Dever::project('invite')) {
  144. $invite = Dever::input('invite');
  145. if ($invite) {
  146. $uid = Dever::load('invite/api')->getUid($invite);
  147. }
  148. }
  149. if (!$day) {
  150. $day = $this->day;
  151. }
  152. if ($day) {
  153. if (strstr($day, '-')) {
  154. $day = str_replace('-', '', $day);
  155. }
  156. } else {
  157. $day = 0;
  158. }
  159. $code = 'dlv1_' . $id. '_' . $parent_page_id . '_' . $page_id . '_' . $index . '_' . $times_id . '_' . $day . '_' . $uid . '_' . $ticket_id;
  160. if ($encode) {
  161. $code = Dever::load('collection/lib/common')->encode($code);
  162. }
  163. return $code;
  164. }
  165. #获取短域名
  166. public function getUrl($url, $param = array())
  167. {
  168. if (Dever::project('short')) {
  169. # 转成短域名
  170. if (isset($param['code']) && $param['code']) {
  171. $param['code'] .= '|collection/lib/common.encode';
  172. }
  173. $url = Dever::load('short/lib/handle')->get(1, $url, $param);
  174. } elseif ($param) {
  175. $url .= http_build_query($param);
  176. }
  177. return $url;
  178. }
  179. protected function getInfoCode()
  180. {
  181. # 获取最新的用户记录
  182. $where['info_id'] = $this->id;
  183. $where['uid'] = $this->uid;
  184. $record = Dever::db('user/record')->one($where);
  185. if ($record) {
  186. $parent_page_id = $record['parent_page_id'];
  187. $page_id = $record['page_id'];
  188. $times = $record['times_id'];
  189. $index = $record['index'];
  190. $day = $record['day'];
  191. } else {
  192. # 获取最新的章节页id
  193. $page_where['info_id'] = $this->id;
  194. $page_where = $this->getTimesId($page_where);
  195. $page = Dever::db('collection/page')->child($page_where);
  196. $parent_page_id = $page_id = 0;
  197. if ($page) {
  198. $parent_page_id = $page[0]['page_id'];
  199. $page_id = $page[0]['id'];
  200. } else {
  201. Dever::alert('内容还没有准备好');
  202. }
  203. $times = 0;
  204. if ($this->times) {
  205. $times = $this->times;
  206. }
  207. $index = 0;
  208. $day = 0;
  209. }
  210. if (!$day) {
  211. # 从用户的时光获取
  212. $my_times = Dever::load('user/lib/times')->getData($this->uid, $this->id);
  213. if ($my_times && $my_times['day']) {
  214. $day = str_replace('-', '', $my_times['day']);
  215. }
  216. }
  217. # 获取门票
  218. $ticket = Dever::load('user/lib/ticket')->check($this->uid, $this->id);
  219. if ($ticket) {
  220. $this->ticket_id = $ticket['id'];
  221. }
  222. # 获取code
  223. $code = $this->getCode($this->id, $parent_page_id, $page_id, $index, $times, $day, $this->uid, $this->ticket_id);
  224. return $code;
  225. }
  226. # 获取时光id
  227. protected function getTimesId($where)
  228. {
  229. # 获取当前年份的最新的时光
  230. if (!$this->times) {
  231. $where['year'] = date('Y');
  232. $times = Dever::db('collection/times')->getNew($where);
  233. if ($times) {
  234. $this->times = $times['id'];
  235. }
  236. }
  237. if ($this->times) {
  238. $where['times_id'] = $this->times;
  239. }
  240. return $where;
  241. }
  242. # 获取按钮的样式
  243. protected function button($info)
  244. {
  245. $button = array(
  246. 'bgcolor' => 'background: linear-gradient(to right, #000000,#000000) !important;color:#fff;',
  247. 'color' => 'color:#fff',
  248. 'name' => array('入口', '排行榜')
  249. );
  250. $button['name'] = explode(',', $info['button']);
  251. if ($info['button_bgcolor'] && $info['button_bgjcolor']) {
  252. $button['bgcolor'] = 'background: linear-gradient(to right, '.$info['button_bgcolor'].','.$info['button_bgjcolor'].') !important;';
  253. }
  254. if ($info['button_ftcolor']) {
  255. $button['color'] = 'color:' . $info['button_ftcolor'];
  256. $button['bgcolor'] .= $button['color'];
  257. }
  258. return $button;
  259. }
  260. }