小程序学习第四天(常用界面api,获取用户信息,weui组件库,云开发初识)

本文介绍了微信小程序中获取用户信息、获取用户手机号的流程,包括wx.getUserProfile接口的使用和授权过程。同时,讲解了WeUI组件库的使用,以及如何在小程序中引入和使用组件。此外,还提及了云开发的基本概念,如存储空间、数据库和云函数,以及如何初始化云开发环境。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 常用界面api及案例

常用界面API:wx.showToast()、wx.showLoading()、wx.hideLoading()、wx.setNavigationBarTitle()

获取用户信息

授权过程:https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/authorize.html

## 通过API获取用户信息

wx.getUserProfile({ })  获取用户信息

页面产生点击事件(例如 `button` 上 `bindtap` 的回调中)后才可调用,每次请求都会弹出授权窗口,用户同意后返回 `userInfo`。该接口用于替换 `wx.getUserInfo`

```html
<button bindtap="getUser">获取用户信息</button>

<view>{ {userInfo.nickName}}</view>
<image src="{ {userInfo.avatarUrl}}"></image>

```

```js
getUser() {
    // 限频接口  2pv
    wx.getUserProfile({
        desc: '完善资料',
        success: res => {
            console.log(res);
            this.setData({
                userInfo: res.userInfo
            })

            // wx.setStorageSync('userInfo', res.userInfo)

            wx.setStorage({
                key: "userInfo",
                data: res.userInfo,
                encrypt:true,
                //success:res=>{
                    //     wx.getStorage({
                    //         key: "userInfo",
                    //         encrypt: true, // 若开启加密存储,setStorage 和 getStorage 需要同时声明 encrypt 的值为 true
                    //         success(res) {
                    //           console.log(res.data)
                    //         }
                    //       })
                    // }
            })
        }
    })
},
    onShow: function () {
        let userInfo = wx.getStorageSync('userInfo')

        

        if (userInfo == "") {
            wx.showToast({
                title: "点击按钮获取用户信息",
                icon: "none"
            })
        } else {
            this.setData({
                userInfo
            })
        }
    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值