为什么我开发工具可以获取登录openID,手机不行呢?
发布于 5 年前 作者 weihao 13494 次浏览 来自 问答

为什么我开发工具可以获取登录openID,手机不行呢?,我小程序没有认证。。。

3 回复

代码在APP.JS 中的

onLaunch  中写 ,如下:

// 校验用户当前session_key是否有效。

wx.checkSession({

success: function () {

//session_key 未过期,并且在本生命周期一直有效

wx.showToast({

title: ‘memberid:’ + wx.getStorageSync(‘memberid’),

});

wx.showModal({

title: ‘’+wx.getStorageSync(‘memberid’),

content: ‘’+wx.getStorageSync(‘openid’),

})

if (wx.getStorageSync(‘openid’)==’’ ){

wx.redirectTo({

url: ‘pages/login/login’,

})

}

console.log(“当前的OPENID:”+wx.getStorageSync(‘openid’))

},

fail: function () {

// session_key 已经失效,需要重新执行登录流程

//重新登录

wx.login({

success: function (res) {

console.log(“当前的微信登录CODE:” + res.code)

if (res.code) {

//发起网络请求

wx.request({

url: that.RequestUrl + “apiuser.php?action=wx_login”,

data: {

code: res.code

},

method: “POST”,

dataType: “json”,

header: {

‘content-type’: ‘application/x-www-form-urlencoded’

},

success: function (res) {

console.log(JSON.stringify(res))

console.log(“当前的返回memberid:” + res.data.m_id)

console.log(“当前的返回openID:”+res.data.openid)

//  根据返回过来的值判断

if (res.data.m_id > 0) {

wx.setStorageSync(‘memberid’, res.data.m_id);

wx.setStorageSync(‘openid’, res.data.openid);

} else {

//已经登录过

// console.log(“res.code” + res.code);

}

}

});

} else {

console.log(‘登录失败!’ + res.errMsg)

}

}

});    

}

})

开调试就有了

怎样获取openId的?请把问题说清楚

回到顶部