index.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. typeIndex: -1,
  21. device_name: '',
  22. device_quantity: '',
  23. device_price: ''
  24. },
  25. /**
  26. * 生命周期函数--监听页面加载
  27. */
  28. onLoad: function (options) {
  29. var id = options.id ? options.id : 1
  30. this.setData({
  31. id
  32. })
  33. api.getProject(this)
  34. api.getByName(this, 'work-points/get', 'work_points');
  35. api.getByName(this, 'devices/get', 'device_types');
  36. },
  37. submit: function(e) {
  38. var type = e.currentTarget.dataset.type
  39. var is_draft = type == 'draft' ? 1 : 2
  40. if(this.data.pointIndex < 0) {
  41. util.error('需求工点必填');
  42. return false;
  43. }
  44. if(this.data.devices.length <= 0) {
  45. util.error('请选择租赁设备');
  46. return false;
  47. }
  48. var work_point = this.data.work_points[this.data.pointIndex]
  49. http({
  50. url: 'orders/create',
  51. data: {
  52. project_id: this.data.id,
  53. work_point_id: work_point.id,
  54. remark: this.data.remark,
  55. devices: this.data.devices,
  56. is_draft: is_draft
  57. },
  58. success: function(res) {
  59. if(res.code == 0) {
  60. util.success('操作成功')
  61. }
  62. }
  63. })
  64. },
  65. switchTab: function(e) {
  66. this.setData({
  67. tabIndex: e.currentTarget.dataset.index
  68. })
  69. },
  70. deleteDevice: function(e) {
  71. var devices = this.data.devices
  72. var index = e.currentTarget.dataset.index
  73. devices.splice(index, 1)
  74. this.setData({
  75. devices
  76. })
  77. },
  78. addDevice: function() {
  79. if(!this.data.device_name) {
  80. util.error('设备名称必填');
  81. return false;
  82. }
  83. if(!this.data.typeIndex < 0) {
  84. util.error('设备类型必填')
  85. return false
  86. }
  87. if(!this.data.device_quantity) {
  88. util.error('设备数量必填')
  89. return false
  90. }
  91. if(!this.data.device_price) {
  92. util.error('设备单价必填')
  93. return false
  94. }
  95. var devices = this.data.devices
  96. var type = this.data.device_types[this.data.typeIndex]
  97. devices.push({
  98. name: this.data.device_name,
  99. type_name: type.name,
  100. type_id: type.id,
  101. quantity: this.data.device_quantity,
  102. price: this.data.device_price
  103. })
  104. this.setData({
  105. devices
  106. })
  107. },
  108. onChange: function(e) {
  109. var name = e.currentTarget.dataset.name
  110. this.setData({
  111. [name]: e.detail.value
  112. })
  113. },
  114. switchShowAdd: function(e) {
  115. this.setData({
  116. showAdd: e.currentTarget.dataset.show
  117. })
  118. },
  119. /**
  120. * 生命周期函数--监听页面初次渲染完成
  121. */
  122. onReady: function () {
  123. },
  124. /**
  125. * 生命周期函数--监听页面显示
  126. */
  127. onShow: function () {
  128. },
  129. /**
  130. * 生命周期函数--监听页面隐藏
  131. */
  132. onHide: function () {
  133. },
  134. /**
  135. * 生命周期函数--监听页面卸载
  136. */
  137. onUnload: function () {
  138. },
  139. /**
  140. * 页面相关事件处理函数--监听用户下拉动作
  141. */
  142. onPullDownRefresh: function () {
  143. },
  144. /**
  145. * 页面上拉触底事件的处理函数
  146. */
  147. onReachBottom: function () {
  148. },
  149. /**
  150. * 用户点击右上角分享
  151. */
  152. onShareAppMessage: function () {
  153. }
  154. })