index.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  1. const app = getApp();
  2. const {
  3. getBanner,
  4. listByDevice
  5. } = require('../../utils/api.js');
  6. import timeUtil from '../../utils/time_util.js';
  7. import strings from '../../utils/strings.js';
  8. Page({
  9. data: {
  10. nvabarData: {
  11. showCapsule: 0, //是否显示左上角图标 1表示显示 0表示不显示
  12. title: 'OhPlay', //导航栏 中间的标题
  13. },
  14. ///下午好
  15. isLogin: false,
  16. greeting: "",
  17. bannerList: [],
  18. autoplay: true,
  19. interval: 3000, // 切换时间间隔
  20. duration: 500, // 滑动动画时长
  21. circular: true, // 衔接滑动
  22. indexPage: 0,
  23. ///是否展示频道
  24. showChannel: false,
  25. ////首页
  26. navBarHeight: app.globalData.navBarHeight,
  27. MenuButtonheight: app.globalData.MenuButtonheight,
  28. MenuButtonTop: app.globalData.MenuButtonTop,
  29. actionIndex: null,
  30. luoma: ["Ⅰ", "Ⅱ", "Ⅲ", "Ⅳ", "Ⅴ", "Ⅵ", " Ⅶ", "Ⅷ", "Ⅸ", "Ⅹ", "Ⅺ", "Ⅻ"],
  31. channelData: [],
  32. deviceList: [],
  33. deviceListIndex: null,
  34. isOneLoading: true,
  35. uid: null,
  36. isSetWake: false,
  37. thisDeviceMac: null,
  38. battery: 4, // 0≤电量<20,0格
  39. ////我的界面
  40. loginStatus: true,
  41. nickName: "未登录",
  42. userPic: './../../img/head_pic.png'
  43. },
  44. onLoad(options) {
  45. var that = this;
  46. that.onUserInfoLoad();
  47. that.onBannerLoad();
  48. },
  49. onShow() {
  50. var that = this;
  51. that.onHomeShow();
  52. that.onBannerShow();
  53. },
  54. onHomeShow() {
  55. var that = this;
  56. var userInfo = wx.getStorageSync("userInfo") || "";
  57. if (!strings.isEmpty(userInfo)) {
  58. // 获取设备本地数据
  59. if (app.globalData.newDeviceId) {
  60. that.getDeviceData();
  61. }
  62. // 更新
  63. else if (that.data.deviceListIndex !== null) {
  64. that.actionDevice(that.data.deviceListIndex);
  65. }
  66. }
  67. // 取消订阅
  68. else {
  69. if (that.data.thisDeviceMac !== null) {
  70. app.unsubscribe(`/${that.data.thisDeviceMac}/user/pub_response`);
  71. };
  72. }
  73. },
  74. ///开始时home的
  75. getDeviceData() {
  76. var that = this;
  77. var devicelist = wx.getStorageSync("devicelist") || "";
  78. if (!strings.isEmpty(devicelist)) {
  79. const resData = JSON.parse(devicelist);
  80. that.setData({
  81. deviceList: resData
  82. });
  83. // 连接mqtt
  84. if (app.globalData.client === null) {
  85. app.connect();
  86. } else if (app.globalData.newDeviceId) {
  87. that.setData({
  88. actionIndex: null,
  89. deviceListIndex: null,
  90. });
  91. // 监听
  92. let topic = `/AIrSMArT_${resData[0].name.split("BLUFI_")[1]}/status/onoffline`;
  93. app.subscribe(topic);
  94. const Timeout = setTimeout(() => {
  95. clearTimeout(Timeout);
  96. that.actionDevice(0);
  97. }, 1000);
  98. }
  99. // // 有新设备
  100. // if(app.globalData.newDeviceId) {
  101. // that.setData({
  102. // actionIndex: null,
  103. // deviceListIndex: null,
  104. // });
  105. // let topic = `/AIrSMArT_${resData[0].name.split("BLUFI_")[1]}/status/onoffline`;
  106. // app.subscribe(topic);
  107. // }
  108. }
  109. },
  110. // 回调
  111. mqttCallback(type, option) {
  112. // console.log("gadsfadsfqwerq===" + type + "===" + option);
  113. var that = this;
  114. let payloads = null;
  115. if (option) {
  116. payloads = JSON.parse(option.payload);
  117. };
  118. switch (type) {
  119. case "connect":
  120. that.connectSuccess();
  121. break;
  122. case "message_onoffline":
  123. that.online(payloads);
  124. break;
  125. case "message":
  126. // 接收设备播放信息
  127. if (payloads.type === "get_position" && payloads.other) {
  128. let actionIndex = null;
  129. that.data.channelData.map((v, index) => {
  130. if (v.channelNum === payloads.other.channel) {
  131. actionIndex = index;
  132. }
  133. });
  134. that.setData({
  135. actionIndex,
  136. });
  137. } else if (payloads.type === "play" || payloads.type === "play_state") {
  138. if (that.data.deviceListIndex === null) {
  139. return;
  140. }
  141. // 接收设备当前播放状态
  142. const obj = {
  143. DstDeviceName: that.getThisDeviceID()
  144. }
  145. app.PubMsg({
  146. type: "get_position",
  147. ...obj
  148. });
  149. } else if (payloads.type === "get_dev_info") {
  150. // 接收设备当前信息
  151. that.getchannelData(payloads.other.ProdModel);
  152. // 电量
  153. that.setData({
  154. battery: that._battery(payloads.other.Power),
  155. })
  156. // 当前设备木有设置定时
  157. that.setData({
  158. isSetWake: false,
  159. });
  160. payloads.other.alarm.map((v) => {
  161. if (v.enable === "1") {
  162. that.setData({
  163. isSetWake: true
  164. });
  165. }
  166. });
  167. // 更新信息
  168. ///连上就调用2次
  169. that.data.deviceList.map((v, index) => {
  170. // console.log("gadsfadsfadsfa==444===" + payloads.SrcDeviceName);
  171. // console.log("gadsfadsfadsfa==555===" + JSON.stringify(payloads.other));
  172. // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online"}]
  173. /// payloads.other
  174. // {"Guid":"AIrSMArT_7cdfa1fcbb24","ProdModel":"MW-2AX(WIFI-N)","sim_iccid":"","esim_iccid":"","SoftVer":"3.0.7","Firmware":"RTOS","Power":20,"lowBatteryNtf":0,"Volume":24,"Mute":0,"pauseShutdown":600,"PlayState":4,"PlayMode":0,"devName":"猫王小王子OTR-X","deviceId":"","deviceSecret":"","is_tts_enable":1,"SignalQuality":100,"CardIndex":0,"CardSum":0,"NetModel":2,"is_netmode_auto":2,"Imei":"","mid":"","deviceDSN":"","userid":"1","user_id_white":"10000309","alarm":[{"alarm_id":"0","alarm_name":"close","current_timestamp":"2024-03-31 19:20:39","on_off_timestamp":"19:30:39","operation":"off","song_uri":"","weekly_repeat":"0","enable":"0","action":"update","week_actives":[0,0,0,0,0,0,0]},{"alarm_id":"1","alarm_name":"open","current_timestamp":"2023-11-17 12:25:10","on_off_timestamp":"13:35:00","operation":"on","song_uri":"","weekly_repeat":"1","enable":"1","action":"update","week_actives":[1,1,1,1,1,1,1]}]}
  175. ///payloads.SrcDeviceName AIrSMArT_7cdfa1fcbb24
  176. if (payloads.SrcDeviceName && payloads.SrcDeviceName.indexOf(v.name.split("BLUFI_")[1]) !== -1) {
  177. that.data.deviceList[index].ProdModel = payloads.other.ProdModel;
  178. that.data.deviceList[index].devName = payloads.other.devName;
  179. }
  180. });
  181. // 更新缓存
  182. wx.setStorage({
  183. key: "devicelist",
  184. data: JSON.stringify(that.data.deviceList),
  185. success() {
  186. that.setData({
  187. deviceList: that.data.deviceList
  188. })
  189. }
  190. });
  191. } else if (payloads.type === "battery" && payloads.other) {
  192. that.setData({
  193. battery: that._battery(payloads.other.battery),
  194. })
  195. }
  196. break;
  197. default:
  198. }
  199. },
  200. // 格式化电量
  201. _battery(battery) {
  202. let _battery = 0;
  203. if (battery < 20) {
  204. _battery = 0
  205. } else if (20 <= battery && battery < 40) {
  206. _battery = 1
  207. } else if (40 <= battery && battery < 60) {
  208. _battery = 2
  209. } else if (60 <= battery && battery < 80) {
  210. _battery = 3
  211. } else if (80 <= battery && battery <= 100) {
  212. _battery = 4
  213. } else if (battery > 100) {
  214. _battery = 5
  215. };
  216. return _battery
  217. },
  218. connectSuccess() {
  219. // 订阅设备在线信息
  220. var that = this;
  221. that.subscribeDevicesStatus();
  222. },
  223. // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online","ProdModel":"MW-2AX(WIFI-N)","devName":"猫王小王子OTR-X"}]
  224. ///连上就调用2次
  225. /// payloads:{"uuid":"AIrSMArT_7cdfa1fcbb24","state":"online","userid":"1"}
  226. online(payloads) {
  227. // 设置在线状态
  228. var that = this;
  229. console.log("gadsfadsfadsfa==777===" + JSON.stringify(payloads));
  230. that.data.deviceList.map((v, index) => {
  231. if (payloads.uuid && payloads.uuid.indexOf(v.name.split("BLUFI_")[1]) !== -1) {
  232. that.data.deviceList[index].state = payloads.state;
  233. }
  234. });
  235. that.setData({
  236. deviceList: that.data.deviceList,
  237. });
  238. // 更新缓存
  239. wx.setStorage({
  240. key: "devicelist",
  241. data: JSON.stringify(that.data.deviceList)
  242. });
  243. // 如没有选中,选中最新的
  244. (() => {
  245. if (that.data.deviceListIndex === null) {
  246. let itue = false;
  247. that.data.deviceList.map((v, index) => {
  248. if (v.state === "online" && !itue) {
  249. itue = true;
  250. that.actionDevice(index);
  251. }
  252. });
  253. }
  254. // else {
  255. // that.actionDevice(that.data.deviceListIndex);
  256. // };
  257. })();
  258. // 当前播放设备离线
  259. if (that.data.deviceListIndex !== null && that.data.deviceList[that.data.deviceListIndex].state !== "online") {
  260. that.setData({
  261. actionIndex: null,
  262. deviceListIndex: null,
  263. });
  264. };
  265. },
  266. // 订阅设备在线状态
  267. subscribeDevicesStatus() {
  268. var that = this;
  269. that.data.deviceList.forEach((value) => {
  270. let topic = `/AIrSMArT_${value.name.split("BLUFI_")[1]}/status/onoffline`;
  271. app.subscribe(topic);
  272. })
  273. },
  274. subscribeCurrDevice() {
  275. var that = this;
  276. if (!(app.globalData.client && app.globalData.client.connected)) {
  277. console.log("未连接MQTT服务器");
  278. const str = setInterval(() => {
  279. clearInterval(str);
  280. that.subscribeCurrDevice();
  281. }, 500);
  282. return;
  283. };
  284. if (that.data.deviceList.length === 0 || that.data.deviceListIndex === null) {
  285. return
  286. };
  287. let topic = `/AIrSMArT_${that.data.deviceList[that.data.deviceListIndex].name.split("BLUFI_")[1]}/user/pub_response`;
  288. app.subscribe(topic);
  289. const obj = {
  290. DstDeviceName: that.getThisDeviceID()
  291. };
  292. app.PubMsg({
  293. type: "get_dev_info",
  294. ...obj
  295. });
  296. },
  297. actionMusic(e) {
  298. var that = this;
  299. if (e.currentTarget.dataset.index === that.data.actionIndex) {
  300. return;
  301. };
  302. if (that.data.deviceListIndex === null) {
  303. wx.showToast({
  304. title: '请选择设备',
  305. icon: "none"
  306. })
  307. return;
  308. };
  309. that.setData({
  310. actionIndex: e.currentTarget.dataset.index
  311. });
  312. const other = {
  313. "url": "",
  314. "media_data": "",
  315. "user_id": `${app.globalData.userInfo.deviceUid}`,
  316. "timestamp": `${Math.round(new Date() / 1000)}`,
  317. "channel_id": `${that.data.channelData[e.currentTarget.dataset.index].channelNum}`,
  318. "order": "",
  319. "resource_from": "",
  320. "songAlbumID": "",
  321. "version": 3,
  322. "is_debug": app.globalData.is_debug
  323. };
  324. app.PubMsg({
  325. type: "play",
  326. DstDeviceName: that.getThisDeviceID(),
  327. other
  328. });
  329. },
  330. getThisDeviceID() {
  331. var that = this;
  332. return `AIrSMArT_${that.data.deviceList[that.data.deviceListIndex].name.split("BLUFI_")[1]}`
  333. },
  334. getchannelData(clientType) {
  335. var that = this;Ï
  336. if (that.data.deviceListIndex === null) {
  337. return;
  338. };
  339. listByDevice({
  340. clientType
  341. }).then((res) => {
  342. that.setData({
  343. channelData: res
  344. });
  345. // 接收设备当前播放状态
  346. const obj = {
  347. DstDeviceName: that.getThisDeviceID()
  348. }
  349. app.PubMsg({
  350. type: "get_position",
  351. ...obj
  352. });
  353. wx.setStorage({
  354. key: "channelData",
  355. data: res
  356. });
  357. // 有新设备
  358. if (app.globalData.newDeviceId) {
  359. app.globalData.newDeviceId = null;
  360. return;
  361. // 去掉此功能,先留着吧
  362. // const other= {
  363. // "url": "",
  364. // "media_data": "",
  365. // "user_id": `${app.globalData.userInfo.deviceUid}`,
  366. // "timestamp": `${Math.round(new Date() / 1000)}`,
  367. // "channel_id": `${res[1].channelNum}`,
  368. // "order": "",
  369. // "resource_from": "",
  370. // "songAlbumID":"",
  371. // "version":3,
  372. // "is_debug": app.globalData.is_debug
  373. // };
  374. // app.PubMsg({
  375. // type: "play",
  376. // DstDeviceName: that.getThisDeviceID(),
  377. // other
  378. // });
  379. }
  380. })
  381. },
  382. goChannel() {
  383. var that = this;
  384. wx.setStorage({
  385. key: "channelDeta",
  386. data: that.data.channelData[that.data.actionIndex],
  387. success() {
  388. wx.navigateTo({
  389. url: './../channelDetails/channelDetails'
  390. })
  391. }
  392. })
  393. },
  394. updata() {
  395. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  396. wx.getUpdateManager().applyUpdate()
  397. },
  398. onUnload() {
  399. if (app.globalData.client === null) {
  400. return;
  401. };
  402. app.globalData.client.end(true);
  403. app.globalData.client.end(true);
  404. app.globalData.client = null;
  405. },
  406. onTapIndex(e) {
  407. var that = this;
  408. var index = e.currentTarget.dataset.index;
  409. var indexPage = that.data.indexPage;
  410. if (indexPage != index) {
  411. that.setData({
  412. indexPage: index,
  413. });
  414. }
  415. },
  416. goMeAbout() {
  417. wx.navigateTo({
  418. url: './../about/about',
  419. })
  420. },
  421. goMeLogin() {
  422. var that = this;
  423. if (that.data.isLogin) {
  424. // 退出登录
  425. wx.removeStorage({
  426. key: 'userInfo',
  427. success(res) {
  428. var nickName = "未登录";
  429. var greeting = that.getGreetBuNickName(nickName);
  430. that.setData({
  431. isLogin: false,
  432. greeting: greeting,
  433. nickName: nickName,
  434. userPic: "./../../img/head_pic.png",
  435. });
  436. }
  437. });
  438. wx.removeStorage({
  439. key: 'token',
  440. })
  441. } else {
  442. // 登录
  443. wx.navigateTo({
  444. url: './../login/login',
  445. });
  446. }
  447. },
  448. addDevice() {
  449. var that = this;
  450. if (!that.data.isLogin) {
  451. wx.navigateTo({
  452. url: './../login/login'
  453. });
  454. return;
  455. }
  456. that.goDeviceConnect();
  457. },
  458. goDeviceConnect() {
  459. wx.navigateTo({
  460. url: './../deviceList/deviceList',
  461. });
  462. },
  463. ///点击item
  464. // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online","ProdModel":"MW-2AX(WIFI-N)","devName":"猫王小王子OTR-X"}]
  465. onTapItem(e) {
  466. var that = this;
  467. var item = e.currentTarget.dataset.item;
  468. var ProdModel = item.ProdModel;
  469. if (ProdModel == "MW-2AX(WIFI-N)") {
  470. console.log("gadfadsfadsfaf===aa");
  471. if (e.currentTarget.dataset.index === that.data.deviceListIndex) {
  472. console.log("gadfadsfadsfaf===bbb");
  473. that.goWake();
  474. } else {
  475. console.log("gadfadsfadsfaf===ccc");
  476. that.actionDevice(e.currentTarget.dataset.index)
  477. }
  478. return;
  479. }
  480. ///去蓝牙连接处理
  481. that.addConnectBlueDevice({
  482. deviceId: item.deviceId,
  483. name: item.devName,
  484. state: item.state,
  485. clientType: item.ProdModel,
  486. mac: item.mac,
  487. });
  488. },
  489. actionDevice(index) {
  490. var that = this;
  491. const device = that.data.deviceList[index];
  492. if (device.state !== "online") {
  493. return;
  494. };
  495. // 取消订阅
  496. if (that.data.thisDeviceMac !== null) {
  497. app.unsubscribe(`/${that.data.thisDeviceMac}/user/pub_response`);
  498. };
  499. that.setData({
  500. deviceListIndex: index,
  501. thisDeviceMac: `AIrSMArT_${that.data.deviceList[index].name.split("BLUFI_")[1]}`
  502. });
  503. // app.PubMsg({
  504. // type: "get_dev_info",
  505. // DstDeviceName: that.getThisDeviceID()
  506. // });
  507. that.subscribeCurrDevice();
  508. },
  509. goWake() {
  510. var that = this;
  511. if (that.data.deviceListIndex === null) {
  512. return;
  513. };
  514. wx.navigateTo({
  515. url: './../deviceWake/deviceWake?deviceId=' + that.data.deviceList[that.data.deviceListIndex].deviceId + "&clientType=" + that.data.deviceList[that.data.deviceListIndex].ProdModel,
  516. });
  517. },
  518. ///点击banner事件
  519. onTapBanner() {
  520. var that = this;
  521. var item = e.currentTarget.dataset.item;
  522. },
  523. deleteDevice(e) {
  524. var that = this;
  525. wx.showModal({
  526. title: '确定删除?',
  527. success: function (res) {
  528. if (res.confirm) {
  529. if (that.data.deviceList[e.currentTarget.dataset.index].ProdModel != "MW-2AX(WIFI-N)") {
  530. const id = that.data.deviceList[e.currentTarget.dataset.index].deviceId;
  531. const deviceList = that.data.deviceList.filter((item, i) => {
  532. return id !== item.deviceId
  533. });
  534. wx.setStorage({
  535. key: "devicelist",
  536. data: JSON.stringify(deviceList),
  537. success() {
  538. that.setData({
  539. deviceList,
  540. });
  541. // 当前没有设备
  542. if (deviceList.length === 0) {
  543. that.setData({
  544. deviceListIndex: null,
  545. });
  546. }
  547. }
  548. });
  549. return;
  550. }
  551. const id = that.data.deviceList[e.currentTarget.dataset.index].deviceId;
  552. let name = that.data.deviceListIndex !== null ? that.data.deviceList[that.data.deviceListIndex].name : null;
  553. const deviceList = that.data.deviceList.filter((item, i) => {
  554. return id !== item.deviceId
  555. });
  556. // 取消订阅
  557. app.unsubscribe(`/AIrSMArT_${that.data.deviceList[e.currentTarget.dataset.index].name.split("BLUFI_")[1]}/user/pub_response`);
  558. wx.setStorage({
  559. key: "devicelist",
  560. data: JSON.stringify(deviceList),
  561. success() {
  562. that.setData({
  563. deviceList,
  564. thisDeviceMac: null
  565. });
  566. // 当前没有设备
  567. if (deviceList.length === 0) {
  568. that.setData({
  569. actionIndex: null,
  570. deviceListIndex: null,
  571. });
  572. }
  573. if (that.data.deviceListIndex === null) {
  574. return
  575. };
  576. if (e.currentTarget.dataset.index === that.data.deviceListIndex) {
  577. let index_ = null;
  578. deviceList.map((v, index) => {
  579. if (v.state === "online" && index_ === null) {
  580. index_ = index;
  581. }
  582. });
  583. if (index_ !== null) {
  584. that.actionDevice(index_);
  585. } else {
  586. that.setData({
  587. actionIndex: null,
  588. deviceListIndex: null,
  589. });
  590. };
  591. } else {
  592. deviceList.map((v, index) => {
  593. if (v.name === name) {
  594. that.setData({
  595. deviceListIndex: index,
  596. });
  597. }
  598. });
  599. }
  600. }
  601. });
  602. }
  603. }
  604. });
  605. },
  606. // {"applicationType":"[0, 1]","deviceId":"DB:45:DD:76:42:15","img":"https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20220909100711728016597.png","offlineImg":"https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20220909100714667384264.png","connectImg":null,"name":"猫王音响·小王子 OTR-X","bluetoothName":"猫王音响·小王子 OTR-X","bluetoothNames":["猫王音响·小王子 OTR-X"],"isChannelsPlatforms":0,"platform":-1,"typeList":[{"is5g":0,"type":1,"connectType":1,"functionList":[1,3,6],"deviceLinkResp":{"icon1":"https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20220909100644913162836.png","icon2":"https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20220909100648938942906.png","icon":null,"guideUrl":null}},{"is5g":0,"type":2,"connectType":3,"functionList":[1,3],"deviceLinkResp":{"icon1":"https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230313155903515728925.png","icon2":"https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230313155910706032704.png","icon":null,"guideUrl":null}}],"clientType":"MW-2AX(WIFI)","firstVersion":"0.0.1","filter":null,"guideUrl":null,"manufacturer":"ShanJing","deviceType":0,"mac":"9b45dd76e2150000",
  607. /// deviceList
  608. /// 连接方式:bt-0,ble-1,upnp-2,mqtt-3
  609. /// clientType
  610. // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online","ProdModel":"MW-2AX(WIFI-N)","devName":"猫王小王子OTR-X"}]
  611. addConnectBlueDevice(newDevice) {
  612. var that = this;
  613. // 同一个设备
  614. var deviceList = that.data.deviceList;
  615. var tempList = deviceList.filter((v) => v.deviceId === newDevice.deviceId);
  616. if (tempList && tempList.length > 0) {
  617. deviceList = deviceList.filter((v) => v.deviceId !== newDevice.deviceId);
  618. };
  619. deviceList.unshift({
  620. /// 蓝牙ble连接
  621. connectType: 1,
  622. deviceId: newDevice.deviceId,
  623. name: newDevice.deviceId,
  624. state: "online",
  625. ProdModel: newDevice.clientType,
  626. devName: newDevice.name,
  627. mac: newDevice.mac,
  628. });
  629. var finalList = that.updateDeviceList(deviceList);
  630. var indexPage = that.data.indexPage;
  631. if (indexPage != 0) {
  632. that.setData({
  633. indexPage: 0,
  634. deviceListIndex: 0,
  635. deviceList: finalList,
  636. });
  637. } else {
  638. that.setData({
  639. deviceListIndex: 0,
  640. deviceList: finalList,
  641. });
  642. }
  643. // 更新缓存
  644. wx.setStorage({
  645. key: "devicelist",
  646. data: JSON.stringify(finalList)
  647. });
  648. },
  649. addConnectWifiDevice(deviceList) {
  650. var that = this;
  651. var finalList = that.updateDeviceList(deviceList);
  652. var indexPage = that.data.indexPage;
  653. if (indexPage != 0) {
  654. that.setData({
  655. indexPage: 0,
  656. deviceListIndex: 0,
  657. deviceList: finalList,
  658. });
  659. } else {
  660. that.setData({
  661. deviceListIndex: 0,
  662. deviceList: finalList,
  663. });
  664. }
  665. // 更新缓存
  666. wx.setStorage({
  667. key: "devicelist",
  668. data: JSON.stringify(finalList)
  669. });
  670. },
  671. updateDeviceList(deviceList) {
  672. if (deviceList.length == 0) return [];
  673. var finalList = [];
  674. finalList.push(deviceList[0]);
  675. var onLineList = [];
  676. var onNoLineList = [];
  677. for (var i = 0; i < deviceList.length; i++) {
  678. if (i > 0) {
  679. if (deviceList[i].state == "online") {
  680. onLineList.push(deviceList[i])
  681. } else {
  682. onNoLineList.push(deviceList[i])
  683. }
  684. }
  685. }
  686. onLineList.forEach(element => {
  687. if (element.ProdModel == "MW-2AX(WIFI-N)") {
  688. onLineList.push(element)
  689. } else {
  690. onNoLineList.push(element)
  691. }
  692. });
  693. var onLineWifiList = [];
  694. var onLineNoWifiList = [];
  695. onNoLineList.forEach(element => {
  696. if (element.ProdModel == "MW-2AX(WIFI-N)") {
  697. onLineWifiList.push(element)
  698. } else {
  699. onLineNoWifiList.push(element)
  700. }
  701. });
  702. finalList = finalList.concat(onLineWifiList);
  703. finalList = finalList.concat(onLineNoWifiList);
  704. var onNoLineWifiList = [];
  705. var onNoLineNoWifiList = [];
  706. onNoLineList.forEach(element => {
  707. if (element.ProdModel == "MW-2AX(WIFI-N)") {
  708. onNoLineWifiList.push(element)
  709. } else {
  710. onNoLineNoWifiList.push(element)
  711. }
  712. });
  713. finalList = finalList.concat(onNoLineWifiList);
  714. finalList = finalList.concat(onNoLineWifiList);
  715. return finalList;
  716. },
  717. ///******************************* 可折叠 ********************************************///
  718. onUserInfoLoad() {
  719. var that = this;
  720. var userInfo = wx.getStorageSync("userInfo") || "";
  721. if (!strings.isEmpty(userInfo)) {
  722. var phone = userInfo.phone;
  723. if (!phone) {
  724. return;
  725. };
  726. var nickName = userInfo.nickname || "";
  727. var greeting = that.getGreetBuNickName(nickName);
  728. // 获取缓存的频道数据
  729. var channelData = wx.getStorageSync("channelData") || "";
  730. if (!strings.isEmpty(channelData)) {
  731. that.setData({
  732. channelData: channelData,
  733. greeting: greeting,
  734. nickName: nickName,
  735. userPic: userInfo.headUrl || "",
  736. isLogin: true,
  737. })
  738. } else {
  739. that.setData({
  740. greeting: greeting,
  741. nickName: nickName,
  742. userPic: userInfo.headUrl || "",
  743. isLogin: true,
  744. });
  745. }
  746. that.getDeviceData();
  747. } else {
  748. var greeting = that.getGreetBuNickName("");
  749. that.setData({
  750. greeting: greeting,
  751. })
  752. }
  753. },
  754. getGreetBuNickName(nickName) {
  755. var greeting = timeUtil.getGreet();
  756. if (nickName != "" && nickName != "未登录") {
  757. greeting = greeting + ',' + nickName;;
  758. }
  759. return greeting;
  760. },
  761. onBannerLoad() {
  762. var that = this;
  763. var bannerList = wx.getStorageSync("homeBanner") || [];
  764. if (that.data.bannerList.length == 0) {
  765. that.setData({
  766. bannerList: bannerList
  767. });
  768. }
  769. },
  770. onBannerShow() {
  771. var that = this;
  772. getBanner({}).then((res) => {
  773. that.setData({
  774. bannerList: res
  775. });
  776. wx.setStorageSync("homeBanner", res);
  777. })
  778. },
  779. })