addData.vue 8.9 KB

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