index.js 4.0 KB

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