微信分享

<?php
/**
 * Description of WxShare
 *微信分享
 * @author xinjun
 */
namespace Controller\Wx;

use Controller\Home\HomeBase;
use Model\Wx\UserModel;
use Model\Wx\TokenModel;
use Framework\Net\Request;

class WxShare extends HomeBase
{
    public function __construct()
    {
        parent::__construct();
    }
    
    /**
     * 分享配置信息
     * @return type
     */
    public function getConfig()
    {
        $url = $_POST[‘url‘];
        $conf = array();
        $conf[‘debug‘] = true;
        $conf[‘appId‘] = $this->getAppid();
        $conf[‘timestamp‘] = time();
        $conf[‘nonceStr‘] = "Wm3WZYTPz0wzccnW";
        $conf[‘signature‘] = $this->getSignature($conf[‘nonceStr‘],$conf[‘timestamp‘],$url);
        $conf[‘jsApiList‘] = [‘onMenuShareTimeline‘,‘onMenuShareAppMessage‘];
//        print_r($conf);exit;
         return $this->getData($conf);//json_encode($conf,JSON_UNESCAPED_SLASHES) ;
    }

    /**
     * 获取appid
     * @return type
     */
    public function getAppid()
    {
        $tokenModel = new TokenModel();
        $appidData = $tokenModel->getAccessToken(array(‘nf_type‘=>1));
        return $appidData[‘nf_appid‘];
    }

    /**
     * 获取签名signature
     * @param type $UNSTR
     * @param type $TIMESTAMP
     * @return type
     */
    public function getSignature($UNSTR,$TIMESTAMP,$url)
    {
        // 获取 jsapi_ticket 值
        $jsapi_ticket = $this->getTicket();
        $signature = ‘jsapi_ticket=‘ . $jsapi_ticket . ‘&noncestr=‘ . $UNSTR . ‘&timestamp=‘ . $TIMESTAMP . ‘&url=‘ . $url;
        return sha1($signature);
    }

    /**
     * 获取 ticket
     * @return boolean 
     */
    public function getTicket()
    {
        // 判断数据库jsapi_ticket更新时间
        // 超过 1000 秒则重新获取 并更新数据库  ( 每日调用次数为  100万次  2016年12月15日 )
        //取基础access_token
        $tokenModel = new TokenModel();
        $tokenData = $tokenModel->getAccessToken(array(‘id‘ => 2));
        $accesstokenData = $tokenModel->getAccessToken(array(‘id‘ => 1));
        if (($tokenData[‘nf_val‘] != ‘‘)&& (($tokenData[‘nf_time‘] + 6000) > time())) {
            return $tokenData[‘nf_val‘];
        } else {
            // 更新 jsapi_ticket
            $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=".$accesstokenData[‘nf_val‘]."&type=jsapi";
            $https = true;
            $method = ‘get‘;
            $data = null;
            $jsonJsapi_ticket = $this->request($url, $https, $method, $data);
            $jsapi_ticket = json_decode($jsonJsapi_ticket, true);

            if (!isset($jsapi_ticket[‘ticket‘])) {
                return false;
            }
            $upTicket = $tokenModel->edit(array(‘nf_val‘=>$jsapi_ticket[‘ticket‘],‘nf_time‘=>time()), array(‘id‘=>2));
            if ($upTicket) {
                $data = $tokenModel->getAccessToken(array(‘id‘ => 2));
                if(!empty($data)){
                    $jsapi_ticket[‘ticket‘] = $data[‘nf_val‘];
                    return  $jsapi_ticket[‘ticket‘];
                }else{
                    return false;
                }    
            } else {
                return false;
            }
        }
    }
}

  

微信分享

上一篇:微信小程序UI组件、开发框架、实用库


下一篇:【DB宝19】在Docker中使用MySQL高可用之MHA