Uni-app 微信小程序头像上传

uni-app 提供了很多图片处理的Api,比如 uni.chooseImage()方法,但基本都是本地、拍照上传,本地临时、持久存储,要想跟服务端交互,就需要使用上传Api uni.uploadFile()。但项目需求是想将图片转为base64格式后上传,经多方查找,找到了微信提供的Api方法 wx.getFileSystemManager()。

wx.getFileSystemManager

wx.FileSystemManager.readFile() 方法可以根据提供的本地文件路径来读取本地文件内容,这正好符合项目需求,查看 uni-app Api 文档,也有实现这个方法 uni.FileSystemManager.readFile() ,但不知是否已经支持。经体验版测试,功能效果跟微信Api一致。

/*
 * 上传头像
 * 注:使用vscode编辑器时,会有报错提醒,可忽略
 */
 public chooseImage(): any {
    if (this.state) {
      return false;
    }
    this.state = true;
    uni.chooseImage({
      count: 1,
      sizeType: 'compressed',
      success: (res: any) => {
        this.state = false;
        if (res.tempFiles[0].size >= 200000) {
          uni.showToast({ title: '请上传低于200K的图片', icon: 'none', duration: 2000 });
          return false;
        }
        uni.compressImage({
          src: res.tempFilePaths[0],
          quality: 0,
          success: (res) => {
            uni.getFileSystemManager().readFile({
              filePath: res.tempFilePath, // 选择图片返回的相对路径
              encoding: 'base64', // 编码格式
              success: async (res: any) => {
                this.userInfoData.headImg = 'data:image/png;base64,' + res.data;
              }
            });
          }
        });
      },
      fail: (err: Error) => (this.state = false)
    });
  }
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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 ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值