Base.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. return $state;
  81. }
  82. /**
  83. * 生成登录session串 先做个简单的吧
  84. *
  85. * @return mixed
  86. */
  87. protected function session($uid)
  88. {
  89. $secret = md5($uid . '_' . Dever::config('base')->secret);
  90. return $secret;
  91. }
  92. /**
  93. * 验证登录session串
  94. *
  95. * @return mixed
  96. */
  97. protected function check_session($uid, $session)
  98. {
  99. $secret = $this->session($uid);
  100. if ($secret != $session) {
  101. Dever::alert('请重新登录');
  102. }
  103. return true;
  104. }
  105. /**
  106. * 获取对应的类型
  107. *
  108. * @return mixed
  109. */
  110. protected function type($type = 1)
  111. {
  112. $config = array
  113. (
  114. 1 => 'news',
  115. 2 => 'course',
  116. );
  117. return $config[$type];
  118. }
  119. }