人工智能 大数据 计算机科学

在服务器接收小程序的request请求,并对数据库进行多条件查询

2018-12-20  本文已影响0人  joyitsai

1. nodejs接收小程序客户端的请求数据

(1) 在之前的《微信小程序与服务器的交互原理》已经介绍了微信小程序与服务器交互的基本过程和代码,以下是小程序客户端向服务器请求的接口代码:
wx.request({
        url: 'https://www.joyitsai.cn/weapp/checkArea',

        // 请求服务器时携带的请求数据
        data: {
          schoolArea: school_area,
          Grade: grade,
          checkWay: checkWay,
          keyWord: keyWord
        },
        method: 'GET',

        // 携带的参数会以url格式传到服务器,信息头我们设置为url编码,utf8编码
        header: {
          'content-type': 'application/x-www-form-urlencoded;charset=utf-8' 
        },
        // 提取响应数据
        success: function (res) {
          var data = res.data.data;
          console.log(data);
       
        }, // success: function(){}
        fail: function (res) {
          console.log('请求数据失败');
        }
      });

}
(2) 我们在服务器端的路由函数中,接收小程序端发送的URL请求和携带参数:
app.use(async ctx => {
  ctx; // 这是 Context
  ctx.request; // 这是 koa Request
  ctx.response; // 这是 koa Response
});

为方便起见许多上下文的访问器和方法直接委托给它们的 ctx.request或 ctx.response ,不然的话它们是相同的。 例如 ctx.type 和 ctx.length 委托给 response 对象,ctx.path 和 ctx.method 委托给 request。
request.querystring(ctx.querystring): 根据 ? 获取原始查询字符串
request.query(ctx.query): 获取解析的查询字符串, 当没有查询字符串时,返回一个空对象。

例如 http://xxxxx/?color=blue&size=small,通过ctx.query获取的解析数据对象为:

{
  color: 'blue',
  size: 'small'
}
module.exports = async(ctx) => {

    let url=ctx.url; //获取url
    console.log('Here is the url of ctx:');
    console.log(url);

    // 从context中直接获取URL携带的数据
    let ctx_query = ctx.query; //query返回格式化的对象
    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请求携带的数据为:'+ schoolArea + ', ' + grade + ', ' + checkway + ', ' + keyword);
}

2. 通过nodejs的sequelize框架向数据库进行数据查询

在上面的过程中,我们已经获取到了来自微信小程序端的resuqest请求所携带的数据:schoolArea,grade,checkway,keyword
在前面的文章中《微信小程序与服务器的交互原理》,已经介绍过了如何通过sequelize来连接mysql数据库和创建数据表模型。下面,我们将这几个参数作为查询条件,在mysql数据库中进行数据查询:

// 数据表模型.findAll()
var schoolInfo = await SchoolArea.findAll({
   where:{
      //字段名:查询条件参数
      area: schoolArea,   
      grade: grade,
      school: keyword
   }
});

通过findAll()查询数据返回的结果是一个数组,其中的每个元素是查询到的每条数据的对象,格式如下:

 [ Instance {     // 第一条数据对象
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 },
  // 第二条数据对象
  Instance{
      ...
  }
]
// 服务器nodejs
ctx.body={
        data: {downloads: schoolInfo},
        其他你需要的数据...
    }
上一篇 下一篇

猜你喜欢

热点阅读