index.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. 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. check: function(e) {
  91. var type = e.currentTarget.dataset.type
  92. var that = this
  93. var msg = '确认通过审核吗?'
  94. if(type == 'reject') msg = '确认驳回申请吗?'
  95. else if(type == 'pass' || type == 're-submit') msg = '确认提交吗?'
  96. else if(type == 'back') msg = '确认归还吗?'
  97. Dialog.confirm({
  98. title: '提示',
  99. message: msg,
  100. })
  101. .then(() => {
  102. that.submitCheck(e)
  103. })
  104. },
  105. submitCheck: function (e) {
  106. var type = e.currentTarget.dataset.type
  107. var is_change = e.currentTarget.dataset.change
  108. var that = this
  109. if(this.data.devices.length <= 0) {
  110. util.error('请选择调用设备');
  111. return false;
  112. }
  113. http({
  114. url: 'orders/check',
  115. data: {
  116. id: this.data.order_id,
  117. type: type,
  118. remark: this.data.remark,
  119. devices: this.data.devices,
  120. is_change: is_change
  121. },
  122. success: function (res) {
  123. if (res.code == 0) {
  124. util.success('操作成功')
  125. setTimeout(function() {
  126. that.init()
  127. }, 1000)
  128. }
  129. }
  130. })
  131. },
  132. initData: function() {
  133. var order = this.data.order,
  134. work_points = this.data.work_points,
  135. pointIndex = this.data.pointIndex
  136. for(var i = 0; i < work_points.length; ++i) {
  137. if(work_points[i].id == order.work_point_id) {
  138. pointIndex = i;
  139. break;
  140. }
  141. }
  142. this.setData({
  143. pointIndex,
  144. remark: order.remark,
  145. devices: order.inner_devices,
  146. loadedOrder: true
  147. })
  148. },
  149. submit: function(e) {
  150. var type = e.currentTarget.dataset.type
  151. var is_draft = type == 'draft' ? 1 : 2
  152. var submit_type = this.data.type
  153. if(this.data.pointIndex < 0) {
  154. util.error('需求工点必填');
  155. return false;
  156. }
  157. if(this.data.devices.length <= 0) {
  158. util.error('请选择调用设备');
  159. return false;
  160. }
  161. var work_point = this.data.work_points[this.data.pointIndex]
  162. var url = (submit_type == 'create' || type == 're-rent') ? 'orders/createInner' : 'orders/updateInner'
  163. wx.setStorageSync('sg-added-devices', [])
  164. http({
  165. url: url,
  166. data: {
  167. id: this.data.order_id,
  168. project_id: this.data.id,
  169. work_point_id: work_point.id,
  170. remark: this.data.remark,
  171. devices: this.data.devices,
  172. is_draft: is_draft,
  173. type: type
  174. },
  175. success: function(res) {
  176. if(res.code == 0) {
  177. util.success('操作成功')
  178. }
  179. }
  180. })
  181. },
  182. switchTab: function(e) {
  183. this.setData({
  184. tabIndex: e.currentTarget.dataset.index
  185. })
  186. },
  187. onChange: function(e) {
  188. var name = e.currentTarget.dataset.name
  189. this.setData({
  190. [name]: e.detail.value
  191. })
  192. },
  193. /**
  194. * 生命周期函数--监听页面初次渲染完成
  195. */
  196. onReady: function () {
  197. },
  198. /**
  199. * 生命周期函数--监听页面显示
  200. */
  201. onShow: function () {
  202. if(this.data.type == 'create' || this.data.loadedOrder) {
  203. var devices = wx.getStorageSync('sg-added-devices')
  204. devices = devices ? devices : []
  205. this.setData({
  206. devices
  207. })
  208. }
  209. },
  210. goAdd: function(e) {
  211. var devices = this.data.devices
  212. wx.setStorageSync('sg-added-devices', devices)
  213. this.navigate(e)
  214. },
  215. deleteDevice: function(e) {
  216. var index = e.currentTarget.dataset.index
  217. var devices = this.data.devices
  218. devices.splice(index, 1)
  219. this.setData({
  220. devices
  221. })
  222. },
  223. /**
  224. * 生命周期函数--监听页面隐藏
  225. */
  226. onHide: function () {
  227. },
  228. /**
  229. * 生命周期函数--监听页面卸载
  230. */
  231. onUnload: function () {
  232. },
  233. /**
  234. * 页面相关事件处理函数--监听用户下拉动作
  235. */
  236. onPullDownRefresh: function () {
  237. },
  238. /**
  239. * 页面上拉触底事件的处理函数
  240. */
  241. onReachBottom: function () {
  242. },
  243. /**
  244. * 用户点击右上角分享
  245. */
  246. onShareAppMessage: function () {
  247. }
  248. })