Core.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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 getParent()
  49. {
  50. if (!$this->share_uid) {
  51. $info = Dever::load('user/lib/collection')->check($this->uid, $this->id);
  52. if ($info && $info['source_uid']) {
  53. $this->share_uid = $info['source_uid'];
  54. }
  55. }
  56. }
  57. # 检查内容
  58. protected function checkInfo()
  59. {
  60. $this->info = $this->getOne($this->id);
  61. if (!$this->info) {
  62. Dever::alert('内容还没有准备好');
  63. }
  64. return $this->info;
  65. }
  66. # 获取内容
  67. protected function getOne($id)
  68. {
  69. $info = Dever::load('collection/lib/info')->one($id);
  70. if ($info) {
  71. $this->handle($info);
  72. }
  73. return $info;
  74. }
  75. protected function handle(&$info)
  76. {
  77. # 过滤一些不用的信息
  78. unset($info['num_buy']);
  79. unset($info['num_ratio_buy']);
  80. unset($info['num_add_buy']);
  81. unset($info['num_add_view']);
  82. unset($info['num_view']);
  83. unset($info['buy_price']);
  84. unset($info['audit']);
  85. unset($info['status']);
  86. unset($info['reorder']);
  87. unset($info['admin_founder']);
  88. unset($info['admin_editor']);
  89. unset($info['udate']);
  90. unset($info['state']);
  91. unset($info['cdate']);
  92. unset($info['score_type']);
  93. unset($info['score_name']);
  94. $this->id_code = $info['id_code'] = Dever::load('collection/lib/common')->encode($info['id']);
  95. }
  96. # 检查是否可以查看
  97. protected function checkView()
  98. {
  99. if ($this->uid < 1) {
  100. return false;
  101. }
  102. if (!$this->info) {
  103. $this->checkInfo();
  104. }
  105. if ($this->info['buy'] == 1 || $this->info['password']) {
  106. # 是否已经拥有
  107. $this->info['buy_data'] = Dever::load('user/lib/collection')->check($this->uid, $this->info['id']);
  108. return $this->info['buy_data'] ? true : false;
  109. } else {
  110. # 免费
  111. return true;
  112. }
  113. }
  114. # 检测邀请码
  115. protected function checkCode()
  116. {
  117. $this->code = Dever::input('code');
  118. if ($this->code) {
  119. $code = Dever::load('collection/lib/common')->decode($this->code);
  120. $code = explode('_', $code);
  121. $this->id = $code[1];
  122. $this->parent_page_id = $code[2];
  123. $this->page_id = $code[3];
  124. $this->index = $code[4] ? $code[4] : 0;
  125. $this->times = $code[5];
  126. if (isset($code[6]) && $code[6]) {
  127. # 用户设置时间
  128. $this->day = $code[6];
  129. } else {
  130. $this->day = 0;
  131. }
  132. if (isset($code[7]) && $code[7] && $code[7] != $this->uid) {
  133. $this->share_uid = $code[7];
  134. if (isset($code[8]) && $code[8]) {
  135. $this->ticket_id = $code[8];
  136. }
  137. }
  138. } else {
  139. $this->id_code = Dever::input('id');
  140. if ($this->id_code == -1) {
  141. $this->id = $this->id_code;
  142. } else {
  143. $this->id = Dever::load('collection/lib/common')->decode($this->id_code);
  144. }
  145. }
  146. if ($this->share_uid == $this->uid) {
  147. $this->share_uid = 0;
  148. }
  149. }
  150. # 获取邀请码
  151. protected function getCode($id, $parent_page_id, $page_id, $index, $times_id, $day = 0, $uid = 0, $ticket_id = 0, $encode = true)
  152. {
  153. if (Dever::project('invite')) {
  154. $invite = Dever::input('invite');
  155. if ($invite) {
  156. $uid = Dever::load('invite/api')->getUid($invite);
  157. }
  158. }
  159. if (!$day) {
  160. $day = $this->day;
  161. }
  162. if ($day) {
  163. if (strstr($day, '-')) {
  164. $day = str_replace('-', '', $day);
  165. }
  166. } else {
  167. $day = 0;
  168. }
  169. $code = 'dlv1_' . $id. '_' . $parent_page_id . '_' . $page_id . '_' . $index . '_' . $times_id . '_' . $day . '_' . $uid . '_' . $ticket_id;
  170. if ($encode) {
  171. $code = Dever::load('collection/lib/common')->encode($code);
  172. }
  173. return $code;
  174. }
  175. #获取短域名
  176. public function getUrl($url, $param = array())
  177. {
  178. if (Dever::project('short')) {
  179. # 转成短域名
  180. if (isset($param['code']) && $param['code']) {
  181. $param['code'] .= '|collection/lib/common.encode';
  182. }
  183. $url = Dever::load('short/lib/handle')->get(1, $url, $param);
  184. } elseif ($param) {
  185. if (isset($param['code']) && $param['code']) {
  186. $param['code'] = Dever::load('collection/lib/common')->encode($param['code']);
  187. }
  188. $url .= '?' . http_build_query($param);
  189. }
  190. return $url;
  191. }
  192. protected function getInfoCode()
  193. {
  194. # 获取最新的用户记录
  195. $where['info_id'] = $this->id;
  196. $where['uid'] = $this->uid;
  197. $record = Dever::db('user/record')->one($where);
  198. if ($record) {
  199. $parent_page_id = $record['parent_page_id'];
  200. $page_id = $record['page_id'];
  201. $times = $record['times_id'];
  202. $index = $record['index'];
  203. $day = $record['day'];
  204. } else {
  205. # 获取最新的章节页id
  206. $page_where['info_id'] = $this->id;
  207. $page_where = $this->getTimesId($page_where);
  208. $page = Dever::db('collection/page')->child($page_where);
  209. $parent_page_id = $page_id = 0;
  210. if ($page) {
  211. $parent_page_id = $page[0]['page_id'];
  212. $page_id = $page[0]['id'];
  213. } else {
  214. Dever::alert('内容还没有准备好');
  215. }
  216. $times = 0;
  217. if ($this->times) {
  218. $times = $this->times;
  219. }
  220. $index = 0;
  221. $day = 0;
  222. }
  223. if (!$day) {
  224. # 从用户的时光获取
  225. $my_times = Dever::load('user/lib/times')->getData($this->uid, $this->id);
  226. if ($my_times && $my_times['day']) {
  227. $day = str_replace('-', '', $my_times['day']);
  228. }
  229. }
  230. # 获取门票
  231. $ticket = Dever::load('user/lib/ticket')->check($this->uid, $this->id);
  232. if ($ticket) {
  233. $this->ticket_id = $ticket['id'];
  234. }
  235. # 获取code
  236. $code = $this->getCode($this->id, $parent_page_id, $page_id, $index, $times, $day, $this->uid, $this->ticket_id);
  237. return $code;
  238. }
  239. # 获取时光id
  240. protected function getTimesId($where)
  241. {
  242. # 获取当前年份的最新的时光
  243. if (!$this->times) {
  244. $where['year'] = date('Y');
  245. $times = Dever::db('collection/times')->getNew($where);
  246. if ($times) {
  247. $this->times = $times['id'];
  248. }
  249. }
  250. if ($this->times) {
  251. $where['times_id'] = $this->times;
  252. }
  253. return $where;
  254. }
  255. # 获取按钮的样式
  256. protected function button($info)
  257. {
  258. if (isset($info['button'])) {
  259. $button = array(
  260. 'bgcolor' => 'background: linear-gradient(to right, #000000,#000000) !important;color:#fff;',
  261. 'color' => 'color:#fff',
  262. 'name' => array('入口', '排行榜')
  263. );
  264. $button['name'] = explode(',', $info['button']);
  265. if ($info['button_bgcolor'] && $info['button_bgjcolor']) {
  266. $button['bgcolor'] = 'background: linear-gradient(to right, '.$info['button_bgcolor'].','.$info['button_bgjcolor'].') !important;';
  267. }
  268. if ($info['button_ftcolor']) {
  269. $button['color'] = 'color:' . $info['button_ftcolor'];
  270. $button['bgcolor'] .= $button['color'];
  271. }
  272. return $button;
  273. } else {
  274. return array();
  275. }
  276. }
  277. # 获取其他页面的分享
  278. protected function getConfigShare($config = array())
  279. {
  280. $config = $config ? $config : Dever::db('main/config')->one();
  281. $config['desc'] = $config['info'];
  282. $config['pic_cover'] = $config['logo'];
  283. return $this->getInfoShare($config);
  284. }
  285. # 获取合集的分享
  286. protected function getInfoShare($info, $code = false)
  287. {
  288. if ($this->uid && $this->uid > 0) {
  289. $data['title'] = $info['share_title'] ? $info['share_title'] : '{username}邀请你看{name}';
  290. $data['title'] = str_replace('{username}', $this->user['username'], $data['title']);
  291. $data['title'] = str_replace('{name}', $info['name'], $data['title']);
  292. } else {
  293. $data['title'] = $info['name'];
  294. }
  295. $data['content'] = $info['share_content'] ? $info['share_content'] : $info['desc'];
  296. $data['pic'] = $info['share_pic'] ? $info['share_pic'] : $info['pic_cover'];
  297. $url = Dever::input('url');
  298. $data['source_link'] = '';
  299. $url = $this->getUrl($url);
  300. $data['link'] = $url;
  301. return $data;
  302. }
  303. }