小程序 获取用户头像、昵称、手机号的组件封装(最新版)

在父组件引入该组件

javascript 复制代码
<!-- 授权信息 -->
  <auth-mes showModal="{{showModal}}" id='autnMes' bind:onConfirm="onConfirm"></auth-mes>

子组件详细代码为:

authMes.wxml

html 复制代码
<!-- components/authMes/authMes.wxml -->
<van-popup show="{{ showModal }}" round bind:close="closeHandle" custom-class="auth-box" custom-style="width: 84%;z-index:10002;" overlay-style="z-index:10001;">
  <view class="auth-wrap">
    <view>
      <view class="tips-tit">提示</view>
      <view class="tips-txt">为提供更好的服务,我们邀请您填写昵称,头像等公开信息</view>
    </view>
    <view class="auth-cont">
      <view class="auth-itm">
        <view class="itm-tit">头像</view>
        <view class="avatar-cont">
          <button class="avatar-btn" open-type="chooseAvatar" bind:chooseavatar="onChooseAvatar">
            <image class="avatar" src="{{avatarMes.avatarUrl}}" wx:if="{{avatarMes.avatarUrl}}"></image>
            <image class="avatar" src="../../images/common/auth_default.png" wx:if="{{!avatarMes.avatarUrl}}"></image>
            <image class="icon-r" src="../../images/common/icon_arrow_black.png" />
          </button>
        </view>
      </view>
      <view class="auth-itm">
        <view class="itm-tit">昵称</view>
        <input class="nick-name" placeholder="点击输入" type="nickname" value="{{avatarMes.nickName}}" bindblur="bindblur" placeholder-class="input-holder"></input>
      </view>
      <view class="auth-itm">
        <view class="itm-tit">手机号</view>
        <view>
          <button slot="button" plain size="mini" type="" open-type="getPhoneNumber" bindgetphonenumber="getPhone" class="phone-btn">
            <text wx:if="{{!avatarMes.mobile}}">获取手机号</text>
            <text wx:if="{{avatarMes.mobile}}" style="color:#302e2d;">{{avatarMes.mobile}}</text>
          </button>
        </view>
      </view>
    </view>
    <view class="auth-btn">
      <view class="btn" catchtap="cancel">取消</view>
      <view class="btn com-btn" catchtap="confirm">确认</view>
    </view>
  </view>
</van-popup>

authMes.js

javascript 复制代码
// components/authMes/authMes.js
// 获取应用实例
const app = getApp();
const { enums } = require("../../common/config/enums");
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    // 小程序弹窗
    showModal: {
      type: Boolean,
      value: false,
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    avatarMes: {
      avatarUrl: "",
      nickName: "",
      mobile: "",
    },
  },

  /**
   * 组件的方法列表
   */
  methods: {
    /** 获取昵称信息 */
    bindblur(res) {
      const value = res.detail.value;
      this.data.avatarMes.nickName = value;
    },
    // 拿到头像信息的临时路径
    onChooseAvatar(e) {
      const { avatarUrl } = e.detail;
      console.log('ddd', avatarUrl);
      this.uploadAva(e.detail.avatarUrl);
    },
    uploadAva(tempFilePaths) {
      var that = this;
      wx.uploadFile({
        url: app.siteinfo.apiUrl + '图片上传接口', //需要用HTTPS,同时在微信公众平台后台添加服务器地址
        filePath: tempFilePaths, //上传的文件本地地址
        name: "Image", //服务器定义的Key值
        header: {
          'content-type': 'multipart/form-data',
          'cookie': wx.getStorageSync('cookie')
        },
        formData: {
          //接口所需的其他上传字段
          uploadDir: enums.UploadDir.Personal.Value,
          fileType: enums.FileType.Image.Value,
        },
        // 附近数据,这里为路径
        success: function (res) {
          wx.hideLoading();
          if (res.statusCode == 200) {
            var result = JSON.parse(res.data);
            if (result.status) {
              // var imgUrl = [{ name: 'headImgUrl', url: result.data.fileurl }];
              that.setData({
                'avatarMes.avatarUrl': result.data.fileurl
              })
            } else {
              app.alert.show(res.errmsg);
            }
          } else {
            app.alert.show(res);
          }
        },
        fail: function (err) {
          console.log(err);
        }
      });
    },
    // 输入
    onChange(e) {
      let field = 'avatarMes.' + e.currentTarget.dataset.field;
      this.setData({
        [field]: e.detail
      });
    },
    // 手机号授权
    getPhone(mobile) {
      console.log('mobile.detail', mobile.detail)
      if (mobile.detail && mobile.detail.code) {
        let code = mobile.detail.code;
        app.apis('手机号解码接口', { code: code }).then(res => {
          if (res.status) {
            this.setData({
              'avatarMes.mobile': res.data.Mobile
            });
          } else {
            app.alert.show('获取手机号码失败,请重试!' + res.errmsg);
          }
        })
      } else {
        app.toast.show(mobile.detail.errMsg);
      }
    },
    //点击遮罩层关闭
    closeHandle() {
      this.setData({
        showModal: false
      })
    },
    // 取消
    cancel() {
      this.setData({
        showModal: false
      })
    },
    // 确认
    confirm() {
      this.triggerEvent('onConfirm', { avatar: this.data.avatarMes });
    },
  },
})

