随笔 - 75  文章 - 0  评论 - 1  阅读 - 56493

微信分享

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?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;
            }
        }
    }
}

  

posted on   泪滴  阅读(190)  评论(0编辑  收藏  举报
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示