当前位置:主页 > 查看内容

微信小程序获取用户信息,获取位置,for指令,获取图片(大白菜

发布时间:2021-04-18 00:00| 位朋友查看

简介:获取用户信息 第一种官方不推荐 !-- 获取用户名 -- view 当前用户名{{name}} / view view 获取用户头像 image src {{path}} style height : 200rpx ; width : 200rpx / image / view view bindtap getUsrName 获取房前用户名: / view data : { message : 大……

获取用户信息

第一种官方不推荐
<!-- 获取用户名 -->

<view>当前用户名:{{name}}</view>

<view>获取用户头像

 <image src="{{path}}" style="height:200rpx;width:200rpx"></image>

</view>

<view bindtap="getUsrName">获取房前用户名:</view>
 data: {
    message:"大白菜",
    name:'',
    path:'/img/mryhtx.png'
  },
      
  changData:function(){
    // 获取数据
    console.log(this.data.message);
    //修改数据 (错误  后端修改)
    // this.data.message="沙雕大白菜";
    // 前端正确修改方式
    this.setData({message:"大沙雕大白菜"})
  },
      
  getUsrName:function(){
  var that = this;
  console.log('sdf');
  // 调用微信接口,获取当前用户信息
  wx.getUserInfo({
    success: function(res) {
      console.log('success',res)
      // 获取用户名和头像
      that.setData({
        // 用户名
        name:res.userInfo.nickName,
        // 头像
        path:res.userInfo.avatarUrl
      })
    }, 
    file:function(res){
      // 调用失败后触发
    }
  })
}
第二种官方推荐的方法
<button open-type="getUserInfo" bindgetuserinfo="xxxx" >获取用户信息 </button>
 xxxx:function(){
  wx.getUserInfo({
    success: function(res) {
      console.log('success',res)
      // 获取用户名和头像
    },
    file:function(res){
      // 调用失败后触发
    }
  })
//手动授权
wx.openSetting(){}
获取用户位置
<view bindtap="getLocalPath">{{localPath}}</view>
Page({
  data: {
  localPath:"当前位置信息"
  },
 	getLocalPath:function(){
    var that=this;
  wx.chooseLocation({
    success:function(res){
      that.setData({localPath:res.address});
    },
  })
}
})
for指令
//<text>商品列表展示:</text>
//<view>
  //<view wx:for="{{dataList}}">{{index}}-{{item}}</view>
//</view>

<text>商品列表展示:</text>
<view>
  <view wx:for="{{dataList}}" wx:for-index="idx" wx:for-item="x">{{idx}}-{{x}}</view>
</view>

<view>
  {{userInfo.name}}
  {{userInfo.age}}
</view>
<view>
  <view wx:for="{{userInfo}}">{{index}}-{{item}}</view>
</view>
// pages/goods/goods.js
Page({
  data: {
    dataList:[],//列表
    userInfo:{//字典
     name:'',
     age:
    }
  }
})
获取图片
<!--pages/publish/publish.wxml-->
<text>pages/publish/publish.wxml</text>
<view bindtap="uploadImage">请上传图片</view>
<view class="container">
  <image wx:for="{{imageList}}" src="{{item}}"> </image>
</view>
Page({
  data: {
  imageList:[]
  },
  uploadImage:function(){
    var that=this;
   wx.chooseImage({
     count: 9,
     siceType:,
     sourceType	:,
     success:function(res){
      
      // that.setData({
      //   imageList:res.tempFilePaths
      // });
      //
      that.setData({
      imageList:that.data.imageList.concat(res.tempFilePaths)
      });
     }
   });
  }
})
.container image{
  width: 200rpx;
  height:200rpx;
  padding:5rpx;
}
  • 注意事项:图片只是上传到内存
;原文链接:https://blog.csdn.net/m0_50641264/article/details/115414466
本站部分内容转载于网络,版权归原作者所有,转载之目的在于传播更多优秀技术内容,如有侵权请联系QQ/微信:153890879删除,谢谢!

推荐图文


随机推荐