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

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