Base.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. namespace Content\Lib;
  3. use Dever;
  4. class Base
  5. {
  6. # 定义返回数据
  7. protected $data;
  8. public function __construct($state = 1)
  9. {
  10. if ($state == 2) {
  11. return;
  12. }
  13. # 获取小程序id
  14. $id = Dever::input('appid');
  15. if (!$id || $id < 0) {
  16. Dever::alert('错误的小程序id');
  17. }
  18. # 获取小程序基本信息
  19. $this->data['info'] = Dever::load('set/info-one', $id);
  20. if (!$this->data['info']) {
  21. Dever::alert('小程序信息不存在');
  22. }
  23. $this->data['info'] = $this->handlePic($this->data['info']);
  24. }
  25. /**
  26. * 替换所有图片地址为https
  27. *
  28. * @return mixed
  29. */
  30. protected function handlePic($data)
  31. {
  32. $config = array
  33. (
  34. 'pic', 'content', 'logo', 'top', 'img', 'link'
  35. );
  36. foreach ($config as $k => $v) {
  37. if (isset($data[$v]) && $data[$v]) {
  38. $data[$v] = $this->replacePic($data[$v]);
  39. }
  40. }
  41. return $data;
  42. }
  43. /**
  44. * 替换所有图片地址为https
  45. *
  46. * @return mixed
  47. */
  48. protected function replacePic($pic)
  49. {
  50. $pic = Dever::pic($pic);
  51. if (strstr($pic, 'http://')) {
  52. $pic = str_replace('http://', 'https://', $pic);
  53. }
  54. return $pic;
  55. }
  56. /**
  57. * 将数据中的图片地址进行替换
  58. *
  59. * @return mixed
  60. */
  61. protected function one($data, $state = 1)
  62. {
  63. $data = $this->handlePic($data);
  64. if ($state == 2 && isset($data['pic']) && $data['pic']) {
  65. //$data['pic'] = explode(',', $data['pic']);
  66. }
  67. if (isset($data['video']) && $data['video']) {
  68. $data['video'] = Dever::load('content/v1/video.mp4', $data['video']);
  69. }
  70. if (isset($data['sdate']) && $data['sdate']) {
  71. $data['sdate'] = date('m/d', $data['sdate']);
  72. }
  73. if (isset($data['link']) && $data['link']) {
  74. $data['link'] = base64_encode($data['link']);
  75. $data['link'] = 'https://cm.5dev.cn/applet/content/v1/share.url?link=' . $data['link'];
  76. }
  77. if (isset($data['edate']) && $data['edate']) {
  78. $data['edate'] = date('m/d', $data['edate']);
  79. }
  80. $data['cdate'] = date('Y-m-d', $data['cdate']);
  81. //$data['cdate'] = Dever::mdate($data['cdate'], 2);
  82. if (isset($data['author_id'])) {
  83. $data['author'] = $this->handlePic(Dever::load('set/author-one', $data['author_id']));
  84. //$data['author']['cdate'] = date('Y.m.d', $data['author']['cdate']);
  85. $data['author']['cdate'] = $data['cdate'];
  86. }
  87. return $data;
  88. }
  89. /**
  90. * 验证用户
  91. *
  92. * @return mixed
  93. */
  94. protected function check_user()
  95. {
  96. $uid = Dever::input('uid');
  97. $session = Dever::input('session');
  98. $state = $this->check_session($uid, $session);
  99. if (!$state) {
  100. Dever::alert('请重新登录');
  101. }
  102. return $state;
  103. }
  104. /**
  105. * 生成登录session串 先做个简单的吧
  106. *
  107. * @return mixed
  108. */
  109. protected function session($uid, $prefix = 'user')
  110. {
  111. $secret = md5($prefix . '_' . $uid . '_' . Dever::config('base')->secret);
  112. return $secret;
  113. }
  114. /**
  115. * 验证登录session串
  116. *
  117. * @return mixed
  118. */
  119. protected function check_session($uid, $session, $prefix = 'user')
  120. {
  121. $secret = $this->session($uid, $prefix);
  122. if ($secret != $session) {
  123. return false;
  124. }
  125. return true;
  126. }
  127. /**
  128. * 获取对应的类型
  129. *
  130. * @return mixed
  131. */
  132. public function type($type = 1)
  133. {
  134. $config = array
  135. (
  136. 1 => 'content/news',
  137. 2 => 'content/course',
  138. 3 => 'content/meeting',
  139. 4 => 'comment/review',
  140. );
  141. return $config[$type];
  142. }
  143. /**
  144. * 统计数据
  145. *
  146. * @return mixed
  147. */
  148. public function count($source_table, $source_id, $type, $set)
  149. {
  150. $table = $this->type($source_table);
  151. $count = Dever::db($type)->total(array('option_info_id' => $this->data['info']['id'], 'option_source_table' => $source_table, 'option_source_id' => $source_id));
  152. $id = Dever::db($table)->update(array('set_' . $set => $count, 'where_id' => $source_id));
  153. //Dever::db($table)->addReview($source_id);
  154. }
  155. }