码迷,mamicode.com
首页 > 微信 > 详细

微信授权登录+微信公众号支付

时间:2017-06-03 19:15:25      阅读:410      评论:0      收藏:0      [点我收藏+]

标签:授权   erb   ber   调用   用户信息   silver   content   list   sns   

记录工作备忘

  采用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

 

微信授权登录+微信公众号支付

标签:授权   erb   ber   调用   用户信息   silver   content   list   sns   

原文地址:http://www.cnblogs.com/qiuphp/p/6938105.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!