作一款小程序,若是須要判斷用戶,固然要獲取一些基本信息,例如頭像,暱稱,openid。因此本次案例就直接上代碼了。php
index.wxmlhtml
<!--index.wxml--> <view class="container"> <view class="userinfo"> <button wx:if="{{!hasUserInfo && canIUse}}" open-type="getUserInfo" bindgetuserinfo="getUserInfo"> 獲取頭像暱稱 </button> <block wx:else> <image bindtap="bindViewTap" class="userinfo-avatar" src="{{userInfo.avatarUrl}}" mode="cover"></image> <text class="userinfo-nickname">{{userInfo.nickName}}</text> </block> </view> <view class="usermotto"> <text class="user-motto">{{motto}}</text> </view> </view>
index.js前端
//index.js //獲取應用實例 const app = getApp() Page({ data: { motto: 'Hello World', userInfo: {}, hasUserInfo: false, canIUse: wx.canIUse('button.open-type.getUserInfo') }, onLoad: function () { if (app.globalData.userInfo) { this.setData({ userInfo: app.globalData.userInfo, hasUserInfo: true }) } else if (this.data.canIUse){ // 因爲 getUserInfo 是網絡請求,可能會在 Page.onLoad 以後才返回 // 因此此處加入 callback 以防止這種狀況 app.userInfoReadyCallback = res => { this.setData({ userInfo: res.userInfo, hasUserInfo: true }) } } else { // 在沒有 open-type=getUserInfo 版本的兼容處理 wx.getUserInfo({ success: res => { app.globalData.userInfo = res.userInfo this.setData({ userInfo: res.userInfo, hasUserInfo: true }) } }) } }, getUserInfo: function(e) { console.log(e) app.globalData.userInfo = e.detail.userInfo //獲取openid wx.login({ success: function (res) { console.log(res.code) //發送請求獲取openid wx.request({ url: '你的域名/openid.php?code=code', //接口地址 data: { code: res.code }, header: { 'content-type': 'application/json' //默認值 }, success: function (res) { //返回openid console.log(res.data.openid) //向數據庫註冊用戶,驗證用戶 var that = this; wx.request({ url: '你的域名/server.php?nickname=' + e.detail.userInfo.nickName + '&avatarUrl=' + e.detail.userInfo.avatarUrl + '&openid=' + res.data.openid, data: { }, header: { 'content-type': 'application/json' }, success: function (res) { //打印用戶信息 console.log(res.data) } }) } }) } }) this.setData({ userInfo: e.detail.userInfo, hasUserInfo: true, }) } })
index.wxssmysql
/**index.wxss**/ .userinfo { display: flex; flex-direction: column; align-items: center; } .userinfo-avatar { width: 128rpx; height: 128rpx; margin: 20rpx; border-radius: 50%; } .userinfo-nickname { color: #aaa; } .usermotto { margin-top: 200px; }
openid.php
這是經過code獲取openid的後端sql
<?php //聲明CODE,獲取小程序傳過來的CODE $code = $_GET["code"]; //配置appid $appid = "你的小程序APPID"; //配置appscret $secret = "你的小程序APPSRCRET"; //api接口 $api = "https://api.weixin.qq.com/sns/jscode2session?appid={$appid}&secret={$secret}&js_code={$code}&grant_type=authorization_code"; //獲取GET請求 function httpGet($url){ $curl = curl_init(); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_TIMEOUT, 500); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true); curl_setopt($curl, CURLOPT_URL, $url); $res = curl_exec($curl); curl_close($curl); return $res; } //發送 $str = httpGet($api); echo $str; ?>
server.php
這是把用戶名、頭像、openid存到數據庫的後端數據庫
<?php header("Content-type:text/html;charset=utf8"); $nickname = $_GET["nickname"]; $avatarUrl = $_GET["avatarUrl"]; $openid = $_GET["openid"]; $con = mysql_connect("數據庫地址","數據庫帳號","數據庫密碼"); mysql_select_db("數據庫名", $con); mysql_query("INSERT INTO 表名 (nickname, avatarUrl, openid) VALUES ('$nickname', '$avatarUrl', '$openid')"); echo "註冊成功!"; mysql_close($con); ?>