Core.php 7.6 KB

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