小程序开发注意事项
小程序分享
- 微信小程序的分享只能分享到朋友 不能分享到朋友圈
- 微信小程序的分享可以指定分享页面, 并带参数值
onShareAppMessage(options)
在 Page 中定义 onShareAppMessage 函数,设置该页面的转发信息。
- 只有定义了此事件处理函数,右上角菜单才会显示 “转发” 按钮
- 用户点击转发按钮的时候会调用
- 此事件需要 return 一个 Object,用于自定义转发内容
options 参数说明
参数 | 类型 | 说明 |
---|---|---|
from | String | 转发事件来源。button:页面内转发按钮;menu:右上角转发菜单 |
target | Object | 如果 from 值是 button,则 target 是触发这次转发事件的 button,否则为 undefined |
自定义转发字段
字段 | 说明 | 默认值 | 最低版本 |
---|---|---|---|
title | 转发标题 | 当前小程序名称 | |
path | 转发路径 | 当前页面 path ,必须是以 / 开头的完整路径 | |
success | 转发成功的回调函数 | 1.1.0 | |
fail | 转发失败的回调函数 | 1.1.0 | |
complete | 转发结束的回调函数(转发成功、失败都会执行 | 1.1.0 |
回调结果:
回调类型 | errMsg | 说明 |
---|---|---|
success | shareAppMessage:ok | 转发成功 |
fail | shareAppMessage:fail cancel | 用户取消转发 |
fail | shareAppMessage:fail (detail message) | 转发失败,其中 detail message 为详细失败信息 |
success回调参数说明:
参数 | 类型 | 说明 | 最低版本 |
---|---|---|---|
shareTickets | StringArray | shareTicket 数组,每一项是一个 shareTicket ,对应一个转发对象 | 1.1.0 |
代码示例
Page({
onShareAppMessage: function (res) {
if (res.from === 'button') {
// 来自页面内转发按钮
console.log(res.target)
}
return {
title: '自定义转发标题',
path: '/page/user?id=123', // 该链接可指定分享出去页面打开的链接 并以get参数的写法传递参数
success: function(res) {
// 转发成功
},
fail: function(res) {
// 转发失败
}
}
}
})
接收的写法
Page({
onLoad:function(options){ // 分享过来的页面的参数会传递到 onload 函数 以 options 变量的形式传递进来
console.log(options); // 打印出分享过来传递的参数
}
})
分享后拿值的注意点
-
分享过来后假如需要在页面初始化的时候就拿到数据进行处理 可以这样处理(用户带值分享或页面带值跳转)
Page({ // 页面加载监听 wx.onLoad:function(options){ // options 就是分享过来 或跳转传递过来的值的集合 }, // 页面初次渲染完成 wx.onReady:function(){ } })
-
在页面的其他地方拿到数据可以是这样的(用户带值分享或页面带值跳转)
Page({ submit:function(){ var key1 = this.options.key1; var key2 = this.options.key2; // key1 和 key2 就是页面访问时传递过来的数据 } })
生成二维码
https 加密配置须知
- 域名确定的时候就要申请 https 证书
- 证书的地方先有阿里云 https 证书申请 申请一般一天就能下来 当时不会通过
- 申请过程中查看进度 按提示完成 dns 或 文件的校验
- wdcp 服务器管理面板配置 https 证书比较方便 (干细胞的小程序使用的是nginx + php)
其他资料
nginx 配置代理
nginx的示例代码
nginx 做代理 代理 http https
本架构是 所有访问由nginx 进行分发 转发到后台的各服务器再进行处理
server
{
listen 80;
server_name wxxcx.hbbettercell.com;
location / {
proxy_pass http://172.20.10.40;
proxy_cache off;
proxy_cache_valid 200 302 1d;
proxy_cache_valid 301 30d;
proxy_cache_valid any 5m;
proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504;
}
access_log logs/wxxcx.access.log;
expires 30d;
}
server
{
listen 443;
server_name wxxcx.hbbettercell.com;
ssl on;
ssl_certificate wxxcx.hbbettercell.com.pem;
ssl_certificate_key wxxcx.hbbettercell.com.key;
location / {
proxy_pass https://172.20.10.40:443;
proxy_cache off;
proxy_set_header Host $host:443;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Via "nginx";
proxy_cache_valid 200 302 1d;
proxy_cache_valid 301 30d;
proxy_cache_valid any 5m;
proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504;
}
access_log logs/wxxcx.access.log;
expires 30d;
}