Core.php 7.4 KB

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