w-ui-v1 1.0.52 → 1.0.54

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/index.ts +3 -1
  2. package/package.json +1 -1
  3. package/utils/nfc.ts +260 -0
package/index.ts CHANGED
@@ -17,6 +17,7 @@ import WReportTable from './w-report-table/w-report-table.vue'
17
17
  import WFormControlVue from './w-form-control/w-form-control.vue'
18
18
  import {menu} from './utils/apis/menu'
19
19
  import request from './utils/http'
20
+ import nfc from './utils/nfc'
20
21
  const coms: any[] = [
21
22
  wTest,
22
23
  wLogin,
@@ -43,6 +44,7 @@ function install(Vue: App) {
43
44
  }
44
45
  export const api={
45
46
  request,
46
- menu
47
+ menu,
48
+ nfc
47
49
  }
48
50
  export default install // 这个方法以后再使用的时候可以被vue.use调用
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "w-ui-v1",
3
- "version": "1.0.52",
3
+ "version": "1.0.54",
4
4
  "description": "w-ui",
5
5
  "author": "wgxshh",
6
6
  "license": "ISC",
package/utils/nfc.ts ADDED
@@ -0,0 +1,260 @@
1
+ var NfcAdapter;
2
+ var NdefRecord;
3
+ var NdefMessage;
4
+ var _getCardNo;
5
+
6
+ export default {
7
+ initNFC() {
8
+ if (uni.getSystemInfoSync().platform == 'android') {
9
+ listenNFCStatus()
10
+ }
11
+ },
12
+ readNFC(callback) {
13
+ if (uni.getSystemInfoSync().platform == 'android') {
14
+ readData(callback);
15
+ }
16
+ },
17
+ closeNFC() {
18
+ if (uni.getSystemInfoSync().platform == 'android') {
19
+ closeReadAndWrite();
20
+ }
21
+ }
22
+ }
23
+
24
+ function listenNFCStatus() {
25
+ try {
26
+ var main = plus.android.runtimeMainActivity();
27
+ var Intent = plus.android.importClass('android.content.Intent');
28
+ var Activity = plus.android.importClass('android.app.Activity');
29
+ var PendingIntent = plus.android.importClass('android.app.PendingIntent');
30
+ var IntentFilter = plus.android.importClass('android.content.IntentFilter');
31
+ NfcAdapter = plus.android.importClass('android.nfc.NfcAdapter');
32
+ var nfcAdapter = NfcAdapter.getDefaultAdapter(main);
33
+
34
+ if (nfcAdapter == null) {
35
+ uni.showToast({
36
+ title: '设备不支持NFC!',
37
+ icon: 'none'
38
+ })
39
+ return;
40
+ }
41
+
42
+ if (!nfcAdapter.isEnabled()) {
43
+ uni.showToast({
44
+ title: '请在系统设置中先启用NFC功能!',
45
+ icon: 'none'
46
+ });
47
+ return;
48
+ }
49
+
50
+ var intent = new Intent(main, main.getClass());
51
+ intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
52
+ var pendingIntent = PendingIntent.getActivity(main, 0, intent, 0);
53
+ var ndef = new IntentFilter("android.nfc.action.TECH_DISCOVERED");
54
+ ndef.addDataType("*/*");
55
+ var intentFiltersArray = [ndef];
56
+ var techListsArray = [
57
+ ["android.nfc.tech.IsoDep"],
58
+ ["android.nfc.tech.NfcA"],
59
+ ["android.nfc.tech.NfcB"],
60
+ ["android.nfc.tech.NfcF"],
61
+ ["android.nfc.tech.Nfcf"],
62
+ ["android.nfc.tech.NfcV"],
63
+ ["android.nfc.tech.NdefFormatable"],
64
+ ["android.nfc.tech.MifareClassic"],
65
+ ["android.nfc.tech.MifareUltralight"]
66
+ ];
67
+ plus.globalEvent.addEventListener("newintent",
68
+ function() {
69
+ setTimeout(handle_nfc_data1, 1000);
70
+ }, false);
71
+ plus.globalEvent.addEventListener("pause", function(e) {
72
+ if (nfcAdapter) {
73
+ nfcAdapter.disableForegroundDispatch(main);
74
+ }
75
+ }, false);
76
+ plus.globalEvent.addEventListener("resume", function(e) {
77
+ if (nfcAdapter) {
78
+ //console.log('resume');
79
+ nfcAdapter.enableForegroundDispatch(main, pendingIntent, intentFiltersArray, techListsArray);
80
+ }
81
+ }, false);
82
+ nfcAdapter.enableForegroundDispatch(main, pendingIntent, intentFiltersArray, techListsArray);
83
+ } catch (e) {
84
+ console.error(e);
85
+ }
86
+ }
87
+
88
+ function handle_nfc_data1() {
89
+ NdefRecord = plus.android.importClass("android.nfc.NdefRecord");
90
+ NdefMessage = plus.android.importClass("android.nfc.NdefMessage");
91
+ var main = plus.android.runtimeMainActivity();
92
+ var intent = main.getIntent();
93
+ //console.log("action type:" + intent.getAction());
94
+ if ("android.nfc.action.TECH_DISCOVERED" == intent.getAction()) {
95
+ if (readyWriteData) {
96
+ //__write(intent);
97
+ readyWriteData = false;
98
+ } else if (readyRead) {
99
+ __read(intent);
100
+ readyRead = false;
101
+ }
102
+ }
103
+ }
104
+
105
+ function showToast(msg) {
106
+ plus.nativeUI.toast(msg);
107
+ }
108
+
109
+ // function __write(intent) {
110
+ // try {
111
+ // waiting.setTitle('请勿移开标签\n正在写入...');
112
+ // var text = document.getElementById('text').value;
113
+ // console.log("text=" + text);
114
+ // var textBytes = plus.android.invoke(text, "getBytes");
115
+ // var textRecord = new NdefRecord(NdefRecord.TNF_MIME_MEDIA,
116
+ // plus.android.invoke("text/plain", "getBytes"), plus.android.invoke("", "getBytes"), textBytes);
117
+ // var message = new NdefMessage([textRecord]);
118
+ // var Ndef = plus.android.importClass('android.nfc.tech.Ndef');
119
+ // var NdefFormatable = plus.android.importClass('android.nfc.tech.NdefFormatable');
120
+ // var tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
121
+ // var ndef = Ndef.get(tag);
122
+ // if (ndef != null) {
123
+ // var size = message.toByteArray().length;
124
+ // console.log("size=" + size);
125
+ // ndef.connect();
126
+ // if (!ndef.isWritable()) {
127
+ // showToast("tag不允许写入");
128
+ // waiting.close();
129
+ // return;
130
+ // }
131
+ // if (ndef.getMaxSize() < size) {
132
+ // showToast("文件大小超出容量");
133
+ // waiting.close();
134
+ // return;
135
+ // }
136
+
137
+ // ndef.writeNdefMessage(message);
138
+ // waiting.close();
139
+ // showToast("写入数据成功.");
140
+ // return;
141
+ // } else {
142
+ // var format = NdefFormatable.get(tag);
143
+ // if (format != null) {
144
+ // try {
145
+ // format.connect();
146
+ // format.format(message);
147
+ // showToast("格式化tag并且写入message");
148
+ // waiting.close();
149
+ // return;
150
+ // } catch (e) {
151
+ // showToast("格式化tag失败.");
152
+ // waiting.close();
153
+ // return;
154
+ // }
155
+ // } else {
156
+ // showToast("Tag不支持NDEF");
157
+ // waiting.close();
158
+ // return;
159
+ // }
160
+ // }
161
+ // } catch (e) {
162
+ // console.log("error=" + e);
163
+ // waiting.close();
164
+ // alert('写入失败');
165
+ // }
166
+
167
+ // }
168
+
169
+ function __read(intent) {
170
+ try {
171
+ var content = "";
172
+ waiting.setTitle('请勿移开卡\n正在读取数据...');
173
+ var tag = plus.android.importClass("android.nfc.Tag");
174
+ tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
175
+ var bytesId = intent.getByteArrayExtra(NfcAdapter.EXTRA_ID);
176
+ waiting.close();
177
+ var tagid = bytesToHexString(tag.getId())
178
+ if (typeof _getCardNo === 'function') {
179
+ _getCardNo(tagid);
180
+ }
181
+ } catch (e) {
182
+ uni.showToast({
183
+ title: e,
184
+ icon: 'none'
185
+ });
186
+ }
187
+ }
188
+
189
+ function bytesToHexString(inarray) {
190
+ var i, j, x;
191
+ var hex = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A",
192
+ "B", "C", "D", "E", "F"
193
+ ];
194
+ var out = "";
195
+ for (j = 0; j < inarray.length; ++j) {
196
+ x = parseInt(inarray[j]) & 0xff;
197
+ i = (x >> 4) & 0x0f;
198
+ out += hex[i];
199
+ i = x & 0x0f;
200
+ out += hex[i];
201
+ }
202
+ return out;
203
+ }
204
+
205
+ function reverseTwo(str) {
206
+
207
+ var str1 = "";
208
+ for (var i = 1; i <= str.length; i++) {
209
+ str1 += str[i - 1];
210
+ if (i % 2 == 0) {
211
+ if (i == str.length) {
212
+ break;
213
+ }
214
+ str1 += ":";
215
+ }
216
+ }
217
+ var str2 = "";
218
+ for (var i = str1.split(":").length - 1; i >= 0; i--) {
219
+ str2 += str1.split(":")[i];
220
+ }
221
+ return str2;
222
+ }
223
+
224
+ if (uni.getSystemInfoSync().platform == 'android') {
225
+ //plus.globalEvent.addEventListener('plusready', listenNFCStatus, false);
226
+ }
227
+
228
+ var waiting;
229
+ var readyWriteData = false;
230
+ var readyRead = false;
231
+
232
+ function writeData() {
233
+ var textEle = plus.globalEvent.getElementById('text');
234
+ if (!textEle.value) {
235
+ uni.showToast({
236
+ title: '请输入要写入的内容!',
237
+ icon: 'none'
238
+ });
239
+ return;
240
+ }
241
+ readyWriteData = true;
242
+ waiting = plus.nativeUI.showWaiting("请将NFC卡靠近!");
243
+ }
244
+
245
+ function readData(getCardNo) {
246
+ readyRead = true;
247
+ _getCardNo = getCardNo
248
+ waiting = plus.nativeUI.showWaiting("请将NFC卡靠近!", {
249
+ modal: false
250
+ });
251
+ }
252
+
253
+ function closeReadAndWrite() {
254
+ readyWriteData = false;
255
+ readyRead = false;
256
+
257
+ if (waiting) {
258
+ waiting.close();
259
+ }
260
+ }