1、修改app.js
// app.js App({ onLaunch() { // 登录 wx.login({ success: res => { // 发送 res.code 到后台换取 openId, sessionKey, unionId if (res.code) { // console.log(res.code) var that = this wx.request({ url: that.globalData.url + 'api/sys_Login/LoginIn_WeChat', data: { code: res.code }, header: { "content-type": "application/json" }, method: 'POST', success: res=> { // console.log(res); that.globalData.openid = res.data; wx.setStorage({ key: 'openid', data: res.data, }) } }) } else { console.log('登录失败!') } } }) }, globalData: { url: 'http://127.0.0.1:8888/', token: null, openid: null, userInfo: null, } })
2、修改index.
<view class="usermotto"> <button type="primary" bindtap="getMyWork">微信登陆工作台</button> </view>
getMyWork(e){ const openid = wx.getStorageSync('openid') || "" wx.request({ url: app.globalData.url + 'api/sys_Login/LoginIn_token', data: { "openid": openid}, method: 'GET', success:res=>{ if (res.data =='noUserInfo'){ //不存在用户 wx.getUserProfile({ desc: '展示用户信息', success: res => { // console.log(res) wx.request({ url: app.globalData.url + 'api/sys_Login/addUser_WeChat', data: { "openid": app.globalData.openid, "nickname": res.userInfo.nickName, "avatarUrl": res.userInfo.avatarUrl, "gender": res.userInfo.gender, }, header: { 'content-type': 'application/json' //默认值 }, method: 'POST', success: res => { // console.log(res) if(res){ wx.navigateTo({ url: '../logs/logs' }) } } }) this.setData({ userInfo: res.userInfo, hasUserInfo: true }) } }) }else{ if(res.data=='hasUserInfo'){ wx.navigateTo({ url: '../logs/logs' }) }else{ // console.log(res.data) app.globalData.token = res.data wx.setStorage({key: 'token',data: res.data}) wx.navigateTo({ url: '../logs/logs' }) } } } }) }
标签:openid,新建,微信,globalData,url,WeChat,res,data,wx From: https://www.cnblogs.com/shiliumu/p/16895447.html