Python flask实战订餐系统微信小程序-30api入口页面制作及微信小程序段接口的调用

wechat 虚幻 1036℃ 0评论

微信登录功能的实现

通过小程序的前端 配合python-flask的后端,实现登录接口的功能

在我们正式写代码之前 读一下微信小程序的官方文档。

https://developers.weixin.qq.com/miniprogram/dev/api/open-api/login/wx.login.html

api入口页面制作

创建api文件夹

这个文件夹主要就是给小程序使用的

20210808194616 - Python flask实战订餐系统微信小程序-30api入口页面制作及微信小程序段接口的调用

init.py添加如下代码:

from flask import Blueprint

route_api = Blueprint('api_page', __name__)

@route_api.route( "/" )
def index():
    return "mina Api V1.0"

www.py引入蓝图

from application import app
from web.controllers.index import route_index
from web.controllers.user.User import route_user
from web.controllers.static import route_static
from web.controllers.account.Account import route_account
from web.controllers.finance.Finance import route_finance
from web.controllers.food.Food import route_food
from web.controllers.member.Member import route_member
from web.controllers.stat.Stat import route_stat
from web.controllers.api import route_api

app.register_blueprint(route_index, url_prefix="/")
app.register_blueprint(route_user, url_prefix="/user")
app.register_blueprint(route_static, url_prefix="/static")
app.register_blueprint(route_account, url_prefix="/account")
app.register_blueprint(route_finance, url_prefix="/finance")
app.register_blueprint( route_food,url_prefix = "/food" )
app.register_blueprint( route_member,url_prefix = "/member" )
app.register_blueprint( route_stat,url_prefix = "/stat" )
app.register_blueprint( route_api,url_prefix = "/api" )

访问接口

20210808195421 - Python flask实战订餐系统微信小程序-30api入口页面制作及微信小程序段接口的调用

微信小程序段接口的调用

我们希望进入到订单系统之前,需要先登录, 当我们点击“走 订餐是~”的时候,需要有登录信息。

打开index.wxml

可以参考这个文件:https://developers.weixin.qq.com/miniprogram/dev/api/open-api/user-info/wx.getUserInfo.html

20210808201637 - Python flask实战订餐系统微信小程序-30api入口页面制作及微信小程序段接口的调用

打开index.js,在这里,

pages/index/index.js下面定义一个login方法:

20210808201933 - Python flask实战订餐系统微信小程序-30api入口页面制作及微信小程序段接口的调用

此时 点击登录授权 就可以看到打印了test

20210808201853 - Python flask实战订餐系统微信小程序-30api入口页面制作及微信小程序段接口的调用

如何获取用户的信息

从文档中 可以看到e这个变量中提供用户信息

20210808202351 - Python flask实战订餐系统微信小程序-30api入口页面制作及微信小程序段接口的调用

所以 我们可以通过将这个e打印出来看一下

20210808203541 - Python flask实战订餐系统微信小程序-30api入口页面制作及微信小程序段接口的调用20210808203501 - Python flask实战订餐系统微信小程序-30api入口页面制作及微信小程序段接口的调用

将数据通过ajax请求传送给后端:

https://developers.weixin.qq.com/miniprogram/dev/api/network/request/wx.request.html

  login:function (e) {
    if (!e.detail.userInfo){
      app.alert({'content':'登录失败,请再次点击'});
      return;
    }
    var data=e.detail.userInfo;

    wx.request({
      url: 'http://127.0.0.1:8999/xxx/xxx',
      method: 'POST',
      data:data,
      header: app.getRequestHeader(),
      success :function(res) {
      }
    })
  }

这里的这个调用就不需要了

20210808205051 - Python flask实战订餐系统微信小程序-30api入口页面制作及微信小程序段接口的调用

小程序端提示如下错误信息:

20210808205222 - Python flask实战订餐系统微信小程序-30api入口页面制作及微信小程序段接口的调用

这里就本地项目需要设置一下:

https://mp.weixin.qq.com/

20210808210027 - Python flask实战订餐系统微信小程序-30api入口页面制作及微信小程序段接口的调用

此时 再次点击授权登录 就可以看到已经发出了请求:

20210808211228 - Python flask实战订餐系统微信小程序-30api入口页面制作及微信小程序段接口的调用

3:30

转载请注明:虚坏叔叔 » Python flask实战订餐系统微信小程序-30api入口页面制作及微信小程序段接口的调用

喜欢 (2)

您必须 登录 才能发表评论!