index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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: 0,
  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');
  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. selectDevice: function(e) {
  78. var newIndex = e.currentTarget.dataset.index == this.data.selectIndex ? -1 : e.currentTarget.dataset.index
  79. this.setData({
  80. selectIndex: newIndex
  81. })
  82. },
  83. initData: function() {
  84. var order = this.data.order,
  85. work_points = this.data.work_points,
  86. pointIndex = this.data.pointIndex
  87. for(var i = 0; i < work_points.length; ++i) {
  88. if(work_points[i].id == order.work_point_id) {
  89. pointIndex = i;
  90. break;
  91. }
  92. }
  93. var devices = order.order_devices
  94. var local_devices = []
  95. for(var i = 0; i < devices.length; ++i) {
  96. var device = devices[i]
  97. local_devices.push({
  98. type_name: device.device_type ? device.device_type.name : '',
  99. type_id: device.device_type ? device.device_type.id : '',
  100. name: device.device_name ? device.device_name.name : '',
  101. spec: device.spec ? device.spec.name : '',
  102. rent: device.rent_type ? device.rent_type.name : '',
  103. quantity: device.quantity,
  104. price: device.price / 100,
  105. start_date: device.start_date,
  106. end_date: device.end_date
  107. })
  108. }
  109. this.setData({
  110. pointIndex,
  111. remark: order.remark,
  112. devices: local_devices
  113. })
  114. this.updateDeviceStat()
  115. },
  116. submit: function(e) {
  117. var type = e.currentTarget.dataset.type
  118. var is_draft = type == 'draft' ? 1 : 2
  119. var submit_type = this.data.type
  120. if(this.data.pointIndex < 0) {
  121. util.error('需求工点必填');
  122. return false;
  123. }
  124. if(this.data.devices.length <= 0) {
  125. util.error('请选择租赁设备');
  126. return false;
  127. }
  128. var work_point = this.data.work_points[this.data.pointIndex]
  129. var url = submit_type == 'create' ? 'orders/create' : 'orders/update'
  130. var that = this
  131. http({
  132. url: url,
  133. data: {
  134. id: this.data.order_id,
  135. project_id: this.data.id,
  136. work_point_id: work_point.id,
  137. remark: this.data.remark,
  138. devices: this.data.devices,
  139. is_draft: is_draft
  140. },
  141. success: function(res) {
  142. if(res.code == 0) {
  143. util.success('操作成功')
  144. setTimeout(function() {
  145. var url = '/pages/order/index?id=' + that.data.id
  146. if(is_draft == 1) {
  147. wx.navigateBack({
  148. delta: 0,
  149. })
  150. } else {
  151. wx.redirectTo({
  152. url: url,
  153. })
  154. }
  155. }, 1000)
  156. }
  157. }
  158. })
  159. },
  160. switchTab: function(e) {
  161. this.setData({
  162. tabIndex: e.currentTarget.dataset.index
  163. })
  164. },
  165. deleteDevice: function() {
  166. var devices = this.data.devices
  167. var index = this.data.selectIndex
  168. devices.splice(index, 1)
  169. this.setData({
  170. devices,
  171. selectIndex: -1
  172. })
  173. this.updateDeviceStat()
  174. },
  175. editDevice: function() {
  176. var devices = this.data.devices
  177. var index = this.data.selectIndex
  178. if(index < 0) return false
  179. var device = devices[index]
  180. var typeIndex = -1
  181. var device_types = this.data.device_types
  182. var default_dates = [device.start_date, device.end_date]
  183. var names = null
  184. var nameIndex = -1
  185. for(var i = 0; i < device_types.length; ++i) {
  186. if(device_types[i].id == device.type_id) {
  187. typeIndex = i;
  188. names = device_types[i].names
  189. break;
  190. }
  191. }
  192. var specs = null
  193. var specIndex = -1
  194. if(names) {
  195. for(var i = 0; i < names.length; ++i) {
  196. if(names[i].name == device.name) {
  197. specs = names[i].specs
  198. nameIndex = i;
  199. break;
  200. }
  201. }
  202. }
  203. if(specs) {
  204. for(var i = 0; i < specs.length; ++i) {
  205. if(specs[i].name == device.spec) {
  206. specIndex = i;
  207. break;
  208. }
  209. }
  210. }
  211. var rent_types = this.data.rent_types
  212. var rentIndex = -1;
  213. for(var i = 0; i < rent_types.length; ++i) {
  214. if(rent_types[i].name == device.rent) {
  215. rentIndex = i;
  216. break;
  217. }
  218. }
  219. this.setData({
  220. device_name: device.name,
  221. typeIndex,
  222. nameIndex,
  223. specIndex,
  224. rentIndex,
  225. start_date: device.start_date,
  226. end_date: device.end_date,
  227. device_quantity: device.quantity,
  228. device_price: device.price,
  229. showAdd: true,
  230. dialog_type: 'edit',
  231. default_dates: default_dates
  232. })
  233. },
  234. getCustom: function(name) {
  235. var index = name + 'Index'
  236. var caseName = util.firstCase(name)
  237. var custom = 'custom' + caseName
  238. var customVal = custom + 'Val'
  239. var select = 'select' + caseName
  240. var data = this.data
  241. return data[custom] ? data[customVal] : (data[index] >= 0 ? data[select] : '')
  242. },
  243. stopClose() {
  244. this.setData({
  245. showAdd: true
  246. })
  247. },
  248. addDevice: function() {
  249. if(this.data.typeIndex < 0) {
  250. util.error('设备类型必填');
  251. this.stopClose()
  252. return false;
  253. }
  254. if(!this.getCustom('name')) {
  255. util.error('设备名称必填')
  256. this.stopClose()
  257. return false
  258. }
  259. if(!this.getCustom('spec')) {
  260. util.error('规格型号必填')
  261. this.stopClose()
  262. return false
  263. }
  264. if(!this.getCustom('rent')) {
  265. util.error('租赁方式必填')
  266. this.stopClose()
  267. return false
  268. }
  269. if(!this.data.device_quantity) {
  270. util.error('设备数量必填')
  271. this.stopClose()
  272. return false
  273. }
  274. if(!this.data.device_price) {
  275. util.error('设备单价必填')
  276. this.stopClose()
  277. return false
  278. }
  279. if(!this.data.start_date) {
  280. util.error('租赁时间必填')
  281. this.stopClose()
  282. return false
  283. }
  284. var devices = this.data.devices
  285. var type = this.data.device_types[this.data.typeIndex]
  286. var name = this.getCustom('name')
  287. var spec = this.getCustom('spec')
  288. var rent = this.getCustom('rent')
  289. var device = {
  290. type_name: type.name,
  291. type_id: type.id,
  292. name: name,
  293. spec: spec,
  294. rent: rent,
  295. quantity: this.data.device_quantity,
  296. price: this.data.device_price,
  297. start_date: this.data.start_date,
  298. end_date: this.data.end_date
  299. }
  300. var dialog_type = this.data.dialog_type
  301. if(dialog_type == 'create') {
  302. devices.push(device)
  303. } else {
  304. devices[this.data.selectIndex] = device
  305. }
  306. this.setData({
  307. devices,
  308. typeIndex: -1,
  309. nameIndex: -1,
  310. specIndex: -1,
  311. rentIndex: -1
  312. })
  313. this.updateDeviceStat()
  314. },
  315. onClose: function(e) {
  316. if(e.detail == 'confirm') return false;
  317. return true
  318. },
  319. updateDeviceStat() {
  320. var devices = this.data.devices
  321. var device_total = devices.length
  322. var device_money = 0
  323. for(var i = 0; i < devices.length; ++i) {
  324. device_money = device_money + Math.round(parseFloat(devices[i].price) * parseInt(devices[i].quantity) * 100)
  325. }
  326. device_money = device_money / 100;
  327. this.setData({
  328. device_total,
  329. device_money
  330. })
  331. },
  332. onChange: function(e) {
  333. var name = e.currentTarget.dataset.name
  334. var val = e.detail.value
  335. if(['customSpec', 'customName', 'customRent'].indexOf(name) != -1) {
  336. val = e.detail;
  337. }
  338. this.setData({
  339. [name]: val
  340. })
  341. if(name == 'customName' && val) {
  342. this.setData({
  343. customSpec: true,
  344. })
  345. }
  346. if(name == 'typeIndex') {
  347. this.setData({
  348. nameIndex: -1,
  349. specIndex: -1
  350. })
  351. }
  352. if(name == 'nameIndex') {
  353. var device_types = this.data.device_types
  354. var typeIndex = this.data.typeIndex
  355. var nameIndex = this.data.nameIndex
  356. var selectName = device_types[typeIndex].names[nameIndex].name
  357. this.setData({
  358. selectName: selectName,
  359. specIndex: -1
  360. })
  361. }
  362. if(name == 'specIndex') {
  363. var device_types = this.data.device_types
  364. var typeIndex = this.data.typeIndex
  365. var nameIndex = this.data.nameIndex
  366. var specIndex = this.data.specIndex
  367. var selectSpec = device_types[typeIndex].names[nameIndex].specs[specIndex].name
  368. this.setData({
  369. selectSpec: selectSpec
  370. })
  371. }
  372. if(name == 'rentIndex') {
  373. var rent_types = this.data.rent_types
  374. var rentIndex = this.data.rentIndex
  375. var selectRent = rent_types[rentIndex].name
  376. this.setData({
  377. selectRent: selectRent
  378. })
  379. }
  380. },
  381. switchShowAdd: function(e) {
  382. var show = e.currentTarget.dataset.show
  383. if(show) {
  384. this.setData({
  385. device_name: '',
  386. typeIndex: -1,
  387. start_date: '',
  388. end_date: '',
  389. device_quantity: '',
  390. device_price: '',
  391. default_dates: []
  392. })
  393. }
  394. this.setData({
  395. showAdd: show,
  396. dialog_type: 'create'
  397. })
  398. },
  399. switchShowDate: function(e) {
  400. this.setData({
  401. showDate: e.currentTarget.dataset.show
  402. })
  403. },
  404. confirmDate: function(e) {
  405. this.switchShowDate(e)
  406. var [start_date, end_date] = e.detail;
  407. start_date = util.formatDate(start_date)
  408. end_date = util.formatDate(end_date)
  409. this.setData({
  410. start_date,
  411. end_date
  412. })
  413. },
  414. /**
  415. * 生命周期函数--监听页面初次渲染完成
  416. */
  417. onReady: function () {
  418. },
  419. /**
  420. * 生命周期函数--监听页面显示
  421. */
  422. onShow: function () {
  423. },
  424. /**
  425. * 生命周期函数--监听页面隐藏
  426. */
  427. onHide: function () {
  428. },
  429. /**
  430. * 生命周期函数--监听页面卸载
  431. */
  432. onUnload: function () {
  433. },
  434. /**
  435. * 页面相关事件处理函数--监听用户下拉动作
  436. */
  437. onPullDownRefresh: function () {
  438. },
  439. /**
  440. * 页面上拉触底事件的处理函数
  441. */
  442. onReachBottom: function () {
  443. },
  444. /**
  445. * 用户点击右上角分享
  446. */
  447. onShareAppMessage: function () {
  448. }
  449. })