index.js 7.5 KB

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