微信获取昵称头像存储服务器(最新方法)
创始人
2024-09-25 05:50:45
0

1.从基础库 2.21.2 开始支持
当小程序需要让用户完善个人资料时,可以通过微信提供的头像昵称填写能力快速完善。
根据相关法律法规,为确保信息安全,由用户上传的图片、昵称等信息微信侧将进行安全检测,组件从基础库2.24.4版本起,已接入内容安全服务端接口(mediaCheckAsync、msgSecCheck),以减少内容安全风险对开发者的影响。具体使用方法
需要将 button 组件 open-type 的值设置为 chooseAvatar,当用户选择需要使用的头像之后,可以通过 bindchooseavatar 事件回调获取到头像信息的临时路径。同时将头像和昵称存到本地中。

代码示例

1、wxml

{imageUrl}}/MZWH/pm/bg-01.png')">     
昵称:

2、js

// pages/homePage3/homePage3.js const app = getApp(); const api = require('../../utils/api.js'); const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0';  Page({    /**    * 页面的初始数据    */   data: {          image: defaultAvatarUrl,     imageUrl: app.data.imageUrl,     isIphoneX:app.data.isIphoneX,     area_id:app.data.area_id,     nickname: ''   }, //用户头像 assd: function (e) {   console.log("文件路径:" + e.detail.avatarUrl);   let headimg = e.detail.avatarUrl;      this.uploadFile(headimg);  },  onChooseAvatar(e) {   console.log(e.detail);   const { avatarUrl } = e.detail    this.setData({     avatarUrl,   }) },  uploadFile: function(filePath) {   console.log("文件路径:" + filePath);   wx.uploadFile({     url: '/upload', // 后端服务器 URL     filePath: filePath,     name: 'file', // 文件字段名     success: (res) => {       console.log('文件上传成功:', res.data);              // 解析 JSON 数据       let data;       try {         data = JSON.parse(res.data);       } catch (e) {         console.error('JSON 解析失败:', e);         return;       }              // 检查解析结果       if (data && data.fileurl) {         wx.setStorageSync('headimg', data.fileurl);         this.setData({           image: data.fileurl         });       } else {         console.error('后端返回数据格式不正确:', data);       }     },     fail: (err) => {       console.error('文件上传失败:', err);     }   }); }, // 用户名处理 formSubmit: function(e) {   let nickname = e.detail.value;     console.log(e.detail.value)   wx.setStorageSync('nickname', nickname);   this.setData({nickname}); },        homePage: function () {     wx.navigateBack({       delta: 2     })   },    /**    * 生命周期函数--监听页面加载    */   onLoad: function (options) {     this.setData({       area_id: app.data.area_id     })   },    /**    * 生命周期函数--监听页面初次渲染完成    */   onReady: function () {    },    /**    * 生命周期函数--监听页面显示    */   onShow: function () {    },    /**    * 生命周期函数--监听页面隐藏    */   onHide: function () {    },    /**    * 生命周期函数--监听页面卸载    */   onUnload: function () {    },    /**    * 页面相关事件处理函数--监听用户下拉动作    */   onPullDownRefresh: function () {    },    /**    * 页面上拉触底事件的处理函数    */   onReachBottom: function () {    },    /**    * 用户点击右上角分享    */   onShareAppMessage: function () {    } })

3、wxss

