Base.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. if (is_numeric($data['cdate'])) {
  81. $data['cdate'] = date('Y-m-d', $data['cdate']);
  82. }
  83. //$data['cdate'] = Dever::mdate($data['cdate'], 2);
  84. if (isset($data['author_id'])) {
  85. $data['author'] = $this->handlePic(Dever::load('set/author-one', $data['author_id']));
  86. //$data['author']['cdate'] = date('Y.m.d', $data['author']['cdate']);
  87. $data['author']['cdate'] = $data['cdate'];
  88. }
  89. return $data;
  90. }
  91. /**
  92. * 验证用户
  93. *
  94. * @return mixed
  95. */
  96. protected function check_user()
  97. {
  98. $uid = Dever::input('uid');
  99. $session = Dever::input('session');
  100. $state = $this->check_session($uid, $session);
  101. if (!$state) {
  102. Dever::alert('请重新登录');
  103. }
  104. return $state;
  105. }
  106. /**
  107. * 生成登录session串 先做个简单的吧
  108. *
  109. * @return mixed
  110. */
  111. protected function session($uid, $prefix = 'user')
  112. {
  113. $secret = md5($prefix . '_' . $uid . '_' . Dever::config('base')->secret);
  114. return $secret;
  115. }
  116. /**
  117. * 验证登录session串
  118. *
  119. * @return mixed
  120. */
  121. protected function check_session($uid, $session, $prefix = 'user')
  122. {
  123. $secret = $this->session($uid, $prefix);
  124. if ($secret != $session) {
  125. return false;
  126. }
  127. return true;
  128. }
  129. /**
  130. * 获取对应的类型
  131. *
  132. * @return mixed
  133. */
  134. public function type($type = 1)
  135. {
  136. $config = array
  137. (
  138. 1 => 'content/news',
  139. 2 => 'content/course',
  140. 3 => 'content/meeting',
  141. 4 => 'comment/review',
  142. );
  143. return $config[$type];
  144. }
  145. /**
  146. * 统计数据
  147. *
  148. * @return mixed
  149. */
  150. public function count($source_table, $source_id, $type, $set)
  151. {
  152. $table = $this->type($source_table);
  153. $count = Dever::db($type)->total(array('option_info_id' => $this->data['info']['id'], 'option_source_table' => $source_table, 'option_source_id' => $source_id));
  154. $id = Dever::db($table)->update(array('set_' . $set => $count, 'where_id' => $source_id));
  155. //Dever::db($table)->addReview($source_id);
  156. }
  157. /**
  158. * vote
  159. *
  160. * @return mixed
  161. */
  162. public function vote($type, $id)
  163. {
  164. $state = 2;
  165. $uid = Dever::input('uid');
  166. if ($uid > 0) {
  167. $data = Dever::load('comment/vote-one', array('option_info_id' => $this->data['info']['id'], 'option_source_table' => $type, 'option_source_id' => $id, 'option_uid' => $uid));
  168. if ($data) {
  169. $state = 1;
  170. }
  171. }
  172. return $state;
  173. }
  174. }