## 重要說明
因小程序和app登錄接口不同,需要在前端進行跨端兼容處理!
## 小程序端必須的配置
小程序端必須配置 app id(申請小程序開發者并獲取 appid 及相關秘鑰,支持個人開發者)。獲取appid后編輯 manifest.json :
```
"mp-weixin" : {
"appid" : "您的app id"
}
```
### 接口地址
https://developers.weixin.qq.com/miniprogram/dev/api/open.html#wxgetuserinfoobject
### app 端必須的配置
app 端支持微信、qq、微博等多種登錄方式,都需要申請對應的開發者并獲取對應的 appid。獲取對應的appid后打開 manifest 可視化操作填寫即可:
### 是否登錄判斷(App.vue)
```
global.isLogin = function(){
try{
var suid = uni.getStorageSync('suid');
var srand = uni.getStorageSync('srand');
}catch(e){
//TODO handle the exception
}
if(suid == '' || srand == ''){
return false;
}else{
return [suid, srand];
}
};
```
### 需要登錄的頁面判斷
```
var res = global.isLogin();
if(!res){
uni.showModal({
title:'請登錄',
content:"請登錄",
success:function(){
uni.navigateTo({
url:"/pages/login"
});
}
})
}
```
## 登錄頁面開發
```
<template>
<view style="padding:35px;">
<!-- #ifdef MP-WEIXIN -->
<button type="primary" open-type="getUserInfo" @getuserinfo="getuserinfo" withCredentials="true">微信登錄</button>
<!-- #endif -->
<!-- #ifdef APP-PLUS -->
<button type="primary" open-type="getUserInfo" @click="getuserinfo" withCredentials="true">微信登錄</button>
<!-- #endif -->
<button style="margin-top:50px;">手機號碼登錄</button>
</view>
</template>
<script>
var _self;
export default {
data:{
},
onLoad:function(){
_self = this;
},
methods:{
getuserinfo : function(res1){
console.log(res1);
//如果只需要opendid 和非加密數據至此登錄完成
//此處連接數據庫利用openid 就可以進行登錄環節
//免費的視頻教程 http://www.hcoder.net/tutorials/info_141.html
wx.login({
success:function(res2){
//獲取 sessionKey
wx.request({
url : 'https:///hoa.hcoder.net/xcxencode/?c=sk&appid=wxbb7f9f1f2c6f4f33&secret=739b970b832f0df158f54c494a08e440&code='+res2.code,
success:function(res3){
console.log(res3);
//記錄到本地
try{
uni.setStorageSync('sk', res3.data.session_key);
uni.setStorageSync('openid', res3.data.openid);
}catch(e){
//TODO handle the exception
}
uni.hideLoading();
//以下步驟可以獲取加密信息,需要授權
//獲取加密信息
if(!res1.detail.iv){
uni.showToast({
title:"您取消了授權,登錄失敗",
icon:"none"
});
return false;
}
try{
var sessionKey = uni.getStorageSync('sk');
console.log(sessionKey);
}catch(e){
//TODO handle the exception
}
uni.request({
/**
* $appid = $_POST['appid'];
$sessionKey = $_POST['sessionKey'];
$encryptedData = $_POST['encryptedData'];
$iv = $_POST['iv'];
*/
method : "POST",
url : 'https:///hoa.hcoder.net/xcxencode/',
header : {'content-type':'application/x-www-form-urlencoded'},
data : {
appid : "wxbb7f9f1f2c6f4f33",
sessionKey : sessionKey,
iv : res1.detail.iv,
encryptedData : res1.detail.encryptedData
},
success:function(res4){
//"???{"openId":"oS6of0V0rdp9nY_BuvCnQUasOHYc","nickName":"深海",
//"gender":1,"language":"zh_CN","city":"Xi'an","province":"Shaanxi",
//"country":"China","avatarUrl":"https://wx.qlogo.cn/mmopen/vi_32/7iags6YD4enyU"
console.log(res4);
//至此登錄完成
}
});
}
})
}
});
}
}
}
</script>
<style>
</style>
```
- 第1講 : 創建項目、部署 VUE 、入口頁面布局
- 第2講,快速開始第一個項目
- 第3講 : uni-app 開發規范及目錄結構
- 第4講 : uni-app 頁面樣式與布局
- 第5講 : uni-app 配置文件 - pages.json
- 第6講 : 配置文件 - manifest.json
- 第7講 : uni-app 頁面生命周期
- 第8講 : uni-app 模板語法 - 數據綁定
- 第9講Class 與 Style 綁定 (動態菜單激活示例)
- 第10講 : uni-app 事件處理、事件綁定、事件傳參
- 第11講 : uni-app 組件 - 基礎組件
- 第12講 : uni-app 組件 - 表單組件
- 第13講 : uni-app 組件 - navigator(導航)及頁
- 第14講 : uni-app 組件 - 媒體組件
- 第15講 : uni-app 組件 - 地圖組件
- 第16講 : uni-app 接口 - 網絡請求
- 第17講 : uni-app 接口 - 從本地相冊選擇圖片或使
- 第18講 : uni-app 上傳(圖片上傳實戰)
- 第19講 : uni-app 接口 - 數據緩存
- 第20講 : uni-app 設備相關
- 第21講 : uni-app 交互反饋
- 第22講 : uni-app 設置導航條
- 第23講 : uni-app 導航(頁面流轉)
- 第24講 : uni-app 下拉刷新
- 第25講 : uni-app 上拉加載更多
- 第26講 : uni-app 第三方登錄(小程序篇)
- 第27講 : uni-app 登錄(h5+ app 篇)
- 第28講 : 自定義組件創建及使用