index.js 17 KB

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