登录页面
<button bind:tap="getUserProfile">授权</button>
js处理事件
Page({ /** * 页面的初始数据 */ data: { userInfo: {}, hasUserInfo: false, avatarUrl: '', nickname: '', }, getUserProfile(e) { wx.getUserProfile({ desc: '用于完善用户信息', success: (res) => { // console.log(res.userInfo) // 获取code码 this.setData({ userInfo: res.userInfo, nick_name: res.userInfo.nickName, avatarUrl: res.userInfo.avatarUrl, hasUserInfo: true }) wx.login({ success: (res) => { let code = res.code; var userInfo = this.data.userInfo var nickname = this.data.nick_name var url = this.data.avatarUrl console.log(nickname); if (code) { wx.request({ url: 'http://www.zf.com/api/wxlogin', //开发者服务器接口地址", data: { code: code, nickname: nickname, url: url }, //请求的参数", method: 'POST', dataType: 'json', //如果设为json,会尝试对返回 success: res => { console.log(res.data.data); if (res.data.code == 200) { wx.setStorageSync('token', res.data.data, 7200 - 60); wx.switchTab({ url: '/pages/find/find' })//登录成功跳转底部选项卡我的页面 } }, }); } } }) }, }) }, })
php后台代码
public function wxlogin(Request $request){ $code['code']=$request['code']; $url =sprintf(config('wx.code'),$code['code']); // $url = sprintf($url, $appid, $appkey, $code); $info= json_decode(file_get_contents($url),true); unset($code['session_key']); $code['openid']=$code['code']; $token= md5(md5($code['openid']),time()).rand(1,1000); // print_r($code['openid']);die(); Cache::set($token,7200); // Renting::create($code['openid']); // Renting::create($token); dd($token); }