scene.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. // pages/scene/scene.js
  2. Page({
  3. /**
  4. * 页面的初始数据
  5. */
  6. data: {
  7. remove:true,//删除工程
  8. Modify:true,//修改工程名称
  9. array:[],//分组
  10. brray:[],
  11. keyid:[],
  12. inputVal:[],
  13. getName:[],//发送往服务器的工程名称参数
  14. subscript:0,
  15. Id:[],
  16. key:[],
  17. developline:[],
  18. status:0,
  19. Status:"",
  20. },
  21. //添加分组
  22. apption:function(){
  23. var that = this
  24. wx.showModal({
  25. title:'添加工程',
  26. editable:true,
  27. placeholderText:'请输入名称',
  28. success:function(ever){
  29. if(ever.confirm){
  30. if (ever.content == '') {
  31. wx.showModal({
  32. title:'提示',
  33. content:'请输入工程名',
  34. showCancel:false
  35. })
  36. }
  37. else{
  38. let token = wx.getStorageSync('token')
  39. wx.request({
  40. url: 'http://110.40.223.170:8120/api/minigrogram/lightcontrol/groups',
  41. method:'POST',
  42. header:{'Content-Type': 'application/json',
  43. 'Authorization': token},
  44. data:{
  45. groupName:ever.content
  46. },
  47. success:res => {
  48. console.log(res)
  49. if(res.data.code == 200){
  50. if(wx.getStorageSync('group') == ''){
  51. wx.setStorageSync('group', [])
  52. var a = wx.getStorageSync('group')
  53. a.push({id:res.data.data.id,name:ever.content,device:[]})
  54. wx.setStorageSync('group', a)
  55. }
  56. else{
  57. var b = wx.getStorageSync('group')
  58. b.push({id:res.data.data.id,name:ever.content,device:[]})
  59. wx.setStorageSync('group', b)
  60. }
  61. that.setData({
  62. array:wx.getStorageSync('group'),
  63. })
  64. console.log(wx.getStorageSync('group'))
  65. }
  66. else{
  67. console.log("添加分组失败")
  68. }
  69. }
  70. })
  71. }
  72. }
  73. }
  74. })
  75. },
  76. //删除分组
  77. removeapption:function(e){
  78. var that = this
  79. wx.showModal({
  80. title:'提示',
  81. content:'确定删除该工程',
  82. success:function(res){
  83. if (res.confirm) {
  84. var index = e.currentTarget.dataset.indexdel//获取对应下标
  85. var Idindex = that.data.array[index].id
  86. var token = wx.getStorageSync('token')
  87. wx.request({
  88. url: 'http://110.40.223.170:8120/api/minigrogram/lightcontrol/groups?id=' + Idindex,//
  89. method:'DELETE',
  90. header:{'Content-Type': 'application/json',
  91. 'Authorization': token},
  92. success:res => {
  93. if(res.data.code == 200){
  94. if(wx.getStorageSync('group') != ''){
  95. var deleteid = wx.getStorageSync('group')
  96. deleteid.splice(index,1)
  97. wx.setStorageSync('group', deleteid)
  98. that.setData({
  99. array:wx.getStorageSync('group'),
  100. })
  101. }
  102. }
  103. else{
  104. console.log('删除分组失败')
  105. }
  106. }
  107. })
  108. }
  109. }
  110. })
  111. },
  112. //修改分组名称
  113. ShowChanges:function(e){
  114. var that = this
  115. wx.showModal({
  116. title:'修改工程名称',
  117. editable:true,
  118. placeholderText:'请输入名称',
  119. success:res =>{
  120. console.log(res)
  121. if (res.confirm) {
  122. var index = e.currentTarget.dataset.indexdel//获取对应下标
  123. var Idindex = that.data.array[index].id//获取对应下标的id值
  124. if (res.content == '') {
  125. wx.showModal({
  126. title:'提示',
  127. content:'请输入工程名',
  128. showCancel:false
  129. })
  130. }
  131. else{
  132. var token = wx.getStorageSync('token')
  133. wx.request({
  134. url: 'http://110.40.223.170:8120/api/minigrogram/lightcontrol/groups',
  135. method:'PUT',
  136. header:{
  137. 'Content-Type': 'application/json',
  138. 'Authorization': token
  139. },
  140. data:{
  141. groupName:res.content,
  142. id:Idindex
  143. },
  144. success:ever =>{
  145. console.log(ever)
  146. if (ever.data.code == 200) {
  147. let group = wx.getStorageSync('group')
  148. group[index].name = res.content
  149. wx.setStorageSync('group',group)
  150. that.setData({
  151. array:wx.getStorageSync('group')
  152. })
  153. }
  154. else{
  155. console.log('修改分组名称失败')
  156. }
  157. }
  158. })
  159. }
  160. }
  161. }
  162. })
  163. },
  164. //扫码(添加设备)
  165. scanCodeEvent: function(e){
  166. var that = this
  167. var index = e.currentTarget.dataset.indexdel//获取对应下标
  168. var Idindex = that.data.array[index].id//分组Id
  169. Idindex = parseInt(Idindex)
  170. //获取设备
  171. wx.scanCode({
  172. success(res){
  173. var arr = res.result.split(' ');
  174. var a = arr[0]//设备id
  175. var b = arr[1]//设备key
  176. //获取该设备的在线状态和编码
  177. wx.request({
  178. url: 'https://api.heclouds.com/devices/' + a,
  179. method:'GET',
  180. header:{
  181. 'api-key':b,
  182. },
  183. success(ever){
  184. var online = ever.data.data.online//设备在线状态
  185. switch (online) {
  186. case true:
  187. that.setData({status:1})
  188. that.setData({Status:"在线"})
  189. break;
  190. default:
  191. that.setData({status:0})
  192. that.setData({Status:"离线"})
  193. break;
  194. }
  195. var title = ever.data.data.title//设备编码
  196. let token = wx.getStorageSync('token')
  197. wx.request({
  198. url: 'http://110.40.223.170:8120/api/minigrogram/lightcontrol/devices',
  199. method:'POST',
  200. header:{'Content-Type': 'application/json',
  201. 'Authorization': token},
  202. data:{
  203. groupId:Idindex,//分组id
  204. deviceSn:title,//设备编码
  205. deviceName:title,//设备名称
  206. deviceId:a,//设备id
  207. deviceKey:b,//设备key
  208. status:that.data.status//设备在线状态
  209. },
  210. success:su =>{
  211. if (su.data.code == 200){
  212. //将设备的信息储存至本地
  213. let group = wx.getStorageSync('group')
  214. group[index].device.push({id:su.data.data.id,deviceSn:title,deviceName:title,status:that.data.Status,deviceId:a,
  215. deviceKey:b})
  216. wx.setStorageSync('group', group)
  217. that.setData({
  218. array:wx.getStorageSync('group')
  219. })
  220. console.log(that.data.array)
  221. }
  222. else{
  223. console.log('添加设备失败')
  224. }
  225. }
  226. })
  227. }
  228. })
  229. }
  230. })
  231. },
  232. //修改设备名称
  233. modifyequipmentname:function(e){
  234. var that = this
  235. var key = e.currentTarget.dataset.indexdel
  236. var group = wx.getStorageSync('group')
  237. var deviceId = group[key[0]].device[key[1]].id
  238. var token = wx.getStorageSync('token')
  239. wx.showModal({
  240. title:'修改设备名称',
  241. editable:true,
  242. placeholderText:'请输入名称',
  243. success:res =>{
  244. if (res.confirm) {
  245. if (res.content == '') {
  246. wx.showModal({
  247. title:'提示',
  248. showCancel:false,
  249. content:'请输入名称'
  250. })
  251. }
  252. else{
  253. wx.request({
  254. url: 'http://110.40.223.170:8120/api/minigrogram/lightcontrol/devices',
  255. method:'PUT',
  256. header:{'Content-Type': 'application/json','Authorization': token},
  257. data:{
  258. deviceName:res.content,
  259. id:deviceId
  260. },
  261. success:ever =>{
  262. console.log(ever)
  263. if(ever.data.code == 200){
  264. group[key[0]].device[key[1]].deviceName = res.content
  265. wx.setStorageSync('group', group)
  266. that.setData({
  267. array:wx.getStorageSync('group')
  268. })
  269. }
  270. else{
  271. console.log('修改设备名称失败')
  272. }
  273. }
  274. })
  275. }
  276. }
  277. }
  278. })
  279. },
  280. //删除设备
  281. Deletedevice:function(e){
  282. var that = this
  283. var token = wx.getStorageSync('token')
  284. wx.showModal({
  285. title:'删除设备',
  286. content:'确定删除该设备吗?',
  287. success:function(res){
  288. if(res.confirm){
  289. let key = e.currentTarget.dataset.indexdel//获得分组和设备的下标值([分组下标,设备下标])
  290. let group = wx.getStorageSync('group')
  291. let subscript = group[key[0]].device[key[1]].id//设备对应的id
  292. wx.request({
  293. url: 'http://110.40.223.170:8120/api/minigrogram/lightcontrol/devices?id=' + subscript,
  294. method:'DELETE',
  295. header:{'Content-Type': 'application/json','Authorization': token},
  296. success:nrg => {
  297. console.log(nrg)
  298. if (nrg.data.code == 200) {
  299. group[key[0]].device.splice(key[1],1)
  300. wx.setStorageSync('group', group)
  301. that.setData({
  302. array:wx.getStorageSync('group')
  303. })
  304. }
  305. else{
  306. console.log('删除列表失败')
  307. }
  308. }
  309. })
  310. }
  311. }
  312. })
  313. },
  314. Refresh:function(){
  315. },
  316. //展开设备
  317. develops:function(){
  318. },
  319. /**
  320. * 生命周期函数--监听页面加载
  321. */
  322. onLoad(options) {
  323. var that = this
  324. let token = wx.getStorageSync('token')
  325. // wx.request({
  326. // url:'http://110.40.223.170:8120/api/minigrogram/lightcontrol/groups',
  327. // method:'GET',
  328. // header:{'Content-Type': 'application/json','Authorization': token},
  329. // success:tp => {
  330. // console.log(tp)
  331. // }
  332. // })
  333. // wx.request({
  334. // url: 'http://110.40.223.170:8120/api/minigrogram/lightcontrol/devices?id=' + '5',
  335. // method:'DELETE',
  336. // header:{'Content-Type': 'application/json','Authorization': token},
  337. // success:nrg => {
  338. // console.log(nrg)
  339. // }
  340. // })
  341. console.log(wx.getStorageSync('group'))
  342. // that.data.array = []
  343. // wx.setStorageSync('information', )
  344. // console.log(wx.getStorageSync('information'))
  345. // wx.setStorageSync('id', [])
  346. // wx.setStorageSync('values', [])
  347. that.setData({
  348. array:wx.getStorageSync('group'),
  349. })
  350. console.log(that.data.array)
  351. },
  352. /**
  353. * 生命周期函数--监听页面初次渲染完成
  354. */
  355. onReady() {
  356. },
  357. /**
  358. * 生命周期函数--监听页面显示
  359. */
  360. onShow() {
  361. },
  362. /**
  363. * 生命周期函数--监听页面隐藏
  364. */
  365. onHide() {
  366. },
  367. /**
  368. * 生命周期函数--监听页面卸载
  369. */
  370. onUnload() {
  371. },
  372. /**
  373. * 页面相关事件处理函数--监听用户下拉动作
  374. */
  375. onPullDownRefresh() {
  376. },
  377. /**
  378. * 页面上拉触底事件的处理函数
  379. */
  380. onReachBottom() {
  381. },
  382. /**
  383. * 用户点击右上角分享
  384. */
  385. onShareAppMessage() {
  386. }
  387. })