addData.vue 9.3 KB

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