You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

381 lines
8.8 KiB

1 week ago
  1. <template>
  2. <view class="personal-pet-cat container">
  3. <view class="personal-pet-img">
  4. <view class="personal-pet-info-title">
  5. 宠物头像
  6. </view>
  7. <view style="display: flex;justify-content: center;">
  8. <u-upload
  9. accept="image"
  10. :capture="['album','camera']"
  11. :fileList="fileList"
  12. @afterRead="afterRead"
  13. @delete="deletePic"
  14. :max-count="1"
  15. name="pet"
  16. width="60"
  17. height="60"
  18. :custom-style="{flex:0}"
  19. >
  20. <image src="https://catmdogf.oss-cn-shanghai.aliyuncs.com/CMDF/front/personal/index/cat_new.png" style="width: 60px;height: 60px;"></image>
  21. </u-upload>
  22. </view>
  23. </view>
  24. <PetBaseInfo :petType="petType" :petBaseInfo.sync="petBaseInfo" />
  25. <PetHealthInfo :petType="petType" :petHealthInfo.sync="petHealthInfo"/>
  26. <view class="personal-pet-info-btns">
  27. <view class="personal-pet-btns">
  28. <view class="personal-pet-btn">
  29. <u-button :disabled="optionType !='edit'" color="#FFF4E4" @click="deletePet">
  30. <view style="color: #FFAA48;">
  31. 删除
  32. </view>
  33. </u-button>
  34. </view>
  35. <view class="personal-pet-btn" @click="save">
  36. <u-button color="#FFBF60" :loading="loading">
  37. <view style="color: #fff;">
  38. 保存
  39. </view>
  40. </u-button>
  41. </view>
  42. </view>
  43. </view>
  44. <u-modal :show="showDel"
  45. @confirm="confirmDel"
  46. @cancel="showDel = false;"
  47. ref="uModal"
  48. showCancelButton
  49. :asyncClose="true"
  50. :content='delContent'>
  51. </u-modal>
  52. </view>
  53. </template>
  54. <script>
  55. import {addPet,getPetDetails,updatePet,delPet} from '@/api/system/pet'
  56. import PetBaseInfo from './components/petBaseInfo.vue'
  57. import PetHealthInfo from './components/petHealthInfo.vue'
  58. export default{
  59. data(){
  60. return {
  61. loading:false,
  62. fileList:[],
  63. petId:'',
  64. petType:'dog',
  65. optionType:'add',
  66. isNewOrder:false,
  67. delContent:'',
  68. photo:'',
  69. petBaseInfo:{
  70. name: '',
  71. gender: '',
  72. breed: '',
  73. bodyType: '',
  74. birthDate: '',
  75. personality: ''
  76. },
  77. petHealthInfo: {
  78. vaccineStatus: '',
  79. dewormingStatus: '',
  80. sterilization: '',
  81. doglicenseStatus:'',
  82. healthStatus: [],
  83. remark:''
  84. },
  85. showDel:false,
  86. }
  87. },
  88. components:{
  89. PetBaseInfo,
  90. PetHealthInfo
  91. },
  92. onLoad: function(option) {
  93. console.log(option.petType); //打印出上个页面传递的参数。
  94. this.petType = option.petType;
  95. this.optionType=option.optionType;
  96. if(this.optionType=='edit'){
  97. this.petId=option.petId;
  98. this.getPetDetails(option.petId);
  99. }
  100. if(option.isNewOrder){
  101. this.isNewOrder=true;
  102. }
  103. },
  104. methods:{
  105. // 删除图片
  106. deletePic(event) {
  107. this.fileList.splice(event.index, 1)
  108. },
  109. // 新增图片
  110. async afterRead(event) {
  111. // 当设置 multiple 为 true 时, file 为数组格式,否则为对象格式
  112. let lists = [].concat(event.file)
  113. let fileListLen = this.fileList.length
  114. lists.map((item) => {
  115. this.fileList.push({
  116. ...item,
  117. status: 'uploading',
  118. message: '上传中'
  119. })
  120. })
  121. for (let i = 0; i < lists.length; i++) {
  122. const result = await this.uploadFilePromise(lists[i].url)
  123. let item = this.fileList[fileListLen]
  124. this.fileList.splice(fileListLen, 1, Object.assign(item, {
  125. status: 'success',
  126. message: '',
  127. url: result
  128. }))
  129. fileListLen++
  130. }
  131. },
  132. uploadFilePromise(url) {
  133. return new Promise((resolve, reject) => {
  134. let a = uni.uploadFile({
  135. url: 'https://store-test.catmdogd.com/test-api/h5/oss/upload',
  136. filePath: url,
  137. name: 'file',
  138. formData: {
  139. user: 'test'
  140. },
  141. success: (res) => {
  142. setTimeout(() => {
  143. if(res&&res.data){
  144. let resData = JSON.parse(res.data);
  145. resolve(resData.url);
  146. }
  147. reject("上传失败");
  148. }, 1000)
  149. }
  150. });
  151. })
  152. },
  153. getPetDetails(petId){
  154. getPetDetails(petId).then(res=>{
  155. if(res&& res.id){
  156. const { photo, name, gender,breed, bodyType, birthDate, personality, vaccineStatus, dewormingStatus, sterilization, doglicenseStatus, healthStatus,remark } = res;
  157. this.petBaseInfo = {
  158. name,
  159. gender,
  160. breed,
  161. bodyType,
  162. birthDate,
  163. personality
  164. };
  165. this.petHealthInfo = {
  166. vaccineStatus,
  167. dewormingStatus,
  168. sterilization,
  169. doglicenseStatus,
  170. healthStatus,
  171. remark
  172. }
  173. this.fileList=[{url:photo}]
  174. }else{
  175. this.$modal.showToast('获取pet失败')
  176. }
  177. })
  178. },
  179. save(){
  180. console.log(this.petBaseInfo)
  181. console.log(this.petHealthInfo)
  182. if(!(this.fileList.length>0&&this.fileList[0].url)){
  183. this.$modal.showToast('请上传宠物照片!')
  184. return;
  185. }
  186. this.photo = this.fileList[0].url;
  187. let params = { ...{petType:this.petType,photo:this.photo}, ...this.petBaseInfo,...this.petHealthInfo}
  188. if(!params.name){
  189. this.$modal.showToast('请填写宠物昵称!')
  190. return;
  191. }
  192. if(!params.breed){s
  193. this.$modal.showToast('请选择宠物品种!')
  194. return;
  195. }
  196. if(!params.bodyType){
  197. this.$modal.showToast('请选择宠物体重!')
  198. return;
  199. }
  200. if(!params.personality){
  201. this.$modal.showToast('请选择宠物性格,可多选!')
  202. return;
  203. }
  204. if(!params.vaccineStatus){
  205. this.$modal.showToast('请选择宠物疫苗情况!')
  206. return;
  207. }
  208. if(!params.dewormingStatus){
  209. this.$modal.showToast('请选择宠物驱虫情况!')
  210. return;
  211. }
  212. if(params.healthStatus==null || !params.healthStatus.length){
  213. this.$modal.showToast('请选择宠物健康情况,可多选!')
  214. return;
  215. }
  216. this.loading=true
  217. if(this.optionType=='edit'){
  218. params.id = this.petId;
  219. updatePet(params).then(res=>{
  220. if(res&&res.code==200){
  221. uni.showToast({
  222. title: '保存成功',
  223. duration: 3000,
  224. icon:"none"
  225. })
  226. setTimeout(() => {
  227. this.loading = false;
  228. if(this.isNewOrder){
  229. uni.redirectTo({url:'/pages/newOrder/petList'});
  230. }else{
  231. let len = getCurrentPages().length;
  232. if(len >= 2) {
  233. uni.navigateBack();
  234. }else{
  235. uni.redirectTo({url:'/pages/personalCenter/pet'});
  236. }
  237. }
  238. }, 1000);
  239. }else {
  240. this.loading=false
  241. uni.showToast({
  242. title: '更新pet失败',
  243. duration: 3000,
  244. icon:"none"
  245. })
  246. }
  247. })
  248. } else if(this.optionType=='add'){
  249. addPet(params).then(res=>{
  250. if(res&&res==1){
  251. uni.showToast({
  252. title: '保存成功',
  253. duration: 3000,
  254. icon:"none"
  255. })
  256. setTimeout(() => {
  257. this.loading = false;
  258. if(this.isNewOrder){
  259. uni.redirectTo({url:'/pages/newOrder/petList'});
  260. }else{
  261. let len = getCurrentPages().length;
  262. if(len >= 2) {
  263. uni.navigateBack();
  264. }else {
  265. uni.redirectTo({url:'/pages/personalCenter/pet'});
  266. }
  267. }
  268. }, 1000);
  269. }else {
  270. this.loading=false
  271. uni.showToast({
  272. title: '新增pet失败',
  273. duration: 3000,
  274. icon:"none"
  275. })
  276. }
  277. })
  278. }
  279. },
  280. deletePet(){
  281. this.delContent = `确定要删除${this.petBaseInfo.name}?`;
  282. this.showDel = true;
  283. },
  284. confirmDel(){
  285. delPet(this.petId).then(res=>{
  286. console.log(res);
  287. uni.showToast({
  288. title: '删除成功',
  289. duration: 3000,
  290. icon:"none"
  291. })
  292. this.showDel=false;
  293. setTimeout(() => {
  294. let len = getCurrentPages().length;
  295. this.loading=false
  296. if(len >= 2) {
  297. uni.navigateBack();
  298. }else {
  299. uni.redirectTo({url:'/pages/personalCenter/pet'});
  300. }
  301. }, 2000);
  302. })
  303. },
  304. }
  305. }
  306. </script>
  307. <style lang="scss">
  308. .personal-pet-cat{
  309. .breed-select{
  310. background-color: #fff;
  311. width: 100%;
  312. padding: 20px;
  313. border-radius: 8px 8px 0 0;
  314. position: absolute;
  315. bottom: 0;
  316. .breed-select-btn{
  317. display: flex;
  318. justify-content: space-around;
  319. align-items: center;
  320. }
  321. }
  322. .personal-pet-info-title{
  323. font-size: 14px;
  324. color: #333;
  325. font-weight: bold;
  326. padding-bottom: 10px;
  327. }
  328. .border-bottom{
  329. border-bottom: 1px solid #efefef;
  330. }
  331. .personal-pet-img{
  332. width: 100%;
  333. height: 118px;
  334. background-color: #fff;
  335. padding: 10px 20px;
  336. }
  337. .personal-pet-basic-info{
  338. background-color: #fff;
  339. margin-top: 10px;
  340. padding: 10px 20px;
  341. }
  342. }
  343. .container {
  344. position: relative;
  345. height: 100%;
  346. padding-bottom: 90px;
  347. .personal-pet-info-btns {
  348. background-color: #FFFFFF;
  349. padding: 10px 20px 40px;
  350. width: 100%;
  351. height: 90px;
  352. position: fixed;
  353. bottom: 0;
  354. z-index: 100;
  355. text-align: center;
  356. .personal-pet-btns{
  357. display: flex;
  358. justify-content: space-around;
  359. align-items: center;
  360. flex-wrap: nowrap;
  361. flex-direction: row;
  362. .personal-pet-btn{
  363. width: 40%;
  364. }
  365. }
  366. }
  367. }
  368. </style>