index.js 9.1 KB

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