problemAdd.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. const util = require("../../../utils/util");
  2. const api = require('../../../api/api.js');
  3. var app = getApp();
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. colorService: '#85868A',
  10. colorChange: '#85868A',
  11. name: '',
  12. problem: {
  13. id: 0,
  14. name: '',
  15. mobile: '',
  16. remark: '',
  17. picUrls: [],
  18. bookTime: "选择服务日期-时间",
  19. changeTime: '选择服务日期-时间',
  20. city: '',
  21. bookTradeNo: ''
  22. },
  23. problemInfo: {},
  24. files: [],
  25. date: [
  26. ["2021-02-01", "2021-02-02", "2021-02-03"],
  27. ["2021-02-04", "2021-02-05", "2021-02-06"]
  28. ],
  29. multiIndex: [0, 2, 0],
  30. multiChangeIndex: 0,
  31. oneHour: ["08:30-09:30", "10:45-11:45", "13:00-14:00", "14:45-15:45", "17:30-18:30", "17:30-19:30", "20:00-21:00"],
  32. twoHour: ["08:00-10:00", "13:00-15:00", "16:00-18:00"],
  33. threeHour: ["08:00-11:00", "12:00-15:00", "13:00-16:00", "17:00-20:00", "17:30-20:30"],
  34. fourHour: ["08:00-12:00", "13:45-17:45", "14:00-18:00", "17:00-21:00", "18:00-22:00"],
  35. sixHour: ["08:00-14:00", "13:00-19:00"],
  36. },
  37. /**
  38. * 生命周期函数--监听页面加载
  39. */
  40. onLoad: function (options) {
  41. console.info(options)
  42. let that = this
  43. that.setData({
  44. 'problem.id': options.id,
  45. name: options.name,
  46. })
  47. that.initServiceDate();
  48. that.getProblemInfo(options.id);
  49. if (options.bookTradeNo) {
  50. that.setData({
  51. 'problem.bookTradeNo': options.bookTradeNo
  52. })
  53. that.getBookTradeInfo();
  54. }
  55. },
  56. getProblemInfo(id) {
  57. let that = this
  58. wx.showLoading();
  59. util.request(api.ProblemInfo, {
  60. id: id
  61. }).then(function (res) {
  62. wx.hideLoading();
  63. console.info(res.data)
  64. if (res.errno === 0) {
  65. that.setData({
  66. problemInfo: res.data
  67. });
  68. }
  69. });
  70. },
  71. getBookTradeInfo() {
  72. let that = this
  73. if (that.data.problem.bookTradeNo) {
  74. util.request(api.BookHmTradeInfo, {
  75. bookTradeNo: that.data.problem.bookTradeNo
  76. }).then(function (res) {
  77. if (res.errno === 0) {
  78. var p = that.data.problem;
  79. p.name = res.data.userName;
  80. p.mobile = res.data.userPhone;
  81. p.bookTime = res.data.bookTime;
  82. p.changeTime = '选择服务日期-时间',
  83. that.setData({
  84. problem: p,
  85. colorService: '#333'
  86. });
  87. }
  88. });
  89. }
  90. },
  91. initServiceDate() { //构造服务日期及时间段
  92. var date = [];
  93. var now = util.formatDate(new Date());
  94. for (var i = 0; i < 7; i++) {
  95. date.push(util.mathChangeDate(now, '+', i, true));
  96. }
  97. var data = [];
  98. data.push(date, ['1小时', '2小时', '3小时', '4小时', '6小时'], this.data.threeHour);
  99. console.info(data)
  100. this.setData({
  101. date: data
  102. })
  103. },
  104. /**
  105. * 生命周期函数--监听页面初次渲染完成
  106. */
  107. onReady: function () {
  108. },
  109. //服务时间段
  110. bindMultiPickerChange(e) {
  111. console.info(e)
  112. var date = this.data.date[0][e.detail.value[0]]
  113. var time = this.data.date[2][e.detail.value[2]]
  114. this.setData({
  115. 'problem.bookTime': date + ' ' + time,
  116. colorService: '#333'
  117. })
  118. },
  119. bindMultiPickerColumnChange(e) {
  120. console.log('修改的列为', e.detail.column, ',值为', e.detail.value);
  121. let that = this
  122. var data = that.data.date;
  123. if (e.detail.column == 1) { //修改时间段列
  124. switch (e.detail.value) {
  125. case 0: //"1小时"
  126. data[2] = that.data.oneHour;
  127. break;
  128. case 1: //"2小时"
  129. data[2] = that.data.twoHour;
  130. break;
  131. case 2: //"3小时"
  132. data[2] = that.data.threeHour;
  133. break;
  134. case 3: //"4小时"
  135. data[2] = that.data.fourHour;
  136. break;
  137. case 4: //"6小时"
  138. data[2] = that.data.sixHour;
  139. break;
  140. }
  141. this.setData({
  142. date: data
  143. })
  144. }
  145. },
  146. bindNeedFollowed(e){//是否需要客服介入
  147. console.info(e.detail.value)
  148. this.setData({
  149. 'problem.isNeedFollowed': e.detail.value
  150. })
  151. },
  152. //期望服务时间
  153. bindChangePickerChange(e) {
  154. var date = this.data.date[0][e.detail.value[0]]
  155. var time = this.data.date[2][e.detail.value[2]]
  156. this.setData({
  157. 'problem.changeTime': date + ' ' + time,
  158. colorChange: '#333'
  159. })
  160. },
  161. /**
  162. * 生命周期函数--监听页面显示
  163. */
  164. onShow: function () {
  165. this.setData({
  166. 'problem.city': app.globalData.city
  167. })
  168. },
  169. saveProblem() {
  170. console.info("handle saveProblem")
  171. let that = this
  172. if (that.data.problem.mobile.length != 11 || that.data.problem.mobile.startsWith("1") == false) {
  173. wx.showToast({
  174. title: '请输入正确手机号',
  175. duration: 3000,
  176. icon:'none'
  177. });
  178. return;
  179. }
  180. if (that.data.problem.bookTime == '选择服务日期-时间') {
  181. wx.showToast({
  182. title: '请选择服务时间',
  183. duration: 3000,
  184. icon:'none'
  185. });
  186. return;
  187. }
  188. if (that.data.problemInfo.isChangeTime && that.data.problem.changeTime == '选择服务日期-时间') {
  189. wx.showToast({
  190. title: '请选择期望改约的服务时间',
  191. duration: 3000,
  192. icon:'none'
  193. });
  194. return;
  195. }
  196. if (that.data.problemInfo.isNeedPic && that.data.problem.picUrls.length == 0) {
  197. wx.showToast({
  198. title: '请上传照片',
  199. duration: 3000,
  200. icon:'none'
  201. });
  202. return;
  203. }
  204. wx.showModal({
  205. title: '提示',
  206. content: '确定要报备此问题吗?',
  207. success: function (sm) {
  208. if (sm.confirm) {
  209. wx.showLoading();
  210. util.request(api.ProblemAdd, that.data.problem, "POST").then(function (res) {
  211. wx.hideLoading();
  212. let msg=typeof that.data.problemInfo.alertMsg=='undefined'? '问题报备成功':that.data.problemInfo.alertMsg;
  213. if (res.errno === 0) {
  214. wx.showModal({
  215. title: '报备成功',
  216. content: msg,
  217. showCancel: false,//是否显示取消按钮
  218. confirmText:"确定",//默认是“确定”
  219. confirmColor: 'skyblue',//确定文字的颜色
  220. success: function (res) {
  221. if (res.cancel) {
  222. //点击取消,默认隐藏弹框
  223. } else {
  224. //点击确定
  225. // wx.switchTab({
  226. // url: '/pages/problem/index',
  227. // })
  228. // wx.navigateTo({
  229. // url: '/pages/upgrade/ticketDetail/ticketDetail?bookTradeNo=' + that.data.problem.bookTradeNo,
  230. // })
  231. wx.navigateBack({
  232. delta: 2,
  233. })
  234. }
  235. },
  236. })
  237. } else {
  238. wx.showToast({
  239. title: res.errmsg,
  240. duration: 3000
  241. });
  242. }
  243. });
  244. }
  245. }
  246. })
  247. },
  248. bindRemark(e) {
  249. this.setData({
  250. 'problem.remark': e.detail.value
  251. })
  252. },
  253. bindName(e) {
  254. this.setData({
  255. 'problem.name': e.detail.value
  256. })
  257. },
  258. bindMobile(e) {
  259. this.setData({
  260. 'problem.mobile': e.detail.value
  261. })
  262. },
  263. chooseImage: function (e) {
  264. if (this.data.files.length >= 5) {
  265. util.showErrorToast('只能上传五张图片')
  266. return false;
  267. }
  268. var that = this;
  269. wx.chooseImage({
  270. count: 1,
  271. sizeType: ['original', 'compressed'],
  272. sourceType: ['album', 'camera'],
  273. success: function (res) {
  274. that.setData({
  275. files: that.data.files.concat(res.tempFilePaths)
  276. });
  277. console.info(res.tempFilePaths[0])
  278. //that.compressImage(res.tempFilePaths[0], (res1) => {
  279. that.upload(res)
  280. //})
  281. }
  282. })
  283. },
  284. upload: function (res) {
  285. var that = this;
  286. const uploadTask = wx.uploadFile({
  287. url: api.StorageUpload(),
  288. filePath: res.tempFilePaths[0],
  289. name: 'file',
  290. success: function (res) {
  291. var _res = JSON.parse(res.data);
  292. if (_res.errno === 0) {
  293. var url = _res.data.fileUrl
  294. var pics = that.data.problem.picUrls;
  295. pics.push(url);
  296. that.setData({
  297. hasPicture: true,
  298. 'problem.picUrls': pics
  299. })
  300. }
  301. },
  302. fail: function (e) {
  303. wx.showModal({
  304. title: '错误',
  305. content: '上传失败',
  306. showCancel: false
  307. })
  308. },
  309. })
  310. uploadTask.onProgressUpdate((res) => {
  311. console.log('上传进度', res.progress)
  312. console.log('已经上传的数据长度', res.totalBytesSent)
  313. console.log('预期需要上传的数据总长度', res.totalBytesExpectedToSend)
  314. })
  315. },
  316. previewImage: function (e) {
  317. wx.previewImage({
  318. current: e.currentTarget.id, // 当前显示图片的http链接
  319. urls: this.data.files // 需要预览的图片http链接列表
  320. })
  321. },
  322. compressImage(path, callback) {
  323. var that = this;
  324. //获取图片信息
  325. wx.getImageInfo({
  326. src: path,
  327. success: function (res) {
  328. //如果图片小于1200宽度 不压缩
  329. if (res.width < 1200) {
  330. callback(res.path);
  331. return
  332. }
  333. console.info("开始压缩")
  334. var ctx = wx.createCanvasContext('photo_canvas'); // 创建画布
  335. var towidth = 1200; //设置canvas尺寸,按宽度500px的比例压缩
  336. var toheight = Math.trunc(1200 * res.height / res.width); //根据图片比例换算出图片高度
  337. that.setData({
  338. canvasHeight: toheight
  339. });
  340. console.info("开始压缩2")
  341. ctx.drawImage(path, 0, 0, res.width, res.height, 0, 0, towidth, toheight);
  342. console.info("开始压缩3")
  343. ctx.draw(false, function () {
  344. wx.canvasToTempFilePath({
  345. canvasId: 'photo_canvas',
  346. fileType: "jpg",
  347. quality: 0.8,
  348. success: function (res) {
  349. console.log(res.tempFilePath);
  350. callback(res.tempFilePath);
  351. },
  352. fail: function (res) {
  353. console.info(res)
  354. }
  355. }, this)
  356. })
  357. console.info("开始压缩4")
  358. }
  359. });
  360. },
  361. /**
  362. * 生命周期函数--监听页面隐藏
  363. */
  364. onHide: function () {
  365. },
  366. /**
  367. * 生命周期函数--监听页面卸载
  368. */
  369. onUnload: function () {
  370. },
  371. /**
  372. * 页面相关事件处理函数--监听用户下拉动作
  373. */
  374. onPullDownRefresh: function () {
  375. },
  376. /**
  377. * 页面上拉触底事件的处理函数
  378. */
  379. onReachBottom: function () {
  380. },
  381. /**
  382. * 用户点击右上角分享
  383. */
  384. onShareAppMessage: function () {
  385. }
  386. })