獲取用戶資訊
第一種官方不推薦
<!-- 獲取用戶名 -->
<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://www.uj5u.com/qita/272196.html
標籤:其他
