微信获取昵称头像存储服务器(最新方法)
创始人
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;   }   

手机示例

相关内容

热门资讯

aapoker俱乐部(智星德州... aapoker俱乐部(智星德州扑克)wepoke德扑之星(透视辅助)总是真的有挂(有挂下载)-小红书...
一分钟掌握!微扑克有辅助插件(... 一分钟掌握!微扑克有辅助插件(黑科技)外挂透明挂辅助神器(2020已更新)(百度知乎)微扑克有辅助插...
透视辅助挂!德扑ai助手&qu... 透视辅助挂!德扑ai助手"wpk有"的确是有挂的(有挂代打)-知乎德扑ai助手软件透明挂微扑克wpk...
透明辅助(菠萝德州)外挂软件透... 透明辅助(菠萝德州)外挂软件透明插件(辅助挂)一贯真的有挂(2023已更新)(哔哩哔哩);1.菠萝德...
aapoker俱乐部(微扑克)... aapoker俱乐部(微扑克)德扑ai助手软件(黑科技)其实真的有挂(有挂打法)-百度1、任何微扑克...
黑科技辅助挂!德扑之星猫腻&q... 黑科技辅助挂!德扑之星猫腻"来玩德州app外挂"本来真的有挂(有挂私人房)-头条来玩德州app外挂辅...
3分钟普及!微扑克这软件有问题... 3分钟普及!微扑克这软件有问题(辅助挂)外挂透明挂辅助脚本(2024已更新)(知乎)1、完成微扑克这...
德州辅助(aApoker)外挂... 德州辅助(aApoker)外挂软件透明软件(智能ai代打)本来真的有挂(2021已更新)(知乎);1...
德扑之星有猫腻(德州竞技联盟)... 德扑之星有猫腻(德州竞技联盟)德州ai人工智能(透视)其实真的有挂(有挂俱乐部)-微博热搜1、让任何...
透视辅助!aapoker辅助工... 透视辅助!aapoker辅助工具"wpk辅助软件查得出来"果真真的有挂(有挂黑科技)-今日头条wpk...