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

此次示例包含表單校驗(yàn)和網(wǎng)絡(luò)請(qǐng)求,代碼如下:
login.js
// pages/login/login.js
Page({
/**
* 頁(yè)面的初始數(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) {
//訪問(wèn)正常
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: "密碼錯(cuò)誤",
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,主要是寫(xiě)通信邏輯的,與咱們平時(shí)寫(xiě)js差異并不大,唯一不同的就是api長(zhǎng)得不樣罷了。
關(guān)于其中的getApp().globalData.urlPath,相當(dāng)于全局變量,不用我每次都寫(xiě)一大串https之類的。
表單校驗(yàn)的效果如圖:


代碼說(shuō)明:
顯示消息提示框(相當(dāng)于js的alert提示):
wx.showToast({
title: "郵箱有誤",
icon: 'none',
duration: 1500
})
獲取input屬性為name的值(相當(dāng)于js中form.email.value,前提是這個(gè)表單name要為form,且form中的input要存在一個(gè)name=”email”)
e.detail.value.email;
跳轉(zhuǎn)代碼(相當(dāng)于window.location.href):
wx.navigateTo({
url: '../manage/manage'
})
至于wx.request,我想只要是寫(xiě)過(guò)ajax的,都很好理解。
login.json:
{
"usingComponents": {}
}
關(guān)于這個(gè)login.json有什么用,我唯一想到的是頁(yè)面的title(其實(shí)相當(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='請(qǐng)輸入郵箱' />
</view>
<view class='section'>
<text>密碼:</text>
<input password='password' name="pwd" placeholder='請(qǐng)輸入密碼' />
</view>
<view class='button'>
<button type='primary' form-type='submit'>登錄</button>
<text>\n</text>
<view bindtap='register' class="register">注冊(cè)</view>
</view>
</form>
</view>
wxml相當(dāng)于視圖(如html或者模板語(yǔ)言(jsp、volocity、freemarker、beetl等))
視圖除了可以寫(xiě)一些標(biāo)簽之類的,還可以寫(xiě)一些邏輯判斷。后面會(huì)講到的。
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;
}
這個(gè)wxss就相當(dāng)于css,定義視圖的樣式,決定視圖長(zhǎng)什么樣(好看不好看)
關(guān)于微信小程序網(wǎng)絡(luò)通信,更多信息可以參考官方文檔:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
基于javascript實(shí)現(xiàn)的購(gòu)物商城商品倒計(jì)時(shí)實(shí)例
本文主要介紹了基于javascript實(shí)現(xiàn)的購(gòu)物商城商品倒計(jì)時(shí)實(shí)例,代碼詳細(xì),可直接復(fù)制試試看效果。需要的朋友可以參考借鑒2016-12-12
Typescript 實(shí)現(xiàn)函數(shù)重載的方式
函數(shù)重載簡(jiǎn)單點(diǎn)說(shuō)就是,同一個(gè)函數(shù)的不同實(shí)現(xiàn)方式,根據(jù)傳入的參數(shù)的類型或者長(zhǎng)度,決定最終調(diào)用哪一個(gè)實(shí)現(xiàn),本文給大家介紹了Typescript 實(shí)現(xiàn)函數(shù)重載的方式,需要的朋友可以參考下2024-05-05
JS中JSON對(duì)象和String之間的互轉(zhuǎn)及處理技巧
JSON:JavaScript 對(duì)象表示法(JavaScript Object Notation),其實(shí)JSON就是一個(gè)JavaScript的對(duì)象(Object)而已。接下來(lái)通過(guò)本文給大家介紹JS中JSON對(duì)象和String之間的互轉(zhuǎn)及處理技巧,需要的朋友一起學(xué)習(xí)吧2016-04-04
js實(shí)現(xiàn)鼠標(biāo)經(jīng)過(guò)時(shí)圖片滾動(dòng)停止的方法
這篇文章主要介紹了js實(shí)現(xiàn)鼠標(biāo)經(jīng)過(guò)時(shí)圖片滾動(dòng)停止的方法,可實(shí)現(xiàn)js滾動(dòng)特效中的鼠標(biāo)懸停停止圖片滾動(dòng)的功能,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-02-02

