Index.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. * @throws \think\db\exception\DataNotFoundException
  18. * @throws \think\db\exception\ModelNotFoundException
  19. * @throws \think\exception\DbException
  20. */
  21. public function fetchBanner()
  22. {
  23. $module_type = input("module_type", "user");
  24. $banners = $this->bannerModel->fetchBanner($module_type, 5);
  25. $this->success($banners);
  26. }
  27. public function fetchArea()
  28. {
  29. $this->success(Area::fetchArea());
  30. }
  31. public function check()
  32. {
  33. $signature = $this->request->param('signature', "");
  34. $timestamp = $this->request->param('timestamp', "");
  35. $nonce = $this->request->param('nonce', "");
  36. $echostr = $this->request->param('echostr', "");
  37. if($this->checkSignature($signature,$timestamp,$nonce)){
  38. ob_clean();
  39. echo $echostr;die;//这里特别注意,如果不用die结束程序会token验证失败
  40. }else{
  41. echo false;
  42. }
  43. }
  44. private function checkSignature($signature,$timestamp,$nonce)
  45. {
  46. $token = "M1xMa324s5sa6geYsdhU342";//这里写你在微信公众平台里面填写的token
  47. $tmpArr = array($token,$timestamp, $nonce);
  48. sort($tmpArr, SORT_STRING);
  49. $tmpStr = implode( $tmpArr );
  50. $tmpStr = sha1( $tmpStr );
  51. if($tmpStr == $signature){
  52. return true;
  53. }else{
  54. return false;
  55. }
  56. }
  57. }