微信小程序----用户位置获取&信息获取&图片上传

1. 用户位置获取

1.1 html

<view bindtap="getLocation">
      <view wx:if="{{address}}">{{address}}</view>
      <view wx:else>请选择位置</view>
 </view>

1.2 app.json添加配置

{
  "pages": [
    "pages/index/index"
  ],
  "permission": {
    "scope.userLocation": {
      "desc": "你的位置信息将用于小程序位置接口的效果展示"
    }
  }
}

1.3 js

data: {
    address: "",
  },
      
getLocation: function () {
    wx.chooseLocation({
      success: (res) => {
        this.setData({
          address: res.address
        })
      }
    });
  },

1.4 res数据格式

 2. 用户信息获取

2.1 html

<view>用户名称:{{ userInfo.nickName }}</view>
<view>
用户头像
<image src="{{ path }}" style="width: 150rpx;height: 150rpx;"></image>
</view>
<button  bindtap="getUserProfile"> 获取头像昵称 </button>

2.2 js

data: {
    userInfo: "",
    path:"/static/1.png",
  },
  getUserProfile: function () {
      // 替换this,里面的this指向不是data,而是调用的函数
    var that = this;
    wx.getUserProfile({
       // 获取信息的介绍
      desc: '骗你钱的',
      success: function (res) {
        // console.log('正确',res)
        that.setData({
          userInfo: res.userInfo,
          path:res.userInfo.avatarUrl,
        })
      },
      fail: function (res) {
        console.log('错误的详细', res)
      }
    })
  },

2.3 res信息格式

 3. 图片上传

3.1 html

<view bindtap="uploadImages">点我上传图片</view>
<view class="contain">
  <image wx:for="{{ imagePathList }}" src="{{ item }}"></image>
</view>

3.2 css

.contain image{
  width: 150rpx;
  height: 150rpx;
  padding: 5rpx;
}

3.2 js

data: {
    imagePathList: ["/static/1.png", "/static/1.png"]
  },
  
uploadImages: function () {
    var that = this
    wx.chooseImage({
      count: 9, // 最多可选择上传的图片 
      sizeType: ['original', 'compressed'], // 图片的像素 原图/压缩图
      sourceType: ['album', 'camera'],     // 图片上传的位置
      success: function (res) {
        console.log(res)
        // 给当前图片列表换成图片数据
        // that.setData({
        //   imagePathList:res.tempFilePaths
        // })
        // 给当前图片列表添加图片数据
        that.setData({
          imagePathList: that.data.imagePathList.concat(res.tempFilePaths)
        })
        
      
      }
    })
  },

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

骑猪去兜风z1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值