Base.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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'
  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['edate']) && $data['edate']) {
  74. $data['edate'] = date('m/d', $data['edate']);
  75. }
  76. $data['cdate'] = date('Y-m-d', $data['cdate']);
  77. //$data['cdate'] = Dever::mdate($data['cdate'], 2);
  78. if (isset($data['author_id'])) {
  79. $data['author'] = $this->handlePic(Dever::load('set/author-one', $data['author_id']));
  80. //$data['author']['cdate'] = date('Y.m.d', $data['author']['cdate']);
  81. $data['author']['cdate'] = $data['cdate'];
  82. }
  83. return $data;
  84. }
  85. /**
  86. * 验证用户
  87. *
  88. * @return mixed
  89. */
  90. protected function check_user()
  91. {
  92. $uid = Dever::input('uid');
  93. $session = Dever::input('session');
  94. $state = $this->check_session($uid, $session);
  95. if (!$state) {
  96. Dever::alert('请重新登录');
  97. }
  98. return $state;
  99. }
  100. /**
  101. * 生成登录session串 先做个简单的吧
  102. *
  103. * @return mixed
  104. */
  105. protected function session($uid, $prefix = 'user')
  106. {
  107. $secret = md5($prefix . '_' . $uid . '_' . Dever::config('base')->secret);
  108. return $secret;
  109. }
  110. /**
  111. * 验证登录session串
  112. *
  113. * @return mixed
  114. */
  115. protected function check_session($uid, $session, $prefix = 'user')
  116. {
  117. $secret = $this->session($uid, $prefix);
  118. if ($secret != $session) {
  119. return false;
  120. }
  121. return true;
  122. }
  123. /**
  124. * 获取对应的类型
  125. *
  126. * @return mixed
  127. */
  128. public function type($type = 1)
  129. {
  130. $config = array
  131. (
  132. 1 => 'content/news',
  133. 2 => 'content/course',
  134. 3 => 'content/meeting',
  135. 4 => 'comment/review',
  136. );
  137. return $config[$type];
  138. }
  139. }