猫妈狗爸伴宠师小程序前端代码
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.

448 lines
8.9 KiB

1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month 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 accept="image" :capture="['album','camera']" :fileList="fileList" @afterRead="afterRead"
  9. @delete="deletePic" :max-count="1" name="pet" width="60" height="60" :custom-style="{flex:0}">
  10. <image src="https://catmdogf.oss-cn-shanghai.aliyuncs.com/CMDF/front/personal/index/cat_new.png"
  11. style="width: 60px;height: 60px;"></image>
  12. </u-upload>
  13. </view>
  14. </view>
  15. <PetBaseInfo :petType="petType" v-model:petBaseInfo="petBaseInfo" />
  16. <PetHealthInfo :petType="petType" v-model:petHealthInfo="petHealthInfo"
  17. @upPetHealthInfo="updatePetHealthInfo"/>
  18. <view class="personal-pet-info-btns">
  19. <view class="personal-pet-btns">
  20. <view class="personal-pet-btn">
  21. <u-button color="#FFF4E4" @click="cancelPet">
  22. <view style="color: #A9A9A9;">
  23. 取消
  24. </view>
  25. </u-button>
  26. </view>
  27. <view class="personal-pet-btn" @click="save">
  28. <u-button color="#FFBF60" :loading="loading">
  29. <view style="color: #fff;">
  30. 保存
  31. </view>
  32. </u-button>
  33. </view>
  34. </view>
  35. </view>
  36. <u-modal :show="showDel" @confirm="confirmDel" @cancel="showDel = false" ref="uModal" showCancelButton
  37. :asyncClose="true" :content="delContent">
  38. </u-modal>
  39. </view>
  40. </template>
  41. <script setup>
  42. import {
  43. ref,
  44. onMounted
  45. } from 'vue';
  46. import {
  47. addPet,
  48. getPetDetails,
  49. updatePet,
  50. delByPetId
  51. } from '@/api/pet/index.js';
  52. import PetBaseInfo from './components/petBaseInfo.vue';
  53. import PetHealthInfo from './components/petHealthInfo.vue';
  54. import {
  55. useRoute,
  56. useRouter
  57. } from 'vue-router';
  58. import {
  59. onLoad
  60. } from '@dcloudio/uni-app'
  61. const route = useRoute();
  62. const router = useRouter();
  63. const loading = ref(false);
  64. const fileList = ref([]);
  65. const petId = ref('');
  66. const userId = ref('');
  67. const petType = ref('dog');
  68. const optionType = ref('add');
  69. const isNewOrder = ref(false);
  70. const delContent = ref('');
  71. const headImage = ref('');
  72. const petBaseInfo = ref({
  73. nickName: '',
  74. sex: '',
  75. type: '',
  76. weight: '',
  77. birthday: '',
  78. personality: ''
  79. });
  80. const petHealthInfo = ref({
  81. vaccine: '',
  82. deworm: '',
  83. neutered: '',
  84. petCard: '',
  85. health: '',
  86. remark: ''
  87. });
  88. const showDel = ref(false);
  89. const uModal = ref(null);
  90. onLoad((option) => {
  91. petType.value = option.petType;
  92. userId.value = option.userId
  93. optionType.value = option.optionType;
  94. if (optionType.value === 'edit') {
  95. petId.value = option.petId;
  96. getPetDetailsFunc(option.petId);
  97. }
  98. if (option.isNewOrder) {
  99. isNewOrder.value = true;
  100. }
  101. });
  102. const deletePic = (event) => {
  103. fileList.value.splice(event.index, 1);
  104. };
  105. const afterRead = async (event) => {
  106. let lists = [].concat(event.file);
  107. let fileListLen = fileList.value.length;
  108. lists.map((item) => {
  109. fileList.value.push({
  110. ...item,
  111. status: 'uploading',
  112. message: '上传中'
  113. });
  114. });
  115. for (let i = 0; i < lists.length; i++) {
  116. const result = await uploadFilePromise(lists[i].url);
  117. let item = fileList.value[fileListLen];
  118. fileList.value.splice(fileListLen, 1, Object.assign(item, {
  119. status: 'success',
  120. message: '',
  121. url: result
  122. }));
  123. fileListLen++;
  124. }
  125. };
  126. function updatePetHealthInfo(data){
  127. // petHealthInfo.value = {
  128. // ...petHealthInfo.value,
  129. // ...data
  130. // }
  131. }
  132. const uploadFilePromise = (url) => {
  133. return new Promise((resolve, reject) => {
  134. 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. const getPetDetailsFunc = (petId) => {
  154. getPetDetails({
  155. id: petId
  156. }).then((res) => {
  157. if (res.code == 200) {
  158. const {
  159. headImage,
  160. nickName,
  161. sex,
  162. type,
  163. weight,
  164. birthday,
  165. personality,
  166. vaccine,
  167. deworm,
  168. neutered,
  169. petCard,
  170. health,
  171. remark
  172. } = res.data;
  173. petBaseInfo.value = {
  174. nickName,
  175. sex,
  176. type,
  177. weight,
  178. birthday,
  179. personality
  180. };
  181. petHealthInfo.value = {
  182. vaccine,
  183. deworm,
  184. neutered,
  185. petCard,
  186. health,
  187. remark
  188. };
  189. fileList.value = [{
  190. url: headImage
  191. }];
  192. } else {
  193. uni.showToast({
  194. title: '获取pet失败',
  195. duration: 3000,
  196. icon: "none"
  197. });
  198. }
  199. });
  200. };
  201. const save = () => {
  202. if (!(fileList.value.length > 0 && fileList.value[0].url)) {
  203. uni.showToast({
  204. title: '请上传宠物照片!',
  205. duration: 3000,
  206. icon: "none"
  207. });
  208. return;
  209. }
  210. headImage.value = fileList.value[0].url;
  211. let params = {
  212. ...{
  213. petType: petType.value,
  214. headImage: headImage.value
  215. },
  216. userId:userId.value,
  217. ...petBaseInfo.value,
  218. ...petHealthInfo.value
  219. };
  220. console.log(params);
  221. if (!params.nickName) {
  222. uni.showToast({
  223. title: '请填写宠物昵称!',
  224. duration: 3000,
  225. icon: "none"
  226. });
  227. return;
  228. }
  229. if (!params.type) {
  230. uni.showToast({
  231. title: '请输入宠物品种!',
  232. duration: 3000,
  233. icon: "none"
  234. });
  235. return;
  236. }
  237. if (!params.weight) {
  238. uni.showToast({
  239. title: '请选择宠物体重!',
  240. duration: 3000,
  241. icon: "none"
  242. });
  243. return;
  244. }
  245. if (!params.personality) {
  246. uni.showToast({
  247. title: '请输入宠物性格!',
  248. duration: 3000,
  249. icon: "none"
  250. });
  251. return;
  252. }
  253. if (!(params.vaccine >= 0)) {
  254. uni.showToast({
  255. title: '请选择宠物疫苗情况!',
  256. duration: 3000,
  257. icon: "none"
  258. });
  259. return;
  260. }
  261. if (!(params.deworm >= 0)) {
  262. uni.showToast({
  263. title: '请选择宠物驱虫情况!',
  264. duration: 3000,
  265. icon: "none"
  266. });
  267. return;
  268. }
  269. if (!params.health) {
  270. uni.showToast({
  271. title: '请填写宠物健康情况',
  272. duration: 3000,
  273. icon: "none"
  274. });
  275. return;
  276. }
  277. loading.value = true;
  278. if (optionType.value === 'edit') {
  279. params.id = petId.value;
  280. updatePet(params).then((res) => {
  281. if (res && res.code === 200) {
  282. uni.showToast({
  283. title: '保存成功',
  284. duration: 3000,
  285. icon: "none"
  286. });
  287. setTimeout(() => {
  288. loading.value = false;
  289. if (isNewOrder.value) {
  290. router.push('/pages/newOrder/petList');
  291. } else {
  292. const len = getCurrentPages().length;
  293. if (len >= 2) {
  294. uni.navigateBack();
  295. } else {
  296. router.push('/pages/personalCenter/pet');
  297. }
  298. }
  299. }, 1000);
  300. } else {
  301. loading.value = false;
  302. uni.showToast({
  303. title: '更新pet失败',
  304. duration: 3000,
  305. icon: "none"
  306. });
  307. }
  308. });
  309. } else if (optionType.value === 'add') {
  310. addPet(params).then((res) => {
  311. if (res.code == 200) {
  312. uni.showToast({
  313. title: '保存成功',
  314. duration: 3000,
  315. icon: "none"
  316. });
  317. setTimeout(() => {
  318. loading.value = false;
  319. uni.navigateBack();
  320. }, 1000);
  321. } else {
  322. loading.value = false;
  323. uni.showToast({
  324. title: '新增pet失败',
  325. duration: 3000,
  326. icon: "none"
  327. });
  328. }
  329. });
  330. }
  331. };
  332. const cancelPet = () => {
  333. uni.navigateBack(-1);
  334. };
  335. const confirmDel = () => {
  336. delByPetId(petId.value).then((res) => {
  337. uni.showToast({
  338. title: '删除成功',
  339. duration: 3000,
  340. icon: "none"
  341. });
  342. showDel.value = false;
  343. setTimeout(() => {
  344. const len = getCurrentPages().length;
  345. loading.value = false;
  346. if (len >= 2) {
  347. uni.navigateBack();
  348. } else {
  349. router.push('/pages/personalCenter/pet');
  350. }
  351. }, 2000);
  352. });
  353. };
  354. </script>
  355. <style lang="scss" scoped>
  356. .personal-pet-cat {
  357. .breed-select {
  358. background-color: #fff;
  359. box-sizing: border-box;
  360. width: 100%;
  361. padding: 20px;
  362. border-radius: 8px 8px 0 0;
  363. position: absolute;
  364. bottom: 0;
  365. .breed-select-btn {
  366. display: flex;
  367. justify-content: space-around;
  368. align-items: center;
  369. }
  370. }
  371. .personal-pet-info-title {
  372. font-size: 14px;
  373. color: #333;
  374. font-weight: bold;
  375. padding-bottom: 10px;
  376. }
  377. .border-bottom {
  378. border-bottom: 1px solid #efefef;
  379. }
  380. .personal-pet-img {
  381. width: 100%;
  382. height: 118px;
  383. background-color: #fff;
  384. padding: 10px 20px;
  385. }
  386. .personal-pet-basic-info {
  387. background-color: #fff;
  388. margin-top: 10px;
  389. padding: 10px 20px;
  390. }
  391. }
  392. .container {
  393. position: relative;
  394. height: 100%;
  395. padding-bottom: 90px;
  396. .personal-pet-info-btns {
  397. box-sizing: border-box;
  398. background-color: #FFFFFF;
  399. padding: 20rpx 20rpx 40rpx 20rpx;
  400. width: 100%;
  401. height: 160rpx;
  402. position: fixed;
  403. bottom: 0;
  404. z-index: 100;
  405. display: flex;
  406. .personal-pet-btns {
  407. display: flex;
  408. justify-content: space-around;
  409. align-items: center;
  410. flex-wrap: nowrap;
  411. width: 100%;
  412. height: 90rpx;
  413. border-radius: 6px;
  414. font-size: 16px;
  415. color: #FFFFFF;
  416. .personal-pet-btn {
  417. width: 40%;
  418. }
  419. }
  420. }
  421. }
  422. </style>