Index.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace app\api\controller;
  3. use app\api\model\Area;
  4. use app\common\controller\Api;
  5. use think\Request;
  6. class Index extends Api
  7. {
  8. protected $noNeedLogin = ['*'];
  9. private $bannerModel;
  10. public function __construct(Request $request = null)
  11. {
  12. $this->bannerModel = new \app\api\model\Banner();
  13. parent::__construct($request);
  14. }
  15. /**
  16. * 获取banner
  17. * @return void
  18. */
  19. public function fetchBanner()
  20. {
  21. $module_type = input("module_type", "user");
  22. $banners = $this->bannerModel->fetchBanner($module_type, 5);
  23. $this->success($banners);
  24. }
  25. public function fetchArea()
  26. {
  27. $this->success(Area::fetchArea());
  28. }
  29. public function check()
  30. {
  31. $signature = $this->request->param('signature', "");
  32. $timestamp = $this->request->param('timestamp', "");
  33. $nonce = $this->request->param('nonce', "");
  34. $echostr = $this->request->param('echostr', "");
  35. if ($this->checkSignature($signature, $timestamp, $nonce)) {
  36. ob_clean();
  37. echo $echostr;
  38. die;//这里特别注意,如果不用die结束程序会token验证失败
  39. } else {
  40. echo false;
  41. }
  42. }
  43. private function checkSignature($signature, $timestamp, $nonce)
  44. {
  45. $token = "M1xMa324s5sa6geYsdhU342";//这里写你在微信公众平台里面填写的token
  46. $tmpArr = array($token, $timestamp, $nonce);
  47. sort($tmpArr, SORT_STRING);
  48. $tmpStr = implode($tmpArr);
  49. $tmpStr = sha1($tmpStr);
  50. if ($tmpStr == $signature) {
  51. return true;
  52. } else {
  53. return false;
  54. }
  55. }
  56. }