diypreviewing.js 17 KB

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