deviceConnect0.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. const constant = require('../../../utils/constant.js');
  2. Page({
  3. data: {
  4. nvabarData: {
  5. showCapsule: 1, //是否显示左上角图标 1表示显示 0表示不显示
  6. title: '连接配网', //导航栏 中间的标题
  7. callback: () => {
  8. console.log("返回界面3:");
  9. }
  10. },
  11. scopeBluetooth: false,
  12. userFuzzyLocation: true,
  13. isNotruter: false,
  14. _isFromRootPage: true,
  15. isAiDevice: false,
  16. connectDevice: {},
  17. imageUrl: "./../../../img/c.png",
  18. },
  19. ///定位,蓝牙权限,蓝牙适配器
  20. // {"applicationType":"[0, 1]","deviceId":430635766251545,"img":"https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20220812113416291791429.png","offlineImg":"https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20220812113421204031133.png","connectImg":null,"name":"猫王音响x阿基米德 SR1 妙播收音机","bluetoothName":"SR1妙播收音机","bluetoothNames":["SR1妙播收音机","猫王XOG·CyberRadio 001"],"isChannelsPlatforms":1,"platform":0,"typeList":[{"is5g":0,"type":1,"connectType":1,"functionList":[6,1,3],"deviceLinkResp":{"icon1":"https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20220812113427142232548.png","icon2":"https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20220812113431143471942.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/20220812113437930271177.png","icon2":"https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20220812113441600058940.png","icon":null,"guideUrl":null}},{"is5g":0,"type":4,"connectType":3,"functionList":[1,3],"deviceLinkResp":{"icon1":"https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20220812113446634000744.png","icon2":"https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20220812113450885467009.png","icon":null,"guideUrl":null}}],"clientType":"猫王妙播黑胶音箱(BLE)","firstVersion":"1","filter":null,"guideUrl":null,"manufacturer":"ShanJing","deviceType":0}
  21. onLoad(options) {
  22. var that = this;
  23. var param = options.param ?? "{}";
  24. // 解码URL编码的参数,例如:%7B%22%22clientType%22:%22MW-2AXD%22%7D
  25. param = decodeURIComponent(param)
  26. console.log("设备参数:", param)
  27. if (!constant.strings.isEmpty(param)) {
  28. param = JSON.parse(param);
  29. var clientType = param["clientType"]
  30. if (Object.keys(param).length <= 1) {
  31. // 扫描进来的只有一个参数
  32. that.setData({
  33. isAiDevice: true,
  34. })
  35. var classifyProducts = constant.app.globalData.classifyProducts;
  36. if (!constant.strings.isEmpty(classifyProducts)) {
  37. var fullDevice = classifyProducts.filter((v) => v.clientType == clientType);
  38. console.log("找到设备1", fullDevice);
  39. var device = fullDevice[0] ?? param;
  40. that.geSameCategoryList(device);
  41. that.updateDeviceUrl(device);
  42. } else {
  43. ///数据请求
  44. constant.dRequest.deviceList({
  45. unShowLoad: true
  46. }).then((res) => {
  47. if (res) {
  48. var fullDevice = res.filter((v) => v.clientType == clientType);
  49. console.log("找到设备2", fullDevice);
  50. that.setDeviceList(res, true);
  51. var device = fullDevice[0] ?? param;
  52. that.geSameCategoryList(device);
  53. that.updateDeviceUrl(device);
  54. }
  55. });
  56. }
  57. } else {
  58. var device = param;
  59. that.geSameCategoryList(device);
  60. that.updateDeviceUrl(device);
  61. }
  62. }
  63. wx.offAppShow(that.getSetting);
  64. wx.onAppShow(that.getSetting);
  65. that.getSetting();
  66. if (options.isLognlink) {
  67. that.setData({
  68. isNotruter: true,
  69. nvabarData: {
  70. showCapsule: 1,
  71. title: '连接配网',
  72. callback: () => {
  73. constant.routeUtil.goBackRoute(constant.routeRoot.index);
  74. }
  75. },
  76. })
  77. }
  78. },
  79. geSameCategoryList(device) {
  80. if (device.clientType != "MW-2AXD") {
  81. return;
  82. }
  83. // console.log("找到设备:", device);
  84. var that = this;
  85. var res = constant.store.getStore("sameCategoryList")
  86. if (Object.keys(res).length > 0) {
  87. // console.log("找到设备23", res);
  88. that.getDeviceIcon(res, device);
  89. } else {
  90. constant.dRequest.sameCategoryList().then((list) => {
  91. constant.store.setStore("sameCategoryList", list);
  92. // console.log("找到设备33", deviceList);
  93. that.getDeviceIcon(list, device);
  94. })
  95. }
  96. },
  97. getDeviceIcon(deviceList, device) {
  98. var fullDevice
  99. outerLoop:
  100. for (const element of deviceList) {
  101. // console.log("找到设备8", element);
  102. if (element.deviceSameList) {
  103. for (const sameItem of element.deviceSameList) {
  104. if (sameItem.deviceList) {
  105. // console.log("找到设备7", sameItem);
  106. for (const item of sameItem.deviceList) {
  107. // console.log("找到设备6", item);
  108. if (item.clientType == device.clientType) {
  109. fullDevice = item;
  110. break outerLoop;
  111. }
  112. }
  113. }
  114. }
  115. }
  116. }
  117. // console.log("找到设备5", fullDevice);
  118. if (!constant.strings.isEmpty(fullDevice)) {
  119. device.typeList = fullDevice.typeList;
  120. }
  121. },
  122. updateDeviceUrl(param) {
  123. var that = this
  124. that.data.connectDevice = param;
  125. var typeList = param["typeList"] ?? [];
  126. if (!constant.strings.isEmpty(typeList)) {
  127. typeList = typeList.filter((v) => v.connectType == 3);
  128. if (!constant.strings.isEmpty(typeList)) {
  129. console.log("找到设备4", typeList);
  130. that.setData({
  131. imageUrl: typeList[0].deviceLinkResp.icon1,
  132. });
  133. }
  134. }
  135. },
  136. callback() {
  137. // 重置小程序界面到首页
  138. console.log("返回界面4", constant.routePath.index);
  139. if (this.data._isFromRootPage) {
  140. constant.routeUtil.goBack();
  141. } else {
  142. console.log("返回界面4", constant.routePath.index);
  143. wx.reLaunch({
  144. // url: '/pages/index/index',
  145. url: constant.routePath.index,
  146. });
  147. }
  148. },
  149. ///获取定位服务
  150. getSetting() {
  151. var that = this;
  152. wx.getSetting({
  153. success(res) {
  154. if (res.authSetting["scope.userFuzzyLocation"]) {
  155. constant.app.getBluetoothStatus();
  156. that.setData({
  157. userFuzzyLocation: true,
  158. });
  159. } else if (res.authSetting["scope.userFuzzyLocation"] === undefined) {
  160. ///请求定位权限
  161. wx.authorize({
  162. scope: "scope.userFuzzyLocation",
  163. success() {
  164. that.getSetting();
  165. }
  166. });
  167. } else {
  168. that.setData({
  169. userFuzzyLocation: false,
  170. });
  171. wx.showModal({
  172. title: '请打开系统位置获取',
  173. success(res) {
  174. if (res.confirm) {
  175. wx.openSetting({
  176. complete() {
  177. // that.getSetting();
  178. }
  179. })
  180. } else if (res.cancel) { }
  181. }
  182. });
  183. }
  184. }
  185. })
  186. },
  187. ///检测蓝牙权限回调 true:false
  188. getBluetoothStatusCallck(v) {
  189. var that = this;
  190. that.setData({
  191. scopeBluetooth: v
  192. });
  193. },
  194. ///下一步
  195. next() {
  196. var that = this;
  197. ///初始化蓝牙
  198. wx.openBluetoothAdapter({
  199. success(res) {
  200. if (that.data.scopeBluetooth && that.data.userFuzzyLocation) {
  201. var param = "?param=" + JSON.stringify(that.data.connectDevice);
  202. constant.routeUtil.jumpParam(constant.routePath.deviceConnect1, param);
  203. }
  204. },
  205. fail(err) {
  206. if (err.errCode === 10001) {
  207. wx.showToast({
  208. title: '手机蓝牙未打开',
  209. icon: "none",
  210. duration: 2000
  211. })
  212. } else {
  213. wx.showToast({
  214. title: err,
  215. icon: "none",
  216. duration: 2000
  217. })
  218. }
  219. }
  220. });
  221. },
  222. ///手动开启定位权限
  223. setUserFuzzyLocation() {
  224. const that = this;
  225. wx.openSetting({
  226. complete() {
  227. // that.getSetting();
  228. }
  229. })
  230. },
  231. //手动开启蓝牙
  232. setBluetooth() {
  233. wx.openSetting({
  234. complete() {
  235. //constant.app.getBluetoothStatus();
  236. }
  237. })
  238. },
  239. ///返回到首页
  240. notRoter() {
  241. constant.routeUtil.goBackRoute(constant.routeRoot.index);
  242. },
  243. ///设备列表赋值
  244. setDeviceList(res, isLoad) {
  245. var devices = res;
  246. if (isLoad) {
  247. constant.store.setStore("classifyProducts", res);
  248. constant.app.globalData.classifyProducts = devices;
  249. }
  250. },
  251. deviceTap(e) {
  252. var device = e.currentTarget.dataset.device;
  253. console.log(device)
  254. if (!device) {
  255. console.log("设备为空")
  256. return;
  257. }
  258. var clientTypes = constant.lexinDevices.clientTypes;
  259. if (clientTypes.includes(device.clientType)) {
  260. var param = "?param=" + JSON.stringify(device);
  261. // pages/pageB/deviceConnect0/deviceConnect0?param={"clientType":"MW-Mate X(4G_WIFI)"}
  262. // ?param={"applicationType":"[0, 1]","deviceId":432698392051714,"img":"https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230410102304552692164.png","offlineImg":"https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230410102740684077340.png","connectImg":null,"name":"X2 妙播音箱","bluetoothName":"猫王音响·Mate_X2","bluetoothNames":["猫王音响·Mate_X2","猫王音响·X2妙播音箱","猫王音响·Mate_X"],"isChannelsPlatforms":0,"platform":0,"typeList":[{"is5g":0,"type":1,"connectType":1,"functionList":[1,3],"deviceLinkResp":{"icon1":"https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230410102357244734373.png","icon2":"https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230410102404705282697.png","icon":null,"guideUrl":null}},{"is5g":0,"type":2,"connectType":3,"functionList":[1,3,6],"deviceLinkResp":{"icon1":"https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230313152650116909154.png","icon2":"https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230313152717095912088.png","icon":null,"guideUrl":null}},{"is5g":0,"type":4,"connectType":3,"functionList":[1,3,6],"deviceLinkResp":{"icon1":"https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230313152749792939577.png","icon2":"https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230313152759963002418.png","icon":null,"guideUrl":null}}],"clientType":"MW-Mate X(4G_WIFI)","firstVersion":"0.0.1","filter":null,"guideUrl":null,"manufacturer":"ShanJing","deviceType":0}
  263. constant.routeUtil.jumpParam(constant.routePath.deviceConnect0, param);
  264. } else if (device.clientType === "MW-S2" || device.clientType === "猫王妙播黑胶音箱(BLE)") {
  265. var param = "?param=" + JSON.stringify(device);
  266. constant.routeUtil.jumpParam(constant.routePath.connectBle, param);
  267. } else {
  268. wx.showToast({
  269. title: '暂不支持该设备',
  270. })
  271. }
  272. },
  273. onUnload() {
  274. var that = this;
  275. ///传入的监听函数。不传此参数则移除所有监听函数
  276. wx.offAppShow(that.getSetting);
  277. },
  278. })