addData.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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" @click="isView = false" class="submitBtn" type="waring" shape="circle">修改</u-button>
  61. <u-button v-else @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. };
  93. },
  94. onLoad(option) {
  95. console.log(option,option.id,'ppppppppp')
  96. // 查看
  97. if(option && option.id){
  98. this.isView = true
  99. this.itemId=option.id
  100. this.getDetailData()
  101. }else {
  102. console.log('--------新增')
  103. this.photograph = []
  104. this.form.remarks = ''
  105. this.$refs.uForm.resetFields()
  106. }
  107. },
  108. onUnload() {
  109. },
  110. methods: {
  111. // 保留几位小数
  112. numberToFixed: function (key, num, max) {
  113. let val = this.form[key]
  114. let ret = numberToFixed(val, num, max)
  115. this.$nextTick(() => {
  116. this.form[key] = ret
  117. })
  118. },
  119. // 获取详情数据
  120. getDetailData(){
  121. gatherDetail({id:this.itemId}).then(res=>{
  122. if(res.status==200){
  123. console.log(res)
  124. const a = res.data.gatherRubbishList.find(item=>item.rubbishType=='GATHER_BUILDING')
  125. const b = res.data.gatherRubbishList.find(item=>item.rubbishType=='GATHER_OTHER')
  126. const c = res.data.gatherRubbishList.find(item=>item.rubbishType=='GATHER_KITCHEN')
  127. const d = res.data.gatherRubbishList.find(item=>item.rubbishType=='GATHER_HARM')
  128. const e = res.data.gatherRubbishList.find(item=>item.rubbishType=='GATHER_RECYCLING')
  129. this.form.gatherNum_jianzhu= a ? a.gatherNum.toString() : '0'
  130. this.form.gatherNum_chuyu= c ? c.gatherNum.toString() : '0'
  131. this.form.gatherNum_other= b ? b.gatherNum.toString() : '0'
  132. this.form.gatherNum_harm= d ? d.gatherNum.toString() : '0'
  133. this.form.gatherNum_huishou= e ? e.gatherNum.toString() : '0'
  134. this.form.remarks=res.data.remarks
  135. this.photograph=res.data.imageUrlList || []
  136. console.log(this.form,'this.form')
  137. }
  138. })
  139. },
  140. // 拍照或从相册选图片
  141. goPhotograph() {
  142. uni.chooseImage({
  143. count: 1, //默认9
  144. sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  145. sourceType: ['album','camera'], //从相册选择
  146. success: (res) =>{
  147. console.log(res,'rrrrrrrrrrr')
  148. console.log(JSON.stringify(res.tempFilePaths));
  149. console.log(this.photograph.length,'this.photograph')
  150. if(res.tempFiles[0].size>10485760){
  151. uni.showToast({
  152. title: '图片过大无法上传!',
  153. icon: 'none'
  154. })
  155. return
  156. }
  157. uni.showLoading({
  158. title:'正在保存图片...',
  159. mask: true
  160. })
  161. saveImgToAliOss(res.tempFilePaths[0],(ret)=>{
  162. // console.log(ret,'----------ret')
  163. this.photograph.push(ret.data)
  164. // _this.photograph = ret.data
  165. uni.hideLoading()
  166. })
  167. }
  168. });
  169. },
  170. // 预览图片
  171. previewPictures(item) {
  172. const photograph = [item];
  173. uni.previewImage({
  174. urls: photograph,
  175. });
  176. },
  177. // 删除图片
  178. cancelPhotograph(index) {
  179. setTimeout(() => {
  180. this.photograph.splice(index,1)
  181. }, 500);
  182. },
  183. // 提交
  184. submit(){
  185. 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)) {
  186. uni.showToast({
  187. title: '请至少输入一项垃圾分类的数量!',
  188. icon: 'none'
  189. })
  190. return false
  191. } else {
  192. this.$refs.openModal.open()
  193. }
  194. },
  195. // 确认提交
  196. onOk() {
  197. this.$refs.openModal.close()
  198. let params = {
  199. rubbishEntityList: [],
  200. imageUrlList: this.photograph,
  201. remarks: this.form.remarks
  202. }
  203. if(this.itemId){
  204. params.id=this.itemId
  205. }
  206. if(this.form.gatherNum_huishou) {
  207. params.rubbishEntityList.push({
  208. "rubbishType":"GATHER_RECYCLING",
  209. "rubbishWeight": this.form.gatherNum_huishou*1000,
  210. "gatherNum": this.form.gatherNum_huishou,
  211. "unit":"KILOGRAM"
  212. })
  213. }
  214. if(this.form.gatherNum_harm) {
  215. params.rubbishEntityList.push({
  216. "rubbishType":"GATHER_HARM",
  217. "rubbishWeight": this.form.gatherNum_harm*1000,
  218. "gatherNum": this.form.gatherNum_harm,
  219. "unit":"KILOGRAM"
  220. })
  221. }
  222. if(this.form.gatherNum_other) {
  223. params.rubbishEntityList.push({
  224. "rubbishType":"GATHER_OTHER",
  225. "rubbishWeight": this.form.gatherNum_other*1000,
  226. "gatherNum": this.form.gatherNum_other,
  227. "unit":"LITER"
  228. })
  229. }
  230. if(this.form.gatherNum_chuyu) {
  231. params.rubbishEntityList.push({
  232. "rubbishType":"GATHER_KITCHEN",
  233. "rubbishWeight": this.form.gatherNum_chuyu*1000,
  234. "gatherNum": this.form.gatherNum_chuyu,
  235. "unit":"LITER"
  236. })
  237. }
  238. if(this.form.gatherNum_jianzhu) {
  239. params.rubbishEntityList.push({
  240. "rubbishType":"GATHER_BUILDING",
  241. "rubbishWeight": this.form.gatherNum_jianzhu*1000,
  242. "gatherNum": this.form.gatherNum_jianzhu,
  243. "unit":"KILOGRAM"
  244. })
  245. }
  246. this.loading=true
  247. gatherSave(params).then(res=>{
  248. if(res.status == 200) {
  249. uni.showToast({
  250. title: res.message,
  251. icon: 'none'
  252. })
  253. setTimeout(()=>{
  254. uni.navigateBack()
  255. },300)
  256. } else {
  257. uni.showToast({
  258. title: res.message,
  259. icon: 'none'
  260. })
  261. }
  262. this.loading= false
  263. })
  264. },
  265. },
  266. // 必须要在onReady生命周期,因为onLoad生命周期组件可能尚未创建完毕
  267. onReady() {
  268. this.$refs.uForm.setRules(this.rules);
  269. }
  270. }
  271. </script>
  272. <style lang="scss">
  273. .content{
  274. width: 100%;
  275. height: 100vh;
  276. padding: 0;
  277. background: #F5F5F5;
  278. display: flex;
  279. flex-direction: column;
  280. .receiveAddress{
  281. width: 100%;
  282. line-height: 1.5em;
  283. box-sizing: border-box;
  284. font-style: normal;
  285. }
  286. .content-form{
  287. flex:1;
  288. overflow-y: scroll;
  289. }
  290. .form-data{
  291. // padding: 40upx;
  292. margin-bottom: 20upx;
  293. background-color: #fff;
  294. .u-form-item.data-v-5e7216f1 {
  295. padding:20rpx 40rpx;
  296. }
  297. }
  298. .u-form-item{
  299. padding: 0 40upx;
  300. }
  301. .remark{
  302. padding: 40upx ;
  303. }
  304. .qyImg{
  305. padding: 40upx ;
  306. }
  307. .info-main {
  308. margin-top: 14upx;
  309. border-radius: 12upx;
  310. background-color: #fff;
  311. display: flex;
  312. .location-icon {
  313. padding-left: 10upx;
  314. vertical-align: sub;
  315. }
  316. .photograph-camera {
  317. border: 2upx dashed #bfbfbf;
  318. border-radius: 12upx;
  319. width: 140upx;
  320. height: 140upx;
  321. display: flex;
  322. align-items: center;
  323. justify-content: center;
  324. }
  325. .camera-btn{
  326. display: flex;
  327. flex-direction: column;
  328. align-items: center;
  329. color: #999999;
  330. font-size: 24upx;
  331. }
  332. .photograph-con {
  333. margin: 0 20upx;
  334. width: 140upx;
  335. height: 140upx;
  336. text-align: center;
  337. position: relative;
  338. .photograph-icon {
  339. display: inline-block;
  340. padding-top: 48upx;
  341. }
  342. .close-circle-icon {
  343. background-color: #fff;
  344. border-radius: 50%;
  345. position: absolute;
  346. right: -23upx;
  347. top: -23upx;
  348. z-index: 9;
  349. }
  350. }
  351. }
  352. .content-btn{
  353. background-color: #fff;
  354. padding: 40rpx;
  355. }
  356. .submitBtn{
  357. font-size: 32upx;
  358. color: #fff;
  359. border-radius: 50upx;
  360. }
  361. }
  362. </style>