运维小程序
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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