小程序登陆
发布时间:2020-12-16 23:41:59 所属栏目:百科 来源:网络整理
导读:app.js /* * * 当小程序启动,或从后台进入前台显示,会触发 onShow */ onShow: function (options) { this .getUserInfo(); }, /* * * 获取用户信息 */ getUserInfo: function (cb) { var that = this if (that.globalData.userInfo) { typeof cb == " func
app.js /** * 当小程序启动,或从后台进入前台显示,会触发 onShow */ onShow: function (options) { this.getUserInfo(); },/** * 获取用户信息 */ getUserInfo: function (cb) { var that = this if (that.globalData.userInfo) { typeof cb == "function" && cb(that.globalData.userInfo) } else { wx.login({ success: function (res) { var code = res.code wx.getUserInfo({ success: function (res2) { that.globalData.userInfo = res2.userInfo var data = {encryptedData: res2.encryptedData,iv: res2.iv,code: code} // 存储用户信息 util.commonAjax(‘/recentimgtext/saveuser‘,‘POST‘,data).then(function (resolve) { if (resolve.data.status === 200) { wx.setStorageSync(‘userInfo‘,resolve.data.data) typeof cb == "function" && cb(that.globalData.userInfo) } else { console.log(‘用户信息存储失败‘); } }) },fail: function (res2) { wx.navigateTo({ url: ‘/pages/login/login‘,}) } }) } }) } },/** * 全局数据 */ globalData: { userInfo: null,url: ‘http://ci.test.com‘,// url: ‘http://ci.mag998.com‘, } utils.js // 访问数据 const commonAjax = function (url,types,data) { var app = getApp(); var promise = new Promise(function(resolve,reject,defaults) { wx.request({ url: app.globalData.url + url,data: data,method: (types === ‘GET‘) ? ‘GET‘ : ‘POST‘,header: (types === ‘GET‘) ? {‘content-type‘: ‘application/json‘} : {‘content-type‘: ‘application/x-www-form-urlencoded‘},success: resolve,fail: reject,complete: defaults,}) }) return promise; } module.exports = { formatTime: formatTime,commonAjax: commonAjax } login.wxml <button wx:if="{{!hasUserInfo && canIUse}}" open-type="getUserInfo" bindgetuserinfo="getUserInfo" type=‘primary‘ style=‘margin-top:50px;width:80%;‘> 微信登陆 </button> login.js // pages/login/login.js const app = getApp() Page({ data: { hasUserInfo: app.globalData.userInfo,canIUse: wx.canIUse(‘button.open-type.getUserInfo‘),},getUserInfo (e) { // 防止getUserInfo数据未及时返回 app.globalData.userInfo = e.detail.userInfo wx.navigateBack({ delta: 1 }) } }) saveuser控制器 // 存储用户信息 public function saveuser() { $encryptedData = $this->input->post(‘encryptedData‘); $iv = $this->input->post(‘iv‘); $code = $this->input->post(‘code‘); $url = ‘https://api.weixin.qq.com/sns/jscode2session?appid=‘ . RECENT_IMGTEXT_APPID . ‘&secret=‘ . RECENT_IMGTEXT_APPSECRET . ‘&js_code=‘ . $code . ‘&grant_type=authorization_code‘; $data = send_get($url); $sessionKey = json_decode($data)->session_key; $pc = new WXBizDataCrypt(RECENT_IMGTEXT_APPID,$sessionKey); $errCode = $pc->decryptData($encryptedData,$iv,$data); // $data为用户信息 $message = $errCode; $status = 0; if ($errCode == 0) { $userinfo = json_decode($data,true); $openid = $userinfo[‘openId‘]; unset($userinfo[‘watermark‘]); $checkuser = $this->db->get_where($this->recent_user_table,[‘openId‘ => $openid])->row_array(); if (empty($checkuser)) { $userinfo[‘created_at‘] = date("Y-m-d H:i:s"); if ($this->db->insert($this->recent_user_table,$userinfo)); $message = ‘添加成功‘; $status = 200; } else { unset($userinfo[‘openId‘]); $userinfo[‘updated_at‘] = date("Y-m-d H:i:s"); if ($this->db->update($this->recent_user_table,$userinfo,‘openId = "‘ . $openid . ‘"‘)); $message = ‘更新成功‘; $status = 200; } } echo json_encode([‘data‘ => $data,‘message‘ => $message,‘status‘ => $status]); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |