微信小程序开放接口(用户登录,用户地址)

1.获取用户信息

1.1 wx.getUserProfile()
1.2事件绑定
<view>用户名:{
          
   {
          
   name}}</view>
<image src="{
          
   {image}}" style="width: 150rpx; height: 150rpx;"></image>
<button bindtap="getuser">点我</button>

js

Page({
          
   

  /**
   * 页面的初始数据
   */
  data: {
          
   
   name:"",
   image:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
          
   

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  getuser:function(){
          
   
  //this只是作用于pages页面,当方法体中还调用方法是this失效,用that作为中转
    var that=this 
    wx.getUserProfile({
          
   
      desc: 用于开发,
      success:function(res){
          
   
        console.log(res)
       that.setData({
          
   
         name:res.userInfo.nickName,
         image:res.userInfo.avatarUrl
       })
      },
    fail:function(res){
          
   
      console.log(失败)
    }
    })
  },
  
})

注意点:this只是作用于pages页面,当方法体中还调用方法是this失效,用that作为中转

2.获取用户地理位置

2.1 wx.chooseLocation()
2.2事件绑定
<view>位置:{
          
   {
          
   address}}{
          
   {
          
   name}}</view>
<button bindtap="getuser">点我</button>

js

// pages/text/text.js
Page({
          
   

  /**
   * 页面的初始数据
   */
  data: {
          
   
   address:"",
   name:
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
          
   

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  getuser:function(){
          
   
    var that=this
    wx.chooseLocation({
          
   
      latitude: 0,
      success(e){
          
   
        console.log(e)
        that.setData({
          
   
          address:e.address,
          name:e.name
        })
      },
      fail:function(res){
          
   
        console.log(失败)
      }
    })
  },

  
})
2.3效果

-

3.上传图片

3.1 wx.chooseImage()
3.2事件绑定
<view>
  <image wx:for="{
          
   {imagelist}}" src="{
          
   {item}}" style="height: 200rpx; width: 200rpx;"></image>
</view>
<button bindtap="getuser">点我</button>

js

Page({
          
   

  /**
   * 页面的初始数据
   */
  data: {
          
   
   imagelist:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
          
   

  },
  getuser:function(){
          
   
    var that=this
    wx.chooseImage({
          
   
      count: 9,
      sizeType:[original,compressed],
      sourceType:[album,camera],
      success(res){
          
   
        console.log(res)
       that.setData({
          
   
         imagelist:imagelist:that.data.imagelist.concat(res.tempFilePaths) 
       })
      }
    })
  
  },
})

注意:imagelist:that.data.imagelist.concat(res.tempFilePaths) 中concat用于俩个数组的连接

3.3效果
经验分享 程序员 微信小程序 职场和发展