运维小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. // pages/Workorderdetails/index.js
  2. const api = require('../../api/index.js');
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. first: 1,
  9. second: 1,
  10. third: 1,
  11. visible: false,
  12. workorder: [],
  13. optionsid: '',
  14. workorderId: '',
  15. partnerAvite: [],
  16. bindreviewtype: '',
  17. latitude: null,
  18. longitude: null,
  19. locationName: '获取中...',
  20. checkinStatus: 'waiting', // waiting, success, fail
  21. checkinRecords: [],
  22. distance: null, // 距离目标点的距离
  23. canCheckin: false,
  24. showPermissionGuide: null,
  25. workorderContent: '',
  26. switchtype: false,
  27. codeswitchtype:false,
  28. partnerPosition: wx.getStorageSync('partnerPosition'),
  29. scanCode:{},
  30. result:''
  31. },
  32. /**
  33. * 生命周期函数--监听页面加载
  34. */
  35. onLoad(options) {
  36. console.log(options);
  37. this.setData({
  38. optionsid: options.id,
  39. workorderId: options.workorderId
  40. })
  41. this.onworkorder()
  42. },
  43. // 获取当前位置
  44. getCurrentLocation() {
  45. const that = this;
  46. // 先检查位置权限
  47. wx.getSetting({
  48. success: (res) => {
  49. if (!res.authSetting['scope.userLocation']) {
  50. // 如果没有授权,请求授权
  51. wx.authorize({
  52. scope: 'scope.userLocation',
  53. success: () => {
  54. that._getLocation();
  55. },
  56. fail: () => {
  57. wx.showModal({
  58. title: '位置权限提示',
  59. content: '需要位置权限才能使用此功能,是否去设置开启?',
  60. success: (res) => {
  61. if (res.confirm) {
  62. wx.openSetting();
  63. }
  64. }
  65. });
  66. }
  67. });
  68. } else {
  69. // 已经授权,直接获取位置
  70. that._getLocation();
  71. }
  72. }
  73. });
  74. },
  75. // 提取获取位置的具体逻辑
  76. _getLocation() {
  77. const that = this;
  78. wx.getLocation({
  79. type: 'wgs84',
  80. success: (res) => {
  81. console.log('位置获取成功:', res);
  82. that.setData({
  83. latitude: res.latitude,
  84. longitude: res.longitude
  85. });
  86. // that.reverseGeocode(res.latitude, res.longitude);
  87. that.checkDistance();
  88. },
  89. fail: (err) => {
  90. console.error('获取位置失败:', err);
  91. wx.showToast({
  92. title: '获取位置失败,请重试',
  93. icon: 'none'
  94. });
  95. }
  96. })
  97. },
  98. checkDistance() {
  99. // this.data.workorder.devicesLatitudelongitude.split(",")
  100. let position = this.data.workorder.devicesLatitudelongitude;
  101. console.log(position);
  102. const targetLng = position[0]; // 目标经度
  103. const targetLat = position[1]; // 目标纬度
  104. const allowedDistance = 100; // 允许打卡距离(米)
  105. // longitude: 116.66504838681816 2号楼位置
  106. // latitude: 40.088160079721874 2号楼位置
  107. // longitude: 116.66504838932426 工位位置
  108. // latitude: 40.08816007901852 工位位置
  109. const distance = this.calculateDistance(
  110. this.data.latitude,
  111. this.data.longitude,
  112. targetLat,
  113. targetLng
  114. );
  115. this.setData({
  116. distance: distance.toFixed(2),
  117. canCheckin: distance <= allowedDistance
  118. });
  119. console.log(this.data.distance);
  120. console.log(this.data.canCheckin);
  121. },
  122. // 计算两点间距离(Haversine公式)
  123. calculateDistance(lat1, lng1, lat2, lng2) {
  124. const R = 6371000; // 地球半径(米)
  125. const dLat = (lat2 - lat1) * Math.PI / 180;
  126. const dLng = (lng2 - lng1) * Math.PI / 180;
  127. const a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
  128. Math.cos(lat1 * Math.PI / 180) * Math.cos(lat2 * Math.PI / 180) *
  129. Math.sin(dLng / 2) * Math.sin(dLng / 2);
  130. const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
  131. return R * c;
  132. },
  133. onposition() {
  134. this.checkLocationPermission();
  135. },
  136. onworkorder() {
  137. let _this = this
  138. let data = {
  139. workorderId: this.data.workorderId,
  140. id: this.data.optionsid
  141. }
  142. api.request(`/workoder/selectsysworkoderid`, 'POST', data)
  143. .then((res) => {
  144. console.log(res);
  145. if (res.code == 200) {
  146. if (res.data.workorderImg) {
  147. res.data.workorderImg = res.data.workorderImg.split(",")
  148. }
  149. // console.log(res.data.workoderstationimg==undefined);
  150. if (res.data.workoderstationimg) {
  151. res.data.workoderstationimg = res.data.workoderstationimg.split(",")
  152. }
  153. if (res.data.workoderendimg) {
  154. res.data.workoderendimg = res.data.workoderendimg.split(",")
  155. }
  156. if (res.data.workoderPartnerId) {
  157. res.data.workoderPartnerId=res.data.workoderPartnerId.split(",")
  158. }
  159. console.log(res.data.workoderPartnerId);
  160. if (res.data.devicesLatitudelongitude) {
  161. res.data.devicesLatitudelongitude.split(",")
  162. } else {
  163. res.data.devicesLatitudelongitude = ['116.66504838681816', '40.088160079721874']
  164. }
  165. this.setData({
  166. workorder: res.data
  167. })
  168. if (res.data.workorderType == 1) {
  169. console.log('定位开始');
  170. // _this.goToSetting();
  171. // this.checkLocationPermission();
  172. this.getCurrentLocation();
  173. }
  174. }
  175. // .split(" ")
  176. })
  177. .catch((err) => {
  178. console.error('请求失败:', err);
  179. // 在这里处理请求失败的情况
  180. });
  181. },
  182. onFirstChange(e) {
  183. this.setData({ first: e.detail.current });
  184. },
  185. onSecondChange(e) {
  186. this.setData({ second: e.detail.current });
  187. },
  188. onThirdChange(e) {
  189. this.setData({ third: e.detail.current });
  190. },
  191. onstep() {
  192. this.setData({
  193. visible: true,
  194. switchtype:true
  195. })
  196. console.log(this.data.workorder);
  197. if (this.data.workorder.workorderType==2) {
  198. for (let index = 0; index < this.data.workorder.sysRegions.length; index++) {
  199. if (this.data.workorder.sysRegions[index].type==0) {
  200. this.setData({
  201. codeswitchtype:false
  202. })
  203. return
  204. }else{
  205. this.setData({
  206. codeswitchtype:true
  207. })
  208. }
  209. }
  210. }
  211. // wx.navigateTo({
  212. // url: '/pages/workorderstep/index',
  213. // })
  214. },
  215. oncode(){
  216. wx.showToast({
  217. title: '请先完成设备扫码',
  218. icon: 'none',
  219. });
  220. },
  221. // 删除图片
  222. bindCancel(e) {
  223. console.log(e.currentTarget.dataset.index);
  224. let index = e.currentTarget.dataset.index
  225. // 使用 filter 方法创建新数组(推荐)
  226. const newPartnerAvite = this.data.partnerAvite.filter((item, i) => i !== index);
  227. this.setData({
  228. partnerAvite: newPartnerAvite
  229. });
  230. },
  231. onVisibleChange() {
  232. this.setData({
  233. visible: false
  234. })
  235. },
  236. ontextarea(e) {
  237. this.setData({
  238. workorderContent: e.detail.value
  239. })
  240. },
  241. submit() {
  242. if (!this.data.switchtype) {
  243. return
  244. }
  245. this.setData({
  246. switchtype: false
  247. })
  248. let data = {
  249. id: this.data.workorder.id,
  250. workoderImg: this.data.partnerAvite.length > 0 ? this.data.partnerAvite.toString() : '',
  251. workoderDetails: this.data.workorderContent,
  252. devicesRange: this.data.canCheckin ? '到达设备范围' : '不在设备范围'
  253. }
  254. api.request(`/workoder/addwork`, 'POST', data)
  255. .then((res) => {
  256. console.log(res);
  257. if (res.code == 200) {
  258. wx.showToast({
  259. title: '提交成功',
  260. icon: 'success',
  261. });
  262. this.setData({
  263. visible: false,
  264. partnerAvite: [],
  265. switchtype: true,
  266. workorderContent: ''
  267. })
  268. if (this.data.workorder.workorderType == 2) {
  269. wx.reLaunch({
  270. url: '/pages/index/index',
  271. })
  272. }
  273. this.onworkorder()
  274. }
  275. })
  276. .catch((err) => {
  277. console.error('请求失败:', err);
  278. this.setData({
  279. switchtype: true
  280. })
  281. // 在这里处理请求失败的情况
  282. });
  283. },
  284. // 审核
  285. bindreview(e) {
  286. this.setData({
  287. bindreviewtype: e.currentTarget.dataset.type
  288. })
  289. if (e.currentTarget.dataset.type == '不通过') {
  290. this.setData({
  291. visible: true,
  292. })
  293. return
  294. }
  295. let data = {
  296. id: this.data.workorder.id,
  297. examineType: e.currentTarget.dataset.type,
  298. workoderReason: this.data.workorderContent
  299. }
  300. api.request(`/workoder/examine`, 'POST', data)
  301. .then((res) => {
  302. console.log(res);
  303. if (res.code == 200) {
  304. wx.showToast({
  305. title: '提交成功',
  306. icon: 'success',
  307. });
  308. this.setData({
  309. visible: false
  310. })
  311. wx.reLaunch({
  312. url: '/pages/index/index',
  313. })
  314. // this.onworkorder()
  315. }
  316. })
  317. .catch((err) => {
  318. console.error('请求失败:', err);
  319. // 在这里处理请求失败的情况
  320. });
  321. },
  322. // 预览图片
  323. previewImage(e) {
  324. console.log(e);
  325. const src = e.currentTarget.dataset.src;
  326. wx.previewImage({
  327. current: src, // 当前显示图片的http链接
  328. urls: [src] // 需要预览的图片http链接列表
  329. });
  330. },
  331. // 上传头像
  332. bindtoImage() {
  333. let _this = this
  334. // 先检查隐私授权状态
  335. wx.getPrivacySetting({
  336. success: (res) => {
  337. if (res.needAuthorization) {
  338. // 弹出隐私协议弹窗
  339. wx.requirePrivacyAuthorize({
  340. success: () => {
  341. console.log('用户已同意隐私协议');
  342. _this.choosetoImage(); // 继续执行上传
  343. },
  344. fail: () => {
  345. console.log('用户拒绝了隐私协议');
  346. wx.showToast({
  347. title: '需同意隐私协议才能使用',
  348. icon: 'none'
  349. });
  350. }
  351. });
  352. } else {
  353. _this.choosetoImage(); // 已授权,直接上传
  354. }
  355. }
  356. });
  357. },
  358. // 上传头像图片
  359. choosetoImage() {
  360. let _this = this
  361. wx.chooseMedia({
  362. count: 1,
  363. mediaType: ['image', 'video'],
  364. sourceType: ['album', 'camera'],
  365. success(res) {
  366. console.log(res);
  367. const tempFiles = res.tempFiles;
  368. _this.setData({
  369. imageList: tempFiles.map(file => file.tempFilePath),
  370. });
  371. _this.uploadtoImages(tempFiles);
  372. },
  373. fail(err) {
  374. console.error("选择失败:", err);
  375. }
  376. })
  377. },
  378. // 上传到服务器
  379. uploadtoImages(files) {
  380. let _this = this
  381. const token = wx.getStorageSync('token');
  382. files.forEach((file) => {
  383. wx.uploadFile({
  384. url: 'https://esos-iot.com:8443/config/upload/webPost',
  385. filePath: file.tempFilePath,
  386. name: 'file',
  387. formData: {
  388. file: file.tempFilePath
  389. },
  390. header: {
  391. 'Authorization': 'Bearer ' + token // Also add to header if needed
  392. },
  393. success: (res) => {
  394. console.log(res);
  395. const obj = JSON.parse(res.data);
  396. this.data.partnerAvite.push(obj.data.partnerAvite)
  397. this.setData({
  398. partnerAvite: this.data.partnerAvite
  399. })
  400. },
  401. fail: (err) => {
  402. console.error('上传失败', err);
  403. },
  404. });
  405. });
  406. },
  407. scancode(e) {
  408. let _this =this
  409. console.log(e.currentTarget.dataset.deviceid);
  410. wx.scanCode({
  411. onlyFromCamera:true,
  412. success: (res) => {
  413. console.log(res);
  414. let rawData = e.currentTarget.dataset.deviceid.devicesQrcode.split(",")
  415. console.log(rawData);
  416. for (let index = 0; index < rawData.length; index++) {
  417. if (rawData[index] ==res.rawData) {
  418. wx.showToast({
  419. title: '扫码完成',
  420. icon: 'none'
  421. });
  422. _this.onupdatesysdeviceworkorder(e.currentTarget.dataset.deviceid)
  423. return
  424. }
  425. }
  426. wx.showToast({
  427. title: '没有匹配到正确的设备码',
  428. icon: 'none'
  429. });
  430. console.log(this.data.scanCode);
  431. },
  432. fail: (err) => {
  433. console.log(err);
  434. wx.showToast({
  435. title: '扫码失败',
  436. icon: 'none'
  437. });
  438. }
  439. })
  440. },
  441. onupdatesysdeviceworkorder(e) {
  442. let _this = this
  443. let data = {
  444. workorderId: this.data.workorderId,
  445. deviceId: e.deviceId,
  446. type:1
  447. }
  448. api.request(`/device/updatesysdeviceworkorder`, 'POST', data)
  449. .then((res) => {
  450. if (res.code == 200) {
  451. this.onworkorder()
  452. }
  453. })
  454. .catch((err) => {
  455. console.error('请求失败:', err);
  456. // 在这里处理请求失败的情况
  457. });
  458. },
  459. onqrcode(e){
  460. console.log(e.target.dataset.name);
  461. // console.log(this.data.scanCode.result);
  462. // 优化后的复制代码,增加错误处理
  463. wx.setClipboardData({
  464. data: e.target.dataset.name,
  465. success (res) {
  466. console.log('设置剪贴板成功', res)
  467. wx.showToast({
  468. title: '复制成功',
  469. icon: 'none'
  470. });
  471. },
  472. fail (err) {
  473. console.error('设置剪贴板失败:', err)
  474. wx.showToast({
  475. title: '复制失败,请重试',
  476. icon: 'none'
  477. });
  478. }
  479. })
  480. },
  481. /**
  482. * 生命周期函数--监听页面初次渲染完成
  483. */
  484. onReady() {
  485. },
  486. /**
  487. * 生命周期函数--监听页面显示
  488. */
  489. onShow() {
  490. },
  491. /**
  492. * 生命周期函数--监听页面隐藏
  493. */
  494. onHide() {
  495. },
  496. /**
  497. * 生命周期函数--监听页面卸载
  498. */
  499. onUnload() {
  500. },
  501. /**
  502. * 页面相关事件处理函数--监听用户下拉动作
  503. */
  504. onPullDownRefresh() {
  505. },
  506. /**
  507. * 页面上拉触底事件的处理函数
  508. */
  509. onReachBottom() {
  510. },
  511. /**
  512. * 用户点击右上角分享
  513. */
  514. onShareAppMessage() {
  515. }
  516. })