addData.vue 8.8 KB

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