美文网首页
微信分享

微信分享

作者: 十万个魏什么啊 | 来源:发表于2020-08-31 17:35 被阅读0次
<?php
/**
 * Created by PhpStorm.
 * User: EDZ
 * Date: 2019/5/8
 * Time: 16:54
 */

// namespace app\index\Controller;

// use think\Controller;
// use think\facade\Cache;
// use think\facade\Request;

namespace app\api\controller\v1;
use app\api\controller\ApiController;
use app\common\model\AlumnusModel;
use app\common\model\CardModel;
use app\common\model\AlumnusUserModel;
use app\common\model\AspirationModel;
use Firebase\JWT\JWT;
use Qiniu\Auth;
use Qiniu\Processing\PersistentFop;
use think\Db;
use think\Exception;
use think\facade\Cache;
use think\Controller;
use think\Request;
use think\response\Json;
use think\Validate;

define('APPID', 'wx628647e33334453f');
define('APPS', 'ecac0d505c5432a8545a19e331a17bab');


class shareController extends Controller
{
    public function index()
    {
        return $this->fetch();
    }

    /**
     * @author raoguanghui
     * @function 传送签名
     * @time 2017-07-13
     */
    public function get_config()
    {
        $param = $this->request->param();
        $url = $param['params']['url'];
        $url = urldecode($url);
        $config = $this->get_js_config($url);
        $this->return_ajax($config, '获取成功', 2000);
    }

    /**
     * @author raoguanghui
     * @function 生成签名
     * @time 2017-07-13
     */
    public function get_js_config($url_one)
    {

        $noncestr = $this->rand_str(16);
//        halt($noncestr);
        $jsapi_ticket = $this->getJsApiTicket();
//        halt($jsapi_ticket);
        $timestamp = time();
        $protocol  = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
        // $url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
        $url = $url_one;
       // dump($url_one);
       // dump($url);
       // die;

//        halt($url);
//        'jsapi_ticket=sM4AOVdWfPE4DxkXGEs8VMCPGGVi4C3VM0P37wVUCFvkVAy_90u5h9nbSlYy3-Sl-HhTdfl2fzFy1AOcHKP7qg&noncestr=Wm3WZYTPz0wzccnW&timestamp=1414587457&url=http://mp.weixin.qq.com?params=value'
        $string1   = 'jsapi_ticket=' . $jsapi_ticket . '&noncestr=' . $noncestr . '&timestamp=' . $timestamp . '&url=' . $url;
        $signature = sha1($string1);
        // dump($url);
//        halt($signature);
        $config = array(
            'appId'        => APPID, // 必填,公众号的唯一标识
            'nonceStr'     => $noncestr, // 必填,生成签名的随机串
            'timestamp'    => $timestamp, // 必填,生成签名的时间戳
            "url"          => $url,
//            "signature" => $signature,
            "jsapi_ticket" => $jsapi_ticket,
            "rawString"    => $string1,
            'signature'    => $signature// 必填,签名,见附录1

        );
        // dump($config);die;
        //echo $jsapi_ticket.'<br/>';exit;
//        echo json_encode($config);
        return $config;
    }

    private function getJsApiTicket()
    {
        // jsapi_ticket 应该全局存储与更新,以下代码以写入到文件中做示例
        $data = Cache::get('wx_jsapi_ticket');
//        Cache::rm('wx_jsapi_ticket');

        if ($data !== false) {
            $ticket = $data;
        }
        else {
            $accessToken = $this->getAccessToken();
//            halt($accessToken);
            $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=" . $accessToken;
//            tps://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=
            $res = json_decode($this->httpGet($url), true);
//            halt($res);
            $ticket = $res['ticket'];

            if ($ticket) {

                Cache::set('wx_jsapi_ticket', $ticket, 3600);

            }

        }
        return $ticket;

    }

    private function getAccessToken()
    {
        // access_token 应该全局存储与更新,以下代码以写入到文件中做示例
        $appid     = APPID;
        $appsecret = APPS;
        $data      = Cache::get('wx_access_token');
//        dump($appsecret);
//        halt($data);
        if ($data) {
            $access_token = $data;
        }
        else {

            $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret";
            $res = json_decode($this->httpGet($url), true);
//            halt($res);
            $access_token = $res['access_token'];
            if ($access_token) {
//                file_put_contents('access_token.txt',$access_token,8);
                Cache::set('wx_access_token', $access_token, 3600);
            }

        }

        return $access_token;

    }

    private function httpGet($url)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_TIMEOUT, 500);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
        curl_setopt($curl, CURLOPT_URL, $url);
        $res = curl_exec($curl);
        curl_close($curl);
        return $res;
    }

    private function rand_str($length = 8)
    {
        // 密码字符集,可任意添加你需要的字符
        $chars    = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
        $password = '';
        for ($i = 0; $i < $length; $i++) {
            // 这里提供两种字符获取方式
            // 第一种是使用 substr 截取$chars中的任意一位字符;
            // 第二种是取字符数组 $chars 的任意元素
            // $password .= substr($chars, mt_rand(0, strlen($chars) – 1), 1);
            $password .= $chars[mt_rand(0, strlen($chars) - 1)];
        }
        return $password;
    }

    /**
     * return_ajax返回
     * @function ReturnAjax
     * @param array $array
     * @param $msg
     * @param $code
     * @return void
     * @access ${STATIC}
     * @author chaoqun
     * @create_time: 2019/07/03 16:05
     */
    private function return_ajax($array, $msg, $code)
    {
        if ($code == 2000) {
            $return_array = ['code' => $code, 'data' => $array, 'msg' => $msg, 'time' => time()];
        }
        else {
            $return_array = ['code' => $code, 'data' => $array, 'msg' => $msg, 'time' => time()];
        }

        header('Content-type:text/json');
        header('Access-Control-Allow-Origin:*');
        echo json_encode($return_array, JSON_UNESCAPED_UNICODE);
        die;
    }
}

相关文章

网友评论

      本文标题:微信分享

      本文链接:https://www.haomeiwen.com/subject/xqcpsktx.html