index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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', {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. 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. selectName: device.name,
  222. selectSpec: device.spec,
  223. selectRent: device.rent,
  224. typeIndex,
  225. nameIndex,
  226. specIndex,
  227. rentIndex,
  228. start_date: device.start_date,
  229. end_date: device.end_date,
  230. device_quantity: device.quantity,
  231. device_price: device.price,
  232. showAdd: true,
  233. dialog_type: 'edit',
  234. default_dates: default_dates
  235. })
  236. },
  237. getCustom: function(name) {
  238. var index = name + 'Index'
  239. var caseName = util.firstCase(name)
  240. var custom = 'custom' + caseName
  241. var customVal = custom + 'Val'
  242. var select = 'select' + caseName
  243. var data = this.data
  244. return data[custom] ? data[customVal] : (data[index] >= 0 ? data[select] : '')
  245. },
  246. stopClose() {
  247. this.setData({
  248. showAdd: true
  249. })
  250. },
  251. addDevice: function() {
  252. if(this.data.typeIndex < 0) {
  253. util.error('设备类型必填');
  254. this.stopClose()
  255. return false;
  256. }
  257. if(!this.getCustom('name')) {
  258. util.error('设备名称必填')
  259. this.stopClose()
  260. return false
  261. }
  262. if(!this.getCustom('spec')) {
  263. util.error('规格型号必填')
  264. this.stopClose()
  265. return false
  266. }
  267. if(!this.getCustom('rent')) {
  268. util.error('租赁方式必填')
  269. this.stopClose()
  270. return false
  271. }
  272. if(!this.data.device_quantity) {
  273. util.error('设备数量必填')
  274. this.stopClose()
  275. return false
  276. }
  277. if(!this.data.device_price) {
  278. util.error('设备单价必填')
  279. this.stopClose()
  280. return false
  281. }
  282. if(!this.data.start_date) {
  283. util.error('租赁时间必填')
  284. this.stopClose()
  285. return false
  286. }
  287. var devices = this.data.devices
  288. var type = this.data.device_types[this.data.typeIndex]
  289. var name = this.getCustom('name')
  290. var spec = this.getCustom('spec')
  291. var rent = this.getCustom('rent')
  292. var price = Math.round(this.data.device_price * 100) / 100
  293. var device = {
  294. type_name: type.name,
  295. type_id: type.id,
  296. name: name,
  297. spec: spec,
  298. rent: rent,
  299. quantity: this.data.device_quantity,
  300. price: price,
  301. start_date: this.data.start_date,
  302. end_date: this.data.end_date
  303. }
  304. var dialog_type = this.data.dialog_type
  305. if(dialog_type == 'create') {
  306. devices.push(device)
  307. } else {
  308. devices[this.data.selectIndex] = device
  309. }
  310. this.setData({
  311. devices,
  312. typeIndex: -1,
  313. nameIndex: -1,
  314. specIndex: -1,
  315. rentIndex: -1
  316. })
  317. this.updateDeviceStat()
  318. },
  319. onClose: function(e) {
  320. if(e.detail == 'confirm') return false;
  321. return true
  322. },
  323. updateDeviceStat() {
  324. var devices = this.data.devices
  325. var device_total = devices.length
  326. var device_money = 0
  327. for(var i = 0; i < devices.length; ++i) {
  328. device_money = device_money + Math.round(parseFloat(devices[i].price) * parseInt(devices[i].quantity) * 100)
  329. }
  330. device_money = device_money / 100;
  331. this.setData({
  332. device_total,
  333. device_money
  334. })
  335. },
  336. onChange: function(e) {
  337. var name = e.currentTarget.dataset.name
  338. var val = e.detail.value
  339. if(['customSpec', 'customName', 'customRent'].indexOf(name) != -1) {
  340. val = e.detail;
  341. }
  342. this.setData({
  343. [name]: val
  344. })
  345. if(name == 'customName' && val) {
  346. this.setData({
  347. customSpec: true,
  348. })
  349. }
  350. if(name == 'typeIndex') {
  351. this.setData({
  352. nameIndex: -1,
  353. specIndex: -1
  354. })
  355. }
  356. if(name == 'nameIndex') {
  357. var device_types = this.data.device_types
  358. var typeIndex = this.data.typeIndex
  359. var nameIndex = this.data.nameIndex
  360. var selectName = device_types[typeIndex].names[nameIndex].name
  361. this.setData({
  362. selectName: selectName,
  363. specIndex: -1
  364. })
  365. }
  366. if(name == 'specIndex') {
  367. var device_types = this.data.device_types
  368. var typeIndex = this.data.typeIndex
  369. var nameIndex = this.data.nameIndex
  370. var specIndex = this.data.specIndex
  371. var selectSpec = device_types[typeIndex].names[nameIndex].specs[specIndex].name
  372. this.setData({
  373. selectSpec: selectSpec
  374. })
  375. }
  376. if(name == 'rentIndex') {
  377. var rent_types = this.data.rent_types
  378. var rentIndex = this.data.rentIndex
  379. var selectRent = rent_types[rentIndex].name
  380. this.setData({
  381. selectRent: selectRent
  382. })
  383. }
  384. },
  385. switchShowAdd: function(e) {
  386. var show = e.currentTarget.dataset.show
  387. if(show) {
  388. this.setData({
  389. device_name: '',
  390. typeIndex: -1,
  391. start_date: '',
  392. end_date: '',
  393. device_quantity: '',
  394. device_price: '',
  395. default_dates: []
  396. })
  397. }
  398. this.setData({
  399. showAdd: show,
  400. dialog_type: 'create'
  401. })
  402. },
  403. switchShowDate: function(e) {
  404. this.setData({
  405. showDate: e.currentTarget.dataset.show
  406. })
  407. },
  408. confirmDate: function(e) {
  409. this.switchShowDate(e)
  410. var [start_date, end_date] = e.detail;
  411. start_date = util.formatDate(start_date)
  412. end_date = util.formatDate(end_date)
  413. this.setData({
  414. start_date,
  415. end_date
  416. })
  417. },
  418. /**
  419. * 生命周期函数--监听页面初次渲染完成
  420. */
  421. onReady: function () {
  422. },
  423. /**
  424. * 生命周期函数--监听页面显示
  425. */
  426. onShow: function () {
  427. },
  428. /**
  429. * 生命周期函数--监听页面隐藏
  430. */
  431. onHide: function () {
  432. },
  433. /**
  434. * 生命周期函数--监听页面卸载
  435. */
  436. onUnload: function () {
  437. },
  438. /**
  439. * 页面相关事件处理函数--监听用户下拉动作
  440. */
  441. onPullDownRefresh: function () {
  442. },
  443. /**
  444. * 页面上拉触底事件的处理函数
  445. */
  446. onReachBottom: function () {
  447. },
  448. /**
  449. * 用户点击右上角分享
  450. */
  451. onShareAppMessage: function () {
  452. }
  453. })