index.js 25 KB

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