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

445 lines
8.9 KiB

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