| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- <?php
- namespace app\index\controller;
- use app\common\controller\Frontend;
- use think\Db;
- class Index extends Frontend
- {
- protected $noNeedLogin = '*';
- protected $noNeedRight = '*';
- protected $layout = '';
- /**
- *1.服务器接口,微信公众平台填写的url
- * http://域名/控制器/link
- */
- public function link(){
- $echostr=$_GET['echostr'];//微信服务器提供的 随机字符串
- if ($this->check()){//验证签名是否正确
- echo $echostr;
- exit;
- }
- }
- /**
- * 2.验证签名
- */
- public function check(){
- $signature=$_GET['signature']; //微信服务器提供的 微信加密签名
- $timestamp=$_GET['timestamp']; //微信服务器提供的 时间戳
- $nonce=$_GET['nonce']; //微信服务器提供的 随机数
- $token='z9EGslrxPpbicuy48mkw'; //自己定义的 Token
- $tmpArr = array($token,$timestamp,$nonce);//数组
- sort($tmpArr); //排序
- $tmpstr=implode($tmpArr); //数据转字符串
- $tmpstr=sha1($tmpstr); //字符串加密
- if ($tmpstr==$signature){
- return true;
- }else{
- return false;
- }
- }
- /**
- * 获取access_token存进数据库
- */
- public function access_token(){
- $token = Db::name("v_access_token")->find(1);
- echo "<pre>";
- print_r($token);
- echo "<pre>";die;
- $date = date('Y-m-d H:i:s');
- if (strtotime($token['addtime']) > strtotime($date)){
- return $token['access_token'];
- }else{
- $appid = "你的appid";
- $appsecret = "你的appsecret ";
- $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$appsecret}";
- $rt = $this->request_get($url);
- $data['access_token']=$rt['access_token'];
- $data['addtime']= date("Y-m-d H:i:s", strtotime("$date +60 min"));
- $rt =Db::name("v_access_token")->where("id='1'")->save($data);
- if ($rt){
- $token = Db::name("v_access_token")->find(1);
- return $token['access_token'];
- }else{
- return "获取access_token错误";
- }
- }
- }
- /**
- * 3.发送http请求,并返回数据
- * @param $url
- * @return mixed
- */
- public function request_get($url){
- $curl = curl_init();// 1. 初始化一个 cURL 对象
- curl_setopt($curl,CURLOPT_URL,$url);// 2.设置你需要抓取的URL
- curl_setopt($curl,CURLOPT_HEADER,0);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1 );// 3.https必须加这个,不加不好使(不多加解释,东西太多了
- curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
- $res = curl_exec($curl);// 5. 运行cURL,请求网页
- curl_close($curl);// 6. 关闭URL请求
- $json_obj = json_decode($res,true);
- return $json_obj;
- }
- public function user(){
- //1.用户点击静默授权链接 获取用户的code
- $code = input("code");
- echo "<pre>";
- print_r($code);
- echo "<pre>";
- }
- public function index(){
- // $search = input('get.search');
- $search = "05 05289607";
- // 2. 连接 MongoDB(使用 config/database.php 中名为 mongodb 的连接)
- $mongo = \think\Db::connect('mongodb');
- $data = $mongo->name('qcode_goods')
- ->where('shdh',$search)
- ->select();
- echo "<pre>";
- print_r($data);
- echo "<pre>";die;
- return $this->view->fetch();
- }
- /**
- * 判断是否是手机端
- *
- * @return bool
- */
- private function isMobile()
- {
- // 获取用户代理
- $userAgent = $_SERVER['HTTP_USER_AGENT'];
- // 定义手机设备的正则表达式
- $mobileRegex = "/(android|iphone|ipad|ipod|blackberry|iemobile|opera mini|mobile)/i";
- // 判断用户代理是否匹配手机设备
- return preg_match($mobileRegex, $userAgent);
- }
- }
|