index.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  1. const app = getApp()
  2. const util = require("../../../utils/util");
  3. const api = require('../../../api/api.js');
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. isEdit: false,
  10. editBookNo: '',
  11. canvasHeight: 0,
  12. base: {
  13. category: [],
  14. device: [],
  15. position: [],
  16. problem: []
  17. }, //基础数据
  18. selected: {
  19. device: [],
  20. position: [],
  21. problem: [],
  22. },
  23. isShow: false,
  24. pres: [],
  25. //本次新增 临时选中项
  26. id: '',
  27. categoryId: {
  28. id: 0,
  29. name: ''
  30. },
  31. deviceId: {
  32. id: 0,
  33. name: ''
  34. },
  35. positionId: {
  36. id: 0,
  37. name: ''
  38. },
  39. problem: [],
  40. picUrls: [],
  41. files: [],
  42. memo: '',
  43. bookNo: '',
  44. mobile: '',
  45. tempMobile:'',
  46. captcha: '',
  47. opeate: 'add',
  48. isConfirm: false, //是否为用户确认界面
  49. isAgree: false, //是否同意以上条款
  50. userInfo: {},
  51. servantName:''//计师名称
  52. },
  53. getCategory() {
  54. let that = this
  55. util.request(api.PreCategory, {}, "GET").then(res => {
  56. that.setData({
  57. base: res.data
  58. })
  59. })
  60. },
  61. bindCategory(e) {
  62. let that = this
  63. that.setData({
  64. categoryId: {
  65. id: e.currentTarget.dataset.id,
  66. name: e.currentTarget.dataset.name
  67. }
  68. })
  69. //筛选设备
  70. var device = [];
  71. that.data.base.device.forEach(item => {
  72. if (item.categoryId == that.data.categoryId.id)
  73. device.push(item)
  74. })
  75. //位置
  76. var position = that.data.base.position;
  77. console.info(position)
  78. // that.data.base.position.forEach(item => {
  79. // if (item.categoryId == that.data.categoryId.id)
  80. // position.push(item)
  81. // })
  82. //问题
  83. var problem = [];
  84. that.data.base.problem.forEach(item => {
  85. if (item.categoryId == that.data.categoryId.id) {
  86. item.checked = false
  87. problem.push(item)
  88. }
  89. })
  90. that.setData({
  91. 'selected.device': device,
  92. 'selected.position': position,
  93. 'selected.problem': problem,
  94. deviceId: {},
  95. positionId: {},
  96. problem: []
  97. })
  98. },
  99. bindDevice(e) {
  100. let that = this
  101. that.setData({
  102. deviceId: {
  103. id: e.currentTarget.dataset.id,
  104. name: e.currentTarget.dataset.name
  105. }
  106. })
  107. },
  108. bindPosition(e) {
  109. let that = this
  110. that.setData({
  111. positionId: {
  112. id: e.currentTarget.dataset.id,
  113. name: e.currentTarget.dataset.name
  114. }
  115. })
  116. },
  117. bindQuestion(e) { //多选
  118. var id = e.currentTarget.dataset.id;
  119. var name = e.currentTarget.dataset.name;
  120. var problem = [];
  121. var selectedQuestion = this.data.selected.problem;
  122. var isContain = false;
  123. this.data.problem.forEach(item => {
  124. if (item.id == id) {
  125. isContain = true;
  126. }
  127. })
  128. if (!isContain) {
  129. problem = this.data.problem;
  130. problem.push({
  131. id: id,
  132. name: name
  133. });
  134. } else { //如果已经选中 从中删除
  135. this.data.problem.forEach(item => {
  136. if (item.id != id)
  137. problem.push(item)
  138. })
  139. }
  140. //实际选中项
  141. selectedQuestion.forEach(item => {
  142. if (item.id == id) {
  143. item.checked = !isContain;
  144. }
  145. })
  146. this.setData({
  147. problem: problem,
  148. 'selected.problem': selectedQuestion
  149. })
  150. },
  151. bindAgreeChange(e) {
  152. this.setData({
  153. isAgree: e.detail.value
  154. })
  155. },
  156. addPre() {
  157. this.setData({
  158. id: Math.floor((Math.random() * 100000)),
  159. isShow: true,
  160. categoryId: {},
  161. deviceId: {},
  162. positionId: {},
  163. problem: [],
  164. files: [],
  165. picUrls: [],
  166. memo: '',
  167. opeate: 'add',
  168. 'selected.device': [],
  169. 'selected.position': [],
  170. 'selected.problem': [],
  171. })
  172. console.info(this.data.selected.problem)
  173. },
  174. confirmAdd() {
  175. let that = this
  176. //console.info(that.data.categoryId)
  177. console.info(that.data.deviceId.id)
  178. if (that.data.categoryId.id == 0) {
  179. wx.showToast({
  180. title: '请选择服务项',
  181. icon: 'none',
  182. duration: 2000
  183. });
  184. return
  185. }
  186. if (that.data.selected.device.length > 0 && that.data.deviceId.id == undefined) {
  187. wx.showToast({
  188. title: '请选择设备',
  189. icon: 'none',
  190. duration: 2000
  191. });
  192. return
  193. }
  194. if (that.data.problem.length == 0) {
  195. wx.showToast({
  196. title: '请选择存在问题',
  197. icon: 'none',
  198. duration: 2000
  199. });
  200. return
  201. }
  202. // 如果问题为其他,则图片和备注必填
  203. let isOther = this.data.problem.some(item => {
  204. return item.name == '其他'
  205. });
  206. if (isOther && (this.data.memo == '' || this.data.files.length == 0)) {
  207. wx.showToast({
  208. title: '请上传图片和填写问题',
  209. icon: 'none',
  210. duration: 2000
  211. });
  212. return
  213. }
  214. var pre = {
  215. id: that.data.id,
  216. categoryId: that.data.categoryId,
  217. deviceId: that.data.deviceId,
  218. positionId: that.data.positionId,
  219. problem: that.data.problem,
  220. files: that.data.files,
  221. picUrls: that.data.picUrls,
  222. memo: that.data.memo
  223. }
  224. var pres = that.data.pres;
  225. if (that.data.opeate == 'update') { //修改时 将旧的选项删除
  226. for (var i = 0; i < pres.length; i++) {
  227. if (pres[i].id == pre.id) {
  228. pres.splice(i, 1);
  229. break;
  230. }
  231. }
  232. }
  233. pres.push(pre);
  234. that.setData({
  235. pres: pres,
  236. isShow: false
  237. })
  238. },
  239. cancelAdd() {
  240. this.setData({
  241. isShow: false,
  242. categoryId: {},
  243. deviceId: {},
  244. positionId: {},
  245. problem: [],
  246. files: [],
  247. picUrls: []
  248. })
  249. },
  250. modifyPre(e) {
  251. let that = this
  252. var id = e.currentTarget.dataset.id;
  253. that.data.pres.forEach(item => {
  254. if (item.id == id) {
  255. that.setData({
  256. id: item.id,
  257. categoryId: item.categoryId,
  258. deviceId: item.deviceId,
  259. positionId: item.positionId,
  260. problem: item.problem,
  261. files: item.files,
  262. picUrls: item.picUrls,
  263. memo: item.memo,
  264. isShow: true,
  265. opeate: 'update'
  266. });
  267. }
  268. });
  269. //筛选设备
  270. var device = [];
  271. this.data.base.device.forEach(item => {
  272. if (item.categoryId == this.data.categoryId.id)
  273. device.push(item)
  274. })
  275. //位置
  276. var position = this.data.base.position;
  277. // this.data.base.position.forEach(item => {
  278. // if (item.categoryId == this.data.categoryId.id)
  279. // position.push(item)
  280. // })
  281. //问题
  282. var problem = [];
  283. let checkedProblem = this.data.problem.map(i => {
  284. return i.id
  285. });
  286. //console.log(checkedProblem);
  287. this.data.base.problem.forEach(item => {
  288. if (item.categoryId == this.data.categoryId.id) {
  289. item.checked = false;
  290. if (checkedProblem.indexOf(item.id) > -1) {
  291. item.checked = true;
  292. }
  293. problem.push(item)
  294. }
  295. })
  296. this.setData({
  297. 'selected.device': device,
  298. 'selected.position': position,
  299. 'selected.problem': problem,
  300. });
  301. },
  302. deletePre(e) {
  303. let that = this
  304. var id = e.currentTarget.dataset.id;
  305. var pres = []
  306. that.data.pres.forEach(item => {
  307. if (item.id != id)
  308. pres.push(item)
  309. })
  310. that.setData({
  311. pres: pres
  312. })
  313. },
  314. confirmPre() { //下一步用户确认
  315. if (this.data.mobile.length < 11 || this.data.bookNo.length <= 0) {
  316. wx.showToast({
  317. title: '请填写工单号和手机号',
  318. icon: 'none',
  319. duration: 2000
  320. });
  321. return
  322. }
  323. if (this.data.categoryId.id == 0) {
  324. wx.showToast({
  325. title: '请选择服务项和内容',
  326. icon: 'none',
  327. duration: 2000
  328. });
  329. return
  330. }
  331. this.setData({
  332. isConfirm: true
  333. })
  334. },
  335. savePre() { //保存预检单
  336. let that = this
  337. // if (that.data.isAgree == false) {
  338. // wx.showToast({
  339. // title: '请选择服务条款',
  340. // icon: 'none',
  341. // duration: 2000
  342. // });
  343. // return
  344. // }
  345. if (that.data.captcha.length == 0) {
  346. wx.showToast({
  347. title: '请输入验证码',
  348. icon: 'none',
  349. duration: 2000
  350. });
  351. return;
  352. }
  353. var items = [];
  354. that.data.pres.forEach(item => {
  355. var problem = [];
  356. if (item.problem.length > 0) {
  357. item.problem.forEach(element => {
  358. problem.push(element.id)
  359. })
  360. }
  361. var item = {
  362. categoryId: item.categoryId.id,
  363. positionId: item.positionId.id,
  364. deviceId: item.deviceId.id,
  365. picUrls: item.picUrls,
  366. problems: problem,
  367. memo: item.memo
  368. }
  369. items.push(item);
  370. })
  371. var pre = {
  372. mobile: that.data.mobile,
  373. bookNo: that.data.bookNo,
  374. captcha: that.data.captcha,
  375. items: items
  376. }
  377. wx.showLoading({
  378. title: '保存中',
  379. })
  380. if (this.data.isEdit) {
  381. // 编辑
  382. util.request(api.PreUpdate, pre, "POST").then(res => {
  383. wx.hideLoading();
  384. if (res.errno == 0) {
  385. wx.showModal({
  386. title: '提醒',
  387. content: '预检单编辑成功',
  388. showCancel: false, //是否显示取消按钮
  389. success: function (res) {
  390. wx.redirectTo({
  391. // url: '/pages/pre/list/preList',
  392. url: '/pages/new/ticketDetail/ticketDetail?bookTradeNo=' + that.data.bookNo,
  393. })
  394. }
  395. })
  396. } else
  397. wx.showToast({
  398. title: res.errmsg,
  399. icon: 'none',
  400. duration: 2000
  401. });
  402. }).catch(res => {
  403. wx.hideLoading();
  404. wx.showToast({
  405. title: res.errmsg,
  406. icon: 'none',
  407. duration: 2000
  408. });
  409. })
  410. } else {
  411. // 保存
  412. util.request(api.PreSave, pre, "POST").then(res => {
  413. wx.hideLoading();
  414. if (res.errno == 0) {
  415. wx.showModal({
  416. title: '提醒',
  417. content: '预检单提交成功',
  418. showCancel: false, //是否显示取消按钮
  419. success: function (res) {
  420. wx.redirectTo({
  421. // url: '/pages/pre/list/preList',
  422. url: '/pages/new/ticketDetail/ticketDetail?bookTradeNo=' + that.data.bookNo,
  423. })
  424. }
  425. })
  426. } else
  427. wx.showToast({
  428. title: res.errmsg,
  429. icon: 'none',
  430. duration: 2000
  431. });
  432. }).catch(res => {
  433. wx.hideLoading();
  434. wx.showToast({
  435. title: res.errmsg,
  436. icon: 'none',
  437. duration: 2000
  438. });
  439. })
  440. }
  441. },
  442. bindMemo(e) {
  443. this.setData({
  444. memo: e.detail.value
  445. })
  446. },
  447. bindBookNo(e) {
  448. this.setData({
  449. bookNo: e.detail.value
  450. })
  451. },
  452. bindMobile(e) {
  453. this.setData({
  454. mobile: e.detail.value
  455. })
  456. },
  457. bindcaptchaInput(e) {
  458. this.setData({
  459. captcha: e.detail.value
  460. })
  461. },
  462. getCaptcha() {
  463. console.info(this.data.mobile)
  464. if (this.data.mobile.length != 11 || this.data.mobile.startsWith("1") == false) {
  465. wx.showToast({
  466. title: '请输入正确手机号',
  467. duration: 3000
  468. });
  469. return;
  470. }
  471. if (this.data.mobile.indexOf("*")!=-1) {
  472. wx.showToast({
  473. title: '请输入正确手机号',
  474. duration: 3000
  475. });
  476. return;
  477. }
  478. let that=this;
  479. var items = [];
  480. that.data.pres.forEach(item => {
  481. var problem = [];
  482. if (item.problem.length > 0) {
  483. item.problem.forEach(element => {
  484. problem.push(element.id)
  485. })
  486. }
  487. var item = {
  488. categoryId: item.categoryId.id,
  489. categoryName:item.categoryId.name,
  490. positionId: item.positionId.id,
  491. positionName:item.positionId.name,
  492. deviceId: item.deviceId.id,
  493. deviceName:item.deviceId.name,
  494. picUrls: item.picUrls,
  495. problems: problem,
  496. memo: item.memo
  497. }
  498. items.push(item);
  499. })
  500. var pre = {
  501. mobile: that.data.mobile,//
  502. bookNo: that.data.bookNo,
  503. captcha: that.data.captcha,
  504. items: items,
  505. userName:that.data.userName,
  506. address:that.data.address
  507. }
  508. util.request(api.AuthCaptchaPre, pre, "POST").then(res => {
  509. if (res.errno == 0)
  510. wx.showToast({
  511. title: '验证码发送成功',
  512. duration: 3000
  513. });
  514. else
  515. wx.showToast({
  516. title: res.errmsg,
  517. duration: 3000
  518. });
  519. }).catch(res => {
  520. wx.showToast({
  521. title: res.errMsg,
  522. icon: 'error',
  523. duration: 3000
  524. });
  525. })
  526. },
  527. chooseImage: function (e) {
  528. if (this.data.files.length >= 5) {
  529. util.showErrorToast('只能上传五张图片')
  530. return false;
  531. }
  532. var that = this;
  533. wx.chooseImage({
  534. count: 1,
  535. sizeType: ['original', 'compressed'],
  536. sourceType: ['album', 'camera'],
  537. success: function (res) {
  538. that.setData({
  539. files: that.data.files.concat(res.tempFilePaths)
  540. });
  541. that.compressImage(res.tempFilePaths[0], (res1) => {
  542. that.upload(res1)
  543. })
  544. }
  545. })
  546. },
  547. upload: function (path) {
  548. var that = this;
  549. const uploadTask = wx.uploadFile({
  550. url: api.StorageUpload(),
  551. filePath: path, //res.tempFilePaths[0],
  552. name: 'file',
  553. success: function (res) {
  554. var _res = JSON.parse(res.data);
  555. if (_res.errno === 0) {
  556. var url = _res.data.fileUrl
  557. that.data.picUrls.push(url)
  558. that.setData({
  559. hasPicture: true,
  560. picUrls: that.data.picUrls
  561. })
  562. }
  563. },
  564. fail: function (e) {
  565. wx.showModal({
  566. title: '错误',
  567. content: '上传失败',
  568. showCancel: false
  569. })
  570. },
  571. })
  572. uploadTask.onProgressUpdate((res) => {
  573. console.log('上传进度', res.progress)
  574. console.log('已经上传的数据长度', res.totalBytesSent)
  575. console.log('预期需要上传的数据总长度', res.totalBytesExpectedToSend)
  576. })
  577. },
  578. previewImage: function (e) {
  579. wx.previewImage({
  580. current: e.currentTarget.id, // 当前显示图片的http链接
  581. urls: this.data.files // 需要预览的图片http链接列表
  582. })
  583. },
  584. compressImage(path, callback) {
  585. var that = this;
  586. //获取图片信息
  587. wx.getImageInfo({
  588. src: path,
  589. success: function (res) {
  590. //如果图片小于800宽度 不压缩
  591. if (res.width < 1200) {
  592. callback(res.path);
  593. return
  594. }
  595. var ctx = wx.createCanvasContext('photo_canvas'); // 创建画布
  596. var towidth = 1200; //设置canvas尺寸,按宽度500px的比例压缩
  597. var toheight = Math.trunc(1200 * res.height / res.width); //根据图片比例换算出图片高度
  598. that.setData({
  599. canvasHeight: toheight
  600. });
  601. ctx.drawImage(path, 0, 0, res.width, res.height, 0, 0, towidth, toheight);
  602. ctx.draw(false, function () {
  603. wx.canvasToTempFilePath({
  604. canvasId: 'photo_canvas',
  605. fileType: "jpg",
  606. quality: 0.8,
  607. success: function (res) {
  608. console.log(res.tempFilePath);
  609. callback(res.tempFilePath);
  610. }
  611. }, this)
  612. })
  613. }
  614. });
  615. },
  616. getPreDetail() {
  617. util.request(api.PreDetail, {
  618. bookNo: this.data.editBookNo
  619. }, "GET").then(res => {
  620. let list = res.data.items;
  621. let pres = [];
  622. list.forEach(item => {
  623. let obj = {
  624. deviceId: {
  625. id: item.deviceId,
  626. name: item.deviceName
  627. },
  628. categoryId: {
  629. id: item.categoryId,
  630. name: item.categoryName
  631. },
  632. positionId: {
  633. id: item.positionId ? item.positionId : 0,
  634. name: item.positionName ? item.positionName : ''
  635. },
  636. problem: item.problemsNamelist,
  637. memo: item.memo,
  638. files: item.picUrls,
  639. picUrls: item.picUrls
  640. }
  641. pres.push(obj);
  642. });
  643. this.setData({
  644. bookNo: res.data.bookNo,
  645. mobile: res.data.mobile,
  646. pres: pres,
  647. })
  648. })
  649. },
  650. /**
  651. * 生命周期函数--监听页面加载
  652. */
  653. maskPhone(phone) {
  654. if (!phone || phone.length !== 11) return phone; // 确保电话号码有效且长度为11位
  655. return phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2');
  656. },
  657. onLoad: function (options) {
  658. var userInfo = wx.getStorageSync('userInfo');
  659. if (userInfo)
  660. this.setData({
  661. userInfo: userInfo
  662. });
  663. let phone = options.phone;
  664. if (options.bookNo) {
  665. if (phone) {
  666. if(options.bookNo.indexOf("JZ")==-1){
  667. this.setData({
  668. tempMobile: this.maskPhone(phone),
  669. });
  670. }
  671. this.setData({
  672. bookNo: options.bookNo,
  673. mobile: phone
  674. });
  675. } else {
  676. this.setData({
  677. isEdit: true,
  678. editBookNo: options.bookNo
  679. });
  680. }
  681. }
  682. if (this.data.isEdit) {
  683. // 加载编辑预检单信息
  684. this.getPreDetail();
  685. }
  686. },
  687. /**
  688. * 生命周期函数--监听页面初次渲染完成
  689. */
  690. onReady: function () {
  691. },
  692. /**
  693. * 生命周期函数--监听页面显示
  694. */
  695. onShow: function () {
  696. this.getCategory();
  697. },
  698. /**
  699. * 生命周期函数--监听页面隐藏
  700. */
  701. onHide: function () {
  702. },
  703. /**
  704. * 生命周期函数--监听页面卸载
  705. */
  706. onUnload: function () {
  707. },
  708. /**
  709. * 页面相关事件处理函数--监听用户下拉动作
  710. */
  711. onPullDownRefresh: function () {
  712. },
  713. /**
  714. * 页面上拉触底事件的处理函数
  715. */
  716. onReachBottom: function () {
  717. },
  718. /**
  719. * 用户点击右上角分享
  720. */
  721. onShareAppMessage: function () {
  722. }
  723. })