小程序获取用户信息

小程序获取用户信息,第1张

一使用 open-data 组件

基本使用 open-data 是微信小程序官方提供的组件,作用是用来展示微信开放的数据。 展示用户头像:

设置样式 若需要给这个组件设置样式,需要在外层包一个 view 标签,为这个标签写样式。如让这个头像显示成圆形:

二、使用 wxgetUserInfo (不推荐使用)

过去获取用户信息一般都是用这种方式。使用时,若用户未授权,会直接d出授权框。 为了优化用户体验,调用这个接口将无法d出授权框,而是会默认调用失败。

退出登录信息

激活小程序,点击标签。
语法:data-xx='xxx',其中:
(1)data-为固定结构。
(2)xx命名不能有大写,有大写会被自动转换为小写。
(3)属性值是变量需要,'{{xxx}}'。
(4)在事件中通过eventcurrentTargetdatasetxx来获取属性。
微信小程序是一种不用下载就能使用的应用,也是一项创新,经过将近两年的发展,已经构造了新的微信小程序开发环境和开发者生态。微信小程序也是这么多年来中国IT行业里一个真正能够影响到普通程序员的创新成果,已经有超过150万的开发者加入到了微信小程序的开发,与我们一起共同发力推动微信小程序的发展,微信小程序应用数量超过了一百万,覆盖200多个细分的行业,日活用户达到两个亿,微信小程序还在许多城市实现了支持地铁、公交服务。微信小程序发展带来更多的就业机会,2017年小程序带动就业104万人,社会效应不断提升。

