index.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. // pages/create-order/index.js
  2. import http from '../../utils/http'
  3. import util from '../../utils/util'
  4. import api from '../../utils/api'
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. tabs: ['设备租赁订单', '租赁设备添加'],
  11. tabIndex: 1,
  12. work_points: [],
  13. pointIndex: -1,
  14. id: -1,
  15. project: null,
  16. remark: '',
  17. devices: [],
  18. showAdd: false,
  19. device_types: [],
  20. rent_types: [],
  21. typeIndex: -1,
  22. nameIndex: -1,
  23. specIndex: -1,
  24. rentIndex: -1,
  25. selectName: '',
  26. selectSepc: '',
  27. selectRent: '',
  28. customName: false,
  29. customSpec: false,
  30. customRent: false,
  31. customNameVal: '',
  32. customNameSpecVal: '',
  33. customRentVal: '',
  34. device_name: '',
  35. device_quantity: '',
  36. device_price: '',
  37. showDate: false,
  38. start_date: '',
  39. end_date: '',
  40. order_id: '',
  41. // create/edit
  42. type: 'create',
  43. order: {},
  44. selectIndex: -1,
  45. // create/edit
  46. dialog_type: 'create',
  47. default_dates: [],
  48. device_total: 0,
  49. device_money: 0
  50. },
  51. /**
  52. * 生命周期函数--监听页面加载
  53. */
  54. onLoad: function (options) {
  55. var id = options.id ? options.id : 1
  56. var type = options.type ? options.type : 'create'
  57. var order_id = options.order_id ? options.order_id : ''
  58. var that = this
  59. this.setData({
  60. id,
  61. type,
  62. order_id
  63. })
  64. api.getProject(this)
  65. api.getByName(this, 'work-points/get', 'work_points', { project_id: id });
  66. api.getByName(this, 'devices/getThreeLevel', 'device_types');
  67. api.getByName(this, 'rent-types/get', 'rent_types');
  68. if (order_id) {
  69. api.getByName(this, 'orders/detail', 'order', { id: order_id }, function (res) {
  70. that.initData()
  71. });
  72. wx.setNavigationBarTitle({
  73. title: '修订订单',
  74. })
  75. }
  76. },
  77. closeshow() {
  78. this.setData({
  79. showAdd: false
  80. })
  81. },
  82. selectDevice: function (e) {
  83. var newIndex = e.currentTarget.dataset.index == this.data.selectIndex ? -1 : e.currentTarget.dataset.index
  84. this.setData({
  85. selectIndex: newIndex
  86. })
  87. },
  88. initData: function () {
  89. var order = this.data.order,
  90. work_points = this.data.work_points,
  91. pointIndex = this.data.pointIndex
  92. for (var i = 0; i < work_points.length; ++i) {
  93. if (work_points[i].id == order.work_point_id) {
  94. pointIndex = i;
  95. break;
  96. }
  97. }
  98. var devices = order.order_devices
  99. var local_devices = []
  100. for (var i = 0; i < devices.length; ++i) {
  101. var device = devices[i]
  102. local_devices.push({
  103. type_name: device.device_type ? device.device_type.name : '',
  104. type_id: device.device_type ? device.device_type.id : '',
  105. name: device.device_name ? device.device_name.name : '',
  106. spec: device.spec ? device.spec.name : '',
  107. rent: device.rent_type ? device.rent_type.name : '',
  108. quantity: device.quantity,
  109. price: device.price / 100,
  110. start_date: device.start_date,
  111. end_date: device.end_date
  112. })
  113. }
  114. this.setData({
  115. pointIndex,
  116. remark: order.remark,
  117. devices: local_devices
  118. })
  119. this.updateDeviceStat()
  120. },
  121. submit: function (e) {
  122. var type = e.currentTarget.dataset.type
  123. var is_draft = type == 'draft' ? 1 : 2
  124. var submit_type = this.data.type
  125. if (this.data.pointIndex < 0) {
  126. util.error('需求工点必填');
  127. return false;
  128. }
  129. if (this.data.devices.length <= 0) {
  130. util.error('请选择租赁设备');
  131. return false;
  132. }
  133. var work_point = this.data.work_points[this.data.pointIndex]
  134. var url = submit_type == 'create' ? 'orders/create' : 'orders/update'
  135. var that = this
  136. http({
  137. url: url,
  138. data: {
  139. id: this.data.order_id,
  140. project_id: this.data.id,
  141. work_point_id: work_point.id,
  142. remark: this.data.remark,
  143. devices: this.data.devices,
  144. is_draft: is_draft
  145. },
  146. success: function (res) {
  147. if (res.code == 0) {
  148. util.success('操作成功')
  149. setTimeout(function () {
  150. var url = '/pages/order/index?id=' + that.data.id
  151. if (is_draft == 1) {
  152. wx.navigateBack({
  153. delta: 0,
  154. })
  155. } else {
  156. wx.redirectTo({
  157. url: url,
  158. })
  159. }
  160. }, 1000)
  161. }
  162. }
  163. })
  164. },
  165. switchTab: function (e) {
  166. this.setData({
  167. tabIndex: e.currentTarget.dataset.index
  168. })
  169. },
  170. deleteDevice: function () {
  171. var devices = this.data.devices
  172. var index = this.data.selectIndex
  173. devices.splice(index, 1)
  174. this.setData({
  175. devices,
  176. selectIndex: -1
  177. })
  178. this.updateDeviceStat()
  179. },
  180. editDevice: function () {
  181. var devices = this.data.devices
  182. var index = this.data.selectIndex
  183. if (index < 0) return false
  184. var device = devices[index]
  185. var typeIndex = -1
  186. var device_types = this.data.device_types
  187. var default_dates = [device.start_date, device.end_date]
  188. var names = null
  189. var nameIndex = -1
  190. for (var i = 0; i < device_types.length; ++i) {
  191. if (device_types[i].id == device.type_id) {
  192. typeIndex = i;
  193. names = device_types[i].names
  194. break;
  195. }
  196. }
  197. var specs = null
  198. var specIndex = -1
  199. if (names) {
  200. for (var i = 0; i < names.length; ++i) {
  201. if (names[i].name == device.name) {
  202. specs = names[i].specs
  203. nameIndex = i;
  204. break;
  205. }
  206. }
  207. }
  208. if (specs) {
  209. for (var i = 0; i < specs.length; ++i) {
  210. if (specs[i].name == device.spec) {
  211. specIndex = i;
  212. break;
  213. }
  214. }
  215. }
  216. var rent_types = this.data.rent_types
  217. var rentIndex = -1;
  218. for (var i = 0; i < rent_types.length; ++i) {
  219. if (rent_types[i].name == device.rent) {
  220. rentIndex = i;
  221. break;
  222. }
  223. }
  224. this.setData({
  225. device_name: device.name,
  226. selectName: device.name,
  227. selectSpec: device.spec,
  228. selectRent: device.rent,
  229. typeIndex,
  230. nameIndex,
  231. specIndex,
  232. rentIndex,
  233. start_date: device.start_date,
  234. end_date: device.end_date,
  235. device_quantity: device.quantity,
  236. device_price: device.price,
  237. showAdd: true,
  238. dialog_type: 'edit',
  239. default_dates: default_dates
  240. })
  241. },
  242. getCustom: function (name) {
  243. var index = name + 'Index'
  244. var caseName = util.firstCase(name)
  245. var custom = 'custom' + caseName
  246. var customVal = custom + 'Val'
  247. var select = 'select' + caseName
  248. var data = this.data
  249. return data[custom] ? data[customVal] : (data[index] >= 0 ? data[select] : '')
  250. },
  251. stopClose() {
  252. this.setData({
  253. showAdd: true
  254. })
  255. },
  256. addDevice: function () {
  257. if (this.data.typeIndex < 0) {
  258. util.error('设备类型必填');
  259. this.stopClose()
  260. return false;
  261. }
  262. if (!this.getCustom('name')) {
  263. util.error('设备名称必填')
  264. this.stopClose()
  265. return false
  266. }
  267. if (!this.getCustom('spec')) {
  268. util.error('规格型号必填')
  269. this.stopClose()
  270. return false
  271. }
  272. if (!this.getCustom('rent')) {
  273. util.error('租赁方式必填')
  274. this.stopClose()
  275. return false
  276. }
  277. if (!this.data.device_quantity) {
  278. util.error('设备数量必填')
  279. this.stopClose()
  280. return false
  281. }
  282. if (!this.data.device_price) {
  283. util.error('设备单价必填')
  284. this.stopClose()
  285. return false
  286. }
  287. if (!this.data.start_date) {
  288. util.error('租赁时间必填')
  289. this.stopClose()
  290. return false
  291. }
  292. var devices = this.data.devices
  293. var type = this.data.device_types[this.data.typeIndex]
  294. var name = this.getCustom('name')
  295. var spec = this.getCustom('spec')
  296. var rent = this.getCustom('rent')
  297. var price = Math.round(this.data.device_price * 100) / 100
  298. var device = {
  299. type_name: type.name,
  300. type_id: type.id,
  301. name: name,
  302. spec: spec,
  303. rent: rent,
  304. quantity: this.data.device_quantity,
  305. price: price,
  306. start_date: this.data.start_date,
  307. end_date: this.data.end_date
  308. }
  309. var dialog_type = this.data.dialog_type
  310. if (dialog_type == 'create') {
  311. devices.push(device)
  312. } else {
  313. devices[this.data.selectIndex] = device
  314. }
  315. this.setData({
  316. devices,
  317. typeIndex: -1,
  318. nameIndex: -1,
  319. specIndex: -1,
  320. rentIndex: -1
  321. })
  322. this.setData({
  323. showAdd: false
  324. })
  325. this.updateDeviceStat()
  326. },
  327. onClose: function (e) {
  328. if (e.detail == 'confirm') return false;
  329. return true
  330. },
  331. updateDeviceStat() {
  332. var devices = this.data.devices
  333. var device_total = devices.length
  334. var device_money = 0
  335. for (var i = 0; i < devices.length; ++i) {
  336. device_money = device_money + Math.round(parseFloat(devices[i].price) * parseInt(devices[i].quantity) * 100)
  337. }
  338. device_money = device_money / 100;
  339. this.setData({
  340. device_total,
  341. device_money
  342. })
  343. },
  344. onChange: function (e) {
  345. var name = e.currentTarget.dataset.name
  346. var val = e.detail.value
  347. if (['customSpec', 'customName', 'customRent'].indexOf(name) != -1) {
  348. val = e.detail;
  349. }
  350. this.setData({
  351. [name]: val
  352. })
  353. if (name == 'device_price') {
  354. let price
  355. if (/^(\d?)+(\.\d{0,2})?$/.test(val)) { //正则验证,提现金额小数点后不能大于两位数字
  356. price = val;
  357. } else {
  358. price = val.substring(0, val.length - 1);
  359. }
  360. this.setData({
  361. device_price: price
  362. })
  363. }
  364. if (name == 'customName' && val) {
  365. this.setData({
  366. customSpec: true,
  367. })
  368. }
  369. if (name == 'typeIndex') {
  370. this.setData({
  371. nameIndex: -1,
  372. specIndex: -1
  373. })
  374. }
  375. if (name == 'nameIndex') {
  376. var device_types = this.data.device_types
  377. var typeIndex = this.data.typeIndex
  378. var nameIndex = this.data.nameIndex
  379. var selectName = device_types[typeIndex].names[nameIndex].name
  380. this.setData({
  381. selectName: selectName,
  382. specIndex: -1
  383. })
  384. }
  385. if (name == 'specIndex') {
  386. var device_types = this.data.device_types
  387. var typeIndex = this.data.typeIndex
  388. var nameIndex = this.data.nameIndex
  389. var specIndex = this.data.specIndex
  390. var selectSpec = device_types[typeIndex].names[nameIndex].specs[specIndex].name
  391. this.setData({
  392. selectSpec: selectSpec
  393. })
  394. }
  395. if (name == 'rentIndex') {
  396. var rent_types = this.data.rent_types
  397. var rentIndex = this.data.rentIndex
  398. var selectRent = rent_types[rentIndex].name
  399. this.setData({
  400. selectRent: selectRent
  401. })
  402. }
  403. },
  404. switchShowAdd: function (e) {
  405. var show = e.currentTarget.dataset.show
  406. if (show) {
  407. this.setData({
  408. device_name: '',
  409. typeIndex: -1,
  410. start_date: '',
  411. end_date: '',
  412. device_quantity: '',
  413. device_price: '',
  414. default_dates: []
  415. })
  416. }
  417. this.setData({
  418. showAdd: show,
  419. dialog_type: 'create'
  420. })
  421. },
  422. switchShowDate: function (e) {
  423. this.setData({
  424. showDate: e.currentTarget.dataset.show
  425. })
  426. },
  427. confirmDate: function (e) {
  428. this.switchShowDate(e)
  429. var [start_date, end_date] = e.detail;
  430. start_date = util.formatDate(start_date)
  431. end_date = util.formatDate(end_date)
  432. this.setData({
  433. start_date,
  434. end_date
  435. })
  436. },
  437. /**
  438. * 生命周期函数--监听页面初次渲染完成
  439. */
  440. onReady: function () {
  441. },
  442. /**
  443. * 生命周期函数--监听页面显示
  444. */
  445. onShow: function () {
  446. },
  447. /**
  448. * 生命周期函数--监听页面隐藏
  449. */
  450. onHide: function () {
  451. },
  452. /**
  453. * 生命周期函数--监听页面卸载
  454. */
  455. onUnload: function () {
  456. },
  457. /**
  458. * 页面相关事件处理函数--监听用户下拉动作
  459. */
  460. onPullDownRefresh: function () {
  461. },
  462. /**
  463. * 页面上拉触底事件的处理函数
  464. */
  465. onReachBottom: function () {
  466. },
  467. /**
  468. * 用户点击右上角分享
  469. */
  470. onShareAppMessage: function () {
  471. }
  472. })