/* pages/map/map.wxss */  page {   height: 100%;   width: 100%; }  .button {   width: 100%;   height: 200px; }      .button button {   width: 25%;   height: 100px;   margin-top: 50px;   background-color: #ffffff; }   .button image {   width: 150%;   height: 100%; }   .name {   width: 100%;   height: 50px;   border: 1px solid #cecccc;   margin-top: 90px;   display: flex;   border-radius: 10px; /* 适度减小圆角半径,以保留一定的角度 */   overflow: hidden; /* 确保内容超出部分不显示在圆角区域之外 */ }  .name_text {   width: 40%;   height: 50px;   line-height: 50px;   font-weight: bold;   font-size: 15px;   padding-left: 5%; }  .input_wrapper {   width: 60%;   display: flex;   align-items: center; }  .input_field {   width: 100%;   height: 100%;   padding: 0 10px;   border: none;   outline: none;   background-color: transparent;   font-size: 15px; }     .enter {   width: 100%;   display: flex;   justify-content: center;   margin-top: 100px; } .map {   height: 100%;   width: 100%;   background-size: 100% 100%;   -moz-background-size: 100%;   background-repeat: repeat;   display: flex;   flex-direction: column;   justify-content: center;   align-items: center; }  .slideButton {   width: 40rpx;   height: 40rpx;   background-size: 100%;   background-color: gray;   background-position: center;   position: absolute;   top: 30rpx;   left: 30rpx;   border: none;   border-radius: 50%; }  .mapHome {   height: 100%;   width: 100%;   background-size: 100% 100%;   -moz-background-size: 100%;   background-repeat: repeat;   overflow: hidden;   position: relative;   display: flex;   flex-direction: column;   align-items: center;    } .buttonView{   display: flex;   flex-direction: column;   align-items: center;   margin-top: 105%; }  .buttonViewBig{   display: flex;   flex-direction: column;   align-items: center;   margin-top: 125%; }  .mapHome button{   width: 220rpx;   animation: dh 3s infinite linear;   background-repeat:no-repeat; background-color: transparent; border-style: none; background-size: 100% 100%; -moz-background-size: 100%; background-repeat: repeat; border-radius: 175rpx; } .startButton {   width: 260rpx; /* 调整按钮宽度为200rpx */   height: 240rpx;   background-image: url('{{imageUrl}}/MZWH/pm/start.png');   background-size: contain; /* 使用contain保持完整显示图片 */   background-repeat: no-repeat;   background-position: center;   border: none; } @keyframes dh {   0% {        opacity: 1   }     50% {     opacity: 0.5   }     100% {     opacity: 1   }  }  .mapPoint {   position: absolute;   top: 170rpx;   right: 0;   width: 660rpx;    }  .mapButton {   position: absolute;   left: 50%;   margin-left: -220rpx;   bottom: 44rpx;   width: 400rpx; }  .box {   width: 150rpx;   height: 150rpx;   background-color: #309fdf;   border-radius: 100rpx;   position: absolute;   top: 64%;   left: 52%; }  .box {   animation: dh 2s infinite linear;    animation-delay:0.5s; }  .box1 {   width: 150rpx;   height: 150rpx;   background-color: #309fdf;   border-radius: 100rpx;   position: absolute;   top: 64%;   left: 52%; }  .box1 {   animation: dh 2s infinite linear;   }   

手机示例

相关内容

热门资讯

3分钟安装!牌乐门手机麻将有挂... 3分钟安装!牌乐门手机麻将有挂的(透视)详细教程(2020已更新)(哔哩哔哩);科技安装教程;757...
8分钟规律!家家乐牌吧如何开挂... 相信很多朋友都在电脑上玩过家家乐牌吧如何开挂吧,但是很多朋友都在抱怨用电脑玩起来不方便。为此小编给大...
2分钟开桌!闲逸斗地主亲友圈有... 大家肯定在之前闲逸斗地主亲友圈有挂的或者闲逸斗地主亲友圈有挂的中玩过2分钟开桌!闲逸斗地主亲友圈有挂...
分享实测!贵阳天天麻将app软... 分享实测!贵阳天天麻将app软件有假的(透视)详细教程(2020已更新)(哔哩哔哩);一、贵阳天天麻...
四分钟窥牌!捞腌菜外挂(透视)... 捞腌菜外挂新手教程相关信息汇总(需添加指定薇757446909获取下载链接);四分钟窥牌!捞腌菜外挂...
5分钟私人局!微扑克有辅助(透... 5分钟私人局!微扑克有辅助(透视)详细教程(2022已更新)(哔哩哔哩);微扑克有辅助是一款益智类棋...
五分钟ai人工智能!福建天天开... 五分钟ai人工智能!福建天天开心十三水攻略(透视)详细教程(2024已更新)(哔哩哔哩)准备好在we...
七分钟专业!微扑克游戏辅助器(... 七分钟专业!微扑克游戏辅助器(透视)辅助透视(详细教程)-哔哩哔哩是一款可以让一直输的玩家,快速成为...
透视最新!poker辅助软件(... 《poker辅助软件软件透明挂》是一款多人竞技的poker辅助软件辅助透视游戏,你将微扑克对手来到同...
7分钟一直输!陕麻圈的(透视)... 7分钟一直输!陕麻圈的(透视)外挂透明挂辅助器安装(有挂技巧)-哔哩哔哩;小薇(透视辅助)致您一封信...