Base.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. $data['cdate'] = Dever::mdate($data['cdate'], 2);
  65. if (isset($data['author_id'])) {
  66. $data['author'] = $this->handlePic(Dever::load('set/author-one', $data['author_id']));
  67. }
  68. return $data;
  69. }
  70. /**
  71. * 验证用户
  72. *
  73. * @return mixed
  74. */
  75. protected function check_user()
  76. {
  77. $uid = Dever::input('uid');
  78. $session = Dever::input('session');
  79. $state = $this->check_session($uid, $session);
  80. if (!$state) {
  81. Dever::alert('请重新登录');
  82. }
  83. return $state;
  84. }
  85. /**
  86. * 生成登录session串 先做个简单的吧
  87. *
  88. * @return mixed
  89. */
  90. protected function session($uid, $prefix = 'user')
  91. {
  92. $secret = md5($prefix . '_' . $uid . '_' . Dever::config('base')->secret);
  93. return $secret;
  94. }
  95. /**
  96. * 验证登录session串
  97. *
  98. * @return mixed
  99. */
  100. protected function check_session($uid, $session, $prefix = 'user')
  101. {
  102. $secret = $this->session($uid, $prefix);
  103. if ($secret != $session) {
  104. return false;
  105. }
  106. return true;
  107. }
  108. /**
  109. * 获取对应的类型
  110. *
  111. * @return mixed
  112. */
  113. protected function type($type = 1)
  114. {
  115. $config = array
  116. (
  117. 1 => 'news',
  118. 2 => 'course',
  119. );
  120. return $config[$type];
  121. }
  122. }