微信小程序 網(wǎng)絡(luò)通信實現(xiàn)詳解
關(guān)于網(wǎng)絡(luò)通信,這里我使用的是wx.request,官方代碼示例如下:
wx.request({
url: 'test.php', //僅為示例,并非真實的接口地址
data: {
x: '',
y: ''
},
header: {
'content-type': 'application/json' // 默認(rèn)值
},
success (res) {
console.log(res.data)
}
})
對于初學(xué)者而言,官方示例可能會看不怎么懂,所以我就以我自己當(dāng)初項目驅(qū)動學(xué)習(xí)的方式(開發(fā)個人的記賬小程序)來作為學(xué)習(xí)實例。
以登錄來說,效果圖如下:

此次示例包含表單校驗和網(wǎng)絡(luò)請求,代碼如下:
login.js
// pages/login/login.js
Page({
/**
* 頁面的初始數(shù)據(jù)
*/
data: {
username: "",
password: ""
},
register:function(e){
wx.navigateTo({
url: '../register/register'
})
},
formSubmit: function(e) {
console.log(e.detail.value.email);
console.log(e.detail.value.pwd)
var username = e.detail.value.email;
var password = e.detail.value.pwd;
var emailReg = /^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$/;
if (username == null || username == "") {
wx.showToast({
title: "用戶名不能為空",
icon: 'none',
duration: 1500
})
} else if (!emailReg.test(username)) {
wx.showToast({
title: "郵箱有誤",
icon: 'none',
duration: 1500
})
} else if (password == null || password == "") {
wx.showToast({
title: "密碼不能為空",
icon: 'none',
duration: 1500
})
} else {
wx.request({
url: getApp().globalData.urlPath + "sysUser/login",
method: "POST",
data: {
username: username,
password: password
},
header: {
"Content-Type": "application/x-www-form-urlencoded"
},
success: function(res) {
console.log(res.data);
if (res.statusCode == 200) {
//訪問正常
if (res.data.code == "000000") {
wx.showToast({
title: "登陸成功",
icon: 'success',
duration: 2000,
success: function() {
wx.navigateTo({
url: '../manage/manage'
})
wx.setStorage({
key: 'userId',
data: res.data.user.userCode
})
wx.setStorage({
key: 'userName',
data: res.data.user.userName
})
console.log("test:" + wx.getStorageSync('userName'));
}
})
} else if (res.data.code == "111111") {
wx.showToast({
title: "密碼錯誤",
icon: 'none',
duration: 1500
})
} else {
wx.showToast({
title: "該用戶不存在",
icon: 'none',
duration: 1500
})
}
} else {
wx.showLoading({
title: '系統(tǒng)異常',
fail
})
setTimeout(function() {
wx.hideLoading()
}, 2000)
}
}
})
}
}
})
關(guān)于login.js,主要是寫通信邏輯的,與咱們平時寫js差異并不大,唯一不同的就是api長得不樣罷了。
關(guān)于其中的getApp().globalData.urlPath,相當(dāng)于全局變量,不用我每次都寫一大串https之類的。
表單校驗的效果如圖:


代碼說明:
顯示消息提示框(相當(dāng)于js的alert提示):
wx.showToast({
title: "郵箱有誤",
icon: 'none',
duration: 1500
})
獲取input屬性為name的值(相當(dāng)于js中form.email.value,前提是這個表單name要為form,且form中的input要存在一個name=”email”)
e.detail.value.email;
跳轉(zhuǎn)代碼(相當(dāng)于window.location.href):
wx.navigateTo({
url: '../manage/manage'
})
至于wx.request,我想只要是寫過ajax的,都很好理解。
login.json:
{
"usingComponents": {}
}
關(guān)于這個login.json有什么用,我唯一想到的是頁面的title(其實相當(dāng)于html中的title)
lgoin.wxml:
<view class='container'>
<view class='header'>
<text>acs系統(tǒng)</text>
</view>
<view>
<text>\n</text>
</view>
<view class='header'>
</view>
<form bindsubmit="formSubmit">
<view class='section'>
<text>用戶名:</text>
<input type='text' name="email" placeholder='請輸入郵箱' />
</view>
<view class='section'>
<text>密碼:</text>
<input password='password' name="pwd" placeholder='請輸入密碼' />
</view>
<view class='button'>
<button type='primary' form-type='submit'>登錄</button>
<text>\n</text>
<view bindtap='register' class="register">注冊</view>
</view>
</form>
</view>
wxml相當(dāng)于視圖(如html或者模板語言(jsp、volocity、freemarker、beetl等))
視圖除了可以寫一些標(biāo)簽之類的,還可以寫一些邏輯判斷。后面會講到的。
login.wxss:
/* pages/login/login.wxss */
form{
width: 310px;
height: 240px;
line-height: 40px;
/* border: 1px solid red; */
}
input{
border: 1px solid #ccc;
width: 310px;
height: 40px;
}
.button{
margin-top: 20px;
}
.header text{
font-size: 25px;
color: #666;
}
form text{
font-size: 20px;
color: #666;
}
.register{
color:black;
display: block;
width: 310px;
height: 40px;
border: 1px solid #ccc;
text-align: center;
}
這個wxss就相當(dāng)于css,定義視圖的樣式,決定視圖長什么樣(好看不好看)
關(guān)于微信小程序網(wǎng)絡(luò)通信,更多信息可以參考官方文檔:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
javascript實現(xiàn)多張圖片左右無縫滾動效果
本文主要介紹了javascript實現(xiàn)多張圖片左右無縫滾動效果的實例。具有很好的參考價值。下面跟著小編一起來看下吧2017-03-03
JS函數(shù)報錯Uncaught ReferenceError: XX is not
這篇文章主要介紹了JS函數(shù)報錯Uncaught ReferenceError: XX is not defined問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10
如何通過IntersectionObserver實現(xiàn)懶加載
這篇文章主要介紹了通過IntersectionObserver實現(xiàn)懶加載,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-04-04
淺談javascript中for in 和 for each in的區(qū)別
兩個的作用都用來遍歷對象,但為什么有了for in語句了還要for each in語句呢,后來看了下for each in開發(fā)的文檔,for each in是作為E4X標(biāo)準(zhǔn)的一部分在javascript 1.6中發(fā)布的,而且它不是ECMAScript標(biāo)準(zhǔn)的一部分2015-04-04