authMes.less

css 复制代码
.auth-wrap {
  border-radius: 40rpx;
  box-sizing: border-box;
  padding: 24rpx;
  opacity: 1;
  background-color: #ffffff;
  z-index: 100002;
  .tips-tit {
    font-family: PingFangSC-Medium;
    font-size: 32rpx;
    color: #302e2d;
    text-align: center;
    font-weight: 500;
  }
  .tips-txt {
    padding: 40rpx 10rpx;
    font-size: 28rpx;
    color: #282b34;
    letter-spacing: 0;
    text-align: center;
    font-weight: 400;
  }
  .auth-cont {
    .auth-itm {
      background-color: #f6f6f6;
      border-radius: 20rpx;
      padding: 0 24rpx;
      height: 96rpx;
      margin-bottom: 24rpx;
      display: flex;
      align-items: center;
      justify-content: space-between;
      .itm-tit {
        width: 100rpx;
        font-family: PingFangSC-Regular;
        font-size: 28rpx;
        color: #302e2d;
      }

      .avatar-cont {
        width: 18%;
        .avatar-btn {
          width: 100%;
          height: 96rpx;
          padding: 0!important;
          box-sizing: content-box !important;
          background-color: transparent;
          display: flex;
          align-items: center;
          justify-content: space-between;
          .avatar {
            width: 56rpx;
            height: 56rpx;
            display: inline-block;
          }
          &::after {
            border: none;
          }
        }
        .icon-r {
          width: 24rpx;
          height: 24rpx;
          margin-left: 8rpx;
        }
      }
      .nick-name {
        text-align: right;
        font-family: PingFangSC-Regular;
        font-size: 28rpx;
        color: #302e2d;
      }
    }
  }
  .aggre-v {
    display: flex;
    align-items: center;
    font-family: PingFangSC-Regular;
    font-size: 26rpx;
    color: #302e2d;
    font-weight: 400;
    image {
      width: 24rpx;
      height: 24rpx;
      margin-right: 16rpx;
    }
  }

  .auth-btn {
    width: 100%;
    display: grid;
    grid-template-columns: 1fr 1fr;
    grid-column-gap: 24rpx;
    margin: 30rpx 0 20rpx;
    .btn {
      font-family: PingFangSC-Medium;
      font-size: 32rpx;
      color: #99302f;
      background-color: #ffffff;
      border-radius: 20rpx;
      border: 2rpx solid #99302f;
      height: 88rpx;
      line-height: 88rpx;
      text-align: center;
    }
    .com-btn {
      background-color: #ebd6d7;
      border-color: #ebd6d7;
    }
  }
  .phone-btn {
    border: 0;
    text-align: right;
    padding: 0;
    line-height: 1;
    font-family: PingFangSC-Regular;
    font-size: 28rpx;
    color: #99302f;
  }
  .fie-lable {
    font-family: PingFangSC-Regular;
    font-size: 28rpx;
    color: #302e2d;
  }
  .input-holder {
    font-family: PingFangSC-Regular;
    font-size: 28rpx;
    color: #88868b;
  }
}

authMes.json

javascript 复制代码
{
  "component": true,
  "usingComponents": {
    "van-field": "@vant/weapp/field/index",
    "van-popup": "@vant/weapp/popup/index"
  }
}

app.apis是封装的微信小程序请求接口的方法详细见这篇文章

相关推荐
vx_vxbs6616 分钟前
【SSM电动车智能充电服务平台】(免费领源码+演示录像)|可做计算机毕设Java、Python、PHP、小程序APP、C#、爬虫大数据、单片机、文案
java·spring boot·mysql·spring cloud·小程序·php·idea
低代码布道师3 小时前
医疗小程序12出诊列表
低代码·小程序
Coder-coco3 小时前
游戏助手|游戏攻略|基于SprinBoot+vue的游戏攻略系统小程序(源码+数据库+文档)
java·vue.js·spring boot·游戏·小程序·论文·游戏助手
小小王app小程序开发18 小时前
盲盒小程序一番赏衍生玩法:魔王赏、非酋赏、狂欢赏差异化分析
小程序
2501_9151063219 小时前
iOS App 测试工具全景分析,构建从开发调试到线上监控的多阶段工具链体系
android·测试工具·ios·小程序·uni-app·iphone·webview
头发还在的女程序员2 天前
基于JAVA语言的短剧小程序-抖音短剧小程序
java·开发语言·小程序
2501_916007472 天前
iOS 应用性能测试的工程化流程,构建从指标采集到问题归因的多工具协同测试体系
android·ios·小程序·https·uni-app·iphone·webview
book多得2 天前
刷题专用微信小程序推荐
微信小程序·小程序
00后程序员张2 天前
iOS 抓不到包怎么办?从 HTTPS 解密、QUIC 排查到 TCP 数据流分析的完整解决方案
android·tcp/ip·ios·小程序·https·uni-app·iphone
技术与健康2 天前
微信小程序云开发实践:共享环境与LLM整合经验
微信小程序·小程序