addData.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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" 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.form.gatherNum_other) {
  170. params.rubbishEntityList.push({
  171. "rubbishType":"GATHER_OTHER",
  172. "rubbishWeight": this.form.gatherNum_other*240,
  173. "gatherNum": this.form.gatherNum_other,
  174. "unit":"BUCKET"
  175. })
  176. }
  177. if(this.form.gatherNum_chuyu) {
  178. params.rubbishEntityList.push({
  179. "rubbishType":"GATHER_KITCHEN",
  180. "rubbishWeight": this.form.gatherNum_chuyu*240,
  181. "gatherNum": this.form.gatherNum_chuyu,
  182. "unit":"BUCKET"
  183. })
  184. }
  185. if(this.form.gatherNum_jianzhu) {
  186. params.rubbishEntityList.push({
  187. "rubbishType":"GATHER_BUILDING",
  188. "rubbishWeight": this.form.gatherNum_jianzhu*1000,
  189. "gatherNum": this.form.gatherNum_jianzhu,
  190. "unit":"TON"
  191. })
  192. }
  193. this.loading=true
  194. gatherSave(params).then(res=>{
  195. if(res.status == 200) {
  196. uni.showToast({
  197. title: res.message,
  198. icon: 'none'
  199. })
  200. setTimeout(()=>{
  201. uni.navigateBack()
  202. },300)
  203. } else {
  204. uni.showToast({
  205. title: res.message,
  206. icon: 'none'
  207. })
  208. }
  209. this.loading= false
  210. })
  211. },
  212. },
  213. // 必须要在onReady生命周期,因为onLoad生命周期组件可能尚未创建完毕
  214. onReady() {
  215. this.$refs.uForm.setRules(this.rules);
  216. }
  217. }
  218. </script>
  219. <style lang="scss">
  220. .content{
  221. width: 100%;
  222. height: 100vh;
  223. padding: 0;
  224. background: #F5F5F5;
  225. // .buttons{
  226. // font-size: 32upx;
  227. // display: block;
  228. // height: 80upx;
  229. // line-height: 80upx;
  230. // margin: 0 auto;
  231. // background-color: #0DC55F;
  232. // color: #fff;
  233. // border-radius: 50upx;
  234. // }
  235. .receiveAddress{
  236. width: 100%;
  237. line-height: 1.5em;
  238. box-sizing: border-box;
  239. font-style: normal;
  240. }
  241. .form-data{
  242. padding: 0 40upx;
  243. margin-bottom: 20upx;
  244. background-color: #fff;
  245. }
  246. .remark{
  247. padding: 40upx ;
  248. }
  249. .qyImg{
  250. padding: 40upx ;
  251. }
  252. .info-main {
  253. margin-top: 14upx;
  254. border-radius: 12upx;
  255. background-color: #fff;
  256. display: flex;
  257. .location-icon {
  258. padding-left: 10upx;
  259. vertical-align: sub;
  260. }
  261. .photograph-camera {
  262. border: 2upx dashed #bfbfbf;
  263. border-radius: 12upx;
  264. width: 140upx;
  265. height: 140upx;
  266. display: flex;
  267. align-items: center;
  268. justify-content: center;
  269. }
  270. .camera-btn{
  271. display: flex;
  272. flex-direction: column;
  273. align-items: center;
  274. color: #999999;
  275. }
  276. .photograph-con {
  277. margin: 0 20upx;
  278. width: 140upx;
  279. height: 140upx;
  280. text-align: center;
  281. position: relative;
  282. .photograph-icon {
  283. display: inline-block;
  284. padding-top: 48upx;
  285. }
  286. .close-circle-icon {
  287. background-color: #fff;
  288. border-radius: 50%;
  289. position: absolute;
  290. right: -23upx;
  291. top: -23upx;
  292. z-index: 9;
  293. }
  294. }
  295. }
  296. .submitBtn{
  297. font-size: 32upx;
  298. display: block;
  299. width: 670upx;
  300. height: 80upx;
  301. line-height: 80upx;
  302. margin: 0 auto;
  303. color: #fff;
  304. border-radius: 50upx;
  305. }
  306. }
  307. </style>