addData.vue 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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 : '0'
  110. this.form.gatherNum_chuyu= c ? c.gatherNum : '0'
  111. this.form.gatherNum_other= b ? b.gatherNum : '0'
  112. this.form.remarks=res.data.remarks
  113. this.photograph=res.data.imageUrlList || []
  114. }
  115. })
  116. },
  117. // 拍照或从相册选图片
  118. goPhotograph() {
  119. uni.chooseImage({
  120. count: 1, //默认9
  121. sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  122. sourceType: ['album','camera'], //从相册选择
  123. success: (res) =>{
  124. console.log(res,'rrrrrrrrrrr')
  125. console.log(JSON.stringify(res.tempFilePaths));
  126. console.log(this.photograph.length,'this.photograph')
  127. if(res.tempFiles[0].size>10485760){
  128. uni.showToast({
  129. title: '图片过大无法上传!',
  130. icon: 'none'
  131. })
  132. return
  133. }
  134. uni.showLoading({
  135. title:'正在保存图片...',
  136. mask: true
  137. })
  138. saveImgToAliOss(res.tempFilePaths[0],(ret)=>{
  139. // console.log(ret,'----------ret')
  140. this.photograph.push(ret.data)
  141. // _this.photograph = ret.data
  142. uni.hideLoading()
  143. })
  144. }
  145. });
  146. },
  147. // 预览图片
  148. previewPictures(item) {
  149. const photograph = [item];
  150. uni.previewImage({
  151. urls: photograph,
  152. });
  153. },
  154. // 删除图片
  155. cancelPhotograph(index) {
  156. setTimeout(() => {
  157. this.photograph.splice(index,1)
  158. }, 500);
  159. },
  160. // 提交
  161. submit(){
  162. 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)) {
  163. uni.showToast({
  164. title: '请至少输入一项垃圾分类的数量!',
  165. icon: 'none'
  166. })
  167. return false
  168. } else {
  169. this.$refs.openModal.open()
  170. }
  171. },
  172. // 确认提交
  173. onOk() {
  174. this.$refs.openModal.close()
  175. let params = {
  176. rubbishEntityList: [],
  177. imageUrlList: this.photograph,
  178. remarks: this.form.remarks
  179. }
  180. if(this.itemId){
  181. params.id=this.itemId
  182. }
  183. if(this.form.gatherNum_other) {
  184. params.rubbishEntityList.push({
  185. "rubbishType":"GATHER_OTHER",
  186. "rubbishWeight": this.form.gatherNum_other*240,
  187. "gatherNum": this.form.gatherNum_other,
  188. "unit":"BUCKET"
  189. })
  190. }
  191. if(this.form.gatherNum_chuyu) {
  192. params.rubbishEntityList.push({
  193. "rubbishType":"GATHER_KITCHEN",
  194. "rubbishWeight": this.form.gatherNum_chuyu*240,
  195. "gatherNum": this.form.gatherNum_chuyu,
  196. "unit":"BUCKET"
  197. })
  198. }
  199. if(this.form.gatherNum_jianzhu) {
  200. params.rubbishEntityList.push({
  201. "rubbishType":"GATHER_BUILDING",
  202. "rubbishWeight": this.form.gatherNum_jianzhu*1000,
  203. "gatherNum": this.form.gatherNum_jianzhu,
  204. "unit":"TON"
  205. })
  206. }
  207. this.loading=true
  208. gatherSave(params).then(res=>{
  209. if(res.status == 200) {
  210. uni.showToast({
  211. title: res.message,
  212. icon: 'none'
  213. })
  214. setTimeout(()=>{
  215. uni.navigateBack()
  216. },300)
  217. } else {
  218. uni.showToast({
  219. title: res.message,
  220. icon: 'none'
  221. })
  222. }
  223. this.loading= false
  224. })
  225. },
  226. },
  227. // 必须要在onReady生命周期,因为onLoad生命周期组件可能尚未创建完毕
  228. onReady() {
  229. this.$refs.uForm.setRules(this.rules);
  230. }
  231. }
  232. </script>
  233. <style lang="scss">
  234. .content{
  235. width: 100%;
  236. height: 100vh;
  237. padding: 0;
  238. background: #F5F5F5;
  239. // .buttons{
  240. // font-size: 32upx;
  241. // display: block;
  242. // height: 80upx;
  243. // line-height: 80upx;
  244. // margin: 0 auto;
  245. // background-color: #0DC55F;
  246. // color: #fff;
  247. // border-radius: 50upx;
  248. // }
  249. .receiveAddress{
  250. width: 100%;
  251. line-height: 1.5em;
  252. box-sizing: border-box;
  253. font-style: normal;
  254. }
  255. .form-data{
  256. padding: 0 40upx;
  257. margin-bottom: 20upx;
  258. background-color: #fff;
  259. }
  260. .remark{
  261. padding: 40upx ;
  262. }
  263. .qyImg{
  264. padding: 40upx ;
  265. }
  266. .info-main {
  267. margin-top: 14upx;
  268. border-radius: 12upx;
  269. background-color: #fff;
  270. display: flex;
  271. .location-icon {
  272. padding-left: 10upx;
  273. vertical-align: sub;
  274. }
  275. .photograph-camera {
  276. border: 2upx dashed #bfbfbf;
  277. border-radius: 12upx;
  278. width: 140upx;
  279. height: 140upx;
  280. display: flex;
  281. align-items: center;
  282. justify-content: center;
  283. }
  284. .camera-btn{
  285. display: flex;
  286. flex-direction: column;
  287. align-items: center;
  288. color: #999999;
  289. font-size: 24upx;
  290. }
  291. .photograph-con {
  292. margin: 0 20upx;
  293. width: 140upx;
  294. height: 140upx;
  295. text-align: center;
  296. position: relative;
  297. .photograph-icon {
  298. display: inline-block;
  299. padding-top: 48upx;
  300. }
  301. .close-circle-icon {
  302. background-color: #fff;
  303. border-radius: 50%;
  304. position: absolute;
  305. right: -23upx;
  306. top: -23upx;
  307. z-index: 9;
  308. }
  309. }
  310. }
  311. .submitBtn{
  312. font-size: 32upx;
  313. display: block;
  314. width: 670upx;
  315. height: 80upx;
  316. line-height: 80upx;
  317. margin: 0 auto;
  318. color: #fff;
  319. border-radius: 50upx;
  320. }
  321. }
  322. </style>