1. nodejs接收小程序客戶端的請(qǐng)求數(shù)據(jù)
(1) 在之前的《微信小程序與服務(wù)器的交互原理》已經(jīng)介紹了微信小程序與服務(wù)器交互的基本過(guò)程和代碼,以下是小程序客戶端向服務(wù)器請(qǐng)求的接口代碼:
wx.request({
url: 'https://www.joyitsai.cn/weapp/checkArea',
// 請(qǐng)求服務(wù)器時(shí)攜帶的請(qǐng)求數(shù)據(jù)
data: {
schoolArea: school_area,
Grade: grade,
checkWay: checkWay,
keyWord: keyWord
},
method: 'GET',
// 攜帶的參數(shù)會(huì)以u(píng)rl格式傳到服務(wù)器,信息頭我們?cè)O(shè)置為url編碼,utf8編碼
header: {
'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
},
// 提取響應(yīng)數(shù)據(jù)
success: function (res) {
var data = res.data.data;
console.log(data);
}, // success: function(){}
fail: function (res) {
console.log('請(qǐng)求數(shù)據(jù)失敗');
}
});
}
(2) 我們?cè)诜?wù)器端的路由函數(shù)中,接收小程序端發(fā)送的URL請(qǐng)求和攜帶參數(shù):
我們是通過(guò)Koa框架的Context對(duì)象來(lái)接收request和返回response的。詳細(xì)內(nèi)容請(qǐng)參見(jiàn)《Koa中文文檔》
Koa Context 將 node 的 request 和 response 對(duì)象封裝到單個(gè)對(duì)象中,為編寫(xiě) Web 應(yīng)用程序和 API 提供了許多有用的方法。 這些操作在 HTTP 服務(wù)器開(kāi)發(fā)中頻繁使用,它們被添加到此級(jí)別而不是更高級(jí)別的框架,這將強(qiáng)制中間件重新實(shí)現(xiàn)此通用功能。每個(gè)請(qǐng)求都將創(chuàng)建一個(gè) Context,并在中間件中作為接收器引用,或者 ctx 標(biāo)識(shí)符,如以下代碼片段所示:
app.use(async ctx => {
ctx; // 這是 Context
ctx.request; // 這是 koa Request
ctx.response; // 這是 koa Response
});
為方便起見(jiàn)許多上下文的訪問(wèn)器和方法直接委托給它們的 ctx.request或 ctx.response ,不然的話它們是相同的。 例如 ctx.type 和 ctx.length 委托給 response 對(duì)象,ctx.path 和 ctx.method 委托給 request。
request.querystring(ctx.querystring): 根據(jù) ? 獲取原始查詢字符串
request.query(ctx.query): 獲取解析的查詢字符串, 當(dāng)沒(méi)有查詢字符串時(shí),返回一個(gè)空對(duì)象。
例如 http://xxxxx/?color=blue&size=small,通過(guò)ctx.query獲取的解析數(shù)據(jù)對(duì)象為:
{
color: 'blue',
size: 'small'
}
- 以下是路由接口js腳本中接收微信小程序端request請(qǐng)求的代碼:
module.exports = async(ctx) => {
let url=ctx.url; //獲取url
console.log('Here is the url of ctx:');
console.log(url);
// 從context中直接獲取URL攜帶的數(shù)據(jù)
let ctx_query = ctx.query; //query返回格式化的對(duì)象
console.log('Here is the query of the ctx:');
console.log(ctx_query);
let req_querystring=request.querystring; //querystring返回原字符串。
var schoolArea = ctx.query.schoolArea;
var grade = ctx.query.Grade;
var checkway = ctx.query.checkWay;
var keyword = ctx.query.keyWord;
console.log('此次request請(qǐng)求攜帶的數(shù)據(jù)為:'+ schoolArea + ', ' + grade + ', ' + checkway + ', ' + keyword);
}
2. 通過(guò)nodejs的sequelize框架向數(shù)據(jù)庫(kù)進(jìn)行數(shù)據(jù)查詢
在上面的過(guò)程中,我們已經(jīng)獲取到了來(lái)自微信小程序端的resuqest請(qǐng)求所攜帶的數(shù)據(jù):schoolArea,grade,checkway,keyword
在前面的文章中《微信小程序與服務(wù)器的交互原理》,已經(jīng)介紹過(guò)了如何通過(guò)sequelize來(lái)連接mysql數(shù)據(jù)庫(kù)和創(chuàng)建數(shù)據(jù)表模型。下面,我們將這幾個(gè)參數(shù)作為查詢條件,在mysql數(shù)據(jù)庫(kù)中進(jìn)行數(shù)據(jù)查詢:
-
project.findAll({where: {}})來(lái)進(jìn)行多條件查詢:
// 數(shù)據(jù)表模型.findAll()
var schoolInfo = await SchoolArea.findAll({
where:{
//字段名:查詢條件參數(shù)
area: schoolArea,
grade: grade,
school: keyword
}
});
通過(guò)findAll()查詢數(shù)據(jù)返回的結(jié)果是一個(gè)數(shù)組,其中的每個(gè)元素是查詢到的每條數(shù)據(jù)的對(duì)象,格式如下:
[ Instance { // 第一條數(shù)據(jù)對(duì)象
0|app | dataValues:
0|app | { id: '0',
0|app | area: 'xx',
0|app | grade: 'xx',
0|app | school: 'xx',
0|app | residence: 'xx' },
0|app | _previousDataValues:
0|app | { ... },
0|app | _changed: {},
0|app | '$modelOptions':
0|app | { timestamps: true,
0|app | instanceMethods: {},
0|app | ...
0|app | hasPrimaryKeys: true },
0|app | '$options':
0|app | {... },
0|app | hasPrimaryKeys: true,
0|app | __eagerlyLoadedAssociations: [],
0|app | isNewRecord: false },
// 第二條數(shù)據(jù)對(duì)象
Instance{
...
}
]
- 通過(guò)Koa Context對(duì)象返回查詢結(jié)果:
ctx.body(等同于ctx.response.body)作為http請(qǐng)求的響應(yīng)主體,可以攜帶響應(yīng)數(shù)據(jù),返回給小程序客戶端,在小程序客戶端通過(guò)wx.request{ ..., success: function(res){ res.data }}獲取ctx.body的響應(yīng)數(shù)據(jù)(參見(jiàn)本文第一段代碼)。
// 服務(wù)器nodejs
ctx.body={
data: {downloads: schoolInfo},
其他你需要的數(shù)據(jù)...
}