更新记录

2.0.5(2024-04-24)

修改readme

2.0.4(2024-04-14)

修复

2.0.3(2024-04-14)

修复网页登陆授权中文乱码

查看更多

平台兼容性

阿里云 腾讯云 支付宝云
× ×

云函数类插件通用教程

使用云函数类插件的前提是:使用HBuilderX 2.9+


one-wechat

支持阿里云

配置IP白名单

到微信服务号后台添加IP白名单,添加IP列表如下:
47.92.132.2
47.92.152.34
47.92.87.58
47.92.207.183
8.142.185.204
参考:[代理服务器IP列表](https://doc.dcloud.net.cn/uniCloud/cf-functions.html#eip)

填写配置信息

打开common/one-wechat/config.js文件,填写appid跟secret
打开database,上传one-wecha-cache.schema.json

获取JS-SDK配置信息

公众号设置-功能设置-设置JS接口安全域名

前端示例

需安装jweixin-module:

npm install jweixin-module --save

//https://ask.dcloud.net.cn/article/36284
var jWeixin = require('jweixin-module')
var surl = encodeURIComponent(window.location.href.split('#')[0]); //解决URL中带参数的问题
uniCloud.callFunction({
    name:'one-wechat-function',
    data:{
        type:'getJssdkConfig',
        url:surl
    },
    success(res) {
        if(res.result.state == 400){
            alert(res.result.msg)
        }
        let config = res.result.data.jssdkConfig
        config.debug = true //默认为false
        config.jsApiList = ["updateAppMessageShareData"]
        jWeixin.config(config);
        jWeixin.ready(function() {
            jWeixin.updateAppMessageShareData({
                title: '标题', // 分享标题  
                desc: "描述内容", // 分享描述  
                link: window.location.href, // 分享链接  
                imgUrl: '分享图标', // 分享图标                                
                success: function() {
                    // 用户确认分享后执行的回调函数  
                }
            })
        })
    }
})

云函数使用示例

你也可以使用自己的云函数进行调用(需要在自己的云函数,将公共模块one-wechat添加进去)

'use strict';
const one_wechat = require('one-wechat')
exports.main = async (event, context) => {
    let config_res = await one_wechat.getJssdkConfig(event.url)
    return config_res
}

网页授权

公众号设置-功能设置-设置网页授权安全域名

前端示例

export default {
    data() {
    },
    methods: {
        //点击登陆
        login: function() {
            uniCloud.callFunction({
                name: 'one-wechat-fuction',
                data: {
                    type: 'getAuthorizeUrl',
                    url: encodeURIComponent(window.location.href) //回调地址
                },
                success(res) {
                    uni.setStorageSync('try_get_user', true)
                    window.location.href = res.result
                }
            })
        }
    },
    onLoad() {
        const try_get_user = uni.getStorageSync('try_get_user')
        if (try_get_user && try_get_user == true) {
            uniCloud.callFunction({
                name: 'one-wechat-fuction',
                data: {
                    type: 'getUserByUrl',
                    url: window.location.href
                }
            }).then((res) => {
                uni.setStorageSync('try_get_user', false)   //防止页面刷新后继续调用函数
                if (res.result.state == 200) {
                    uni.setStorageSync('user_info', res.result.data.user_info)
                } else {
                    alert(res.result.msg)
                }
            })
        }
    }
}

隐私、权限声明

1. 本插件需要申请的系统权限列表:

2. 本插件采集的数据、发送的服务器地址、以及数据用途说明:

插件不采集任何数据

3. 本插件是否包含广告,如包含需详细说明广告表达方式、展示频率:

许可协议

MIT协议

使用中有什么不明白的地方,就向插件作者提问吧~ 我要提问