smdtManager.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. <template>
  2. <view class="container">
  3. <view class="header">
  4. <text class="title">SMDT设备管理插件示例</text>
  5. <view class="status-indicator">
  6. <text class="status-text" :class="{ 'status-ready': isPluginReady, 'status-error': !isPluginReady }">
  7. {{ isPluginReady ? '✅ 插件已就绪' : '❌ 插件未就绪' }}
  8. </text>
  9. </view>
  10. </view>
  11. <!-- 系统功能 -->
  12. <view class="section">
  13. <text class="section-title">系统功能</text>
  14. <view class="button-group">
  15. <button @click="getSystemFontSize" class="btn">获取系统字体大小</button>
  16. <button @click="setSystemFontSize" class="btn">设置系统字体大小</button>
  17. <button @click="getTimeFormat" class="btn">获取时间格式</button>
  18. <button @click="setTimeFormat" class="btn">设置时间格式</button>
  19. <button @click="setVolume" class="btn">设置音量</button>
  20. <button @click="setAdbDebug" class="btn">设置ADB调试</button>
  21. </view>
  22. </view>
  23. <!-- 网络功能 -->
  24. <view class="section">
  25. <text class="section-title">网络功能</text>
  26. <view class="button-group">
  27. <button @click="getWifiRssi" class="btn">获取WiFi信号强度</button>
  28. <button @click="getMacAddress" class="btn">获取MAC地址</button>
  29. <button @click="getImeiNumber" class="btn">获取IMEI号</button>
  30. <button @click="setWifiAp" class="btn">设置WiFi热点</button>
  31. </view>
  32. </view>
  33. <!-- 显示功能 -->
  34. <view class="section">
  35. <text class="section-title">显示功能</text>
  36. <view class="button-group">
  37. <button @click="setLcdBackLight" class="btn">设置屏幕亮度</button>
  38. <button @click="setDisplayDensity" class="btn">设置显示密度</button>
  39. </view>
  40. </view>
  41. <!-- 硬件功能 -->
  42. <view class="section">
  43. <text class="section-title">硬件功能</text>
  44. <view class="button-group">
  45. <button @click="setLedLighted" class="btn">控制LED灯</button>
  46. <button @click="getLedState" class="btn">获取LED状态</button>
  47. <button @click="getSDcardPath" class="btn">获取SD卡路径</button>
  48. <button @click="getUdiskPath" class="btn">获取U盘路径</button>
  49. <button @click="setUsbPower" class="btn">控制USB电源</button>
  50. </view>
  51. </view>
  52. <!-- 结果显示 -->
  53. <view class="result-section">
  54. <text class="result-title">操作结果:</text>
  55. <text class="result-text">{{ result }}</text>
  56. </view>
  57. </view>
  58. </template>
  59. <script>
  60. // 导入插件管理器和API
  61. import { PluginManager, SystemAPI, NetworkAPI, DisplayAPI, HardwareAPI } from './smdtManager.js';
  62. export default {
  63. data() {
  64. return {
  65. result: '等待操作...',
  66. isPluginReady: false,
  67. smdtManager:null
  68. }
  69. },
  70. async onLoad() {
  71. this.result = '正在初始化插件...';
  72. try {
  73. this.smdtManager = uni.requireNativePlugin('SmdtManager');
  74. this.smdtManager.initialize((res) => {
  75. if(res.success){
  76. this.isPluginReady = true;
  77. this.result = '插件初始化成功,可以开始使用功能';
  78. uni.showToast({
  79. title: '插件初始化成功',
  80. icon: 'success'
  81. });
  82. }
  83. });
  84. // await PluginManager.initialize();
  85. // this.isPluginReady = true;
  86. // this.result = '插件初始化成功,可以开始使用功能';
  87. } catch (error) {
  88. this.isPluginReady = false;
  89. this.result = `插件初始化失败: ${error.message}`;
  90. // 显示用户友好的错误提示
  91. uni.showModal({
  92. title: '插件初始化失败',
  93. content: '设备功能暂不可用,请检查设备兼容性或联系技术支持',
  94. showCancel: false
  95. });
  96. }
  97. },
  98. methods: {
  99. // 通用错误处理方法
  100. handleError(error, operation) {
  101. console.error(`${operation}失败:`, error);
  102. this.result = `${operation}失败: ${error.message}`;
  103. if (error.message.includes('初始化失败')) {
  104. uni.showModal({
  105. title: '设备兼容性问题',
  106. content: '当前设备可能不支持此功能,请联系技术支持',
  107. showCancel: false
  108. });
  109. }
  110. },
  111. // 系统功能方法
  112. async getSystemFontSize() {
  113. if (!this.isPluginReady) {
  114. this.result = '插件未就绪,请等待初始化完成';
  115. return;
  116. }
  117. try {
  118. const fontSize = await SystemAPI.getSystemFontSize();
  119. this.result = `系统字体大小: ${fontSize}`;
  120. } catch (error) {
  121. this.handleError(error, '获取系统字体大小');
  122. }
  123. },
  124. async setSystemFontSize() {
  125. if (!this.isPluginReady) {
  126. this.result = '插件未就绪,请等待初始化完成';
  127. return;
  128. }
  129. try {
  130. const result = await SystemAPI.setSystemFontSize(1.2);
  131. this.result = `设置字体大小结果: ${result}`;
  132. uni.showToast({
  133. title: '字体大小设置成功',
  134. icon: 'success'
  135. });
  136. } catch (error) {
  137. this.handleError(error, '设置系统字体大小');
  138. }
  139. },
  140. async getTimeFormat() {
  141. if (!this.isPluginReady) {
  142. this.result = '插件未就绪,请等待初始化完成';
  143. return;
  144. }
  145. try {
  146. const timeFormat = await SystemAPI.getTimeFormat();
  147. this.result = `时间格式: ${timeFormat === 1 ? '24小时制' : '12小时制'}`;
  148. } catch (error) {
  149. this.handleError(error, '获取时间格式');
  150. }
  151. },
  152. async setTimeFormat() {
  153. if (!this.isPluginReady) {
  154. this.result = '插件未就绪,请等待初始化完成';
  155. return;
  156. }
  157. try {
  158. const result = await SystemAPI.setTimeFormat(1); // 1为24小时制,0为12小时制
  159. this.result = `设置时间格式结果: ${result}`;
  160. uni.showToast({
  161. title: '时间格式设置成功',
  162. icon: 'success'
  163. });
  164. } catch (error) {
  165. this.handleError(error, '设置时间格式');
  166. }
  167. },
  168. async setVolume() {
  169. if (!this.isPluginReady) {
  170. this.result = '插件未就绪,请等待初始化完成';
  171. return;
  172. }
  173. try {
  174. const result = await SystemAPI.setVolume(50); // 音量范围通常是0-100
  175. this.result = `设置音量结果: ${result}`;
  176. uni.showToast({
  177. title: '音量设置成功',
  178. icon: 'success'
  179. });
  180. } catch (error) {
  181. this.handleError(error, '设置音量');
  182. }
  183. },
  184. async setAdbDebug() {
  185. if (!this.isPluginReady) {
  186. this.result = '插件未就绪,请等待初始化完成';
  187. return;
  188. }
  189. try {
  190. const result = await SystemAPI.setAdbDebug(true);
  191. this.result = `设置ADB调试结果: ${result}`;
  192. uni.showToast({
  193. title: 'ADB调试设置成功',
  194. icon: 'success'
  195. });
  196. } catch (error) {
  197. this.handleError(error, '设置ADB调试');
  198. }
  199. },
  200. // 网络功能方法
  201. async getWifiRssi() {
  202. if (!this.isPluginReady) {
  203. this.result = '插件未就绪,请等待初始化完成';
  204. return;
  205. }
  206. try {
  207. const rssi = await NetworkAPI.getWifiRssi();
  208. this.result = `WiFi信号强度: ${rssi} dBm`;
  209. } catch (error) {
  210. this.handleError(error, '获取WiFi信号强度');
  211. }
  212. },
  213. async getMacAddress() {
  214. if (!this.isPluginReady) {
  215. this.result = '插件未就绪,请等待初始化完成';
  216. return;
  217. }
  218. try {
  219. const macAddress = await NetworkAPI.getMacAddress();
  220. this.result = `MAC地址: ${macAddress}`;
  221. } catch (error) {
  222. this.handleError(error, '获取MAC地址');
  223. }
  224. },
  225. async getImeiNumber() {
  226. if (!this.isPluginReady) {
  227. this.result = '插件未就绪,请等待初始化完成';
  228. return;
  229. }
  230. try {
  231. const imei = await NetworkAPI.getImeiNumber();
  232. this.result = `IMEI号: ${imei}`;
  233. } catch (error) {
  234. this.handleError(error, '获取IMEI号');
  235. }
  236. },
  237. async setWifiAp() {
  238. if (!this.isPluginReady) {
  239. this.result = '插件未就绪,请等待初始化完成';
  240. return;
  241. }
  242. try {
  243. const result = await NetworkAPI.setWifiAp('SMDT_AP', '12345678');
  244. this.result = `设置WiFi热点结果: ${result}`;
  245. uni.showToast({
  246. title: 'WiFi热点设置成功',
  247. icon: 'success'
  248. });
  249. } catch (error) {
  250. this.handleError(error, '设置WiFi热点');
  251. }
  252. },
  253. // 显示功能方法
  254. async setLcdBackLight() {
  255. if (!this.isPluginReady) {
  256. this.result = '插件未就绪,请等待初始化完成';
  257. return;
  258. }
  259. try {
  260. const result = await DisplayAPI.setLcdBackLight(128); // 亮度范围通常是0-255
  261. this.result = `设置屏幕亮度结果: ${result}`;
  262. uni.showToast({
  263. title: '屏幕亮度设置成功',
  264. icon: 'success'
  265. });
  266. } catch (error) {
  267. this.handleError(error, '设置屏幕亮度');
  268. }
  269. },
  270. async setDisplayDensity() {
  271. if (!this.isPluginReady) {
  272. this.result = '插件未就绪,请等待初始化完成';
  273. return;
  274. }
  275. try {
  276. const result = await DisplayAPI.setDisplayDensity(320); // DPI值
  277. this.result = `设置显示密度结果: ${result}`;
  278. uni.showToast({
  279. title: '显示密度设置成功',
  280. icon: 'success'
  281. });
  282. } catch (error) {
  283. this.handleError(error, '设置显示密度');
  284. }
  285. },
  286. // 硬件功能方法
  287. async setLedLighted() {
  288. if (!this.isPluginReady) {
  289. this.result = '插件未就绪,请等待初始化完成';
  290. return;
  291. }
  292. try {
  293. const result = await HardwareAPI.setLedLighted(1, true);
  294. this.result = `控制LED灯结果: ${result}`;
  295. uni.showToast({
  296. title: 'LED灯控制成功',
  297. icon: 'success'
  298. });
  299. } catch (error) {
  300. this.handleError(error, '控制LED灯');
  301. }
  302. },
  303. async getLedState() {
  304. if (!this.isPluginReady) {
  305. this.result = '插件未就绪,请等待初始化完成';
  306. return;
  307. }
  308. try {
  309. const state = await HardwareAPI.getLedState(1);
  310. this.result = `LED状态: ${state ? '开启' : '关闭'}`;
  311. } catch (error) {
  312. this.handleError(error, '获取LED状态');
  313. }
  314. },
  315. async getSDcardPath() {
  316. if (!this.isPluginReady) {
  317. this.result = '插件未就绪,请等待初始化完成';
  318. return;
  319. }
  320. try {
  321. const path = await HardwareAPI.getSDcardPath();
  322. this.result = `SD卡路径: ${path}`;
  323. } catch (error) {
  324. this.handleError(error, '获取SD卡路径');
  325. }
  326. },
  327. async getUdiskPath() {
  328. if (!this.isPluginReady) {
  329. this.result = '插件未就绪,请等待初始化完成';
  330. return;
  331. }
  332. try {
  333. const path = await HardwareAPI.getUdiskPath();
  334. this.result = `U盘路径: ${path}`;
  335. } catch (error) {
  336. this.handleError(error, '获取U盘路径');
  337. }
  338. },
  339. async setUsbPower() {
  340. if (!this.isPluginReady) {
  341. this.result = '插件未就绪,请等待初始化完成';
  342. return;
  343. }
  344. try {
  345. const result = await HardwareAPI.setUsbPower(true);
  346. this.result = `控制USB电源结果: ${result}`;
  347. uni.showToast({
  348. title: 'USB电源控制成功',
  349. icon: 'success'
  350. });
  351. } catch (error) {
  352. this.handleError(error, '控制USB电源');
  353. }
  354. }
  355. }
  356. }
  357. </script>
  358. <style>
  359. .container {
  360. padding: 20px;
  361. background-color: #f5f5f5;
  362. }
  363. .header {
  364. text-align: center;
  365. margin-bottom: 30px;
  366. }
  367. .title {
  368. font-size: 24px;
  369. font-weight: bold;
  370. color: #333;
  371. }
  372. .status-indicator {
  373. margin-top: 10px;
  374. }
  375. .status-text {
  376. font-size: 14px;
  377. padding: 5px 10px;
  378. border-radius: 15px;
  379. background-color: #f0f0f0;
  380. }
  381. .status-ready {
  382. color: #27ae60;
  383. background-color: #d5f4e6;
  384. }
  385. .status-error {
  386. color: #e74c3c;
  387. background-color: #fdeaea;
  388. }
  389. .section {
  390. margin-bottom: 30px;
  391. background-color: white;
  392. border-radius: 10px;
  393. padding: 20px;
  394. box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  395. }
  396. .section-title {
  397. font-size: 18px;
  398. font-weight: bold;
  399. color: #2c3e50;
  400. margin-bottom: 15px;
  401. display: block;
  402. }
  403. .button-group {
  404. display: flex;
  405. flex-direction: column;
  406. gap: 10px;
  407. }
  408. .btn {
  409. background-color: #3498db;
  410. color: white;
  411. border: none;
  412. border-radius: 5px;
  413. padding: 12px 20px;
  414. font-size: 16px;
  415. margin: 5px 0;
  416. }
  417. .btn:active {
  418. background-color: #2980b9;
  419. }
  420. .result-section {
  421. background-color: white;
  422. border-radius: 10px;
  423. padding: 20px;
  424. box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  425. }
  426. .result-title {
  427. font-size: 16px;
  428. font-weight: bold;
  429. color: #2c3e50;
  430. display: block;
  431. margin-bottom: 10px;
  432. }
  433. .result-text {
  434. font-size: 14px;
  435. color: #34495e;
  436. line-height: 1.5;
  437. word-wrap: break-word;
  438. }
  439. </style>