微信获取昵称头像存储服务器(最新方法)
创始人
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分钟了解!微信卡农辅助(辅助挂)真是真的有挂(详细辅助攻略);大神普及一款德州ai内幕,确定了微信...
第一分钟透视!大唐麻将大的周有... 第一分钟透视!大唐麻将大的周有没有辅助(辅助挂)分享教程(从来存在有挂)1、许多玩家不知道大唐麻将大...
推荐一款"新道游辅助... 推荐一款"新道游辅助软件"新道游辅助软件(一直存在有挂)1、每一步都需要思考,不同水平的挑战会更加具...
发现一款!新玄龙插件(辅助挂)... 发现一款!新玄龙插件(辅助挂)从来真的是有挂(详细辅助力荐教程);人气非常高,ai更新快且高清可以动...
七分钟了解!打哈儿床将脚本(辅... 七分钟了解!打哈儿床将脚本(辅助挂)竟然真的有挂(详细辅助工具);小薇(透视辅助)致您一封信;亲爱打...
8分钟辅助器!新道游透视黑科技... 8分钟辅助器!新道游透视黑科技(辅助挂)必胜教程(从前是有挂);致您一封信;亲爱新道游透视黑科技玩家...
实测分享"多乐小程序... 实测分享"多乐小程序破解版修改器"多乐小程序破解版修改器(原来真的有挂)1、多乐小程序破解版修改器透...
我来向大家传授!多乐辅助app... 《我来向大家传授!多乐辅助app(辅助挂)原本是有挂(详细辅助透牌教程)》 多乐辅助app软件透明挂...
第十分钟了解!海米大厅辅助(辅... 第十分钟了解!海米大厅辅助(辅助挂)一贯真的是有挂(详细辅助攻略);致您一封信;亲爱海米大厅辅助玩家...
第6分钟脚本!余干中至五十k有... 第6分钟脚本!余干中至五十k有挂装吗(辅助挂)透视教程(总是存在有挂)1、这是跨平台的余干中至五十k...