微信授權登錄+微信公眾號支付


記錄工作備忘

  采用TP3.2.3來二開--主要是微信授權登錄、js自定義分享、支付+回調

  1)先在公眾號獲取相關配置參數,配置公眾號的網頁授權域名、js接口域名

// 微信配置參數
'wechat_option' => array(
    'appid' => 'w****a769023a',
    'appsecret' => 'c****0accc10d19e5f85541b9'
    ),
// 微信支付配置
'WEIXINPAY_CONFIG' => array(
    'APPID' => 'wx***5a769023a',
    'MCHID' => '1***001',  //商戶號
    'KEY' => 'f****djgfjgyukuyklr',
    'APPSECRET' => 'cc*****10d19e5f85541b9',
        // 'NOTIFY_URL' => 'http://t4.hzrt.cn/member/notify'
    'NOTIFY_URL' => 'http://t***t.cn/Api/Weixinpay/notify'  //支付回調地址,外網能直接訪問(避免繼承memberbase控制器)
                    ),

  2)授權登錄

a.先獲取code

b.通過code獲取access_token、openid

c.通過access_token、openid獲取用戶信息

if(is_weixin()){
  $code = I('get.code');
  $wechat_option = C('wechat_option');
  $appid = $wechat_option['appid'];
  $appsecret = $wechat_option['appsecret'];
  if(empty($code)){
    // 獲取code
    $redirect_uri = 'http://t4.hzrt.cn/user/login';
    $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$appid}&redirect_uri={$redirect_uri}&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect";
    redirect($url);
 }
 // 通過code獲取access_token,openid
 $result = json_decode(file_get_contents("https://api.weixin.qq.com/sns/oauth2/access_token?appid={$appid}&secret={$appsecret}&code={$code}&grant_type=authorization_code"), true);
 $access_token = $result['access_token'];
 $openid = $result['openid'];
 // 通過access_token,openid獲取用戶信息
 $userinfo = json_decode(file_get_contents("https://api.weixin.qq.com/sns/userinfo?access_token={$access_token}&openid={$openid}"), true);
// 執行后續自己的操作   
$row = $this->users_model->where(array('openid' => $userinfo['openid']))->find();
 // 執行后續自己的操作

  3)微信js自定義分享

a.建立好JSSDK類文件

b.封裝好分享參數並實例化JSSDK

c.頁面引入接口js文件,配置接口config調用

<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
// 引入接口js文件
    <script type="text/javascript">
              //  接口配置
        wx.config({
          debug: false,
          appId: '<?php echo $signPackage["appId"];?>',
          timestamp: <?php echo $signPackage["timestamp"];?>,
          nonceStr: '<?php echo $signPackage["nonceStr"];?>',
          signature: '<?php echo $signPackage["signature"];?>',
          jsApiList: [
            // 所有要調用的 API 都要加到這個列表中
            'onMenuShareTimeline',
            'onMenuShareAppMessage'
          ]
        });
        wx.ready(function () {
            wx.checkJsApi({
           jsApiList: [
            'onMenuShareTimeline',
            'onMenuShareAppMessage'
           ],
          success: function (res) {
           // alert(JSON.stringify(res));
          }
          });
          // 在這里調用 API
          wx.onMenuShareTimeline({
          title: "{$share['title']}", // 分享標題
          link: "{$share['url']}", // 分享鏈接
          imgUrl: "http://{$serverdo}{$share['img_url']}", // 分享圖標
          success: function () { 
              // 用戶確認分享后執行的回調函數
                $.ajax({
                    type : 'post',
                    url : '{:U("portal/member/share_success")}',
                    data : {
                        'silver_bean' : 10
                    },
                    dataType : 'json',
                    success:function(){

                    }
                });
            
          },
          cancel: function () { 
              // 用戶取消分享后執行的回調函數
          }
          });

  4)微信公眾號調用jsapi支付

 

2.http://www.cnblogs.com/0201zcr/p/5131602.html

3.http://www.cnblogs.com/txw1958/p/weixin-js-sharetimeline.html

4.http://baijunyao.com/article/78

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM