index.js 8.4 KB

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