目錄
flask
微信小程序
flask
1.項目后端步驟
具體步驟可參考flask官網(wǎng):flask 中文網(wǎng)
(1)創(chuàng)建虛擬環(huán)境
(2)激活虛擬環(huán)境
(3)在虛擬環(huán)境里邊安裝Flask
(4) 粘貼官網(wǎng)的最小flask代碼
(5)啟動flask服務(wù)
2.代碼書寫
from flask import Flask
app = Flask(__name__)
@app.route("/")
def welcome():
return '<p>welcome to lyy’s home</p>'
@app.route("/login/<username>/<password>")
def login(username,password):
right_result = {'code':200, 'result': f'welcome{username}'}
wrong_result = {'code':500, 'result': "sorry! it's wrong"}
if username == 'lyy' and password == 'xxxx':
return right_result
elif username == 'ztf' and password == 'xxxx':
return right_result
else:
return wrong_result
3.運行flask測試
微信小程序
?在index.wxml中添加用戶登錄的標(biāo)簽
<!--index.wxml-->
<view class="container">
用戶名:<input bindinput="usernameInput" type="text" value=""/>
密碼:<input bindinput="passwordInput" id="password" type="password" value=""/>
<button type="primary" bindtap="login">登錄</button>
</view>
在index.js書寫函數(shù)配置,登錄成功則跳轉(zhuǎn)頁面,登陸失敗顯示登陸失敗字樣
// index.js
Page({
data() {
return {
username: "",
password: ""
}
},
usernameInput:function (e) {
console.log(e.detail.value)
let a = e.detail.value
this.username = a;
},
passwordInput: function (e) {
this.password = e.detail.value;
},
login() {
wx.request({
url: 'http://127.0.0.1:5000/login/' + this.username + "/" + this.password,
success: function(res) {
console.log(res)
let code = res.data.code;
if (code != 200) {
wx.showToast({
title: '登陸失敗',
})
return;
}
console.log("success")
wx.switchTab({
url: '/pages/hello/hello',
})
}
})
}
})
文章來源:http://www.zghlxwxcb.cn/news/detail-602229.html
?文章來源地址http://www.zghlxwxcb.cn/news/detail-602229.html
到了這里,關(guān)于用戶登錄案例練習(xí)(flask+微信小程序)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!