index.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. tabs: ['维修记录填写', '维修配件添加'],
  11. tabIndex: 1,
  12. work_points: [],
  13. pointIndex: -1,
  14. id: -1,
  15. project: null,
  16. remark: '',
  17. inner_devices: [],
  18. deviceIndex: -1,
  19. parts: [],
  20. part_name: '',
  21. part_change: '',
  22. part_price: "",
  23. showAdd: false,
  24. order_id: '',
  25. // create/edit
  26. type: 'create',
  27. // create/edit
  28. dialog_type: 'create',
  29. default_dates: [],
  30. showDevice: false,
  31. money: 0
  32. },
  33. /**
  34. * 生命周期函数--监听页面加载
  35. */
  36. onLoad: function (options) {
  37. var id = options.id ? options.id : 1
  38. var type = options.type ? options.type : 'create'
  39. var order_id = options.order_id ? options.order_id : ''
  40. this.setData({
  41. id,
  42. type,
  43. order_id
  44. })
  45. api.getProject(this)
  46. api.getByName(this, 'work-points/get', 'work_points');
  47. api.getByName(this, 'inner-devices/get', 'inner_devices', { project_id: id, status: 'using' });
  48. },
  49. switchChecked(e) {
  50. var name = e.currentTarget.dataset.name
  51. var items = this.data[name]
  52. console.log(items)
  53. var index = e.currentTarget.dataset.index
  54. for (var i = 0; i < items.length; ++i) {
  55. items[i].checked = false;
  56. }
  57. items[index].checked = true;
  58. this.setData({
  59. [name]: items,
  60. deviceIndex: index
  61. })
  62. },
  63. submit: function (e) {
  64. // if(this.data.pointIndex < 0) {
  65. // util.error('需求工点必填');
  66. // return false;
  67. // }
  68. if (this.data.deviceIndex < 0) {
  69. util.error('维修设备必填');
  70. return false;
  71. }
  72. if (!this.data.reason) {
  73. util.error('维修原因必填');
  74. return false;
  75. }
  76. if (!this.data.day) {
  77. util.error('维修天数必填');
  78. return false;
  79. }
  80. // var work_point = this.data.work_points[this.data.pointIndex]
  81. var device = this.data.inner_devices[this.data.deviceIndex]
  82. var url = 'repair-devices/create'
  83. http({
  84. url: url,
  85. data: {
  86. project_id: this.data.id,
  87. // work_point_id: work_point.id,
  88. inner_device_id: device.id,
  89. money: this.data.money,
  90. reason: this.data.reason,
  91. day: this.data.day,
  92. remark: this.data.remark,
  93. parts: this.data.parts
  94. },
  95. success: function (res) {
  96. if (res.code == 0) {
  97. util.success('操作成功')
  98. setTimeout(function () {
  99. wx.navigateBack({
  100. delta: 0,
  101. })
  102. }, 1000)
  103. }
  104. }
  105. })
  106. },
  107. switchTab: function (e) {
  108. this.setData({
  109. tabIndex: e.currentTarget.dataset.index
  110. })
  111. },
  112. delete: function (e) {
  113. var parts = this.data.parts
  114. var index = e.currentTarget.dataset.index
  115. this.setData({
  116. money: (this.data.money - (parts[index].price - 0)).toFixed(2)
  117. })
  118. parts.splice(index, 1)
  119. this.setData({
  120. parts,
  121. })
  122. },
  123. add: function () {
  124. if (!this.data.part_name) {
  125. util.error('维修部位必填');
  126. return false;
  127. }
  128. if (!this.data.part_change) {
  129. util.error('更换配件必填');
  130. return false;
  131. }
  132. if (!this.data.part_price) {
  133. util.error('配件价格必填');
  134. return false;
  135. }
  136. var parts = this.data.parts
  137. var part = {
  138. name: this.data.part_name,
  139. change: this.data.part_change,
  140. price: this.data.part_price
  141. }
  142. parts.push(part)
  143. this.setData({
  144. parts,
  145. showAdd: false,
  146. money: this.data.money += (part.price - 0)
  147. })
  148. },
  149. cancel: function () {
  150. this.setData({
  151. showAdd: false
  152. })
  153. },
  154. onChange: function (e) {
  155. var name = e.currentTarget.dataset.name
  156. var val = e.detail.value
  157. this.setData({
  158. [name]: val
  159. })
  160. if (name == 'part_price') {
  161. let price
  162. if (/^(\d?)+(\.\d{0,2})?$/.test(val)) { //正则验证,提现金额小数点后不能大于两位数字
  163. price = val;
  164. } else {
  165. price = val.substring(0, val.length - 1);
  166. }
  167. this.setData({
  168. part_price: price
  169. })
  170. }
  171. if (name == 'pointIndex') {
  172. var work_point = this.data.work_points[val]
  173. var work_point_id = work_point ? work_point.id : ''
  174. var that = this
  175. api.getByName(this, 'inner-devices/get', 'inner_devices', {
  176. work_point_id: work_point_id
  177. }, function () {
  178. that.setData({
  179. deviceIndex: -1
  180. })
  181. });
  182. }
  183. },
  184. switchShow(e) {
  185. var name = e.currentTarget.dataset.name
  186. var val = !this.data[name]
  187. this.setData({
  188. [name]: val
  189. })
  190. },
  191. switchShowAdd: function (e) {
  192. var show = e.currentTarget.dataset.show
  193. if (show) {
  194. this.setData({
  195. part_name: '',
  196. part_change: '',
  197. part_price: '',
  198. })
  199. }
  200. this.setData({
  201. showAdd: true,
  202. dialog_type: 'create'
  203. })
  204. },
  205. switchShowDate: function (e) {
  206. this.setData({
  207. showDate: e.currentTarget.dataset.show
  208. })
  209. },
  210. confirmDate: function (e) {
  211. this.switchShowDate(e)
  212. var [start_date, end_date] = e.detail;
  213. start_date = util.formatDate(start_date)
  214. end_date = util.formatDate(end_date)
  215. this.setData({
  216. start_date,
  217. end_date
  218. })
  219. },
  220. /**
  221. * 生命周期函数--监听页面初次渲染完成
  222. */
  223. onReady: function () {
  224. },
  225. /**
  226. * 生命周期函数--监听页面显示
  227. */
  228. onShow: function () {
  229. },
  230. /**
  231. * 生命周期函数--监听页面隐藏
  232. */
  233. onHide: function () {
  234. },
  235. /**
  236. * 生命周期函数--监听页面卸载
  237. */
  238. onUnload: function () {
  239. },
  240. /**
  241. * 页面相关事件处理函数--监听用户下拉动作
  242. */
  243. onPullDownRefresh: function () {
  244. },
  245. /**
  246. * 页面上拉触底事件的处理函数
  247. */
  248. onReachBottom: function () {
  249. },
  250. /**
  251. * 用户点击右上角分享
  252. */
  253. onShareAppMessage: function () {
  254. }
  255. })