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

390 lines
8.6 KiB

2 months ago
2 months ago
2 months ago
  1. <template>
  2. <view class="personal-pet">
  3. <view v-if="petList.length > 0" class="personal-pet-list">
  4. <view v-for="(item, index) in petList" :key="index">
  5. <view
  6. :class="['personal-pet-list-item', item.gender === '男生' ? 'personal-pet-list-item_backgroud_m' : 'personal-pet-list-item_backgroud_f' ]">
  7. <view class="personal-pet-info">
  8. <view>
  9. <u-avatar :src="item.headImage || defaultPhoto" size="60" shape="circle"></u-avatar>
  10. </view>
  11. <view class="personal-pet-info-1">
  12. <view class="personal-pet-info-2">
  13. <view class="personal-pet-name">
  14. {{ item.nickName }}
  15. </view>
  16. <view class="personal-pet-sex">
  17. <img :src="item.sex === 0 ? '../../../static/images/sex/boy.svg' : '../../../static/images/sex/girl.svg'"
  18. alt="sex" style="width: 16px;height: 16px;" />
  19. </view>
  20. </view>
  21. <view class="personal-pet-info-3" style="width: 100%;">
  22. <view style="max-width: 35%;">
  23. {{ item.type || '未知' }}
  24. </view>
  25. <view class="personal-pet-info-age" style="max-width: 90px;">
  26. {{ item.age || '未知' }}
  27. </view>
  28. <view style="max-width: 25%;">
  29. 体重{{ item.weight }}kg
  30. </view>
  31. </view>
  32. </view>
  33. </view>
  34. <view class="personal-pet-info-disposition ellipsis">
  35. 性格 {{ item.personality }}
  36. </view>
  37. <view class="personal-pet-info-btns">
  38. <view class="personal-pet-info-btn" @click="editPet(item)">
  39. <u-icon name="edit-pen" color="#7d8196" size="16" style="margin-right: 5px;"></u-icon>
  40. <view style="margin-left: 5px;">
  41. 编辑
  42. </view>
  43. </view>
  44. <view class="personal-pet-info-btn" @click="deletePet(item)">
  45. <u-icon name="trash" color="#7d8196" size="16"></u-icon>
  46. <view style="margin-left: 5px;">
  47. 删除
  48. </view>
  49. </view>
  50. </view>
  51. </view>
  52. </view>
  53. </view>
  54. <view v-else class="personal-pet-none">
  55. <img src="https://catmdogf.oss-cn-shanghai.aliyuncs.com/CMDF/front/personal/pet/catdog.png" alt="pet"
  56. style="width: 149px;height: 124px;" mode="widthFix" />
  57. <view class="personal-pet-none-text">这里还没有您的宠物,请点击添加吧~</view>
  58. </view>
  59. <view class="personal-pet-add">
  60. <button class="personal-pet-add-btn" @click="addPet">
  61. <view style="font-size: 16px;font-weight: 500;">
  62. 添加宠物
  63. </view>
  64. </button>
  65. </view>
  66. <!-- <view >
  67. <u-picker :showToolbar='false' :show="show" :columns="petTypes" @change="petTypeChange" @cancel="cancel"
  68. @confirm="confirm"></u-picker>
  69. </view> -->
  70. <u-modal :show="showDel" @confirm="confirmDel" @cancel="cancelDel" ref="uModal" showCancelButton
  71. :asyncClose="true" :content="delContent"></u-modal>
  72. </view>
  73. </template>
  74. <script setup>
  75. import {
  76. ref,
  77. onMounted
  78. } from 'vue';
  79. // import {
  80. // getDictList
  81. // } from "@/api/system/user.js"
  82. import {
  83. getpetList,
  84. delByPetId
  85. } from "@/api/pet/index.js";
  86. import {
  87. useRouter
  88. } from 'vue-router';
  89. import {
  90. onShow,
  91. onPullDownRefresh
  92. } from '@dcloudio/uni-app'
  93. const router = useRouter();
  94. const defaultPhoto = ref('https://catmdogf.oss-cn-shanghai.aliyuncs.com/CMDF/front/personal/pet/catdog.png');
  95. const petList = ref([]);
  96. const show = ref(false);
  97. const showDel = ref(false);
  98. const petTypes = ref([]);
  99. const delContent = ref('');
  100. const deleteId = ref('');
  101. const petType = ref('');
  102. const getPetList = async () => {
  103. petList.value = [];
  104. try {
  105. const res = await getpetList();
  106. if (res && res.data) {
  107. petList.value = res.data;
  108. showDel.value = false;
  109. }
  110. } catch (error) {
  111. console.error('获取宠物列表失败', error);
  112. }
  113. };
  114. // const getPetTypeList = async () => {
  115. // try {
  116. // const res = await getDictList('pet_type');
  117. // if (res.code === 200) {
  118. // const petType = res.data.map(e => e.dictLabel);
  119. // petTypes.value = [petType];
  120. // } else {
  121. // console.error('获取pet type失败');
  122. // }
  123. // } catch (error) {
  124. // console.error('获取pet type列表失败', error);
  125. // }
  126. // };
  127. const addPet = () => {
  128. uni.navigateTo({
  129. url: "/otherPages/userManage/pet/petInfo"
  130. })
  131. };
  132. const cancel = () => {
  133. show.value = false;
  134. };
  135. const petTypeChange = (e) => {
  136. petType.value = e.value[0];
  137. };
  138. const confirm = (e) => {
  139. show.value = false;
  140. console.log(petType.value);
  141. if (petType.value === '猫猫') {
  142. router.push({
  143. path: `/pages/personalCenter/petInfo`,
  144. query: {
  145. petType: 'cat',
  146. optionType: 'add'
  147. }
  148. });
  149. }
  150. if (petType.value === '狗狗') {
  151. router.push({
  152. path: `/pages/personalCenter/petInfo`,
  153. query: {
  154. petType: 'dog',
  155. optionType: 'add'
  156. }
  157. });
  158. }
  159. if (!petType.value) {
  160. if (e.value[0] === '猫猫') {
  161. router.push({
  162. path: `/pages/personalCenter/petInfo`,
  163. query: {
  164. petType: 'cat',
  165. optionType: 'add'
  166. }
  167. });
  168. }
  169. if (e.value[0] === '狗狗') {
  170. router.push({
  171. path: `/pages/personalCenter/petInfo`,
  172. query: {
  173. petType: 'dog',
  174. optionType: 'add'
  175. }
  176. });
  177. }
  178. }
  179. };
  180. const editPet = (item) => {
  181. if (item.petType === '猫猫' || item.petType === 'cat') {
  182. router.push({
  183. path: `/pages/personalCenter/petInfo`,
  184. query: {
  185. petType: 'cat',
  186. optionType: 'edit',
  187. petId: item.id
  188. }
  189. });
  190. }
  191. if (item.petType === '狗狗' || item.petType === 'dog') {
  192. router.push({
  193. path: `/pages/personalCenter/petInfo`,
  194. query: {
  195. petType: 'dog',
  196. optionType: 'edit',
  197. petId: item.id
  198. }
  199. });
  200. }
  201. };
  202. const deletePet = (item) => {
  203. delContent.value = "确定要删除" + item.name + '?';
  204. showDel.value = true;
  205. deleteId.value = item.id;
  206. };
  207. const confirmDel = async () => {
  208. try {
  209. await delByPetId(deleteId.value);
  210. console.log('删除成功');
  211. getPetList();
  212. } catch (error) {
  213. console.error('删除宠物失败', error);
  214. }
  215. };
  216. const cancelDel = () => {
  217. showDel.value = false;
  218. deleteId.value = '';
  219. };
  220. onMounted(() => {
  221. // getPetTypeList();
  222. getPetList();
  223. });
  224. onShow(() => {
  225. getPetList();
  226. });
  227. onPullDownRefresh(() => {
  228. getPetList();
  229. });
  230. </script>
  231. <style lang="scss" scoped>
  232. .personal-pet {
  233. position: relative;
  234. height: 100%;
  235. padding-bottom: 90px;
  236. .personal-pet-add {
  237. box-sizing: border-box;
  238. background-color: #FFFFFF;
  239. padding: 20rpx 20rpx 40rpx 20rpx;
  240. width: 100%;
  241. height: 160rpx;
  242. position: fixed;
  243. bottom: 0;
  244. z-index: 100;
  245. display: flex;
  246. .personal-pet-add-btn {
  247. width: 100%;
  248. height: 90rpx;
  249. border-radius: 6px;
  250. background: #FFB13F;
  251. font-size: 16px;
  252. color: #FFFFFF;
  253. }
  254. }
  255. .personal-pet-list {
  256. .personal-pet-list-add {
  257. width: 100%;
  258. height: 44px;
  259. background-color: #FFFFFF;
  260. display: flex;
  261. justify-content: space-between;
  262. align-items: center;
  263. padding: 0 15px;
  264. .personal-pet-list-add-btn {
  265. font-size: 14px;
  266. color: #AAA;
  267. display: flex;
  268. align-items: center;
  269. }
  270. }
  271. .personal-pet-list-item_backgroud_m {
  272. background: linear-gradient(179deg, #EDF5FE 0.75%, #FFF 34.11%);
  273. }
  274. .personal-pet-list-item_backgroud_f {
  275. background: linear-gradient(179deg, #FFF4F6 0.75%, #FFF 34.11%);
  276. }
  277. .personal-pet-list-item {
  278. margin: 10px 10px 0 10px;
  279. border-radius: 5px;
  280. padding: 20px 10px 10px;
  281. .personal-pet-info {
  282. display: flex;
  283. align-items: center;
  284. justify-content: flex-start;
  285. .personal-pet-info-1 {
  286. margin-left: 10px;
  287. width: calc(100% - 60px);
  288. .personal-pet-info-2 {
  289. display: flex;
  290. flex-wrap: wrap;
  291. .personal-pet-name {
  292. color: #333;
  293. font-size: 16px;
  294. margin-right: 10px;
  295. }
  296. }
  297. .personal-pet-info-3 {
  298. display: flex;
  299. align-items: baseline;
  300. font-size: 14px;
  301. margin-top: 5px;
  302. color: #7D8196;
  303. .personal-pet-info-age {
  304. padding: 0 10px;
  305. margin: 0 10px;
  306. border-left: solid 2px #7D8196;
  307. border-right: solid 2px #7D8196;
  308. }
  309. }
  310. }
  311. }
  312. .personal-pet-info-disposition {
  313. padding: 10px;
  314. color: #7D8196;
  315. font-size: 14px;
  316. background: #f9f9f9;
  317. border-radius: 5px;
  318. margin-top: 10px;
  319. }
  320. .personal-pet-info-btns {
  321. display: flex;
  322. justify-content: flex-end;
  323. margin-top: 10px;
  324. .personal-pet-info-btn {
  325. display: flex;
  326. font-size: 14px;
  327. color: #7D8196;
  328. margin-left: 20px;
  329. }
  330. }
  331. }
  332. }
  333. .personal-pet-none {
  334. display: flex;
  335. justify-content: center;
  336. align-items: center;
  337. flex-wrap: wrap;
  338. margin-top: 40%;
  339. .personal-pet-none-text {
  340. color: #666;
  341. text-align: center;
  342. font-size: 14px;
  343. width: 100%;
  344. margin-top: 10px;
  345. }
  346. }
  347. }
  348. </style>