设置django后台提供计算器小程序
腾讯课堂的马哥叫你学微信小程序开发
申请一个云服务器(ubuntu16.4), 并获得服务器的公网ip地址
解析域名到这个IP, 方法是在域名管理界面添加一条解析记录. 解析成功后我们还不能访问 www.laolijia.club, 我们还需要配置nginx和python代码
sudo add-apt-repository ppa:jonathonf/python-3.6
sudo apt-get update
sudo apt-get install python3.6
sudo apt-get install python3.6-dev
sudo apt-get install python3-gdbm
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.5 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 2
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 100
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 150
curl https://bootstrap.pypa.io/get-pip.py | sudo python3.6
cd ~ && mkdir code && cd code
sudo pip install virtualenv
virtualenv env && source env/bin/activate
sudo apt install python-django-common
sudo apt-get install python-django
pip install django
django-admin startproject calculator
cd calculator
ALLOWED_HOSTS = []
为ALLOWED_HOSTS = ['*']
python3 manage.py runserver 0.0.0.0:8000
http://服务器ip:8000
可以看到下图:python3 manage.py startapp CalculateApi
INSTALLED_APPS
中添加CalculateApi
如下:INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'CalculateApi'
]
from django.contrib import admin
from django.urls import path
from django.conf.urls import url, include
urlpatterns = [
path('admin/', admin.site.urls),
url('^', include('CalculateApi.urls')),
]
/calculate
接口。from django.conf.urls import url
from . import views
urlpatterns = [
url('calculate', views.calculate)
]
from django.http import HttpResponse
def calculate(request):
formula = request.GET['formula']
try:
result = eval(formula, {})
except:
result = 'Error formula'
return HttpResponse(result)
http://服务器ip:8000/calculate?formula=2*3-5
即可得到结果1。由于微信要求使用https协议进行通讯, 而我们目前django 服务是http的, 所以必须安装nginx , 并监听443端口,并将请求转发给 django .
我们使用nginx + uwsgi + django来配置https服务器。
pip install uwsgi
touch uwsgi.ini
vi uwsgi.ini
[uwsgi]
# django项目监听的socket文件(可以使用端口代替)
socket = ./calculator.sock
# django项目所在目录
chdir = /home/ubuntu/code/calculator_py/calculator
# django项目wsgi文件
wsgi-file = calculator/uwsgi.ini
module= calculator.wsgi
master = true
processes = 2
threads = 4
vacuum = true
# 通过touch reload可以重启uwsgi服务器
touch-reload = ./reload
# 日志输出
daemonize = calculator.log
uwsgi --ini uwsgi.ini
touch reload
运行后, 该目录下会多出一些文件, 包括 calculator.sock文件
sudo apt-get install nginx
vi /etc/nginx/nginx.conf
将第一行改为 user ubuntu;
touch /etc/nginx/conf.d/calculator.conf
sudo vi /etc/nginx/conf.d/calculator.conf
添加内容如下:
server{
listen 80;
server_name 129.211.120.99;
charset UTF-8;
client_max_body_size 75M;
location ~ ^/calculate {
uwsgi_pass unix:///home/ubuntu/code/wechat_mp/calculator/calculator.sock;
include /etc/nginx/uwsgi_params;
}
}
其中 server_name 是我们腾讯云主机的公网IP, uwsgi_pass 使我们配置 uwsgi时生成的socket 文件路径
sudo service nginx restart
http://服务器ip/calculate?formula=2*3-4
cd ~
mkdir cert && cd cert
scp /Users/dalong/Downloads/www.laolijia.club/Nginx/* [email protected]:/home/ubuntu/cert/
server{
listen 443;
server_name 129.211.120.99;
ssl on;
ssl_certificate /home/ubuntu/cert/1_www.laolijia.club_bundle.crt;
ssl_certificate_key /home/ubuntu/cert/2_www.laolijia.club.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
charset UTF-8;
client_max_body_size 75M;
location ~ ^/calculate {
uwsgi_pass unix:///home/ubuntu/code/wechat_mp/calculator/calculator.sock;
include /etc/nginx/uwsgi_params;
}
}
其中server_name 是云主机的公网IP, ssl_certificate 是.crt文件路径, ssl_certificate_key 是 .key文件路径
重启nginx服务器,访问服务器的443端口即可访问calculate接口,如https://服务器域名/calculate?formula=2*3-4
如果需要在一台机器上使用多个域名来监听不同服务, 那么就需要配置nginx 的 /etc/nginx/conf.d/calculator.conf 如下:
server{
listen 443;
server_name www.laolijia.club;
ssl on;
ssl_certificate /home/ubuntu/cert/1_www.laolijia.club_bundle.crt;
ssl_certificate_key /home/ubuntu/cert/2_www.laolijia.club.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
charset UTF-8;
client_max_body_size 75M;
location ~ ^/calculate {
uwsgi_pass unix:///home/ubuntu/code/calculator_py/calculator/calculator.sock;
include /etc/nginx/uwsgi_params;
}
}
server{
listen 443;
server_name xiaoli.laolijia.club;
ssl on;
ssl_certificate /home/ubuntu/cert/1_www.laolijia.club_bundle.crt;
ssl_certificate_key /home/ubuntu/cert/2_www.laolijia.club.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
charset UTF-8;
client_max_body_size 75M;
location ~ ^/xiaoliapi {
uwsgi_pass unix:///home/ubuntu/code/xiaolee_assistant_py/xiaoli/xiaoli.sock;
include /etc/nginx/uwsgi_params;
}
}
注意:
<view class="container">
<input type="text" class="input" bindinput='input'/>
<button bindtap="calculate">calbutton>
<view>{{ result }}view>
view>
/**index.wxss**/
.input {
border: 1px solid black;
margin-bottom: 5px;
}
//index.js
//获取应用实例
const app = getApp()
Page({
data: {
result: "暂无结果",
formula: ''
},
//事件处理函数
calculate: function () {
wx.request({
url: 'https://www.laolijia.club/calculate',
data: {
formula: this.data.formula
},
success: res => {
if (res.statusCode == 200) {
this.setData({
result: res.data
})
}
}
})
},
input: function (e) {
this.setData({
formula: e.detail.value
})
}
})
url 是我们服务端的接口
运行小程序模拟器