分享一个让 uniapp 微信小程序支持 EventSource 的方法

微信小程序不支持

EventSource(SSE)是一种HTML5标准,旨在提供一种方式来在Web应用中实现异步通信和事件处理。

然而,根据现有的信息,uni-app并没有直接支持EventSource,这意味着开发者需要寻找其他技术或替代方案来实现类似的效果。

尽管有些主流浏览器支持SSE,但小程序目前还不能兼容这个API,这可能是由于微信平台对新技术的兼容性考虑或是开发生态的限制。

一个替代实现方式

class EventSource {
    constructor(url, retryTime = 0) {
        this.url = url;
        this.retryTime = retryTime;
        this.listeners = {};
        this.requestTask = null
        this.connect()
    }

    connect() {
        this.requestTask = wx.request({
            url: this.url,
            enableChunked: true,
            responseType: 'text',
            method: 'GET',
            timeout: 300e3,
            success: res => {
                this.emit('success', res)
                if (this.retryTime > 0) {
                    setTimeout(() => {
                        this.connect()
                    }, this.retryTime)
                }
            },
            fail: () => {
            }
        });
uniapp微信小程序一键登录可以通过使用button组件中的open-type属性来实现。具体步骤如下: 1. 在button组件中设置open-type属性为getUserInfo,同时绑定getuserinfo事件,例如: ```html <button type="default" open-type="getUserInfo" @getuserinfo="wxLogin">一键登录微信小程序</button> ``` 2. 在对应的方法wxLogin中,可以通过event参数获取到用户的信息,包括用户的头像、昵称等。可以将这些信息传递给后端进行处理。 另外,如果需要获取用户的手机号信息,可以使用open-type属性为getPhoneNumber,并绑定getphonenumber事件。具体步骤如下: 1. 在button组件中设置open-type属性为getPhoneNumber,同时绑定getphonenumber事件,例如: ```html <button shape="circle" type="primary" link="true" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">微信一键登录</button> ``` 2. 在对应的方法getPhoneNumber中,可以通过event参数获取到用户的手机号信息。同样,可以将这些信息传递给后端进行处理。 需要注意的是,为了保证前后端的appid一致,以及确保使用的appid是经过认证的,避免出现错误或调用不通的情况。 #### 引用[.reference_title] - *1* [uni-app实现微信小程序一键登录](https://blog.csdn.net/qq_45797421/article/details/118339987)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [uni-app中使用微信一键登录](https://blog.csdn.net/weixin_49296337/article/details/124755651)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值