Record some things used in WeChat mini programs (continuously updated)

Table of contents

1. First execute onLaunch in app.js, and then execute onLoad in the page.

2. Click on the picture to preview (you can press and hold to save, identify the QR code...)

3. Click to make a call

4. Click to copy

5. Click to jump to another URL


1. First execute onLaunch in app.js, and then execute onLoad in the page.

We sometimes obtain some necessary data in app.js, but under normal circumstances, its execution sequence is:

Execute onloauch first (it may not be finished yet)------onLoad in the page-------then execute onloauch

Solution: execute onload after onLaunch is executed

//app.js

onLaunch() {
        // 登录
        wx.login({
            success: res => { 
                wx.request({
                    url: ``,
                    data: {
                       
                    },
                    success: (res) => { 
                        // 重要代码    checkLoginReadyCallback是在page页面声明的
                        //由于这里是网络请求,可能会在 Page.onLoad 之后才返回
                        // 所以此处加入 callback 以防止这种情况
                        this.globalData.checkLogin = true;
                        if (this.checkLoginReadyCallback) {
                            this.checkLoginReadyCallback(res);
                        } 
                    }
                }) 
            }
        })
    },
    globalData: { 
        checkLogin: false
    }
// page    index.js

 onLoad: function (options) {
        var app = getApp()
        //判断onLaunch是否执行完毕 
        if (app.globalData.checkLogin) {

            //代码

        }else{

            //代码

        }
}

2. Click on the picture to preview (you can press and hold to save, identify the QR code...)

// 点击二维码事件
    preview_img_code() {
        wx.previewImage({
            current: '', // 当前显示图片的 http 链接
            urls: ['',''], // 需要预览的图片 http 链接列表
            success: (res) => {
                console.log(res);
            },
            file: (err) => {
                console.log(err);
            }
        })
    },

3. Click to make a call

wx.makePhoneCall({
            phoneNumber: this.data.datalist.phone,
            success: function () {
                console.log("拨打电话成功!")
            },
            fail: function () {
                console.log("拨打电话失败!")
            }
        })

4. Click to copy

wx.setClipboardData({
            data: this.data.datalist.email,
            success: function (res) {
                wx.showModal({
                    title: '提示',
                    content: '复制成功',
                })
            }
        });

5. Click to jump to another URL

If the developer selects "Do not verify legal domain names, web-view (business domain names)..." in the local settings, there will be no problem during preview and debugging.

Before going online, the administrator of the WeChat public platform must practice configuring "web-view (business domain name)" to open the page that jumps to normally.

//开始页
 wx.navigateTo({
            url: '/pages/webpage/webpage',
        })
//跳转到   页面   
<web-view src="https:"></web-view>

Guess you like

Origin blog.csdn.net/m0_69502730/article/details/128608623
Recommended