Skip to content

Commit 2ba9027

Browse files
author
romzhong
committed
Add samples: vtabs
Fix samples: bluetooth, navigator
1 parent 60598d0 commit 2ba9027

File tree

18 files changed

+344
-25
lines changed

18 files changed

+344
-25
lines changed

miniprogram/app.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,8 @@
154154
"page/API/pages/bluetooth/slave/slave",
155155
"page/API/pages/udp-socket/udp-socket",
156156
"page/API/pages/get-background-fetch-data/get-background-fetch-data",
157-
"page/API/pages/get-background-prefetch-data/get-background-prefetch-data"
157+
"page/API/pages/get-background-prefetch-data/get-background-prefetch-data",
158+
"page/weui/example/vtabs/vtabs"
158159
],
159160
"window": {
160161
"navigationBarTextStyle": "black",

miniprogram/page/API/pages/bluetooth/bluetooth.js

+16-2
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ Page({
162162
serviceId,
163163
success: (res) => {
164164
console.log('getBLEDeviceCharacteristics success', res.characteristics)
165+
165166
for (let i = 0; i < res.characteristics.length; i++) {
166167
const item = res.characteristics[i]
167168
if (item.properties.read) {
@@ -178,6 +179,7 @@ Page({
178179
this._deviceId = deviceId
179180
this._serviceId = serviceId
180181
this._characteristicId = item.uuid
182+
console.log('write')
181183
this.writeBLECharacteristicValue()
182184
}
183185
if (item.properties.notify || item.properties.indicate) {
@@ -209,6 +211,9 @@ Page({
209211
value: ab2hex(characteristic.value)
210212
}
211213
}
214+
wx.showToast({
215+
title: '收到从机数据',
216+
})
212217
// data[`chs[${this.data.chs.length}]`] = {
213218
// uuid: characteristic.characteristicId,
214219
// value: ab2hex(characteristic.value)
@@ -221,14 +226,23 @@ Page({
221226
const buffer = new ArrayBuffer(1)
222227
const dataView = new DataView(buffer)
223228
// eslint-disable-next-line
224-
dataView.setUint8(0, Math.random() * 20| 0)
229+
dataView.setUint8(0, Math.random() * 19| 0)
225230
wx.writeBLECharacteristicValue({
226231
deviceId: this._deviceId,
227232
serviceId: this._serviceId,
228233
characteristicId: this._characteristicId,
229234
value: buffer,
235+
success() {
236+
console.log('writeBLECharacteristicValue: 成功')
237+
},
238+
fail() {
239+
console.log('writeBLECharacteristicValue: 失败')
240+
},
241+
complete() {
242+
console.log('writeBLECharacteristicValue: 结束')
243+
244+
}
230245
})
231-
console.log('write data to slave')
232246
},
233247
closeBluetoothAdapter() {
234248
wx.closeBluetoothAdapter()

miniprogram/page/API/pages/bluetooth/slave/slave.js

+18-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
const uuid1 = '844F4F10-C9A7-4FA2-9A4D-84B55B4BDA7F'
2-
const uuid2 = '86026734-9356-0152-f726-02232202F9DF'
3-
const uuid3 = '0c76801a-62eb-45e5-96a8-37c8882abb2b'
1+
2+
const uuid3 = '0C76801A-62EB-45E5-96A8-37C8882ABB2B'
3+
const serviceId = 'D0611E78-BBB4-4591-A5F8-487910AE4366'
4+
const characteristicId = '8667556C-9A37-4C91-84ED-54EE27D90049'
5+
// 上面需要配置主机的 serviceId 和 characteristicId
6+
47

58
// ArrayBuffer转16进制字符串示例
69
function ab2hex(buffer) {
@@ -38,8 +41,7 @@ Page({
3841
*/
3942
onLoad: function (options) {
4043
wx.onBLEPeripheralConnectionStateChanged(res => {
41-
console.log('123')
42-
console.log('ConnectionStateChanged', res)
44+
console.log('connect')
4345
const connects = this.data.connects
4446
const idx = inArray(connects, 'deviceId', res.deviceId)
4547
if (idx >= 0) {
@@ -80,7 +82,6 @@ Page({
8082
this.server = res.server
8183
this.setData({serverId: this.server.serverId})
8284
this.server.onCharacteristicReadRequest(res => {
83-
console.log('123')
8485
const { serviceId, characteristicId, callbackId } = res
8586
const buffer = new ArrayBuffer(1)
8687
const dataView = new DataView(buffer)
@@ -96,10 +97,13 @@ Page({
9697
callbackId
9798
})
9899
})
99-
100+
// 监听收到数据
100101
this.server.onCharacteristicWriteRequest(res => {
101102
console.log('onCharacteristicWriteRequest', res)
102103
const { serviceId, characteristicId, value, callbackId } = res
104+
wx.showToast({
105+
title: '收到主机数据'
106+
})
103107
this.server.writeCharacteristicValue({
104108
serviceId,
105109
characteristicId,
@@ -123,8 +127,8 @@ Page({
123127
const dataView = new DataView(buffer)
124128
dataView.setUint8(0, n)
125129
this.server.writeCharacteristicValue({
126-
serviceId: uuid1,
127-
characteristicId: uuid2,
130+
serviceId: serviceId,
131+
characteristicId: characteristicId,
128132
value: buffer,
129133
needNotify: true
130134
})
@@ -143,9 +147,9 @@ Page({
143147
dataView2.setInt8(0, 3)
144148

145149
const service = {
146-
uuid: uuid1,
150+
uuid: serviceId,
147151
characteristics: [{
148-
uuid: uuid2,
152+
uuid: characteristicId,
149153
properties: {
150154
write: true,
151155
read: true,
@@ -169,6 +173,7 @@ Page({
169173
}]
170174
}]
171175
}
176+
172177
this.server.addService({
173178
service
174179
}).then(res => {
@@ -177,7 +182,7 @@ Page({
177182
},
178183
removeService() {
179184
this.server.removeService({
180-
serviceId: uuid1
185+
serviceId: serviceId
181186
}).then(res => {
182187
console.log('removeService', res)
183188
})
@@ -190,7 +195,7 @@ Page({
190195
advertiseRequest: {
191196
connectable: true,
192197
deviceName: 'sanford',
193-
serviceUuids: [uuid1],
198+
serviceUuids: [serviceId],
194199
manufacturerData: [{
195200
manufacturerId: 'sanfordsun-pc0',
196201
manufacturerSpecificData: buffer

miniprogram/page/component/pages/navigator/navigator.wxml

+5-5
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66

77
<view class="page-body">
88
<view class="btn-area">
9-
<navigator url="navigate?title=navigate" hover-class="navigator-hover">
10-
<button type="default">跳转到新页面</button>
9+
<navigator url="navigate?title=navigate" hover-class="ohter-navigator">
10+
<button type="default">跳转到新页面</button>
1111
</navigator>
1212
<navigator url="redirect?title=redirect" redirect hover-class="other-navigator-hover">
1313
<button type="default">在当前页打开</button>
1414
</navigator>
15-
<navigator target="miniProgram" open-type="navigate" app-id="wx4f1b24bdc99fa23b" version="release">
16-
<button type="default">打开小程序</button>
17-
</navigator>
15+
<navigator target="miniProgram" hover-class="other-navigator-hover" open-type="navigate" app-id="wx4f1b24bdc99fa23b" version="release">
16+
<button type="default">打开小程序</button>
17+
</navigator>
1818
</view>
1919
</view>
2020

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
3+
Component({
4+
options: {
5+
addGlobalClass: true,
6+
multipleSlots: true
7+
},
8+
properties: {
9+
tabIndex: {
10+
type: Number,
11+
value: 0
12+
}
13+
},
14+
relations: {
15+
'../vtabs/vtabs': {
16+
type: 'parent'
17+
}
18+
},
19+
lifetimes: {
20+
attached: function attached() {}
21+
},
22+
methods: {
23+
calcHeight: function calcHeight(callback) {
24+
var query = this.createSelectorQuery();
25+
query.select('.weui-vtabs-content__item').boundingClientRect(function (rect) {
26+
callback && callback(rect);
27+
}).exec();
28+
}
29+
}
30+
});
31+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"component": true,
3+
"usingComponents": {}
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<view class="weui-vtabs-content__item" id="weui-vtabs-content__{{tabIndex}}">
2+
<slot ></slot>
3+
</view>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.weui-tabs-content__item {
2+
width: 100%;
3+
height: 100%
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
2+
3+
Component({
4+
options: {
5+
addGlobalClass: true,
6+
pureDataPattern: /^_/,
7+
multipleSlots: true
8+
},
9+
properties: {
10+
vtabs: { type: Array, value: [] },
11+
tabBarClass: { type: String, value: '' },
12+
activeClass: { type: String, value: '' },
13+
tabBarLineColor: { type: String, value: '#ff0000' },
14+
tabBarInactiveTextColor: { type: String, value: '#000000' },
15+
tabBarActiveTextColor: { type: String, value: '#ff0000' },
16+
tabBarInactiveBgColor: { type: String, value: '#eeeeee' },
17+
tabBarActiveBgColor: { type: String, value: '#ffffff' },
18+
activeTab: { type: Number, value: 0 },
19+
animation: { type: Boolean, value: true }
20+
},
21+
data: {
22+
currentView: 0,
23+
contentScrollTop: 0,
24+
_heightRecords: [],
25+
_contentHeight: {}
26+
},
27+
observers: {
28+
activeTab: function activeTab(_activeTab) {
29+
this.scrollTabBar(_activeTab);
30+
}
31+
},
32+
relations: {
33+
'../vtabs-content/vtabs-content': {
34+
type: 'child',
35+
linked: function linked(target) {
36+
var _this = this;
37+
38+
target.calcHeight(function (rect) {
39+
_this.data._contentHeight[target.data.tabIndex] = rect.height;
40+
if (_this._calcHeightTimer) {
41+
clearTimeout(_this._calcHeightTimer);
42+
}
43+
_this._calcHeightTimer = setTimeout(function () {
44+
_this.calcHeight();
45+
}, 100);
46+
});
47+
},
48+
unlinked: function unlinked(target) {
49+
delete this.data._contentHeight[target.data.tabIndex];
50+
}
51+
}
52+
},
53+
lifetimes: {
54+
attached: function attached() {}
55+
},
56+
methods: {
57+
calcHeight: function calcHeight() {
58+
var length = this.data.vtabs.length;
59+
var _contentHeight = this.data._contentHeight;
60+
var _heightRecords = [];
61+
var temp = 0;
62+
for (var i = 0; i < length; i++) {
63+
_heightRecords[i] = temp + (_contentHeight[i] || 0);
64+
temp = _heightRecords[i];
65+
}
66+
this.data._heightRecords = _heightRecords;
67+
},
68+
scrollTabBar: function scrollTabBar(index) {
69+
var len = this.data.vtabs.length;
70+
if (len === 0) return;
71+
var currentView = index < 6 ? 0 : index - 5;
72+
if (currentView >= len) currentView = len - 1;
73+
this.setData({ currentView: currentView });
74+
},
75+
handleTabClick: function handleTabClick(e) {
76+
var _heightRecords = this.data._heightRecords;
77+
var index = e.currentTarget.dataset.index;
78+
var contentScrollTop = _heightRecords[index - 1] || 0;
79+
this.triggerEvent('tabclick', { index: index });
80+
this.setData({
81+
activeTab: index,
82+
contentScrollTop: contentScrollTop
83+
});
84+
},
85+
handleContentScroll: function handleContentScroll(e) {
86+
var _heightRecords = this.data._heightRecords;
87+
if (_heightRecords.length === 0) return;
88+
var length = this.data.vtabs.length;
89+
var scrollTop = e.detail.scrollTop;
90+
var index = 0;
91+
if (scrollTop >= _heightRecords[0]) {
92+
for (var i = 1; i < length; i++) {
93+
if (scrollTop >= _heightRecords[i - 1] && scrollTop < _heightRecords[i]) {
94+
index = i;
95+
break;
96+
}
97+
}
98+
}
99+
if (index !== this.data.activeTab) {
100+
this.triggerEvent('change', { index: index });
101+
this.setData({ activeTab: index });
102+
}
103+
}
104+
}
105+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"component": true,
3+
"usingComponents": {}
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
3+
<view class="weui-vtabs">
4+
<view class="weui-vtabs-bar__wrp {{tabBarClass}}">
5+
<scroll-view
6+
scroll-y
7+
class="weui-vtabs-bar__scrollview"
8+
scroll-into-view="weui-vtabs-item__{{currentView}}"
9+
>
10+
<view class="weui-vtabs-bar__content">
11+
<block wx:for="{{vtabs}}" wx:key="title">
12+
<view
13+
id="weui-vtabs-item__{{index}}"
14+
class="weui-vtabs-bar__item"
15+
data-index="{{index}}"
16+
style="background-color: {{activeTab === index ? tabBarActiveBgColor : tabBarInactiveBgColor}}; color: {{activeTab === index ? tabBarActiveTextColor : tabBarInactiveTextColor}}; border-left-color: {{activeTab === index ? tabBarLineColor : tabBarInactiveBgColor}}"
17+
bindtap="handleTabClick"
18+
>
19+
<view class="weui-vtabs-bar__title {{activeTab === index ? activeClass : ''}}">
20+
<text class="">{{item.title}}</text>
21+
</view>
22+
</view>
23+
</block>
24+
</view>
25+
</scroll-view>
26+
</view>
27+
<view class="weui-vtabs-content__wrp">
28+
<scroll-view
29+
scroll-y
30+
class="weui-vtabs-content__scrollview"
31+
scroll-top="{{contentScrollTop}}"
32+
scroll-with-animation="{{animation}}"
33+
bindscroll="handleContentScroll"
34+
>
35+
<view class="weui-vtabs-content">
36+
<slot ></slot>
37+
</view>
38+
</scroll-view>
39+
</view>
40+
</view>

0 commit comments

Comments
 (0)