index.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. parts.splice(index, 1)
  116. this.setData({
  117. parts
  118. })
  119. },
  120. add: function () {
  121. if (!this.data.part_name) {
  122. util.error('维修部位必填');
  123. return false;
  124. }
  125. if (!this.data.part_change) {
  126. util.error('更换配件必填');
  127. return false;
  128. }
  129. if (!this.data.part_price) {
  130. util.error('配件价格必填');
  131. return false;
  132. }
  133. var parts = this.data.parts
  134. var part = {
  135. name: this.data.part_name,
  136. change: this.data.part_change,
  137. price: this.data.part_price
  138. }
  139. parts.push(part)
  140. this.setData({
  141. parts,
  142. showAdd: false,
  143. money: this.data.money += (part.price - 0)
  144. })
  145. },
  146. cancel: function () {
  147. this.setData({
  148. showAdd: false
  149. })
  150. },
  151. onChange: function (e) {
  152. var name = e.currentTarget.dataset.name
  153. var val = e.detail.value
  154. this.setData({
  155. [name]: val
  156. })
  157. if (name == 'part_price') {
  158. let price
  159. if (/^(\d?)+(\.\d{0,2})?$/.test(val)) { //正则验证,提现金额小数点后不能大于两位数字
  160. price = val;
  161. } else {
  162. price = val.substring(0, val.length - 1);
  163. }
  164. this.setData({
  165. part_price: price
  166. })
  167. }
  168. if (name == 'pointIndex') {
  169. var work_point = this.data.work_points[val]
  170. var work_point_id = work_point ? work_point.id : ''
  171. var that = this
  172. api.getByName(this, 'inner-devices/get', 'inner_devices', {
  173. work_point_id: work_point_id
  174. }, function () {
  175. that.setData({
  176. deviceIndex: -1
  177. })
  178. });
  179. }
  180. },
  181. switchShow(e) {
  182. var name = e.currentTarget.dataset.name
  183. var val = !this.data[name]
  184. this.setData({
  185. [name]: val
  186. })
  187. },
  188. switchShowAdd: function (e) {
  189. var show = e.currentTarget.dataset.show
  190. if (show) {
  191. this.setData({
  192. part_name: '',
  193. part_change: '',
  194. part_price: '',
  195. })
  196. }
  197. this.setData({
  198. showAdd: true,
  199. dialog_type: 'create'
  200. })
  201. },
  202. switchShowDate: function (e) {
  203. this.setData({
  204. showDate: e.currentTarget.dataset.show
  205. })
  206. },
  207. confirmDate: function (e) {
  208. this.switchShowDate(e)
  209. var [start_date, end_date] = e.detail;
  210. start_date = util.formatDate(start_date)
  211. end_date = util.formatDate(end_date)
  212. this.setData({
  213. start_date,
  214. end_date
  215. })
  216. },
  217. /**
  218. * 生命周期函数--监听页面初次渲染完成
  219. */
  220. onReady: function () {
  221. },
  222. /**
  223. * 生命周期函数--监听页面显示
  224. */
  225. onShow: function () {
  226. },
  227. /**
  228. * 生命周期函数--监听页面隐藏
  229. */
  230. onHide: function () {
  231. },
  232. /**
  233. * 生命周期函数--监听页面卸载
  234. */
  235. onUnload: function () {
  236. },
  237. /**
  238. * 页面相关事件处理函数--监听用户下拉动作
  239. */
  240. onPullDownRefresh: function () {
  241. },
  242. /**
  243. * 页面上拉触底事件的处理函数
  244. */
  245. onReachBottom: function () {
  246. },
  247. /**
  248. * 用户点击右上角分享
  249. */
  250. onShareAppMessage: function () {
  251. }
  252. })