Core.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 $user;
  12. protected $year;
  13. protected $times;
  14. protected $day = 0;
  15. protected $parent_page_id;
  16. protected $page_id;
  17. protected $info;
  18. protected $key = 'dreamland1985';
  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. $this->checkCode();
  35. # 获取当前的用户信息
  36. $this->token = Dever::input('token');
  37. $this->uid = 1;
  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);
  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_id']);
  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. }
  122. } else {
  123. $this->id_code = Dever::input('id');
  124. if ($this->id_code == -1) {
  125. $this->id = $this->id_code;
  126. } else {
  127. $this->id = Dever::decode($this->id_code, $this->key);
  128. }
  129. }
  130. }
  131. # 获取邀请码
  132. protected function getCode($id, $parent_page_id, $page_id, $index, $times_id, $day = 0, $uid = 0)
  133. {
  134. if (Dever::project('invite')) {
  135. $invite = Dever::input('invite');
  136. if ($invite) {
  137. $uid = Dever::load('invite/api')->getUid($invite);
  138. }
  139. }
  140. if (!$day) {
  141. $day = $this->day;
  142. }
  143. if ($day) {
  144. if (strstr($day, '-')) {
  145. $day = str_replace('-', '', $day);
  146. }
  147. } else {
  148. $day = 0;
  149. }
  150. $key = 'dlv1_' . $id. '_' . $parent_page_id . '_' . $page_id . '_' . $index . '_' . $times_id . '_' . $day . '_' . $uid;
  151. $code = Dever::encode($key, $this->key);
  152. return $code;
  153. }
  154. protected function getInfoCode()
  155. {
  156. # 获取最新的用户记录
  157. $where['info_id'] = $this->id;
  158. $where['uid'] = $this->uid;
  159. $record = Dever::db('user/record')->one($where);
  160. if ($record) {
  161. $parent_page_id = $record['parent_page_id'];
  162. $page_id = $record['page_id'];
  163. $times = $record['times_id'];
  164. $index = $record['index'];
  165. $day = $record['day'];
  166. } else {
  167. # 获取最新的章节页id
  168. $page_where['info_id'] = $this->id;
  169. $page_where = $this->getTimesId($page_where);
  170. $page = Dever::db('collection/page')->child($page_where);
  171. $parent_page_id = $page_id = 0;
  172. if ($page) {
  173. $parent_page_id = $page[0]['page_id'];
  174. $page_id = $page[0]['id'];
  175. } else {
  176. Dever::alert('内容还没有准备好');
  177. }
  178. $times = 0;
  179. if ($this->times) {
  180. $times = $this->times;
  181. }
  182. $index = 0;
  183. $day = 0;
  184. }
  185. # 获取code
  186. $code = $this->getCode($this->id, $parent_page_id, $page_id, $index, $times, $day, $this->uid);
  187. return $code;
  188. }
  189. # 获取时光id
  190. protected function getTimesId($where)
  191. {
  192. # 获取当前年份的最新的时光
  193. if (!$this->times) {
  194. $where['year'] = date('Y');
  195. $times = Dever::db('collection/times')->getNew($where);
  196. if ($times) {
  197. $this->times = $times['id'];
  198. }
  199. }
  200. if ($this->times) {
  201. $where['times_id'] = $this->times;
  202. }
  203. return $where;
  204. }
  205. # 获取按钮的样式
  206. protected function button($info)
  207. {
  208. $button = array(
  209. 'bgcolor' => 'background: linear-gradient(to right, #000000,#000000) !important;color:#fff;',
  210. 'color' => 'color:#fff',
  211. 'name' => array('入口', '排行榜')
  212. );
  213. $button['name'] = explode(',', $info['button']);
  214. if ($info['button_bgcolor'] && $info['button_bgjcolor']) {
  215. $button['bgcolor'] = 'background: linear-gradient(to right, '.$info['button_bgcolor'].','.$info['button_bgjcolor'].') !important;';
  216. }
  217. if ($info['button_ftcolor']) {
  218. $button['color'] = 'color:' . $info['button_ftcolor'];
  219. $button['bgcolor'] .= $button['color'];
  220. }
  221. return $button;
  222. }
  223. }