到demo1-listde的demo1-listdewxml添加输入框和添加按钮
商品
<input bindinput="getName"></input>
商品价格
<input bindinput="getPrice"></input><!-- 输入框 -->
<button bindtap="addGood" type="primary">添加商品</button><!-- 添加按钮 -->
1
2
3
4
5
1
2
3
4
5
到demo1-listde的demo1-listdewxss添加输入框和添加按钮的样式
input{
border: 1px solid gray;/ 1像素,实心,灰色 /
width: 200px;
}
1
2
3
4
1
2
3
4
保存运行则得到
在这里插入描述
2到demo1-listde的demo1-listdejs
在页面顶端设置全局模式
let name = ''
let price = ''
1
2
1
2
获取用户输入的商品名和价格
getName(n) {
name= ndetailvalue
},
getPrice(n) {
price= ndetailvalue
},
1
2
3
4
5
6
1
2
3
4
5
6
把用户添加的商品添加到数据库
addGood() {
consolelog('商品名', name)
consolelog('商品价格', price)
1
2
3
1
2
3
为了避免代码重复
把下图请添加描述
改为
getList(){
wxclouddatabase()collection("goods")
get()
then(res=> {
consolelog('列表请求成功',res)
thissetData({//把请求的数据赋值给list
list:resdata
})
})
catch(res=> {
consolelog('列表请求失败',err)
})
1
2
3
4
5
6
7
8
9
10
11
12
1
2
3
4
5
6
7
8
9
10
11
12
为添加商品制作一个成功或失败提示d窗
if (name == '') {
wxshowToast({//提示d窗
icon:'none',
title: '商品名为空',
})
} else if (price == '') {
wxshowToast({//提示d窗
icon:'none',
title: '价格为空',
})
} else{
consolelog('请添加商品')
wxclouddatabase()collection('goods')
add({
data:{
name: name,
price: price
}
})
then(res =>{
consolelog('添加成功',res)
thisgetList()
})
catch(res =>{
consolelog('添加失败',err)
})
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
保存运行即可在小程序页面添加商品和价格
更改商品价格
1,到demo11-details的demo11-detailswxml添加输入框和添加按钮
<view>商品名:{{goodname}},价格:{{goodprice}}</view><!-- viem默认有换行的效果 -->
更新商品价格
<input bindinput="getPrice"></input>
<button type="primary" bindtap="update">更新商品价格</button>
1
2
3
4
1
2
3
4
到demo11-details的demo11-detailswxss添加输入框和添加按钮的样式
input{
border: 1px solid gray;
width: 200px
}
1
2
3
4
1
2
3
4
保存运行得到
在这里插入描述
2到demo11-details的demo11-detailsjs
在页面顶部添加全局模式
let price = ''
var id = ''
1
2
1
2
把onLoad板块改为
onLoad(options) {//列表携带的数据,传递到了onload方法里
consolelog('列表携带的值',options)
id = optionsid
thisgetDetail()
}
1
2
3
4
5
1
2
3
4
5
为避免下面代码出现重复
把下图的代码
在这里插入描述
改为
getDetail() {
wxclouddatabase()collection('goods')
doc(id)
get()
then(res=> {
consolelog('精品课程详情页请求成功',res)
thissetData({
good:resdata
})
})
catch(res=>{
consolelog('精品课程详情页请求失败',err)
})
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
1
2
3
4
5
6
7
8
9
10
11
12
13
14
获取新价格
getPrice(n) {
price=ndetailvalue
}
1
2
3
1
2
3
修改商品的价格
update() {
consolelog('新的商品价格',price)
if (price == '') {
wxshowToast({
icon:'none',
title: '价格为空',
})
} else {
wxclouddatabase()collection('goods')
doc(id)
update({
data:{
price:price
}
})
then(res =>{
consolelog('更新成功',res)
thisgetDetail()
})
catch(res =>{
consolelog('更新失败',err)
})
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
保存运行即可更改商品的价格
删除商品
1,到demo11-details的demo11-detailswxml添加删除按钮
<button type='primary' bindtap="delete">删除当前商品</button>
1
1
2,到demo11-details的demo11-detailsjs
添加删除商品的代码
delete() {
consolelog('点击了删除键',id)
wxshowModal({
title:'提示',
content:'确定要删除这个商品吗',
success(res) {
if (resconfirm) {
consolelog('确定')
//从数据库删除数据
wxclouddatabase()collection('goods')
doc(id)
remove()
then(res => {
consolelog('删除完成',res)
wxnavigateTo({
url: '/pages/demo1-list/demo1-list',
})
})
catch(res=>{
consolelog('删除失败',err)
})
}else if (rescancel) {
consolelog('取消')
}
}
})
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
保存运行即可
完成后的全部代码
demo1-listjs
let name = ''
let price = ''
Page({
onLoad(){
thisgetList()
},
//获取列表数据
getList(){
wxclouddatabase()collection("goods")
get()
then(res=> {
consolelog('列表请求成功',res)
thissetData({//把请求的数据赋值给list
list:resdata
})
})
catch(res=> {
consolelog('列表请求失败',err)
})
},
//跳转到精品课程详情页
toDetail(n) {
consolelog('跳转到精品课程详情页的id',ncurrentTargetdatasetid)
wxnavigateTo({
url: '/pages/demo11-details/demo11-detailsid=' + ncurrentTargetdatasetid,//跳转到商品详情页,并携带商品id
})
},
//获取用户输入的商品名和价格
getName(n) {
name= ndetailvalue
},
getPrice(n) {
price= ndetailvalue
},
//添加商品到数据库
addGood() {
consolelog('商品名', name)
consolelog('商品价格', price)
if (name == '') {
wxshowToast({//提示d窗
icon:'none',
title: '商品名为空',
})
} else if (price == '') {
wxshowToast({//提示d窗
icon:'none',
title: '价格为空',
})
} else{
consolelog('请添加商品')
wxclouddatabase()collection('goods')
add({
data:{
name: name,
price: price
}
})
then(res =>{
consolelog('添加成功',res)
thisgetList()
})
catch(res =>{
consolelog('添加失败',err)
})
}
}
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
demo1-listwxml
商品名
<input bindinput="getName"></input>
商品价格
<input bindinput="getPrice"></input><!-- 输入框 -->
<button bindtap="addGood" type="primary">添加商品</button><!-- 添加按钮 -->
<view wx:for="{{list}}">
<view bindtap="toDetail" data-id="{{item_id}}">商品名:{{itemname}},价格:{{itemprice}} </view>
</view>
1
2
3
4
5
6
7
8
1
2
3
4
5
6
7
8
demo1-listwxss
input{
border: 1px solid gray;/ 1像素,实心,灰色 /
width: 200px;
}
1
2
3
4
1
2
3
4
demo11-detailsjs
let price = ''
var id = ''
Page({
data:{
good:{}
},
onLoad(options) {//列表携带的数据,传递到了onload方法里
consolelog('列表携带的值',options)
id = optionsid
thisgetDetail()
},
//获取商品的数据
getDetail() {
wxclouddatabase()collection('goods')
doc(id)
get()
then(res=> {
consolelog('精品课程详情页请求成功',res)
thissetData({
good:resdata
})
})
catch(res=>{
consolelog('精品课程详情页请求失败',err)
})
},
//获取新价格
getPrice(n) {
price=ndetailvalue
},
//修改商品价格
update() {
consolelog('新的商品价格',price)
if (price == '') {
wxshowToast({
icon:'none',
title: '价格为空',
})
} else {
wxclouddatabase()collection('goods')
doc(id)
update({
data:{
price:price
}
})
then(res =>{
consolelog('更新成功',res)
thisgetDetail()
})
catch(res =>{
consolelog('更新失败',err)
})
}
},
delete() {
consolelog('点击了删除键',id)
wxshowModal({
title:'提示',
content:'确定要删除这个商品吗',
success(res) {
if (resconfirm) {
consolelog('确定')
//从数据库删除数据
wxclouddatabase()collection('goods')
doc(id)
remove()
then(res => {
consolelog('删除完成',res)
wxnavigateTo({
url: '/pages/demo1-list/demo1-list',
})
})
catch(res=>{
consolelog('删除失败',err)
})
}else if (rescancel) {
consolelog('取消')
}
}
})
}

})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
demo11-detailswxml
<!--pages/demo11-details/demo11-detailswxml-->
<view>商品名:{{goodname}},价格:{{goodprice}}</view><!-- viem默认有换行的效果 -->
更新商品价格
<input bindinput="getPrice"></input>
<button type="primary" bindtap="update">更新商品价格</button>
<button type='primary' bindtap="delete">删除当前商品</button>
demo11-detailswxss

1微信OAuth网页授权服务号(订阅号行)公众号台通微信OAuth网页授权用户网页进行授权 *** 作用户openid
2接入服务器微信允许接入服务器进行发配置接入服务器(参考微信发文档)用户发给公众号消息全部xml表单形式转发服务器
xml形式像:
FromUserName标签内容即该用户OpenID

标签数据在data中定义, 因为还要点击高亮, 所以同时给个状态值

页面循环渲染出来
点击事件bindtap='select',
绑定class: class="{{itemisSelect'active':'select'}}", 点击的时候改变类名
自定义dataset :data-index="{{index}}"

样式效果

JS部份

到这儿, 高亮的就已经写好了 怎么取值呢 下面还有一个保存按钮 给保存按钮一个点击事件:
1定义一个空数据
2遍历上边的data里的selectall数组, 传两个参数, v是所有标签的状态(比如isSelect:false或isSelect:true), i是下标

看打印结果

view标签 加 bindtap事件,用data-name传值,如果view中只有文字,点击整个view区域都可以接收到data-name的值,如果view里面加一个lable标签,那么点击lable包裹的区域,data-name取不到值。
解决方法:把取值方式 由etargetdatasetcarrierName 修改为ecurrentTargetdatasetcarrierName即可!


欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/yw/12779008.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-28
下一篇 2023-05-28

发表评论

登录后才能评论

评论列表(0条)

保存