Base.php 3.4 KB

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