Index.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace app\index\controller;
  3. use app\common\controller\Frontend;
  4. use think\Db;
  5. class Index extends Frontend
  6. {
  7. protected $noNeedLogin = '*';
  8. protected $noNeedRight = '*';
  9. protected $layout = '';
  10. /**
  11. *1.服务器接口,微信公众平台填写的url
  12. * http://域名/控制器/link
  13. */
  14. public function link(){
  15. $echostr=$_GET['echostr'];//微信服务器提供的 随机字符串
  16. if ($this->check()){//验证签名是否正确
  17. echo $echostr;
  18. exit;
  19. }
  20. }
  21. /**
  22. * 2.验证签名
  23. */
  24. public function check(){
  25. $signature=$_GET['signature']; //微信服务器提供的 微信加密签名
  26. $timestamp=$_GET['timestamp']; //微信服务器提供的 时间戳
  27. $nonce=$_GET['nonce']; //微信服务器提供的 随机数
  28. $token='z9EGslrxPpbicuy48mkw'; //自己定义的 Token
  29. $tmpArr = array($token,$timestamp,$nonce);//数组
  30. sort($tmpArr); //排序
  31. $tmpstr=implode($tmpArr); //数据转字符串
  32. $tmpstr=sha1($tmpstr); //字符串加密
  33. if ($tmpstr==$signature){
  34. return true;
  35. }else{
  36. return false;
  37. }
  38. }
  39. /**
  40. * 获取access_token存进数据库
  41. */
  42. public function access_token(){
  43. $token = Db::name("v_access_token")->find(1);
  44. echo "<pre>";
  45. print_r($token);
  46. echo "<pre>";die;
  47. $date = date('Y-m-d H:i:s');
  48. if (strtotime($token['addtime']) > strtotime($date)){
  49. return $token['access_token'];
  50. }else{
  51. $appid = "你的appid";
  52. $appsecret = "你的appsecret ";
  53. $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$appsecret}";
  54. $rt = $this->request_get($url);
  55. $data['access_token']=$rt['access_token'];
  56. $data['addtime']= date("Y-m-d H:i:s", strtotime("$date +60 min"));
  57. $rt =Db::name("v_access_token")->where("id='1'")->save($data);
  58. if ($rt){
  59. $token = Db::name("v_access_token")->find(1);
  60. return $token['access_token'];
  61. }else{
  62. return "获取access_token错误";
  63. }
  64. }
  65. }
  66. /**
  67. * 3.发送http请求,并返回数据
  68. * @param $url
  69. * @return mixed
  70. */
  71. public function request_get($url){
  72. $curl = curl_init();// 1. 初始化一个 cURL 对象
  73. curl_setopt($curl,CURLOPT_URL,$url);// 2.设置你需要抓取的URL
  74. curl_setopt($curl,CURLOPT_HEADER,0);
  75. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1 );// 3.https必须加这个,不加不好使(不多加解释,东西太多了
  76. curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
  77. $res = curl_exec($curl);// 5. 运行cURL,请求网页
  78. curl_close($curl);// 6. 关闭URL请求
  79. $json_obj = json_decode($res,true);
  80. return $json_obj;
  81. }
  82. public function user(){
  83. //1.用户点击静默授权链接 获取用户的code
  84. $code = input("code");
  85. echo "<pre>";
  86. print_r($code);
  87. echo "<pre>";
  88. }
  89. public function index(){
  90. // $search = input('get.search');
  91. $search = "05 05289607";
  92. // 2. 连接 MongoDB(使用 config/database.php 中名为 mongodb 的连接)
  93. $mongo = \think\Db::connect('mongodb');
  94. $data = $mongo->name('qcode_goods')
  95. ->where('shdh',$search)
  96. ->select();
  97. echo "<pre>";
  98. print_r($data);
  99. echo "<pre>";die;
  100. return $this->view->fetch();
  101. }
  102. /**
  103. * 判断是否是手机端
  104. *
  105. * @return bool
  106. */
  107. private function isMobile()
  108. {
  109. // 获取用户代理
  110. $userAgent = $_SERVER['HTTP_USER_AGENT'];
  111. // 定义手机设备的正则表达式
  112. $mobileRegex = "/(android|iphone|ipad|ipod|blackberry|iemobile|opera mini|mobile)/i";
  113. // 判断用户代理是否匹配手机设备
  114. return preg_match($mobileRegex, $userAgent);
  115. }
  116. }