index.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. // create/edit
  19. type: 'create',
  20. order_id: ''
  21. },
  22. /**
  23. * 生命周期函数--监听页面加载
  24. */
  25. onLoad: function (options) {
  26. var id = options.id ? options.id : 1
  27. var type = options.type ? options.type : 'create'
  28. var order_id = options.order_id ? options.order_id : ''
  29. this.setData({
  30. id,
  31. type,
  32. order_id
  33. })
  34. api.getProject(this)
  35. api.getByName(this, 'work-points/get', 'work_points');
  36. if(order_id) {
  37. var that = this
  38. api.getByName(this, 'orders/detail', 'order', {id: order_id}, function(res) {
  39. that.initData()
  40. });
  41. wx.setNavigationBarTitle({
  42. title: '修订订单',
  43. })
  44. }
  45. },
  46. navigate: function(e) {
  47. wx.navigateTo({
  48. url: e.currentTarget.dataset.url,
  49. })
  50. },
  51. initData: function() {
  52. var order = this.data.order,
  53. work_points = this.data.work_points,
  54. pointIndex = this.data.pointIndex
  55. for(var i = 0; i < work_points.length; ++i) {
  56. if(work_points[i].id == order.work_point_id) {
  57. pointIndex = i;
  58. break;
  59. }
  60. }
  61. var devices = order.devices
  62. var local_devices = []
  63. for(var i = 0; i < devices.length; ++i) {
  64. var device = devices[i]
  65. local_devices.push({
  66. name: device.pivot.name,
  67. type_name: device.name,
  68. type_id: device.id,
  69. quantity: device.pivot.quantity,
  70. price: device.pivot.price / 100,
  71. start_date: device.pivot.start_date,
  72. end_date: device.pivot.end_date
  73. })
  74. }
  75. this.setData({
  76. pointIndex,
  77. remark: order.remark,
  78. devices: local_devices
  79. })
  80. },
  81. submit: function(e) {
  82. var type = e.currentTarget.dataset.type
  83. var is_draft = type == 'draft' ? 1 : 2
  84. var submit_type = this.data.type
  85. if(this.data.pointIndex < 0) {
  86. util.error('需求工点必填');
  87. return false;
  88. }
  89. if(this.data.devices.length <= 0) {
  90. util.error('请选择调用设备');
  91. return false;
  92. }
  93. var work_point = this.data.work_points[this.data.pointIndex]
  94. var url = submit_type == 'create' ? 'orders/createInner' : 'orders/updateInner'
  95. http({
  96. url: url,
  97. data: {
  98. id: this.data.order_id,
  99. project_id: this.data.id,
  100. work_point_id: work_point.id,
  101. remark: this.data.remark,
  102. devices: this.data.devices,
  103. is_draft: is_draft
  104. },
  105. success: function(res) {
  106. if(res.code == 0) {
  107. util.success('操作成功')
  108. }
  109. }
  110. })
  111. },
  112. switchTab: function(e) {
  113. this.setData({
  114. tabIndex: e.currentTarget.dataset.index
  115. })
  116. },
  117. onChange: function(e) {
  118. var name = e.currentTarget.dataset.name
  119. this.setData({
  120. [name]: e.detail.value
  121. })
  122. },
  123. /**
  124. * 生命周期函数--监听页面初次渲染完成
  125. */
  126. onReady: function () {
  127. },
  128. /**
  129. * 生命周期函数--监听页面显示
  130. */
  131. onShow: function () {
  132. var devices = wx.getStorageSync('sg-added-devices')
  133. devices = devices ? devices : []
  134. this.setData({
  135. devices
  136. })
  137. },
  138. goAdd: function(e) {
  139. var devices = this.data.devices
  140. wx.setStorageSync('sg-added-devices', devices)
  141. this.navigate(e)
  142. },
  143. deleteDevice: function(e) {
  144. var index = e.currentTarget.dataset.index
  145. var devices = this.data.devices
  146. devices.splice(index, 1)
  147. this.setData({
  148. devices
  149. })
  150. },
  151. /**
  152. * 生命周期函数--监听页面隐藏
  153. */
  154. onHide: function () {
  155. },
  156. /**
  157. * 生命周期函数--监听页面卸载
  158. */
  159. onUnload: function () {
  160. },
  161. /**
  162. * 页面相关事件处理函数--监听用户下拉动作
  163. */
  164. onPullDownRefresh: function () {
  165. },
  166. /**
  167. * 页面上拉触底事件的处理函数
  168. */
  169. onReachBottom: function () {
  170. },
  171. /**
  172. * 用户点击右上角分享
  173. */
  174. onShareAppMessage: function () {
  175. }
  176. })