addData.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. <template>
  2. <view class="content">
  3. <view class="content-form">
  4. <view class="form-data">
  5. <u-form :model="form" ref="uForm" :label-width="140" :error-type="['toast']">
  6. <u-form-item prop="gatherNum_huishou" label="可回收物:">
  7. <u-input :disabled="isView" @input="numberToFixed('gatherNum_huishou',2,999999)" type="digit" placeholder="请输入" v-model="form.gatherNum_huishou" />
  8. <text slot="right" style="color: #000000;">KG</text>
  9. </u-form-item>
  10. <u-form-item prop="gatherNum_harm" label="有害垃圾:" >
  11. <u-input :disabled="isView" @input="numberToFixed('gatherNum_harm',2,999999)" placeholder="请输入" type="digit" v-model="form.gatherNum_harm" />
  12. <text slot="right" style="color: #000000;">KG</text>
  13. </u-form-item>
  14. <u-gap height="20" bg-color="#f5f5f5"></u-gap>
  15. <u-form-item prop="gatherNum_chuyu" label="厨余垃圾:" >
  16. <u-input :disabled="isView" @input="numberToFixed('gatherNum_chuyu',2,999999)" placeholder="请输入" type="digit" v-model="form.gatherNum_chuyu" />
  17. <text slot="right" style="color: #000000;">L(约{{form.gatherNum_chuyu*1}}KG)</text>
  18. </u-form-item>
  19. <u-form-item prop="gatherNum_other" label="其他垃圾:" >
  20. <u-input :disabled="isView" @input="numberToFixed('gatherNum_other',2,999999)" type="digit" placeholder="请输入" v-model="form.gatherNum_other" />
  21. <text slot="right" style="color: #000000;">L(约{{form.gatherNum_other*1}}KG)</text>
  22. </u-form-item>
  23. <u-gap height="20" bg-color="#f5f5f5"></u-gap>
  24. <u-form-item prop="gatherNum_jianzhu" label="建筑垃圾:" >
  25. <u-input :disabled="isView" @input="numberToFixed('gatherNum_jianzhu',2,999999)" placeholder="请输入" type="digit" v-model="form.gatherNum_jianzhu" />
  26. <text slot="right" style="color: #000000;">KG</text>
  27. </u-form-item>
  28. <u-gap height="20" bg-color="#f5f5f5"></u-gap>
  29. <u-form-item prop="remarks" label="信息备注:">
  30. <u-input :disabled="isView" placeholder="请输入" v-model="form.remarks" :maxlength="30" class="receiveAddress" height="0" auto-height :clearable="false"></u-input>
  31. </u-form-item>
  32. </u-form>
  33. </view>
  34. <!-- <view class="form-data remark">
  35. <text>信息备注</text>
  36. <textarea :disabled="isView" :auto-height="true" :maxlength="30" v-model="form.remarks" class="receiveAddress" placeholder="请输入" />
  37. </view> -->
  38. <view class="form-data qyImg">
  39. <text>清运照片</text>
  40. <view class="info-main">
  41. <view v-if="photograph.length<3 && !isView" class="camera-btn">
  42. <view @click="goPhotograph" class="photograph-camera">
  43. <u-icon name="camera" color="#bfbfbf" size="60" ></u-icon>
  44. </view>
  45. <view >
  46. (最多3张)
  47. </view>
  48. <view >
  49. (每张最大不超过10M)
  50. </view>
  51. </view>
  52. <view v-show="photograph.length" v-for="(item,index) in photograph" :key="index" class="photograph-con">
  53. <u-icon v-show="!isView" name="close-circle-fill" color="#fa3534" size="50" class="close-circle-icon" @click="cancelPhotograph(index)"></u-icon>
  54. <u-image width="100%" height="100%" :src="item" @click="previewPictures(item)"></u-image>
  55. </view>
  56. </view>
  57. </view>
  58. </view>
  59. <view class="content-btn">
  60. <u-button v-if="isView && nowDate == gatherTime" @click="isView = false" class="submitBtn" type="waring" shape="circle">修改</u-button>
  61. <u-button v-if="!isView" @click="submit" :loading="loading" class="submitBtn" type="success" shape="circle">提交</u-button>
  62. </view>
  63. <uni-popup ref="openModal" type="center">
  64. <uni-popup-dialog content="确认提交吗?" @confirm="onOk" :title="title"></uni-popup-dialog>
  65. </uni-popup>
  66. </view>
  67. </template>
  68. <script>
  69. import uniPopup from '@/components/uni-popup/uni-popup.vue'
  70. import uniPopupDialog from '@/components/uni-popup/uni-popup-dialog.vue'
  71. import {gatherSave,gatherDetail} from '@/api/index.js'
  72. import {numberToFixed,saveImgToAliOss} from '@/libs/tools.js'
  73. export default {
  74. components: {
  75. uniPopup,uniPopupDialog
  76. },
  77. data() {
  78. return {
  79. title:'',
  80. loading:false, // 提交按钮加载圈
  81. photograph: [], // 图片路径
  82. form: {
  83. gatherNum_huishou: '',
  84. gatherNum_harm: '',
  85. gatherNum_other: '', // 其它垃圾
  86. gatherNum_chuyu: '', // 厨余垃圾
  87. gatherNum_jianzhu: '', // 建筑垃圾
  88. remarks: '' // 备注
  89. },
  90. isView: false ,// 是否是查看详情
  91. itemId:'',
  92. nowDate:'',
  93. gatherTime:""
  94. };
  95. },
  96. onLoad(option) {
  97. console.log(option,option.id,'ppppppppp')
  98. // 查看
  99. if(option && option.id){
  100. this.isView = true
  101. this.itemId=option.id
  102. this.getDetailData()
  103. }else {
  104. console.log('--------新增')
  105. this.photograph = []
  106. this.form.remarks = ''
  107. this.$refs.uForm.resetFields()
  108. }
  109. const date = new Date()
  110. const year = date.getFullYear()
  111. const month = date.getMonth()+1
  112. const day = date.getDate()
  113. this.nowDate=[year,month<10? '0'+month : month,day<10? '0'+day : day].join('-')
  114. console.log(this.nowDate,'----------当前时间')
  115. },
  116. onUnload() {
  117. },
  118. methods: {
  119. // 保留几位小数
  120. numberToFixed: function (key, num, max) {
  121. let val = this.form[key]
  122. let ret = numberToFixed(val, num, max)
  123. this.$nextTick(() => {
  124. this.form[key] = ret
  125. })
  126. },
  127. // 获取详情数据
  128. getDetailData(){
  129. gatherDetail({id:this.itemId}).then(res=>{
  130. if(res.status==200){
  131. console.log(res)
  132. const a = res.data.gatherRubbishList.find(item=>item.rubbishType=='GATHER_BUILDING')
  133. const b = res.data.gatherRubbishList.find(item=>item.rubbishType=='GATHER_OTHER')
  134. const c = res.data.gatherRubbishList.find(item=>item.rubbishType=='GATHER_KITCHEN')
  135. const d = res.data.gatherRubbishList.find(item=>item.rubbishType=='GATHER_HARM')
  136. const e = res.data.gatherRubbishList.find(item=>item.rubbishType=='GATHER_RECYCLING')
  137. this.form.gatherNum_jianzhu= a ? a.gatherNum.toString() : '0'
  138. this.form.gatherNum_chuyu= c ? c.gatherNum.toString() : '0'
  139. this.form.gatherNum_other= b ? b.gatherNum.toString() : '0'
  140. this.form.gatherNum_harm= d ? d.gatherNum.toString() : '0'
  141. this.form.gatherNum_huishou= e ? e.gatherNum.toString() : '0'
  142. this.form.remarks=res.data.remarks
  143. this.photograph=res.data.imageUrlList || []
  144. console.log(this.form,'this.form')
  145. this.gatherTime=res.data.gatherTime.substring(0,10)
  146. console.log(this.gatherTime,'--------------this.gatherTime')
  147. }
  148. })
  149. },
  150. // 拍照或从相册选图片
  151. goPhotograph() {
  152. uni.chooseImage({
  153. count: 1, //默认9
  154. sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  155. sourceType: ['album','camera'], //从相册选择
  156. success: (res) =>{
  157. console.log(res,'rrrrrrrrrrr')
  158. console.log(JSON.stringify(res.tempFilePaths));
  159. console.log(this.photograph.length,'this.photograph')
  160. if(res.tempFiles[0].size>10485760){
  161. uni.showToast({
  162. title: '图片过大无法上传!',
  163. icon: 'none'
  164. })
  165. return
  166. }
  167. uni.showLoading({
  168. title:'正在保存图片...',
  169. mask: true
  170. })
  171. saveImgToAliOss(res.tempFilePaths[0],(ret)=>{
  172. // console.log(ret,'----------ret')
  173. this.photograph.push(ret.data)
  174. // _this.photograph = ret.data
  175. uni.hideLoading()
  176. })
  177. }
  178. });
  179. },
  180. // 预览图片
  181. previewPictures(item) {
  182. const photograph = [item];
  183. uni.previewImage({
  184. urls: photograph,
  185. });
  186. },
  187. // 删除图片
  188. cancelPhotograph(index) {
  189. setTimeout(() => {
  190. this.photograph.splice(index,1)
  191. }, 500);
  192. },
  193. // 提交
  194. submit(){
  195. if((!this.form.gatherNum_huishou ||this.form.gatherNum_huishou==0) && (!this.form.gatherNum_harm ||this.form.gatherNum_harm==0) &&(!this.form.gatherNum_other ||this.form.gatherNum_other==0) && (!this.form.gatherNum_chuyu || this.form.gatherNum_chuyu==0) && (!this.form.gatherNum_jianzhu || this.form.gatherNum_jianzhu==0)) {
  196. uni.showToast({
  197. title: '请至少输入一项垃圾分类的数量!',
  198. icon: 'none'
  199. })
  200. return false
  201. } else {
  202. this.$refs.openModal.open()
  203. }
  204. },
  205. // 确认提交
  206. onOk() {
  207. this.$refs.openModal.close()
  208. let params = {
  209. rubbishEntityList: [],
  210. imageUrlList: this.photograph,
  211. remarks: this.form.remarks
  212. }
  213. if(this.itemId){
  214. params.id=this.itemId
  215. }
  216. if(this.form.gatherNum_huishou) {
  217. params.rubbishEntityList.push({
  218. "rubbishType":"GATHER_RECYCLING",
  219. "rubbishWeight": this.form.gatherNum_huishou*1000,
  220. "gatherNum": this.form.gatherNum_huishou,
  221. "unit":"KILOGRAM"
  222. })
  223. }
  224. if(this.form.gatherNum_harm) {
  225. params.rubbishEntityList.push({
  226. "rubbishType":"GATHER_HARM",
  227. "rubbishWeight": this.form.gatherNum_harm*1000,
  228. "gatherNum": this.form.gatherNum_harm,
  229. "unit":"KILOGRAM"
  230. })
  231. }
  232. if(this.form.gatherNum_other) {
  233. params.rubbishEntityList.push({
  234. "rubbishType":"GATHER_OTHER",
  235. "rubbishWeight": this.form.gatherNum_other*1000,
  236. "gatherNum": this.form.gatherNum_other,
  237. "unit":"LITER"
  238. })
  239. }
  240. if(this.form.gatherNum_chuyu) {
  241. params.rubbishEntityList.push({
  242. "rubbishType":"GATHER_KITCHEN",
  243. "rubbishWeight": this.form.gatherNum_chuyu*1000,
  244. "gatherNum": this.form.gatherNum_chuyu,
  245. "unit":"LITER"
  246. })
  247. }
  248. if(this.form.gatherNum_jianzhu) {
  249. params.rubbishEntityList.push({
  250. "rubbishType":"GATHER_BUILDING",
  251. "rubbishWeight": this.form.gatherNum_jianzhu*1000,
  252. "gatherNum": this.form.gatherNum_jianzhu,
  253. "unit":"KILOGRAM"
  254. })
  255. }
  256. this.loading=true
  257. gatherSave(params).then(res=>{
  258. if(res.status == 200) {
  259. uni.showToast({
  260. title: res.message,
  261. icon: 'none'
  262. })
  263. setTimeout(()=>{
  264. uni.navigateBack()
  265. },300)
  266. } else {
  267. uni.showToast({
  268. title: res.message,
  269. icon: 'none'
  270. })
  271. }
  272. this.loading= false
  273. })
  274. },
  275. },
  276. // 必须要在onReady生命周期,因为onLoad生命周期组件可能尚未创建完毕
  277. onReady() {
  278. this.$refs.uForm.setRules(this.rules);
  279. }
  280. }
  281. </script>
  282. <style lang="scss" scoped>
  283. .content{
  284. width: 100%;
  285. height: 100vh;
  286. padding: 0;
  287. background: #F5F5F5;
  288. display: flex;
  289. flex-direction: column;
  290. .receiveAddress{
  291. width: 100%;
  292. line-height: 1.5em;
  293. box-sizing: border-box;
  294. font-style: normal;
  295. }
  296. .content-form{
  297. flex:1;
  298. overflow-y: scroll;
  299. }
  300. .form-data{
  301. // padding: 40upx;
  302. margin-bottom: 20upx;
  303. background-color: #fff;
  304. /deep/ .u-form-item {
  305. padding:20rpx 40rpx;
  306. }
  307. }
  308. .u-form-item{
  309. padding: 0 40upx;
  310. }
  311. .remark{
  312. padding: 40upx ;
  313. }
  314. .qyImg{
  315. padding: 40upx ;
  316. }
  317. .info-main {
  318. margin-top: 14upx;
  319. border-radius: 12upx;
  320. background-color: #fff;
  321. display: flex;
  322. .location-icon {
  323. padding-left: 10upx;
  324. vertical-align: sub;
  325. }
  326. .photograph-camera {
  327. border: 2upx dashed #bfbfbf;
  328. border-radius: 12upx;
  329. width: 140upx;
  330. height: 140upx;
  331. display: flex;
  332. align-items: center;
  333. justify-content: center;
  334. }
  335. .camera-btn{
  336. display: flex;
  337. flex-direction: column;
  338. align-items: center;
  339. color: #999999;
  340. font-size: 24upx;
  341. }
  342. .photograph-con {
  343. margin: 0 20upx;
  344. width: 140upx;
  345. height: 140upx;
  346. text-align: center;
  347. position: relative;
  348. .photograph-icon {
  349. display: inline-block;
  350. padding-top: 48upx;
  351. }
  352. .close-circle-icon {
  353. background-color: #fff;
  354. border-radius: 50%;
  355. position: absolute;
  356. right: -23upx;
  357. top: -23upx;
  358. z-index: 9;
  359. }
  360. }
  361. }
  362. .content-btn{
  363. // background-color: #fff;
  364. padding: 40rpx;
  365. }
  366. .submitBtn{
  367. font-size: 32upx;
  368. color: #fff;
  369. border-radius: 50upx;
  370. }
  371. }
  372. </style>