index.js 10 KB

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