index.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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. import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. tabs: ['内部设备调用', '调用设备添加'],
  12. tabIndex: 0,
  13. work_points: [],
  14. pointIndex: -1,
  15. id: -1,
  16. project: null,
  17. remark: '',
  18. devices: [],
  19. // create/edit
  20. type: 'create',
  21. order_id: '',
  22. loadedOrder: false,
  23. role: null,
  24. // 审核(check)|确认(pass)|重新提交(re-submit)|退回(back)
  25. actionType: null,
  26. order: null,
  27. canEdit: true,
  28. showBack: false,
  29. showRent: false,
  30. showDate: false,
  31. deviceIndex: -1
  32. },
  33. /**
  34. * 生命周期函数--监听页面加载
  35. */
  36. onLoad: function (options) {
  37. // options = {
  38. // id: 1,
  39. // type: 'edit',
  40. // order_id: 194
  41. // }
  42. var id = options.id ? options.id : 1
  43. var type = options.type ? options.type : 'create'
  44. var order_id = options.order_id ? options.order_id : ''
  45. this.setData({
  46. id,
  47. type,
  48. order_id
  49. })
  50. this.init()
  51. },
  52. init() {
  53. var order_id = this.data.order_id
  54. api.getProject(this)
  55. api.getByName(this, 'work-points/get', 'work_points', {project_id: this.data.id});
  56. if (order_id) {
  57. var that = this
  58. api.getByName(this, 'orders/detail', 'order', {
  59. id: order_id
  60. }, function (res) {
  61. api.getByName(that, 'orders/getRole', 'role', {
  62. id: that.data.order ? that.data.order.project_id : ''
  63. }, function (res) {
  64. that.updateActionType()
  65. that.initData()
  66. });
  67. });
  68. wx.setNavigationBarTitle({
  69. title: '订单详情',
  70. })
  71. }
  72. },
  73. updateActionType: function () {
  74. var actionType = ''
  75. var role = this.data.role
  76. var order = this.data.order
  77. if (order.level == role.level) {
  78. if (['checking', 'checked'].indexOf(order.status_key) != -1 && role && role.rights && role.rights.applyCheck) actionType = 'check';
  79. else if (order.status_key == 'reject' && role.key == 'machine') actionType = 're-submit'
  80. else if (order.status_key == 'checked' && role.key == 'machine') actionType = 'back'
  81. }
  82. var canEdit = (actionType == 're-submit');
  83. if (order.is_draft == 1) {
  84. canEdit = true
  85. actionType = 'edit'
  86. }
  87. this.setData({
  88. actionType,
  89. canEdit
  90. })
  91. },
  92. navigate: function (e) {
  93. wx.navigateTo({
  94. url: e.currentTarget.dataset.url,
  95. })
  96. },
  97. switchChecked: function (e) {
  98. var index = e.detail.index
  99. var devices = this.data.devices
  100. devices[index].checked = !devices[index].checked
  101. this.setData({
  102. devices
  103. })
  104. },
  105. switchShow: function (e) {
  106. var name = e.currentTarget.dataset.name
  107. var val = this.data[name]
  108. this.setData({
  109. [name]: !val
  110. })
  111. },
  112. check: function (e) {
  113. var type = e.currentTarget.dataset.type
  114. var that = this
  115. var msg = '确认通过审核吗?'
  116. if (type == 'reject') msg = '确认驳回申请吗?'
  117. else if (type == 'pass' || type == 're-submit') msg = '确认提交吗?'
  118. else if (type == 'back') {
  119. this.switchShow(e)
  120. return false
  121. } else if (type == 're-rent') {
  122. this.switchShow(e)
  123. return false
  124. }
  125. Dialog.confirm({
  126. title: '提示',
  127. message: msg,
  128. })
  129. .then(() => {
  130. that.submitCheck(e)
  131. })
  132. },
  133. editDate: function (e) {
  134. var deviceIndex = e.detail.index
  135. var device = this.data.devices[deviceIndex]
  136. var default_dates = [device.start_date, device.end_date]
  137. this.setData({
  138. showDate: true,
  139. default_dates,
  140. deviceIndex
  141. })
  142. },
  143. confirmDate: function (e) {
  144. var [start_date, end_date] = e.detail;
  145. start_date = util.formatDate(start_date)
  146. end_date = util.formatDate(end_date)
  147. var devices = this.data.devices
  148. var deviceIndex = this.data.deviceIndex
  149. if (this.data.actionType == 'back') {
  150. var init_end_date = this.data.devices[deviceIndex].init_end_date
  151. if (init_end_date >= start_date) {
  152. util.error('该设备还在使用中,请重新选择续租时间')
  153. return false;
  154. }
  155. }
  156. devices[deviceIndex].start_date = start_date
  157. devices[deviceIndex].end_date = end_date
  158. devices[deviceIndex].is_change = true
  159. this.setData({
  160. devices,
  161. showDate: false
  162. })
  163. },
  164. submitCheck: function (e) {
  165. var type = e.currentTarget.dataset.type
  166. var is_change = e.currentTarget.dataset.change
  167. var that = this
  168. if (this.data.devices.length <= 0) {
  169. util.error('请选择调用设备');
  170. return false;
  171. }
  172. if (type == 'back') {
  173. var devices = this.data.devices
  174. var find = false;
  175. for (var i = 0; i < devices.length; ++i) {
  176. if (devices[i].checked) {
  177. find = true;
  178. break;
  179. }
  180. }
  181. if (!find) {
  182. util.error('归还设备不能为空');
  183. return false;
  184. }
  185. this.switchShow(e)
  186. }
  187. http({
  188. url: 'orders/check',
  189. data: {
  190. id: this.data.order_id,
  191. type: type,
  192. remark: this.data.remark,
  193. devices: this.data.devices,
  194. is_change: is_change
  195. },
  196. success: function (res) {
  197. if (res.code == 0) {
  198. util.success('操作成功')
  199. setTimeout(function () {
  200. that.init()
  201. }, 1000)
  202. }
  203. }
  204. })
  205. },
  206. initData: function () {
  207. var order = this.data.order,
  208. work_points = this.data.work_points,
  209. pointIndex = this.data.pointIndex
  210. for (var i = 0; i < work_points.length; ++i) {
  211. if (work_points[i].id == order.work_point_id) {
  212. pointIndex = i;
  213. break;
  214. }
  215. }
  216. this.setData({
  217. pointIndex,
  218. remark: order.remark,
  219. devices: order.inner_devices,
  220. loadedOrder: true
  221. })
  222. },
  223. submit: function (e) {
  224. var type = e.currentTarget.dataset.type
  225. var is_draft = type == 'draft' ? 1 : 2
  226. var submit_type = this.data.type
  227. if (this.data.pointIndex < 0) {
  228. util.error('需求工点必填');
  229. return false;
  230. }
  231. if (this.data.devices.length <= 0) {
  232. util.error('请选择调用设备');
  233. return false;
  234. }
  235. if (type == 're-rent') {
  236. var devices = this.data.devices
  237. for (var i = 0; i < devices.length; ++i) {
  238. if (devices[i].start_date <= devices[i].init_end_date) {
  239. util.error('有设备还在使用中,请重新选择续租时间')
  240. return false;
  241. }
  242. }
  243. this.setData({
  244. showRent: false
  245. })
  246. }
  247. var work_point = this.data.work_points[this.data.pointIndex]
  248. var url = (submit_type == 'create' || type == 're-rent') ? 'orders/createInner' : 'orders/updateInner'
  249. wx.setStorageSync('sg-added-devices', [])
  250. var that = this
  251. http({
  252. url: url,
  253. data: {
  254. id: this.data.order_id,
  255. project_id: this.data.id,
  256. work_point_id: work_point.id,
  257. remark: this.data.remark,
  258. devices: this.data.devices,
  259. is_draft: is_draft,
  260. type: type
  261. },
  262. success: function (res) {
  263. if (res.code == 0) {
  264. util.success('操作成功')
  265. setTimeout(function () {
  266. var url = '/pages/order-inner/index?id=' + that.data.id
  267. if (is_draft == 1) {
  268. wx.navigateBack({
  269. delta: 0,
  270. })
  271. } else {
  272. wx.redirectTo({
  273. url: url,
  274. })
  275. }
  276. }, 1000)
  277. }
  278. }
  279. })
  280. },
  281. switchTab: function (e) {
  282. this.setData({
  283. tabIndex: e.currentTarget.dataset.index
  284. })
  285. },
  286. onChange: function (e) {
  287. var name = e.currentTarget.dataset.name
  288. this.setData({
  289. [name]: e.detail.value
  290. })
  291. },
  292. /**
  293. * 生命周期函数--监听页面初次渲染完成
  294. */
  295. onReady: function () {
  296. },
  297. /**
  298. * 生命周期函数--监听页面显示
  299. */
  300. onShow: function () {
  301. if (this.data.type == 'create' || this.data.loadedOrder) {
  302. var devices = wx.getStorageSync('sg-added-devices')
  303. devices = devices ? devices : []
  304. this.setData({
  305. devices
  306. })
  307. }
  308. },
  309. goAdd: function (e) {
  310. var devices = this.data.devices
  311. wx.setStorageSync('sg-added-devices', devices)
  312. this.navigate(e)
  313. },
  314. deleteDevice: function (e) {
  315. wx.showModal({
  316. title: "提示",
  317. content: "是否确定删除",
  318. success: (res) => {
  319. if (res.confirm) {
  320. var index = e.detail.index
  321. var devices = this.data.devices
  322. devices.splice(index, 1)
  323. this.setData({
  324. devices
  325. })
  326. }
  327. }
  328. })
  329. },
  330. /**
  331. * 生命周期函数--监听页面隐藏
  332. */
  333. onHide: function () {
  334. },
  335. /**
  336. * 生命周期函数--监听页面卸载
  337. */
  338. onUnload: function () {
  339. },
  340. /**
  341. * 页面相关事件处理函数--监听用户下拉动作
  342. */
  343. onPullDownRefresh: function () {
  344. },
  345. /**
  346. * 页面上拉触底事件的处理函数
  347. */
  348. onReachBottom: function () {
  349. },
  350. /**
  351. * 用户点击右上角分享
  352. */
  353. onShareAppMessage: function () {
  354. }
  355. })