1、設置頁面
1.1 新建setting頁面
app.json
image.png
image.png
1.2 在tab選項卡中加入setting頁面
app.json
image.png
image.png
1.3 setting頁面的骨架
setting.wxml
<view class="container">
<view class="category-item personal-info">
<view class="user-avatar">
<image src="{{userInfo.avatarUrl}}"></image>
</view>
<view class="user-name">
<view class="user-nickname">
<text>{{userInfo.nickName}}</text>
</view>
<view class="user-locaiton">
<text>所在地:{{userInfo.city}}</text>
</view>
</view>
</view>
<view class="category-item">
<block wx:for="{{cache}}">
<view class="detail-item" catchtap="{{item.tap}}">
<image src="{{item.iconurl}}"></image>
<text>{{item.title}}</text>
<view class="detail-item-btn"></view>
</view>
</block>
</view>
<view class="category-item">
<block wx:for="{{device}}">
<view class="detail-item" catchtap="{{item.tap}}">
<image src="{{item.iconurl}}"></image>
<text>{{item.title}}</text>
<view class="detail-item-btn"></view>
</view>
</block>
</view>
<view class="category-item">
<block wx:for="{{api}}">
<view class="detail-item" catchtap="{{item.tap}}">
<image src="{{item.iconurl}}"></image>
<text>{{item.title}}</text>
<view class="detail-item-btn"></view>
</view>
</block>
</view>
<view class="category-item">
<block wx:for="{{others}}">
<view class="detail-item" catchtap="{{item.tap}}">
<image src="{{item.iconurl}}"></image>
<text>{{item.title}}</text>
<view class="detail-item-btn"></view>
</view>
</block>
</view>
</view>
由于每個面板下的子項較多,且有可能經常添加子項,因此沒有直接將子項硬編碼,而是采用了一種配置式的編寫方法。每個項目下的子項目內容都將在js文件中配置,然后通過數據綁定和列表渲染動態(tài)的填充到wxml中
1.4 setting頁面的樣式
setting.wxss
.container {
background-color: #efeff4;
width: 100%;
height: 100%;
flex-direction: column;
display: flex;
align-items: center;
min-height: 100vh;
}
.category-item {
width: 100%;
margin: 20rpx 0;
border-top: 1rpx solid #d9d9d9;
border-bottom: 1rpx solid #d9d9d9;
background-color: #fff;
}
.category-item.personal-info {
height: 130rpx;
display: flex;
padding: 20rpx 0;
}
.category-item.personal-info .user-avatar {
margin: 0 30rpx;
width: 130rpx;
height: 130rpx;
position: relative;
}
.category-item.personal-info .user-avatar image {
vertical-align: top;
width: 100%;
height: 100%;
position: absolute;
top: 0;
left:0;
border-radius: 2rpx;
}
.category-item.personal-info .user-name {
margin-right: 30rpx;
flex: 1;
padding-top: 10rpx;
}
.detail-item{
display: flex;
margin-left: 30rpx;
border-bottom: 1px solid RGBA(217, 217, 217, .4);
height:85rpx;
align-items: center;
}
.detail-item:last-child{
border-bottom:none;
}
.detail-item image{
height: 40rpx;
width:40rpx;
}
.detail-item text{
color:#7F8389;
font-size:24rpx;
flex:1;
margin-left: 30rpx;
}
.detail-item .detail-item-btn{
width: 50rpx;
color: #d9d9d9;
height: 40rpx;
margin-right: 20rpx;
text-align: center;
}
.detail-item .detail-item-btn::after{
display: inline-block;
content:'';
width: 16rpx;
height: 16rpx;
color: #d9d9d9;
margin-top: 8rpx;
border:3rpx #d9d9d9 solid;
border-top-color:transparent;
border-left-color:transparent;
transform: rotate(-45deg);
}
1.5 setting頁面的配置
setting.js
Page({
/**
* 頁面的初始數據
*/
data: {
cache: [
{ iconurl: '/images/icon/wx_app_clear.png', title: '緩存清理', tap: 'clearCache' }
],
device: [
{ iconurl: '/images/icon/wx_app_cellphone.png', title: '系統(tǒng)信息', tap: 'showSystemInfo' },
{ iconurl: '/images/icon/wx_app_network.png', title: '網絡狀態(tài)', tap: 'showNetWork' },
{ iconurl: '/images/icon/wx_app_location.png', title: '地圖顯示', tap: 'showMap' },
{ iconurl: '/images/icon/wx_app_compass.png', title: '指南針', tap: 'showCompass' },
{ iconurl: '/images/icon/wx_app_lonlat.png', title: '當前位置逞刷、速度', tap: 'showLonLat' },
{ iconurl: '/images/icon/wx_app_shake.png', title: '搖一搖', tap: 'shake' },
{ iconurl: '/images/icon/wx_app_scan_code.png', title: '二維碼', tap: 'scanQRCode' }
],
api: [
{ iconurl: '/images/icon/wx_app_list.png', title: '下載pdf、word文檔', tap: 'downloadDocumentList' },
{ iconurl: '', title: '用戶登陸', tap: 'login' },
{ iconurl: '', title: '校驗用戶信息', tap: 'check' },
{ iconurl: '', title: '獲取用戶加密信息', tap: 'decrypted' },
{ iconurl: '', title: '模板消息', tap: 'tplMessage' },
{ iconurl: '', title: '微信支付', tap: 'wxPay' }
],
others: [
{ iconurl: '', title: 'wx:key示例', tap: 'showWxKeyDemo' },
{ iconurl: '', title: 'scroll-view高級用法演示', tap: 'showScrollViewDemo' }
],
},
/**
* 生命周期函數--監(jiān)聽頁面加載
*/
onLoad: function (options) {
},
/**
* 生命周期函數--監(jiān)聽頁面初次渲染完成
*/
onReady: function () {
},
/**
* 生命周期函數--監(jiān)聽頁面顯示
*/
onShow: function () {
},
/**
* 生命周期函數--監(jiān)聽頁面隱藏
*/
onHide: function () {
},
/**
* 生命周期函數--監(jiān)聽頁面卸載
*/
onUnload: function () {
},
/**
* 頁面相關事件處理函數--監(jiān)聽用戶下拉動作
*/
onPullDownRefresh: function () {
},
/**
* 頁面上拉觸底事件的處理函數
*/
onReachBottom: function () {
},
/**
* 用戶點擊右上角分享
*/
onShareAppMessage: function () {
}
})
image.png
1.6 setting頁面導航欄配置
setting.json
{
"navigationBarTextStyle": "white",
"navigationBarTitleText": "設置"
}
image.png
2妻熊、獲取用戶基本信息
2.1 新建g_userInfo全局變量
app.js
image.png
2.2 獲取用戶基本信息
image.png
因為小程序API的更新夸浅,需要用戶主動觸發(fā)授權才可以獲取用戶的基本信息,因此將授權工作放在歡迎頁面的點擊按鈕上
image.png
welcome.js
var app = getApp();
Page({
/**
* 頁面的初始數據
*/
data: {
canIUse: wx.canIUse('button.open-type.getUserInfo'),//檢查有沒有使用權限
loginCode:null
},
onGotUserInfo:function(){
var that = this;
wx.getSetting({
success: function (res) {
console.log("授權結果成功:" + res)
if (res.authSetting['scope.userInfo']) {
// 已經授權扔役,可以直接調用 getUserInfo 獲取頭像昵稱
wx.getUserInfo({
lang:"zh_CN",
withCredentials: true,
success: function (res) {
app.globalData.g_userInfo = res.userInfo;
// //將用戶的基本信息保存到緩存中
wx.setStorageSync("user", res.userInfo);
console.log("用戶基本信息獲取成功:" + res.userInfo)
},
fail: function (res) {
console.log("用戶基本信息獲取失敺:" + res)
}
})
}
that.jumpToPostPage();
},
fail: function () {
console.log("授權結果失敗:" + res)
}
})
},
getUserInfo: function () {
var userInfoStorage = wx.getStorageSync("user");
if (!userInfoStorage) {
console.log("緩存中沒有用戶基本信息")
//如果緩存中沒有用戶信息厅目,那么獲取用戶信息
var that = this;
wx.login({
success: function (res) {
console.log("登錄成功:" + res.code);
that.setData({
loginCode: res.code
})
}
})
} else {
//如果緩存中已經有用戶信息番枚,那么將用戶信息保存到全局變量中
console.log("緩存中已經有用戶基本信息")
app.globalData.g_userInfo = userInfoStorage;
}
},
//跳轉到post列表頁面
jumpToPostPage:function(){
wx.switchTab({
url: '../post/post',
success: function () {
console.log("跳轉頁面成功")
},
fail: function () {
console.log("跳轉頁面失敗")
},
complete: function () {
console.log("跳轉頁面完成")
},
});
},
/**
* 生命周期函數--監(jiān)聽頁面加載
*/
onLoad: function (options) {
this.getUserInfo();
},
/**
* 生命周期函數--監(jiān)聽頁面初次渲染完成
*/
onReady: function () {
},
/**
* 生命周期函數--監(jiān)聽頁面顯示
*/
onShow: function () {
},
/**
* 生命周期函數--監(jiān)聽頁面隱藏
*/
onHide: function () {
console.log("歡迎頁面被隱藏")
},
/**
* 生命周期函數--監(jiān)聽頁面卸載
*/
onUnload: function () {
console.log("歡迎頁面被卸載")
},
/**
* 頁面相關事件處理函數--監(jiān)聽用戶下拉動作
*/
onPullDownRefresh: function () {
},
/**
* 頁面上拉觸底事件的處理函數
*/
onReachBottom: function () {
},
/**
* 用戶點擊右上角分享
*/
onShareAppMessage: function () {
}
})
image.png
2.3 獲取用戶信息并綁定
setting.js
image.png
image.png
image.png
3法严、數據緩存的異步操作
使用異步的清除緩存方法: wx.clearStorage
3.1 showModal公共方法
setting.js
//顯示模態(tài)窗口
showModal:function(title,content,callback){
wx.showModal({
title: title,
content: content,
confirmColor:"#1F4BA5",
cancelColor:"#7FB389",
success:function(res){
if (res.confirm){
// callback;
callback && callback();
}
}
})
},
//緩存清理
clearCache:function(){
this.showModal("緩存清理","確定要清除本地緩存嗎损敷?",function(){
wx.clearStorage({
success:function(msg){
wx.showToast({
title: '緩存清理成功',
duration:1000,
mask:true,
icon:"success"
})
},
fail:function(e){
console.log(e)
}
});
})
}
image.png
image.png
4、獲取系統(tǒng)信息
4.1 新增device頁面
app.json
image.png
4.2 showDeviceInfo函數
setting.js
image.png
4.3 device子頁面的骨架
device.wxml
<view class="container">
<view class="category-item personal-info">
<view class="user-avatar">
<image src="{{userInfo.avatarUrl}}"></image>
</view>
<view class="user-name">
<view class="user-nickname">
<text>{{userInfo.nickName}}</text>
</view>
<view class="user-locaiton">
<text>所在地:{{userInfo.city}}</text>
</view>
</view>
</view>
<view class="category-item">
<block wx:for="{{cache}}">
<view class="detail-item" catchtap="{{item.tap}}">
<image src="{{item.iconurl}}"></image>
<text>{{item.title}}</text>
<view class="detail-item-btn"></view>
</view>
</block>
</view>
<view class="category-item">
<block wx:for="{{device}}">
<view class="detail-item" catchtap="{{item.tap}}">
<image src="{{item.iconurl}}"></image>
<text>{{item.title}}</text>
<view class="detail-item-btn"></view>
</view>
</block>
</view>
<view class="category-item">
<block wx:for="{{api}}">
<view class="detail-item" catchtap="{{item.tap}}">
<image src="{{item.iconurl}}"></image>
<text>{{item.title}}</text>
<view class="detail-item-btn"></view>
</view>
</block>
</view>
<view class="category-item">
<block wx:for="{{others}}">
<view class="detail-item" catchtap="{{item.tap}}">
<image src="{{item.iconurl}}"></image>
<text>{{item.title}}</text>
<view class="detail-item-btn"></view>
</view>
</block>
</view>
</view>
4.4 device子頁面的樣式
device.wxss
.container {
background-color: #efeff4;
width: 100%;
height: 100%;
flex-direction: column;
display: flex;
align-items: center;
min-height: 100vh;
}
.category-item {
width: 100%;
margin: 20rpx 0;
border-top: 1rpx solid #d9d9d9;
border-bottom: 1rpx solid #d9d9d9;
background-color: #fff;
}
.detail-item{
display: flex;
margin-left: 30rpx;
border-bottom: 1px solid RGBA(217, 217, 217, .4);
height:85rpx;
align-items: center;
}
.detail-item:last-child{
border-bottom:none;
}
.detail-item text{
color:#7F8389;
font-size:24rpx;
flex:1;
}
.detail-item text:last-child{
color:#7F8389;
font-size:24rpx;
flex:1;
text-align: right;
padding-right: 20rpx;
}
4.5 獲取系統(tǒng)信息
device.js
Page({
data: {
phoneInfo: [],
softInfo: [],
screenInfo: [],
},
onLoad: function () {
var that = this;
wx.getSystemInfo({
success: function (res) {
console.log(res)
that.setData({
phoneInfo: [
{ key: "手機型號", val: res.model },
{ key: "手機語言", val: res.language }
],
softInfo: [
{ key: "微信版本", val: res.version },
{ key: "操作系統(tǒng)版本", val: res.system },
{ key: "客戶端平臺", val: res.platform }
],
screenInfo: [
{ key: "屏幕像素比", val: res.pixelRatio },
{ key: "屏幕尺寸", val: res.windowWidth + '×' + res.windowHeight }
]
});
}
});
wx.setNavigationBarTitle({
title: '系統(tǒng)信息',
})
}
});
image.png
5深啤、獲取網絡狀態(tài)
image.png
image.png
5.1 獲取網絡狀態(tài)信息
setting.js
image.png
image.png
6拗馒、獲取當前位置信息和當前速度信息
image.png
image.png
6.1 獲取當前位置和速度信息
setting.js
//獲取當前位置經緯度和當前速度
getLonLat:function(callback){
var that = this;
wx.getLocation({
type: "gcj02",
success: function(res) {
callback(res.longitude,res.latitude,res.speed);
},
})
},
6.2 顯示當前位置和速度信息
setting.js
showLonLat:function(){
var that = this;
this.getLonLat(function(lon,lat,speed){
console.log("lon=========" + lon);
console.log("lat=========" + lat);
var lonStr = lon >= 0 ? '東經' : '西經',
latStr = lat >= 0 ? '北緯' : '南緯';
//保留2位小數
lon = lon.toFixed(2);
lat = lat.toFixed(2);
console.log("lon.toFixed(2)=========" + lon);
console.log("lat.toFixed(2)=========" + lat);
lonStr += lon;
latStr += lat;
speed = (speed || 0).toFixed(2);
that.showModal('當前位置和速度', '當前位置:' + lonStr + ',' + latStr + '。速度:' + speed + 'm/s');
});
}
image.png
image.png
7溯街、使用微信內置地圖查看位置信息
image.png
image.png
7.1 在地圖上顯示坐標點
setting.js
//在地圖上顯示坐標點
showMap:function(){
this.getLonLat(function(lon,lat){
wx.openLocation({
latitude: lat,
longitude: lon,
scale:15,
name:"荔枝樓",
address:"中山大道車陂路口高地大街76號高地大廈2樓",
fail:function(){
wx.showToast({
title: '地圖打開失敗',
duration:100,
icon:"cancel"
})
}
})
});
}
image.png
8诱桂、監(jiān)聽羅盤數據制作一個簡易指南針
image.png
image.png
image.png
image.png
8.1 在地圖上顯示坐標點
setting.wxml
image.png
8.2 初始化變量值
setting.js
image.png
8.3 顯示羅盤
setting.js
//顯示羅盤
showCompass:function(){
var that = this;
this.setData({
compassHidden:false
})
wx.onCompassChange(function(res){
if(!that.data.compassHidden){
this.setData({
compassVal:res.direction.toFixed(2)
})
}
})
},
8.4 隱藏羅盤
setting.js
//隱藏羅盤
hideCompass:function(){
this.setData({
compassHidden:true
})
}
image.png
9、在小程序中實現(xiàn)搖一搖
image.png
9.1 添加搖一搖計數面板
setting.wxml
image.png
9.2 添加shakeInfo和shakeData變量
setting.js
image.png
9.3 搖一搖具體實現(xiàn)
setting.js
//搖一搖
shake: function () {
var that = this;
//啟用搖一搖
this.gravityModalConfirm(true);
wx.onAccelerometerChange(function (res) {
//搖一搖核心代碼呈昔,判斷手機晃動幅度
var x = res.x.toFixed(4),
y = res.y.toFixed(4),
z = res.z.toFixed(4);
var flagX = that.getDelFlag(x, that.data.shakeData.x),
flagY = that.getDelFlag(y, that.data.shakeData.y),
flagZ = that.getDelFlag(z, that.data.shakeData.z);
that.data.shakeData = {
x: res.x.toFixed(4),
y: res.y.toFixed(4),
z: res.z.toFixed(4)
};
if (flagX && flagY || flagX && flagZ || flagY && flagZ) {
// 如果搖一搖幅度足夠大挥等,則認為搖一搖成功
if (that.data.shakeInfo.enabled) {
that.data.shakeInfo.enabled = false;
that.playShakeAudio();
}
}
});
},
9.4 開啟或關閉搖一搖
setting.js
//啟用或者停用搖一搖功能
gravityModalConfirm: function (flag) {
if (flag !== true) {
flag = false;
}
var that = this;
this.setData({
shakeInfo: {
gravityModalHidden: !that.data.shakeInfo.gravityModalHidden,
num: 0,
enabled: flag
}
})
},
9.5 計算偏移量
setting.js
//計算搖一搖的偏移量
getDelFlag: function (val1, val2) {
return (Math.abs(val1 - val2) >= 1);
},
9.6 搖一搖成功后播放聲音
setting.js
// 搖一搖成功后播放聲音并累加搖一搖次數
playShakeAudio: function () {
var that = this;
wx.playBackgroundAudio({
dataUrl: 'http://7xqnxu.com1.z0.glb.clouddn.com/wx_app_shake.mp3',
title: '',
coverImgUrl: ''
});
wx.onBackgroundAudioStop(function () {
that.data.shakeInfo.num++;
that.setData({
shakeInfo: {
num: that.data.shakeInfo.num,
enabled: true,
gravityModalHidden: false
}
});
});
},
10、掃碼
image.png
image.png
10.1實現(xiàn)掃碼功能
setting.js
//實現(xiàn)掃碼功能
scanQRCode:function(){
var that = this;
wx.scanCode({
success:function(res){
console.log(res);
that.showModal("掃描二維碼/條形碼",res.result,false);
},
fail:function(res){
that.showModal("掃描二維碼/條形碼", "掃描失敗堤尾,請重試肝劲!", false);
}
})
}
11、獲取小程序頁面二維碼
- 小程序提供了一個在線二維碼生成接口:
https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=ACCESS_TOKEN
1)HTTP類型為POST
2)接收的參數:path 、width(默認430)
3)調用接口前要先獲取access_token
獲取方法:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140183
12辞槐、下載并預覽pdf掷漱、word等多種類型文檔
12.1 新增download頁面
app.json
image.png
12.2 跳轉到download頁面
setting.js
image.png
12.3 download頁面的骨架代碼
dowmload.wxml
<view class="container">
<view class="file-type-head">
<text>文件類型</text>
</view>
<view class="category-item">
<block wx:for="{{fileTypeList}}">
<view class="detail-item" catchtap="downloadFile" data-type="{{item.type}}">
<image src="{{item.iconurl}}"></image>
<text>{{item.title}}</text>
<view class="detail-item-btn"></view>
</view>
</block>
</view>
</view>
12.4 download頁面的樣式代碼
dowmload.wxss
.file-type-head {
display: flex;
align-items: center;
height: 70rpx;
width: 100%;
text-indent: 30rpx;
}
.file-type-head text {
font-size: 32rpx;
}
.category-item {
margin-top: 0;
}
.container {
background-color: #efeff4;
width: 100%;
height: 100%;
flex-direction: column;
display: flex;
align-items: center;
min-height: 100vh;
}
.category-item {
width: 100%;
margin: 20rpx 0;
border-top: 1rpx solid #d9d9d9;
border-bottom: 1rpx solid #d9d9d9;
background-color: #fff;
}
.category-item.personal-info {
height: 130rpx;
display: flex;
padding: 20rpx 0;
}
.category-item.personal-info .user-avatar {
margin: 0 30rpx;
width: 130rpx;
height: 130rpx;
position: relative;
}
.category-item.personal-info .user-avatar image {
vertical-align: top;
width: 100%;
height: 100%;
position: absolute;
top: 0;
left: 0;
border-radius: 2rpx;
}
.category-item.personal-info .user-name {
margin-right: 30rpx;
flex: 1;
padding-top: 10rpx;
}
.detail-item {
display: flex;
margin-left: 30rpx;
border-bottom: 1px solid RGBA(217, 217, 217, 0.4);
height: 85rpx;
align-items: center;
}
.detail-item:last-child {
border-bottom: none;
}
.detail-item image {
height: 40rpx;
width: 40rpx;
}
.detail-item text {
color: #7f8389;
font-size: 24rpx;
flex: 1;
margin-left: 30rpx;
}
.detail-item .detail-item-btn {
width: 50rpx;
color: #d9d9d9;
height: 40rpx;
margin-right: 20rpx;
/*background-color: red;*/
text-align: center;
}
.detail-item .detail-item-btn::after {
display: inline-block;
content: '';
width: 16rpx;
height: 16rpx;
color: #d9d9d9;
margin-top: 8rpx;
border: 3rpx #d9d9d9 solid;
border-top-color: transparent;
border-left-color: transparent;
transform: rotate(-45deg);
}
12.5 download頁面的樣式代碼
dowmload.js
image.png
12.6 下載并且預覽文檔
dowmload.js
downloadFile: function (event) {
var type = event.currentTarget.dataset.type,
url = 'https://coding.net/u/airbreak/p/wx_app_files/git/raw/master/top10.';
switch (type) {
case "pdf":
url += 'pdf';
break;
case "word":
url += 'docx';
break;
case "excel":
url += 'xlsx';
break;
default:
url += 'pptx';
break;
}
wx.downloadFile({
url: url,
success: function (res) {
var filePath = res.tempFilePath;
console.log(filePath);
wx.openDocument({
filePath: filePath,
success: function (res) {
console.log('打開文檔成功')
},
fail: function (res) {
console.log(res)
}, complete: function (res) {
console.log(res);
}
})
},
fail: function () {
console.log('下載失敗');
}
})
},
image.png
image.png
image.png