赞
踩
- //app.js
- App({
- onLaunch: function() {
- wx.login({
- success: function(res) {
- if (res.code) {
- //发起网络请求
- wx.request({
- url: 'https://test.com/onLogin',
- data: {
- code: res.code
- }
- })
- } else {
- console.log('获取用户登录态失败!' + res.errMsg)
- }
- }
- });
- }
- })
后端获取数据方法如下:
- //获取用户openid
- function getopenid(){
- $js_code = I('post.code');
- if(empty($js_code)) return array('status'=>0,'info'=>'缺少js_code');
-
- $appid = 'xxxxxxxxxxxxx';
- $appsecret = 'xxxxxxxxxxxxxxxxxxxxxx';
- $curl = 'https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code';
- $curl = sprintf($curl,$appid,$appsecret,$js_code);
- $result = request($curl);
- return array('status'=>1,'info'=>json_decode($result,true));
- }
数据返回说明
- //正常返回的JSON数据包
- {
- "openid": "OPENID",
- "session_key": "SESSIONKEY",
- "unionid": "UNIONID"
- }
- //错误时返回JSON数据包(示例为Code无效)
- {
- "errcode": 40029,
- "errmsg": "invalid code"
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。