Base.php 3.2 KB

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