add.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // pages/car/add.js
  2. const app = getApp()
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. showModal: false,
  9. defaultText: '请选择',
  10. isSubmitting: false,
  11. shipTypeArray: [], //船型
  12. shipType: '',
  13. shipArray:[], //船主
  14. shipIndex:null,
  15. certificatePathList:[], //船舶证书
  16. contractPathList:[], //安全环保合同
  17. },
  18. /**
  19. * 生命周期函数--监听页面加载
  20. */
  21. onLoad: function (options) {
  22. //船型
  23. let treeData = app.globalData.treeArr
  24. for (const element of treeData) {
  25. let arrStr;
  26. switch (element.dictValue) {
  27. case 'ship_type':
  28. arrStr = "shipTypeArray";
  29. break;
  30. }
  31. this.setData({
  32. [arrStr]: element.children
  33. })
  34. }
  35. // 默认填充船主信息
  36. this.setData({
  37. shipId : wx.getStorageSync('id'),
  38. shipMobile : wx.getStorageSync('phone')
  39. })
  40. //获取船主下拉列表
  41. this.loadUserData()
  42. },
  43. //船主
  44. loadUserData:function(){
  45. app.request.GET({
  46. url: app.API.roleUser,
  47. params: {
  48. roleName : '船主',
  49. },
  50. page: this,
  51. successFun: true
  52. }).then(res => {
  53. this.setData({
  54. shipArray : res.data.data,
  55. shipIndex : app.util.getDicIndex(res.data.data, this.data.shipId, 'id')
  56. })
  57. if(this.data.shipId){
  58. this.setData({
  59. shipMobile : this.data.shipArray[this.data.shipIndex].phone,
  60. })
  61. }
  62. })
  63. },
  64. //下拉选择
  65. bindPickerChange: function (e) {
  66. let { pickername } = e.target.dataset
  67. let getIndex = pickername + 'Index'
  68. app.util.getPickerIndex(this, getIndex, e);
  69. switch (pickername) {
  70. case 'ship': //选择船主
  71. this.setData({
  72. shipMobile : this.data.shipArray[this.data.shipIndex].phone,
  73. })
  74. break;
  75. }
  76. },
  77. // 单选
  78. dangerStatusChange: function (e) {
  79. this.setData({
  80. [e.currentTarget.dataset.radiotype]: e.detail.value
  81. })
  82. },
  83. //多选
  84. checkboxChange: function (e) {
  85. this.setData({
  86. [e.currentTarget.dataset.checkboxtype]: e.detail
  87. })
  88. },
  89. //图片上传
  90. afterRead:function(event){
  91. const { file } = event.detail;
  92. const { type } = event.currentTarget.dataset
  93. app.request.uploadDIY({
  94. url: app.API.uploadImgMap,
  95. page: this,
  96. filePaths:file.url,
  97. setfiled:'file',
  98. params: {},
  99. //isToken: false,
  100. isLoading: false,
  101. successFun: true
  102. }).then(res => {
  103. const imgArray = this.data[type];
  104. imgArray.push({
  105. ...file,
  106. url: app.host.BASEIMG_URL + JSON.parse(res.data).data.imageFile,
  107. imgUrl: JSON.parse(res.data).data.imageFile
  108. });
  109. this.setData({
  110. [type]: imgArray
  111. });
  112. })
  113. },
  114. // 删除图片
  115. deleteImg: function (event) {
  116. const { type } = event.currentTarget.dataset
  117. let getFileList = this.data[type];
  118. getFileList.splice(event.detail.index, 1)
  119. this.setData({
  120. [type]: getFileList
  121. })
  122. },
  123. //提交事件
  124. formSubmit: function ({detail:{value}}) {
  125. var warn = ""; //弹框时提示的内容
  126. /** 船舶证书 certificatePathList (必传) 图片处理 START **/
  127. let getcertificatePathList = this.data.certificatePathList
  128. if(getcertificatePathList.length < 1){
  129. warn = "请上传船舶证书图片!";
  130. }
  131. let certificateNameList = []
  132. let certificatePathList = []
  133. getcertificatePathList.forEach((element,index) => {
  134. certificateNameList.push('图片'+(index+1)+'.'+element.imgUrl.split('.')[1])
  135. certificatePathList.push(element.imgUrl)
  136. });
  137. value.certificateNameList = certificateNameList
  138. value.certificatePathList = certificatePathList
  139. /** 图片处理 END **/
  140. /** 安全环保合同 contractPathList (非必传) 图片处理 START **/
  141. let getcontractPathList = this.data.contractPathList
  142. if(getcontractPathList.length < 1){
  143. // warn = "请上传安全环保合同!";
  144. }else{
  145. // 上传了安保合同才处理
  146. let contractNameList = []
  147. let contractPathList = []
  148. getcontractPathList.forEach((element,index) => {
  149. contractNameList.push('图片'+(index+1)+'.'+element.imgUrl.split('.')[1])
  150. contractPathList.push(element.imgUrl)
  151. });
  152. value.contractNameList = contractNameList
  153. value.contractPathList = contractPathList
  154. }
  155. /** 图片处理 END **/
  156. if (value.shipNumber == '') {
  157. warn = "请输入船舶号";
  158. } else if (value.shipType == '') {
  159. warn = "请输入船型";
  160. } else if (value.shipId == '') {
  161. warn = "请选择船主";
  162. }
  163. if (warn != '') {
  164. app.util.checkForm(warn);
  165. } else {
  166. app.request.POST({
  167. url: app.API.shipAdd,
  168. params: value,
  169. page: this,
  170. isLoadingTxt: '提交中...',
  171. isSubmitting: true,
  172. successFun: true
  173. }).then(res => {
  174. wx.showToast({
  175. title: '新增成功',
  176. icon: 'success',
  177. duration: 2000,
  178. mask:true,
  179. complete: function () {
  180. setTimeout(() => {
  181. wx.navigateBack()
  182. }, 1500) //延迟时间
  183. }
  184. })
  185. })
  186. }
  187. }
  188. })