授權圖片展示
php代碼實現
public function index() { // 獲取code $code = input("code"); // 獲取昵稱 $nickname = input("nickname"); // // 從微信公眾平台獲得appid $appid = "wxeb284243ebf4674f"; // // 從微信公眾平台獲得secret $secret = "e3e7a8ef60ee32842ef1e6313ebadfb0"; // // 發送請求換取openid和sessionkey $url = "https://api.weixin.qq.com/sns/jscode2session?appid=$appid&secret=$secret&js_code=$code&grant_type=authorization_code"; // 發送請求 $res=json_decode(file_get_contents($url),true); // 組裝數據 $info=[ 'nikename'=>$nickname, 'appid' => $res['openid'], 'sceret' =>$res['session_key'] ]; // 判斷庫里是用戶數據是否存在 $kot=Db::table('sqdl')->where('appid',$info['appid'])->find(); if ($kot){ return json(['data'=>$kot,'msg'=>'success','code'=>200]); } // 判斷數據是否入庫 if(Db::table('sqdl')->insert($info)){ $info['id']=Db::getLastInsID(); return json(['data'=>$info,'msg'=>'success','code'=>200]); }else{ return json(['data'=>$info,'msg'=>'success','code'=>500]); } }
js實現:
bindGetUserInfo(evt){ // // 判斷是否獲取到用戶信息 if(evt.detail.userInfo){ // 無線上appid直接跳轉到初始頁面 wx.switchTab({ url: '/pages/dome/dome', }) // 有線上appid可以使用下面代碼執行 // // console.log(evt); // wx.login({ // success (res) { // // console.log(res) // if (res.code) { // // console.log(res.code) // // 發起網絡請求 // wx.request({ // url: 'http://www.minmin.com/thankapi/public/index.php/admin/good/index', // data: {code: res.code,nickname:evt.detail.userInfo.nickName}, // success(e){ // // console.log(e.data.code); // if(e.data.code==200){ // wx.switchTab({ // url: '/pages/dome/dome', // }) // } // } // }) // } else { // console.log('登錄失敗!' + res.errMsg) // } // } // }) }else{ // 用戶拒絕授權登錄 console.log('不讓你登錄'); } }
wxml實現:
<view> <view class='headView'> <view class='headImageView'> <image class='headImage' src='/img/ganguo.png' mode='scaleToFill'></image> </view> <view class='titleText'>申請獲取以下權限</view> <view class='contentText'>獲得你的公開信息(昵稱,頭像,手機等)</view> <button class='authBtn' type='primary' open-type='getUserInfo' bind:getuserinfo='bindGetUserInfo'>授權登錄</button> </view> </view>
wxss實現:
.headView { margin: 90rpx 50rpx 90rpx 50rpx; /*上右下左*/ } .headImageView { display: block; margin-left: 25px; margin-top: 25px; margin-right: 25px; margin-bottom: 0px; height: 50px; } .headImage { display: flex; width: 150px; height: 80px; } .titleText { margin-left: 25px; margin-top: 25px; margin-bottom: 10px; font-size: 14px; color: #020e0f; text-align: center; } .contentText { margin-left: 25px; margin-top: 0px; margin-bottom: 0px; font-size: 14px; color: #666; text-align: center; } .authBtn { margin-top: 35px; margin-left: 25px; margin-right: 25px; height: 45px; font-size: 17.5px; }