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.

514 lines
16 KiB

5 months ago
5 months ago
5 months ago
5 months ago
5 months 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. <view class="avatar-container" @click="selectImage">
  9. <image v-if="avatarPath" :src="avatarPath" class="avatar-image" mode="aspectFill"></image>
  10. <image v-else src="https://catmdogf.oss-cn-shanghai.aliyuncs.com/CMDF/front/personal/index/cat_new.png"
  11. class="avatar-image" mode="aspectFill"></image>
  12. <view class="avatar-overlay">
  13. <text class="avatar-text">点击上传</text>
  14. </view>
  15. </view>
  16. </view>
  17. </view>
  18. <PetBaseInfo :petType="petType" :petBaseInfo.sync="petBaseInfo" />
  19. <PetHealthInfo :petType="petType" :petHealthInfo.sync="petHealthInfo" />
  20. <view class="personal-pet-info-btns">
  21. <view class="personal-pet-btns">
  22. <view class="personal-pet-btn">
  23. <u-button :disabled="optionType != 'edit'" color="#FFF4E4" @click="deletePet">
  24. <view style="color: #FFAA48;">
  25. 删除
  26. </view>
  27. </u-button>
  28. </view>
  29. <view class="personal-pet-btn" @click="save">
  30. <u-button color="#FFBF60" :loading="loading">
  31. <view style="color: #fff;">
  32. 保存
  33. </view>
  34. </u-button>
  35. </view>
  36. </view>
  37. </view>
  38. <u-modal :show="showDel" @confirm="confirmDel" @cancel="showDel = false;" ref="uModal" showCancelButton
  39. :asyncClose="true" :content='delContent'>
  40. </u-modal>
  41. <!-- 裁剪组件 -->
  42. <ksp-cropper
  43. mode="free"
  44. :width="200"
  45. :height="200"
  46. :maxWidth="1024"
  47. :maxHeight="1024"
  48. :url="cropperUrl"
  49. @cancel="onCropperCancel"
  50. @ok="onCropperOk">
  51. </ksp-cropper>
  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. // 新增裁剪相关数据
  70. cropperUrl: '',
  71. avatarPath: '',
  72. petBaseInfo: {
  73. name: '',
  74. gender: '',
  75. breed: '',
  76. bodyType: '',
  77. birthDate: '',
  78. personality: ''
  79. },
  80. petHealthInfo: {
  81. vaccineStatus: '',
  82. dewormingStatus: '',
  83. sterilization: '',
  84. doglicenseStatus: '',
  85. healthStatus: [],
  86. remark: ''
  87. },
  88. showDel: false,
  89. }
  90. },
  91. components: {
  92. PetBaseInfo,
  93. PetHealthInfo
  94. },
  95. onLoad: function (option) {
  96. console.log(option.petType); //打印出上个页面传递的参数。
  97. this.petType = option.petType;
  98. this.optionType = option.optionType;
  99. if (this.optionType == 'edit') {
  100. this.petId = option.petId;
  101. this.getPetDetails(option.petId);
  102. }
  103. if (option.isNewOrder) {
  104. this.isNewOrder = true;
  105. }
  106. // 监听品种选择事件
  107. uni.$on('breedSelected', this.handleBreedSelected);
  108. },
  109. methods: {
  110. // 选择图片
  111. selectImage() {
  112. uni.chooseImage({
  113. count: 1,
  114. sizeType: ['original', 'compressed'],
  115. sourceType: ['album', 'camera'],
  116. success: (res) => {
  117. // 设置裁剪组件的url,显示裁剪界面
  118. this.cropperUrl = res.tempFilePaths[0];
  119. },
  120. fail: (err) => {
  121. console.log('选择图片失败:', err);
  122. uni.showToast({
  123. title: '选择图片失败',
  124. icon: 'none'
  125. });
  126. }
  127. });
  128. },
  129. // 裁剪取消
  130. onCropperCancel() {
  131. this.cropperUrl = '';
  132. },
  133. // 裁剪完成
  134. async onCropperOk(ev) {
  135. try {
  136. this.cropperUrl = '';
  137. this.avatarPath = ev.path;
  138. // 上传裁剪后的图片
  139. const uploadResult = await this.uploadFilePromise(ev.path);
  140. this.photo = uploadResult;
  141. // 更新fileList用于兼容原有逻辑
  142. this.fileList = [{
  143. url: uploadResult,
  144. status: 'success',
  145. message: ''
  146. }];
  147. uni.showToast({
  148. title: '头像上传成功',
  149. icon: 'success'
  150. });
  151. } catch (error) {
  152. console.log('上传失败:', error);
  153. uni.showToast({
  154. title: '上传失败,请重试',
  155. icon: 'none'
  156. });
  157. }
  158. },
  159. // 删除图片
  160. deletePic(event) {
  161. this.fileList.splice(event.index, 1)
  162. this.avatarPath = '';
  163. this.photo = '';
  164. },
  165. // 新增图片 - 保留原有方法以兼容
  166. async afterRead(event) {
  167. // 当设置 multiple 为 true 时, file 为数组格式,否则为对象格式
  168. let lists = [].concat(event.file)
  169. let fileListLen = this.fileList.length
  170. lists.map((item) => {
  171. this.fileList.push({
  172. ...item,
  173. status: 'uploading',
  174. message: '上传中'
  175. })
  176. })
  177. for (let i = 0; i < lists.length; i++) {
  178. const result = await this.uploadFilePromise(lists[i].url)
  179. let item = this.fileList[fileListLen]
  180. this.fileList.splice(fileListLen, 1, Object.assign(item, {
  181. status: 'success',
  182. message: '',
  183. url: result
  184. }))
  185. fileListLen++
  186. }
  187. },
  188. uploadFilePromise(url) {
  189. return new Promise((resolve, reject) => {
  190. let a = uni.uploadFile({
  191. url: 'https://store-test.catmdogd.com/test-api/h5/oss/upload',
  192. filePath: url,
  193. name: 'file',
  194. formData: {
  195. user: 'test'
  196. },
  197. success: (res) => {
  198. setTimeout(() => {
  199. if (res && res.data) {
  200. let resData = JSON.parse(res.data);
  201. resolve(resData.url);
  202. }
  203. reject("上传失败");
  204. }, 1000)
  205. }
  206. });
  207. })
  208. },
  209. getPetDetails(petId) {
  210. getPetDetails(petId).then(res => {
  211. if (res && res.id) {
  212. const { photo, name, gender, breed, bodyType, birthDate, personality, vaccineStatus, dewormingStatus, sterilization, doglicenseStatus, healthStatus, remark } = res;
  213. this.petBaseInfo = {
  214. name,
  215. gender,
  216. breed,
  217. bodyType,
  218. birthDate,
  219. personality
  220. };
  221. this.petHealthInfo = {
  222. vaccineStatus,
  223. dewormingStatus,
  224. sterilization,
  225. doglicenseStatus,
  226. healthStatus,
  227. remark
  228. }
  229. this.fileList = [{ url: photo }]
  230. this.avatarPath = photo;
  231. this.photo = photo; // 确保photo字段也被正确设置
  232. } else {
  233. this.$modal.showToast('获取pet失败')
  234. }
  235. })
  236. },
  237. save() {
  238. console.log(this.petBaseInfo)
  239. console.log(this.petHealthInfo)
  240. if (!(this.fileList.length > 0 && this.fileList[0].url)) {
  241. this.$modal.showToast('请上传宠物照片!')
  242. return;
  243. }
  244. // 确保使用最新的头像URL
  245. this.photo = this.fileList[0].url;
  246. let params = {
  247. petType: this.petType,
  248. photo: this.photo,
  249. ...this.petBaseInfo,
  250. ...this.petHealthInfo
  251. }
  252. console.log('保存参数:', params); // 添加调试日志
  253. if (!params.name) {
  254. this.$modal.showToast('请填写宠物昵称!')
  255. return;
  256. }
  257. if (!params.breed) {
  258. this.$modal.showToast('请选择宠物品种!')
  259. return;
  260. }
  261. if (!params.bodyType) {
  262. this.$modal.showToast('请选择宠物体重!')
  263. return;
  264. }
  265. if (!params.personality) {
  266. this.$modal.showToast('请选择宠物性格,可多选!')
  267. return;
  268. }
  269. if (!params.vaccineStatus) {
  270. this.$modal.showToast('请选择宠物疫苗情况!')
  271. return;
  272. }
  273. if (!params.dewormingStatus) {
  274. this.$modal.showToast('请选择宠物驱虫情况!')
  275. return;
  276. }
  277. if (params.healthStatus == null || !params.healthStatus.length) {
  278. this.$modal.showToast('请选择宠物健康情况,可多选!')
  279. return;
  280. }
  281. this.loading = true
  282. if (this.optionType == 'edit') {
  283. params.id = this.petId;
  284. updatePet(params).then(res => {
  285. if (res && res.code == 200) {
  286. uni.showToast({
  287. title: '保存成功',
  288. duration: 3000,
  289. icon: "none"
  290. })
  291. setTimeout(() => {
  292. this.loading = false;
  293. if (this.isNewOrder) {
  294. uni.redirectTo({ url: '/pages/newOrder/petList' });
  295. } else {
  296. let len = getCurrentPages().length;
  297. if (len >= 2) {
  298. uni.navigateBack();
  299. } else {
  300. uni.redirectTo({ url: '/pages/personalCenter/pet' });
  301. }
  302. }
  303. }, 1000);
  304. } else {
  305. this.loading = false
  306. uni.showToast({
  307. title: '更新pet失败',
  308. duration: 3000,
  309. icon: "none"
  310. })
  311. }
  312. })
  313. } else if (this.optionType == 'add') {
  314. addPet(params).then(res => {
  315. if (res && res == 1) {
  316. uni.showToast({
  317. title: '保存成功',
  318. duration: 3000,
  319. icon: "none"
  320. })
  321. setTimeout(() => {
  322. this.loading = false;
  323. if (this.isNewOrder) {
  324. uni.redirectTo({ url: '/pages/newOrder/petList' });
  325. } else {
  326. let len = getCurrentPages().length;
  327. if (len >= 2) {
  328. uni.navigateBack();
  329. } else {
  330. uni.redirectTo({ url: '/pages/personalCenter/pet' });
  331. }
  332. }
  333. }, 1000);
  334. } else {
  335. this.loading = false
  336. uni.showToast({
  337. title: '新增pet失败',
  338. duration: 3000,
  339. icon: "none"
  340. })
  341. }
  342. })
  343. }
  344. },
  345. deletePet() {
  346. this.delContent = `确定要删除${this.petBaseInfo.name}?`;
  347. this.showDel = true;
  348. },
  349. confirmDel() {
  350. delPet(this.petId).then(res => {
  351. console.log(res);
  352. uni.showToast({
  353. title: '删除成功',
  354. duration: 3000,
  355. icon: "none"
  356. })
  357. this.showDel = false;
  358. setTimeout(() => {
  359. let len = getCurrentPages().length;
  360. this.loading = false
  361. if (len >= 2) {
  362. uni.navigateBack();
  363. } else {
  364. uni.redirectTo({ url: '/pages/personalCenter/pet' });
  365. }
  366. }, 2000);
  367. })
  368. },
  369. // 处理品种选择结果
  370. handleBreedSelected(data) {
  371. console.log('接收到品种选择结果:', data);
  372. if (data && data.breed && data.petType === this.petType) {
  373. this.petBaseInfo.breed = data.breed;
  374. console.log('更新品种信息:', data.breed);
  375. }
  376. },
  377. },
  378. onUnload() {
  379. // 移除事件监听器
  380. uni.$off('breedSelected', this.handleBreedSelected);
  381. }
  382. }
  383. </script>
  384. <style lang="scss">
  385. .personal-pet-cat {
  386. .breed-select {
  387. background-color: #fff;
  388. width: 100%;
  389. padding: 20px;
  390. border-radius: 8px 8px 0 0;
  391. position: absolute;
  392. bottom: 0;
  393. .breed-select-btn {
  394. display: flex;
  395. justify-content: space-around;
  396. align-items: center;
  397. }
  398. }
  399. .personal-pet-info-title {
  400. font-size: 14px;
  401. color: #333;
  402. font-weight: bold;
  403. padding-bottom: 10px;
  404. }
  405. .border-bottom {
  406. border-bottom: 1px solid #efefef;
  407. }
  408. .personal-pet-img {
  409. width: 100%;
  410. height: 118px;
  411. background-color: #fff;
  412. padding: 10px 20px;
  413. }
  414. .personal-pet-basic-info {
  415. background-color: #fff;
  416. margin-top: 10px;
  417. padding: 10px 20px;
  418. }
  419. }
  420. .container {
  421. position: relative;
  422. height: 100%;
  423. padding-bottom: 90px;
  424. .personal-pet-info-btns {
  425. background-color: #FFFFFF;
  426. padding: 10px 20px 40px;
  427. width: 100%;
  428. height: 90px;
  429. position: fixed;
  430. bottom: 0;
  431. z-index: 100;
  432. text-align: center;
  433. .personal-pet-btns {
  434. display: flex;
  435. justify-content: space-around;
  436. align-items: center;
  437. flex-wrap: nowrap;
  438. flex-direction: row;
  439. .personal-pet-btn {
  440. width: 40%;
  441. }
  442. }
  443. }
  444. }
  445. // 新增头像相关样式
  446. .avatar-container {
  447. position: relative;
  448. width: 60px;
  449. height: 60px;
  450. border-radius: 50%;
  451. overflow: hidden;
  452. border: 2px solid #f0f0f0;
  453. .avatar-image {
  454. width: 100%;
  455. height: 100%;
  456. }
  457. .avatar-overlay {
  458. position: absolute;
  459. top: 0;
  460. left: 0;
  461. right: 0;
  462. bottom: 0;
  463. background-color: rgba(0, 0, 0, 0.3);
  464. display: flex;
  465. align-items: center;
  466. justify-content: center;
  467. opacity: 0;
  468. transition: opacity 0.3s;
  469. .avatar-text {
  470. color: #fff;
  471. font-size: 10px;
  472. text-align: center;
  473. }
  474. }
  475. &:active .avatar-overlay {
  476. opacity: 1;
  477. }
  478. }
  479. </style>