猫妈狗爸伴宠师小程序前端代码
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
  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 :disabled="optionType!='edit'" 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. doglicenseStatus: '',
  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({ id : petId }).then((res) => {
  146. if (res.code == 200) {
  147. const {
  148. headImage,
  149. nickName,
  150. sex,
  151. type,
  152. weight,
  153. birthday,
  154. personality,
  155. vaccine,
  156. deworm,
  157. neutered,
  158. doglicenseStatus,
  159. health,
  160. remark
  161. } = res.data;
  162. petBaseInfo.value = {
  163. nickName,
  164. sex,
  165. type,
  166. weight,
  167. birthday,
  168. personality
  169. };
  170. petHealthInfo.value = {
  171. vaccine,
  172. deworm,
  173. neutered,
  174. doglicenseStatus,
  175. health,
  176. remark
  177. };
  178. fileList.value = [{
  179. url: headImage
  180. }];
  181. } else {
  182. uni.showToast({
  183. title: '获取pet失败',
  184. duration: 3000,
  185. icon: "none"
  186. });
  187. }
  188. });
  189. };
  190. const save = () => {
  191. if (!(fileList.value.length > 0 && fileList.value[0].url)) {
  192. uni.showToast({
  193. title: '请上传宠物照片!',
  194. duration: 3000,
  195. icon: "none"
  196. });
  197. return;
  198. }
  199. headImage.value = fileList.value[0].url;
  200. let params = {
  201. ...{
  202. petType: petType.value,
  203. headImage: headImage.value
  204. },
  205. ...petBaseInfo.value,
  206. ...petHealthInfo.value
  207. };
  208. if (!params.nickName) {
  209. uni.showToast({
  210. title: '请填写宠物昵称!',
  211. duration: 3000,
  212. icon: "none"
  213. });
  214. return;
  215. }
  216. if (!params.type) {
  217. uni.showToast({
  218. title: '请输入宠物品种!',
  219. duration: 3000,
  220. icon: "none"
  221. });
  222. return;
  223. }
  224. if (!params.weight) {
  225. uni.showToast({
  226. title: '请选择宠物体重!',
  227. duration: 3000,
  228. icon: "none"
  229. });
  230. return;
  231. }
  232. if (!params.personality) {
  233. uni.showToast({
  234. title: '请输入宠物性格!',
  235. duration: 3000,
  236. icon: "none"
  237. });
  238. return;
  239. }
  240. if (!params.vaccine) {
  241. uni.showToast({
  242. title: '请选择宠物疫苗情况!',
  243. duration: 3000,
  244. icon: "none"
  245. });
  246. return;
  247. }
  248. if (!params.deworm) {
  249. uni.showToast({
  250. title: '请选择宠物驱虫情况!',
  251. duration: 3000,
  252. icon: "none"
  253. });
  254. return;
  255. }
  256. if (!params.health) {
  257. uni.showToast({
  258. title: '请填写宠物健康情况',
  259. duration: 3000,
  260. icon: "none"
  261. });
  262. return;
  263. }
  264. loading.value = true;
  265. if (optionType.value === 'edit') {
  266. params.id = petId.value;
  267. updatePet(params).then((res) => {
  268. if (res && res.code === 200) {
  269. uni.showToast({
  270. title: '保存成功',
  271. duration: 3000,
  272. icon: "none"
  273. });
  274. setTimeout(() => {
  275. loading.value = false;
  276. if (isNewOrder.value) {
  277. router.push('/pages/newOrder/petList');
  278. } else {
  279. const len = getCurrentPages().length;
  280. if (len >= 2) {
  281. uni.navigateBack();
  282. } else {
  283. router.push('/pages/personalCenter/pet');
  284. }
  285. }
  286. }, 1000);
  287. } else {
  288. loading.value = false;
  289. uni.showToast({
  290. title: '更新pet失败',
  291. duration: 3000,
  292. icon: "none"
  293. });
  294. }
  295. });
  296. } else if (optionType.value === 'add') {
  297. addPet(params).then((res) => {
  298. if (res.code == 200) {
  299. uni.showToast({
  300. title: '保存成功',
  301. duration: 3000,
  302. icon: "none"
  303. });
  304. setTimeout(() => {
  305. loading.value = false;
  306. uni.navigateBack();
  307. }, 1000);
  308. } else {
  309. loading.value = false;
  310. uni.showToast({
  311. title: '新增pet失败',
  312. duration: 3000,
  313. icon: "none"
  314. });
  315. }
  316. });
  317. }
  318. };
  319. const cancelPet = () => {
  320. uni.navigateBack(-1);
  321. };
  322. const confirmDel = () => {
  323. delByPetId(petId.value).then((res) => {
  324. uni.showToast({
  325. title: '删除成功',
  326. duration: 3000,
  327. icon: "none"
  328. });
  329. showDel.value = false;
  330. setTimeout(() => {
  331. const len = getCurrentPages().length;
  332. loading.value = false;
  333. if (len >= 2) {
  334. uni.navigateBack();
  335. } else {
  336. router.push('/pages/personalCenter/pet');
  337. }
  338. }, 2000);
  339. });
  340. };
  341. </script>
  342. <style lang="scss">
  343. .personal-pet-cat {
  344. .breed-select {
  345. background-color: #fff;
  346. box-sizing: border-box;
  347. width: 100%;
  348. padding: 20px;
  349. border-radius: 8px 8px 0 0;
  350. position: absolute;
  351. bottom: 0;
  352. .breed-select-btn {
  353. display: flex;
  354. justify-content: space-around;
  355. align-items: center;
  356. }
  357. }
  358. .personal-pet-info-title {
  359. font-size: 14px;
  360. color: #333;
  361. font-weight: bold;
  362. padding-bottom: 10px;
  363. }
  364. .border-bottom {
  365. border-bottom: 1px solid #efefef;
  366. }
  367. .personal-pet-img {
  368. width: 100%;
  369. height: 118px;
  370. background-color: #fff;
  371. padding: 10px 20px;
  372. }
  373. .personal-pet-basic-info {
  374. background-color: #fff;
  375. margin-top: 10px;
  376. padding: 10px 20px;
  377. }
  378. }
  379. .container {
  380. position: relative;
  381. height: 100%;
  382. padding-bottom: 90px;
  383. .personal-pet-info-btns {
  384. box-sizing: border-box;
  385. background-color: #FFFFFF;
  386. padding: 20rpx 20rpx 40rpx 20rpx;
  387. width: 100%;
  388. height: 160rpx;
  389. position: fixed;
  390. bottom: 0;
  391. z-index: 100;
  392. display: flex;
  393. .personal-pet-btns {
  394. display: flex;
  395. justify-content: space-around;
  396. align-items: center;
  397. flex-wrap: nowrap;
  398. width: 100%;
  399. height: 90rpx;
  400. border-radius: 6px;
  401. font-size: 16px;
  402. color: #FFFFFF;
  403. .personal-pet-btn {
  404. width: 40%;
  405. }
  406. }
  407. }
  408. }
  409. </style>