index.js 9.0 KB

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