Index.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. return $tmpStr == $signature;
  51. }
  52. }