index.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. showDate: false,
  25. start_date: '',
  26. end_date: '',
  27. order_id: '',
  28. // create/edit
  29. type: 'create',
  30. order: {},
  31. selectIndex: -1,
  32. // create/edit
  33. dialog_type: 'create',
  34. default_dates: []
  35. },
  36. /**
  37. * 生命周期函数--监听页面加载
  38. */
  39. onLoad: function (options) {
  40. var id = options.id ? options.id : 1
  41. var type = options.type ? options.type : 'create'
  42. var order_id = options.order_id ? options.order_id : ''
  43. this.setData({
  44. id,
  45. type,
  46. order_id
  47. })
  48. api.getProject(this)
  49. api.getByName(this, 'work-points/get', 'work_points');
  50. api.getByName(this, 'devices/get', 'device_types');
  51. if(order_id) {
  52. var that = this
  53. api.getByName(this, 'orders/detail', 'order', {id: order_id}, function(res) {
  54. that.initData()
  55. });
  56. wx.setNavigationBarTitle({
  57. title: '修订订单',
  58. })
  59. }
  60. },
  61. selectDevice: function(e) {
  62. var newIndex = e.currentTarget.dataset.index == this.data.selectIndex ? -1 : e.currentTarget.dataset.index
  63. this.setData({
  64. selectIndex: newIndex
  65. })
  66. },
  67. initData: function() {
  68. var order = this.data.order,
  69. work_points = this.data.work_points,
  70. pointIndex = this.data.pointIndex
  71. for(var i = 0; i < work_points.length; ++i) {
  72. if(work_points[i].id == order.work_point_id) {
  73. pointIndex = i;
  74. break;
  75. }
  76. }
  77. var devices = order.devices
  78. var local_devices = []
  79. for(var i = 0; i < devices.length; ++i) {
  80. var device = devices[i]
  81. local_devices.push({
  82. name: device.pivot.name,
  83. type_name: device.name,
  84. type_id: device.id,
  85. quantity: device.pivot.quantity,
  86. price: device.pivot.price / 100,
  87. start_date: device.pivot.start_date,
  88. end_date: device.pivot.end_date
  89. })
  90. }
  91. this.setData({
  92. pointIndex,
  93. remark: order.remark,
  94. devices: local_devices
  95. })
  96. },
  97. submit: function(e) {
  98. var type = e.currentTarget.dataset.type
  99. var is_draft = type == 'draft' ? 1 : 2
  100. var submit_type = this.data.type
  101. if(this.data.pointIndex < 0) {
  102. util.error('需求工点必填');
  103. return false;
  104. }
  105. if(this.data.devices.length <= 0) {
  106. util.error('请选择租赁设备');
  107. return false;
  108. }
  109. var work_point = this.data.work_points[this.data.pointIndex]
  110. var url = submit_type == 'create' ? 'orders/create' : 'orders/update'
  111. http({
  112. url: url,
  113. data: {
  114. id: this.data.order_id,
  115. project_id: this.data.id,
  116. work_point_id: work_point.id,
  117. remark: this.data.remark,
  118. devices: this.data.devices,
  119. is_draft: is_draft
  120. },
  121. success: function(res) {
  122. if(res.code == 0) {
  123. util.success('操作成功')
  124. }
  125. }
  126. })
  127. },
  128. switchTab: function(e) {
  129. this.setData({
  130. tabIndex: e.currentTarget.dataset.index
  131. })
  132. },
  133. deleteDevice: function() {
  134. var devices = this.data.devices
  135. var index = this.data.selectIndex
  136. devices.splice(index, 1)
  137. this.setData({
  138. devices,
  139. selectIndex: -1
  140. })
  141. },
  142. editDevice: function() {
  143. var devices = this.data.devices
  144. var index = this.data.selectIndex
  145. if(index < 0) return false
  146. var device = devices[index]
  147. var typeIndex = -1
  148. var device_types = this.data.device_types
  149. var default_dates = [device.start_date, device.end_date]
  150. for(var i = 0; i < device_types.length; ++i) {
  151. if(device_types[i].id == device.type_id) {
  152. typeIndex = i;
  153. break;
  154. }
  155. }
  156. this.setData({
  157. device_name: device.name,
  158. typeIndex,
  159. start_date: device.start_date,
  160. end_date: device.end_date,
  161. device_quantity: device.quantity,
  162. device_price: device.price,
  163. showAdd: true,
  164. dialog_type: 'edit',
  165. default_dates: default_dates
  166. })
  167. },
  168. addDevice: function() {
  169. if(!this.data.device_name) {
  170. util.error('设备名称必填');
  171. return false;
  172. }
  173. if(!this.data.typeIndex < 0) {
  174. util.error('设备类型必填')
  175. return false
  176. }
  177. if(!this.data.device_quantity) {
  178. util.error('设备数量必填')
  179. return false
  180. }
  181. if(!this.data.device_price) {
  182. util.error('设备单价必填')
  183. return false
  184. }
  185. if(!this.data.start_date) {
  186. util.error('租赁时间必填')
  187. return false
  188. }
  189. var devices = this.data.devices
  190. var type = this.data.device_types[this.data.typeIndex]
  191. var device = {
  192. name: this.data.device_name,
  193. type_name: type.name,
  194. type_id: type.id,
  195. quantity: this.data.device_quantity,
  196. price: this.data.device_price,
  197. start_date: this.data.start_date,
  198. end_date: this.data.end_date
  199. }
  200. var dialog_type = this.data.dialog_type
  201. if(dialog_type == 'create') {
  202. devices.push(device)
  203. } else {
  204. devices[this.data.selectIndex] = device
  205. }
  206. this.setData({
  207. devices
  208. })
  209. },
  210. onChange: function(e) {
  211. var name = e.currentTarget.dataset.name
  212. this.setData({
  213. [name]: e.detail.value
  214. })
  215. },
  216. switchShowAdd: function(e) {
  217. var show = e.currentTarget.dataset.show
  218. if(show) {
  219. this.setData({
  220. device_name: '',
  221. typeIndex: -1,
  222. start_date: '',
  223. end_date: '',
  224. device_quantity: '',
  225. device_price: '',
  226. default_dates: []
  227. })
  228. }
  229. this.setData({
  230. showAdd: show,
  231. dialog_type: 'create'
  232. })
  233. },
  234. switchShowDate: function(e) {
  235. this.setData({
  236. showDate: e.currentTarget.dataset.show
  237. })
  238. },
  239. confirmDate: function(e) {
  240. this.switchShowDate(e)
  241. var [start_date, end_date] = e.detail;
  242. start_date = util.formatDate(start_date)
  243. end_date = util.formatDate(end_date)
  244. this.setData({
  245. start_date,
  246. end_date
  247. })
  248. },
  249. /**
  250. * 生命周期函数--监听页面初次渲染完成
  251. */
  252. onReady: function () {
  253. },
  254. /**
  255. * 生命周期函数--监听页面显示
  256. */
  257. onShow: function () {
  258. },
  259. /**
  260. * 生命周期函数--监听页面隐藏
  261. */
  262. onHide: function () {
  263. },
  264. /**
  265. * 生命周期函数--监听页面卸载
  266. */
  267. onUnload: function () {
  268. },
  269. /**
  270. * 页面相关事件处理函数--监听用户下拉动作
  271. */
  272. onPullDownRefresh: function () {
  273. },
  274. /**
  275. * 页面上拉触底事件的处理函数
  276. */
  277. onReachBottom: function () {
  278. },
  279. /**
  280. * 用户点击右上角分享
  281. */
  282. onShareAppMessage: function () {
  283. }
  284. })