index.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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: 1,
  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. console.log(e)
  94. wx.navigateTo({
  95. url: e.currentTarget.dataset.url,
  96. })
  97. },
  98. switchChecked: function (e) {
  99. var index = e.detail.index
  100. var devices = this.data.devices
  101. devices[index].checked = !devices[index].checked
  102. this.setData({
  103. devices
  104. })
  105. },
  106. switchShow: function (e) {
  107. var name = e.currentTarget.dataset.name
  108. var val = this.data[name]
  109. this.setData({
  110. [name]: !val
  111. })
  112. },
  113. check: function (e) {
  114. var type = e.currentTarget.dataset.type
  115. var that = this
  116. var msg = '确认通过审核吗?'
  117. if (type == 'reject') msg = '确认驳回申请吗?'
  118. else if (type == 'pass' || type == 're-submit') msg = '确认提交吗?'
  119. else if (type == 'back') {
  120. this.switchShow(e)
  121. return false
  122. } else if (type == 're-rent') {
  123. this.switchShow(e)
  124. return false
  125. }
  126. Dialog.confirm({
  127. title: '提示',
  128. message: msg,
  129. })
  130. .then(() => {
  131. that.submitCheck(e)
  132. })
  133. },
  134. editDate: function (e) {
  135. var deviceIndex = e.detail.index
  136. var device = this.data.devices[deviceIndex]
  137. var default_dates = [device.start_date, device.end_date]
  138. this.setData({
  139. showDate: true,
  140. default_dates,
  141. deviceIndex
  142. })
  143. },
  144. confirmDate: function (e) {
  145. var [start_date, end_date] = e.detail;
  146. start_date = util.formatDate(start_date)
  147. end_date = util.formatDate(end_date)
  148. var devices = this.data.devices
  149. var deviceIndex = this.data.deviceIndex
  150. if (this.data.actionType == 'back') {
  151. var init_end_date = this.data.devices[deviceIndex].init_end_date
  152. if (init_end_date >= start_date) {
  153. util.error('该设备还在使用中,请重新选择续租时间')
  154. return false;
  155. }
  156. }
  157. devices[deviceIndex].start_date = start_date
  158. devices[deviceIndex].end_date = end_date
  159. devices[deviceIndex].is_change = true
  160. this.setData({
  161. devices,
  162. showDate: false
  163. })
  164. },
  165. submitCheck: function (e) {
  166. var type = e.currentTarget.dataset.type
  167. var is_change = e.currentTarget.dataset.change
  168. var that = this
  169. if (this.data.devices.length <= 0) {
  170. util.error('请选择调用设备');
  171. return false;
  172. }
  173. if (type == 'back') {
  174. var devices = this.data.devices
  175. var find = false;
  176. for (var i = 0; i < devices.length; ++i) {
  177. if (devices[i].checked) {
  178. find = true;
  179. break;
  180. }
  181. }
  182. if (!find) {
  183. util.error('归还设备不能为空');
  184. return false;
  185. }
  186. this.switchShow(e)
  187. }
  188. http({
  189. url: 'orders/check',
  190. data: {
  191. id: this.data.order_id,
  192. type: type,
  193. remark: this.data.remark,
  194. devices: this.data.devices,
  195. is_change: is_change
  196. },
  197. success: function (res) {
  198. if (res.code == 0) {
  199. util.success('操作成功')
  200. setTimeout(function () {
  201. that.init()
  202. }, 1000)
  203. }
  204. }
  205. })
  206. },
  207. initData: function () {
  208. var order = this.data.order,
  209. work_points = this.data.work_points,
  210. pointIndex = this.data.pointIndex
  211. for (var i = 0; i < work_points.length; ++i) {
  212. if (work_points[i].id == order.work_point_id) {
  213. pointIndex = i;
  214. break;
  215. }
  216. }
  217. this.setData({
  218. pointIndex,
  219. remark: order.remark,
  220. devices: order.inner_devices,
  221. loadedOrder: true
  222. })
  223. },
  224. submit: function (e) {
  225. var type = e.currentTarget.dataset.type
  226. var is_draft = type == 'draft' ? 1 : 2
  227. var submit_type = this.data.type
  228. if (this.data.pointIndex < 0) {
  229. util.error('需求工点必填');
  230. return false;
  231. }
  232. if (this.data.devices.length <= 0) {
  233. util.error('请选择调用设备');
  234. return false;
  235. }
  236. if (type == 're-rent') {
  237. var devices = this.data.devices
  238. for (var i = 0; i < devices.length; ++i) {
  239. if (devices[i].start_date <= devices[i].init_end_date) {
  240. util.error('有设备还在使用中,请重新选择续租时间')
  241. return false;
  242. }
  243. }
  244. this.setData({
  245. showRent: false
  246. })
  247. }
  248. var work_point = this.data.work_points[this.data.pointIndex]
  249. var url = (submit_type == 'create' || type == 're-rent') ? 'orders/createInner' : 'orders/updateInner'
  250. wx.setStorageSync('sg-added-devices', [])
  251. var that = this
  252. http({
  253. url: url,
  254. data: {
  255. id: this.data.order_id,
  256. project_id: this.data.id,
  257. work_point_id: work_point.id,
  258. remark: this.data.remark,
  259. devices: this.data.devices,
  260. is_draft: is_draft,
  261. type: type
  262. },
  263. success: function (res) {
  264. if (res.code == 0) {
  265. util.success('操作成功')
  266. setTimeout(function () {
  267. var url = '/pages/order-inner/index?id=' + that.data.id
  268. if (is_draft == 1) {
  269. wx.navigateBack({
  270. delta: 0,
  271. })
  272. } else {
  273. wx.redirectTo({
  274. url: url,
  275. })
  276. }
  277. }, 1000)
  278. }
  279. }
  280. })
  281. },
  282. switchTab: function (e) {
  283. this.setData({
  284. tabIndex: e.currentTarget.dataset.index
  285. })
  286. },
  287. onChange: function (e) {
  288. var name = e.currentTarget.dataset.name
  289. this.setData({
  290. [name]: e.detail.value
  291. })
  292. },
  293. /**
  294. * 生命周期函数--监听页面初次渲染完成
  295. */
  296. onReady: function () {
  297. },
  298. /**
  299. * 生命周期函数--监听页面显示
  300. */
  301. onShow: function () {
  302. if (this.data.type == 'create' || this.data.loadedOrder) {
  303. var devices = wx.getStorageSync('sg-added-devices')
  304. devices = devices ? devices : []
  305. this.setData({
  306. devices
  307. })
  308. }
  309. },
  310. goAdd: function (e) {
  311. var devices = this.data.devices
  312. wx.setStorageSync('sg-added-devices', devices)
  313. this.navigate(e)
  314. },
  315. deleteDevice: function (e) {
  316. wx.showModal({
  317. title: "提示",
  318. content: "是否确定删除",
  319. success: (res) => {
  320. if (res.confirm) {
  321. var index = e.detail.index
  322. var devices = this.data.devices
  323. devices.splice(index, 1)
  324. this.setData({
  325. devices
  326. })
  327. }
  328. }
  329. })
  330. },
  331. /**
  332. * 生命周期函数--监听页面隐藏
  333. */
  334. onHide: function () {
  335. },
  336. /**
  337. * 生命周期函数--监听页面卸载
  338. */
  339. onUnload: function () {
  340. },
  341. /**
  342. * 页面相关事件处理函数--监听用户下拉动作
  343. */
  344. onPullDownRefresh: function () {
  345. },
  346. /**
  347. * 页面上拉触底事件的处理函数
  348. */
  349. onReachBottom: function () {
  350. },
  351. /**
  352. * 用户点击右上角分享
  353. */
  354. onShareAppMessage: function () {
  355. }
  356. })