微信小程序开发笔记
一、首先,下载一个微信开发者工具。前端项目就正常创建,由于本人的前端一塌糊涂,就让AI给我生成了一个我想要的前端项目(包括后面写功能)。
这里开发的时候会用到这个,但是一定注意服务部署到服务器上再本地调试,就要把这个关掉。
踩坑:因为开发时都是本地启动服务,然后发布体验版给朋友测试,发现自己正常进入但是朋友进去加载不出来。后面发现我是跟我的电脑连接同一个wifi,因此不存在跨域问题。
二、后端服务就你们随便写,没什么特别的。
三、记录一下后期服务部署遇到的问题。
1、就是上一篇文章,后端服务部署到服务器上,访问数据库报错。
解决:用户权限修改就好。
2、服务器配置ssl证书(由于微信小程序必须要求https)。
1)由于我的服务器是华为云的,所以我就直接在华为云申请ssl证书了。华为云免费SSL证书申请_【四个步骤】SSL证书免费申请_怎么申请免费SSL证书-华为云
2)上一步根据教程完成后,下载证书上传至服务器/usr/local/nginx/conf/ssl。这个路径具体看你们自己放在哪。
3)修改nginx配置文件之前,需要安装ngx_http_ssl_module
模块:首先把你的nginx备份一下,在nginx的源目录(下载压缩包的目录,不是安装目录),执行./configure --prefix=/usr/local/nginx --with-http_ssl_module,然后make(切记不用make install)。
4)修改配置文件,主要看443端口配置那一部分,其他的有我自己的一些项目配置,不用在意。
#user nobody;
worker_processes 1;#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;#pid logs/nginx.pid;events {worker_connections 1024;
}http {include mime.types;default_type application/octet-stream;#log_format main '$remote_addr - $remote_user [$time_local] "$request" '# '$status $body_bytes_sent "$http_referer" '# '"$http_user_agent" "$http_x_forwarded_for"';#access_log logs/access.log main;sendfile on;#tcp_nopush on;#keepalive_timeout 0;keepalive_timeout 65;#gzip on;# HTTP 服务器块,将请求重定向到 HTTPSserver {listen 80;server_name 你的域名或者ip; return 301 https://$host$request_uri;}# HTTPS 服务器块server {listen 443 ssl;server_name 你的域名或者ip; #ssl证书路径ssl_certificate /usr/local/nginx/conf/ssl/scs1745754367131.crt; ssl_certificate_key /usr/local/nginx/conf/ssl/scs1745754367131.key; # SSL 配置ssl_protocols TLSv1.2 TLSv1.3;ssl_ciphers HIGH:!aNULL:!MD5;ssl_prefer_server_ciphers on;index index.html;root /usr/local/nginx/html/eladmin/dist;# 避免访问出现 404 错误location / {try_files $uri $uri/ @router;index index.html;}location @router {rewrite ^.*$ /index.html last;}# 新增针对 9090 端口所有请求的反向代理配置location /api {proxy_pass http://127.0.0.1:9090;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-Forwarded-Proto $scheme;}#error_page 404 /404.html;# redirect server error pages to the static page /50x.htmlerror_page 500 502 503 504 /50x.html;location = /50x.html {root html;}}
}
5)配置完成后就重启nginx就可以了。不过过程遇到了一些问题,比如ssl证书文件的权限、防火墙端口开放,服务器安全组配置等。不过这些都比较好解决,配置nginx的时候,一步步调试解决就好了。
四、微信小程序的发布
由于发布还需要认证,貌似还要花钱,我只是给女朋友做个点菜小程序,体验版就够用的。
先在微信开发者工具右上角点上传代码,然后再微信小程序后台选为体验版,之后在成员管理中添加你的朋友为体验成员以及把体验版二维码发给他们即可。
完结,撒花!