Core.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. namespace Collection\Lib;
  3. use Dever;
  4. class Core
  5. {
  6. protected $token;
  7. protected $uid;
  8. protected $share_uid = 0;
  9. protected $user;
  10. protected $year;
  11. protected $times;
  12. protected $day = 0;
  13. protected $parent_page_id;
  14. protected $page_id;
  15. protected $info;
  16. protected $key = 'dreamland1985';
  17. /*
  18. # 定义全局的系统类型
  19. 'system_source' => array
  20. (
  21. 1 => 'H5',
  22. 2 => '安卓',
  23. 3 => 'ios',
  24. 4 => 'pc',
  25. 5 => '微信小程序',
  26. 6 => '微信公众号',
  27. ),
  28. */
  29. protected $system_source = 1;
  30. public function __construct()
  31. {
  32. $this->checkCode();
  33. # 获取当前的用户信息
  34. $this->token = Dever::input('token');
  35. $this->uid = 1;
  36. $this->user = $this->getUser($this->uid, $this->id);
  37. $this->user['id'] = $this->uid;
  38. $this->system_source = Dever::input('system_source', 1);
  39. }
  40. # 获取用户信息
  41. protected function getUser($uid, $collection_id)
  42. {
  43. return Dever::load('user/lib/info')->get($uid, $collection_id);
  44. }
  45. # 检查内容
  46. protected function checkInfo()
  47. {
  48. $this->info = Dever::db('collection/info')->one($this->id);
  49. if (!$this->info) {
  50. Dever::alert('内容还没有准备好');
  51. }
  52. return $this->info;
  53. }
  54. # 检查是否可以查看
  55. protected function checkView()
  56. {
  57. if (!$this->info) {
  58. $this->checkInfo();
  59. }
  60. if ($this->info['buy'] == 1) {
  61. # 收费 从订单中读取是否已经购买
  62. $this->info['buy_data'] = Dever::load('user/lib/collection')->check($this->uid, $this->info['id']);
  63. return $this->info['buy_data'] ? true : false;
  64. } else {
  65. # 免费
  66. return true;
  67. }
  68. }
  69. # 检测邀请码
  70. protected function checkCode()
  71. {
  72. $this->code = Dever::input('code');
  73. if ($this->code) {
  74. $code = Dever::decode($this->code, $this->key);
  75. $code = explode('_', $code);
  76. $this->id = $code[1];
  77. $this->parent_page_id = $code[2];
  78. $this->page_id = $code[3];
  79. $this->index = $code[4];
  80. $this->times = $code[5];
  81. if (isset($code[6]) && $code[6]) {
  82. $this->day = $code[6];
  83. } else {
  84. $this->day = 0;
  85. }
  86. if (isset($code[7]) && $code[7] && $code[7] != $this->uid) {
  87. $this->share_uid = $code[7];
  88. }
  89. } else {
  90. $this->id = Dever::input('id');
  91. }
  92. }
  93. # 获取邀请码
  94. protected function getCode($id, $parent_page_id, $page_id, $index, $times_id, $day = 0, $uid = 0)
  95. {
  96. if (Dever::project('invite')) {
  97. $invite = Dever::input('invite');
  98. $uid = Dever::load('invite/api')->getUid($invite);
  99. }
  100. if (!$day) {
  101. $day = $this->day;
  102. }
  103. if ($day) {
  104. if (strstr($day, '-')) {
  105. $day = str_replace('-', '', $day);
  106. }
  107. } else {
  108. $day = 0;
  109. }
  110. $key = 'dlv1_' . $id. '_' . $parent_page_id . '_' . $page_id . '_' . $index . '_' . $times_id . '_' . $day . '_' . $uid;
  111. $code = Dever::encode($key, $this->key);
  112. return $code;
  113. }
  114. protected function getInfoCode()
  115. {
  116. # 获取最新的用户记录
  117. $where['info_id'] = $this->id;
  118. $where['uid'] = $this->uid;
  119. $record = Dever::db('user/record')->one($where);
  120. if ($record) {
  121. $parent_page_id = $record['parent_page_id'];
  122. $page_id = $record['page_id'];
  123. $times = $record['times_id'];
  124. $index = $record['index'];
  125. $day = $record['day'];
  126. } else {
  127. # 获取最新的章节页id
  128. $page_where['info_id'] = $this->id;
  129. $page_where = $this->getTimesId($page_where);
  130. $page = Dever::db('collection/page')->child($page_where);
  131. $parent_page_id = $page_id = 0;
  132. if ($page) {
  133. $parent_page_id = $page[0]['page_id'];
  134. $page_id = $page[0]['id'];
  135. } else {
  136. Dever::alert('内容还没有准备好');
  137. }
  138. $times = 0;
  139. if ($this->times) {
  140. $times = $this->times;
  141. }
  142. $index = 0;
  143. $day = 0;
  144. }
  145. # 获取code
  146. $code = $this->getCode($this->id, $parent_page_id, $page_id, $index, $times, $day);
  147. return $code;
  148. }
  149. # 获取时光id
  150. protected function getTimesId($where)
  151. {
  152. # 获取当前年份的最新的时光
  153. if (!$this->times) {
  154. $where['year'] = date('Y');
  155. $times = Dever::db('collection/times')->getNew($where);
  156. if ($times) {
  157. $this->times = $times['id'];
  158. }
  159. }
  160. if ($this->times) {
  161. $where['times_id'] = $this->times;
  162. }
  163. return $where;
  164. }
  165. # 获取按钮的样式
  166. protected function button($info)
  167. {
  168. $button = array(
  169. 'bgcolor' => 'background: linear-gradient(to right, #000000,#000000) !important;color:#fff;',
  170. 'color' => 'color:#fff',
  171. 'name' => array('入口', '排行榜')
  172. );
  173. $button['name'] = explode(',', $info['button']);
  174. if ($info['button_bgcolor'] && $info['button_bgjcolor']) {
  175. $button['bgcolor'] = 'background: linear-gradient(to right, '.$info['button_bgcolor'].','.$info['button_bgjcolor'].') !important;';
  176. }
  177. if ($info['button_ftcolor']) {
  178. $button['color'] = 'color:' . $info['button_ftcolor'];
  179. $button['bgcolor'] .= $button['color'];
  180. }
  181. return $button;
  182. }
  183. }