Core.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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')->one($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. if (isset($param['code']) && $param['code']) {
  176. $param['code'] = Dever::load('collection/lib/common')->encode($param['code']);
  177. }
  178. $url .= '?' . http_build_query($param);
  179. }
  180. return $url;
  181. }
  182. protected function getInfoCode()
  183. {
  184. # 获取最新的用户记录
  185. $where['info_id'] = $this->id;
  186. $where['uid'] = $this->uid;
  187. $record = Dever::db('user/record')->one($where);
  188. if ($record) {
  189. $parent_page_id = $record['parent_page_id'];
  190. $page_id = $record['page_id'];
  191. $times = $record['times_id'];
  192. $index = $record['index'];
  193. $day = $record['day'];
  194. } else {
  195. # 获取最新的章节页id
  196. $page_where['info_id'] = $this->id;
  197. $page_where = $this->getTimesId($page_where);
  198. $page = Dever::db('collection/page')->child($page_where);
  199. $parent_page_id = $page_id = 0;
  200. if ($page) {
  201. $parent_page_id = $page[0]['page_id'];
  202. $page_id = $page[0]['id'];
  203. } else {
  204. Dever::alert('内容还没有准备好');
  205. }
  206. $times = 0;
  207. if ($this->times) {
  208. $times = $this->times;
  209. }
  210. $index = 0;
  211. $day = 0;
  212. }
  213. if (!$day) {
  214. # 从用户的时光获取
  215. $my_times = Dever::load('user/lib/times')->getData($this->uid, $this->id);
  216. if ($my_times && $my_times['day']) {
  217. $day = str_replace('-', '', $my_times['day']);
  218. }
  219. }
  220. # 获取门票
  221. $ticket = Dever::load('user/lib/ticket')->check($this->uid, $this->id);
  222. if ($ticket) {
  223. $this->ticket_id = $ticket['id'];
  224. }
  225. # 获取code
  226. $code = $this->getCode($this->id, $parent_page_id, $page_id, $index, $times, $day, $this->uid, $this->ticket_id);
  227. return $code;
  228. }
  229. # 获取时光id
  230. protected function getTimesId($where)
  231. {
  232. # 获取当前年份的最新的时光
  233. if (!$this->times) {
  234. $where['year'] = date('Y');
  235. $times = Dever::db('collection/times')->getNew($where);
  236. if ($times) {
  237. $this->times = $times['id'];
  238. }
  239. }
  240. if ($this->times) {
  241. $where['times_id'] = $this->times;
  242. }
  243. return $where;
  244. }
  245. # 获取按钮的样式
  246. protected function button($info)
  247. {
  248. $button = array(
  249. 'bgcolor' => 'background: linear-gradient(to right, #000000,#000000) !important;color:#fff;',
  250. 'color' => 'color:#fff',
  251. 'name' => array('入口', '排行榜')
  252. );
  253. $button['name'] = explode(',', $info['button']);
  254. if ($info['button_bgcolor'] && $info['button_bgjcolor']) {
  255. $button['bgcolor'] = 'background: linear-gradient(to right, '.$info['button_bgcolor'].','.$info['button_bgjcolor'].') !important;';
  256. }
  257. if ($info['button_ftcolor']) {
  258. $button['color'] = 'color:' . $info['button_ftcolor'];
  259. $button['bgcolor'] .= $button['color'];
  260. }
  261. return $button;
  262. }
  263. # 获取其他页面的分享
  264. protected function getConfigShare($config = array())
  265. {
  266. $config = $config ? $config : Dever::db('main/config')->one();
  267. $config['desc'] = $config['info'];
  268. $config['pic_cover'] = $config['logo'];
  269. return $this->getInfoShare($config);
  270. }
  271. # 获取合集的分享
  272. protected function getInfoShare($info, $code = false)
  273. {
  274. if ($this->uid && $this->uid > 0) {
  275. $data['title'] = $info['share_title'] ? $info['share_title'] : '{username}邀请你看{name}';
  276. $data['title'] = str_replace('{username}', $this->user['username'], $data['title']);
  277. $data['title'] = str_replace('{name}', $info['name'], $data['title']);
  278. } else {
  279. $data['title'] = $info['name'];
  280. }
  281. $data['content'] = $info['share_content'] ? $info['share_content'] : $info['desc'];
  282. $data['pic'] = $info['share_pic'] ? $info['share_pic'] : $info['pic_cover'];
  283. $url = Dever::input('url');
  284. $data['source_link'] = '';
  285. $url = $this->getUrl($url);
  286. $data['link'] = $url;
  287. return $data;
  288. }
  289. }