index.js 6.3 KB

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