index.vue 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. <template>
  2. <view class="container">
  3. <!-- 用户信息卡片 -->
  4. <view class="user-card">
  5. <view class="user-header">
  6. <image class="avatar" :src="userInfo.avatar || '/static/icons/user_icon.png'"></image>
  7. <view class="user-detail" v-if="isLogin">
  8. <text class="nickname">{{userInfo.nickName}}</text>
  9. <!-- <text class="user-id">性别: {{userInfo.sex == '0' ? '男' :'女' || '未知'}}</text> -->
  10. </view>
  11. <view class="user-detail" v-else>
  12. <text class="login-text" @click="navigateToLogin">登录/注册</text>
  13. </view>
  14. </view>
  15. </view>
  16. <!-- 地块信息 -->
  17. <view class="info-card">
  18. <view class="card-title">
  19. <text>我的地块</text>
  20. <text class="more" @click="navigateToPlots">查看更多</text>
  21. </view>
  22. <view class="plot-info">
  23. <view class="plot-item">
  24. <text class="number">{{ plotInfo.total }}</text>
  25. <text class="label">总地块数</text>
  26. </view>
  27. <view class="plot-item">
  28. <text class="number">{{ plotInfo.active }}</text>
  29. <text class="label">种植中</text>
  30. </view>
  31. <view class="plot-item">
  32. <text class="number">{{ plotInfo.idle }}</text>
  33. <text class="label">空闲</text>
  34. </view>
  35. </view>
  36. </view>
  37. <!-- 农业服务 -->
  38. <view class="info-card">
  39. <view class="card-title">
  40. <text>农业服务</text>
  41. </view>
  42. <view class="service-grid">
  43. <view
  44. class="service-item"
  45. v-for="(item, index) in serviceList"
  46. :key="index"
  47. @click="navigateToService(item)"
  48. >
  49. <view class="service-icon">
  50. <image v-if="item.iconSvg" class="icon-svg" :src="item.iconSvg"></image>
  51. <text v-else>{{ item.iconText }}</text>
  52. </view>
  53. <text class="service-name">{{ item.name }}</text>
  54. </view>
  55. </view>
  56. </view>
  57. <!-- 常用功能 -->
  58. <view class="info-card">
  59. <view class="function-list">
  60. <view class="function-item" @click="handleContact">
  61. <view class="left">
  62. <text>联系客服</text>
  63. </view>
  64. <text class="arrow"></text>
  65. </view>
  66. <view class="function-item" @click="viewTerms">
  67. <view class="left">
  68. <text>用户协议</text>
  69. </view>
  70. <text class="arrow"></text>
  71. </view>
  72. <view class="function-item" @click="viewPrivacy">
  73. <view class="left">
  74. <text>隐私政策</text>
  75. </view>
  76. <text class="arrow"></text>
  77. </view>
  78. <!-- <view class="function-item" @click="navigateToSettings">
  79. <view class="left">
  80. <text>系统设置</text>
  81. </view>
  82. <text class="arrow">></text>
  83. </view> -->
  84. <view v-if="isLogin" class="function-item" @click="handleLogout">
  85. <view class="left">
  86. <!-- <text class="function-icon">退</text> -->
  87. <text>退出登录</text>
  88. </view>
  89. <text class="arrow"></text>
  90. </view>
  91. </view>
  92. </view>
  93. </view>
  94. </template>
  95. <script setup>
  96. import { ref, reactive } from 'vue'
  97. import { onShow } from '@dcloudio/uni-app'
  98. import { logout, webLogout } from '@/api/services/connect.js'
  99. import { countUserPlots } from '@/api/services/field.js'
  100. import storage from "@/utils/storage.js"
  101. // 查看用户协议
  102. const viewTerms = () => {
  103. uni.navigateTo({
  104. url: '/pages/login/terms'
  105. })
  106. }
  107. // 查看隐私政策
  108. const viewPrivacy = () => {
  109. uni.navigateTo({
  110. url: '/pages/login/privacy'
  111. })
  112. }
  113. // 响应式数据
  114. const plotInfo = reactive({
  115. total: 0,
  116. active: 0,
  117. idle: 0
  118. })
  119. const serviceList = ref([
  120. // {
  121. // name: '农资商城',
  122. // iconText: '商',
  123. // iconSvg: '/static/icons/mall.png',
  124. // path: '/pages/service/mall'
  125. // },
  126. {
  127. name: '农品交易',
  128. iconText: '售',
  129. iconSvg: '/static/icons/sales.png',
  130. path: '/pages/service/sales'
  131. },
  132. {
  133. name: '农事记录',
  134. iconText: '录',
  135. iconSvg: '/static/icons/activity-active.png',
  136. path: '/pages/activity/index'
  137. },
  138. {
  139. name: '设备管理',
  140. iconText: '备',
  141. iconSvg: '/static/icons/device-active.png',
  142. path: '/pages/device/index'
  143. },
  144. // {
  145. // name: '专家咨询',
  146. // iconText: '诊',
  147. // iconSvg: '/static/icons/expert.png',
  148. // path: '/pages/service/expert'
  149. // },
  150. // {
  151. // name: '绿色认证',
  152. // iconText: '证',
  153. // iconSvg: '/static/icons/certification.png',
  154. // path: '/pages/service/certification'
  155. // },
  156. // {
  157. // name: '保险接入',
  158. // iconText: '保',
  159. // iconSvg: '/static/icons/insurance.png',
  160. // path: '/pages/service/insurance'
  161. // }
  162. ])
  163. const userInfo = reactive({
  164. nickName: '',
  165. id: '',
  166. avatar: '',
  167. sex: ''
  168. })
  169. const isLogin = ref(false)
  170. // 方法
  171. const userPlots = () => {
  172. countUserPlots().then((res => {
  173. if (res.data.code == 200) {
  174. const { plotsTotal, inUseCount, leiSureCount } = res.data.data
  175. plotInfo.total = plotsTotal
  176. plotInfo.active = inUseCount
  177. plotInfo.idle = leiSureCount
  178. } else {
  179. console.error("统计地块数量失败!")
  180. }
  181. }))
  182. }
  183. // 检查登录状态
  184. const checkLoginStatus = () => {
  185. console.log("执行Show")
  186. if (storage.getHasLogin()) {
  187. isLogin.value = true
  188. // 获取用户信息
  189. const storedUserInfo = storage.getUserInfo()
  190. console.log("执行Show", storedUserInfo)
  191. if (storedUserInfo) {
  192. userInfo.nickName = storedUserInfo.username || '游客'
  193. userInfo.avatar = storedUserInfo.avatar || '/static/icons/user_icon.png'
  194. userInfo.sex = storedUserInfo.sex
  195. }
  196. } else {
  197. isLogin.value = false
  198. }
  199. }
  200. const navigateToLogin = () => {
  201. uni.navigateTo({
  202. url: '/pages/login/index'
  203. })
  204. }
  205. const navigateToPlots = () => {
  206. if (!storage.getHasLogin()) {
  207. uni.showModal({
  208. title: '提示',
  209. content: '您还未登录,请先登录',
  210. confirmText: '去登录',
  211. cancelText: '取消',
  212. success: function(res) {
  213. if (res.confirm) {
  214. uni.navigateTo({
  215. url: '/pages/login/index'
  216. });
  217. }
  218. },
  219. });
  220. return;
  221. }
  222. uni.navigateTo({
  223. url: '/pages/plots/list'
  224. })
  225. }
  226. const navigateToService = (item) => {
  227. if (!storage.getHasLogin()) {
  228. uni.showModal({
  229. title: '提示',
  230. content: '您还未登录,请先登录',
  231. confirmText: '去登录',
  232. cancelText: '取消',
  233. success: function(res) {
  234. if (res.confirm) {
  235. uni.navigateTo({
  236. url: '/pages/login/index'
  237. });
  238. }
  239. },
  240. });
  241. return;
  242. }
  243. uni.navigateTo({ url: item.path })
  244. }
  245. const handleContact = () => {
  246. uni.makePhoneCall({
  247. phoneNumber: '13379508760' // 替换为实际的客服电话
  248. })
  249. }
  250. const navigateToAbout = () => {
  251. uni.navigateTo({
  252. url: '/pages/about/index'
  253. })
  254. }
  255. const navigateToSettings = () => {
  256. uni.navigateTo({
  257. url: '/pages/settings/index'
  258. })
  259. }
  260. // 使用授权服务处理登出
  261. const handleLogout = () => {
  262. uni.showModal({
  263. title: '提示',
  264. content: '确认退出登录?',
  265. success: (res) => {
  266. if (res.confirm) {
  267. webLogout().then(res => {
  268. console.log("tuichu", res)
  269. storage.setAccessToken("")
  270. storage.setUserInfo({})
  271. storage.setHasLogin(false)
  272. storage.removeCurrentPlot()
  273. storage.removeUserPlots()
  274. isLogin.value = false
  275. userInfo.nickname = '游客'
  276. userInfo.id = ''
  277. userInfo.avatar = ''
  278. plotInfo.total = 0
  279. plotInfo.active = 0
  280. plotInfo.idle = 0
  281. })
  282. }
  283. }
  284. })
  285. }
  286. // 生命周期钩子
  287. onShow(() => {
  288. checkLoginStatus()
  289. if (isLogin.value) {
  290. userPlots()
  291. }
  292. })
  293. </script>
  294. <style>
  295. .arrow::after,.more::after {
  296. content: ">";
  297. }
  298. .container {
  299. min-height: 100vh;
  300. background-color: #f5f5f5;
  301. padding: 20rpx;
  302. }
  303. .user-card {
  304. background-color: #4CAF50;
  305. border-radius: 16rpx;
  306. padding: 30rpx;
  307. margin-bottom: 20rpx;
  308. }
  309. .user-header {
  310. display: flex;
  311. align-items: center;
  312. }
  313. .avatar {
  314. width: 120rpx;
  315. height: 120rpx;
  316. border-radius: 60rpx;
  317. /* border: 4rpx solid #fff; */
  318. }
  319. .user-detail {
  320. margin-left: 20rpx;
  321. color: #fff;
  322. }
  323. .nickname {
  324. font-size: 32rpx;
  325. font-weight: bold;
  326. margin-bottom: 10rpx;
  327. display: block;
  328. }
  329. .user-id {
  330. font-size: 24rpx;
  331. opacity: 0.8;
  332. }
  333. .info-card {
  334. background-color: #fff;
  335. border-radius: 16rpx;
  336. padding: 30rpx;
  337. margin-bottom: 20rpx;
  338. }
  339. .card-title {
  340. display: flex;
  341. justify-content: space-between;
  342. align-items: center;
  343. margin-bottom: 30rpx;
  344. font-size: 32rpx;
  345. font-weight: bold;
  346. }
  347. .more {
  348. color: #666;
  349. font-size: 24rpx;
  350. font-weight: normal;
  351. }
  352. .plot-info {
  353. display: flex;
  354. justify-content: space-around;
  355. }
  356. .plot-item {
  357. text-align: center;
  358. }
  359. .number {
  360. font-size: 36rpx;
  361. font-weight: bold;
  362. color: #4CAF50;
  363. display: block;
  364. }
  365. .label {
  366. font-size: 24rpx;
  367. color: #666;
  368. margin-top: 10rpx;
  369. display: block;
  370. }
  371. .service-grid {
  372. display: grid;
  373. grid-template-columns: repeat(4, 1fr);
  374. gap: 30rpx;
  375. padding: 10rpx 0;
  376. }
  377. .service-item {
  378. display: flex;
  379. flex-direction: column;
  380. align-items: center;
  381. }
  382. .service-icon {
  383. width: 88rpx;
  384. height: 88rpx;
  385. background-color: #E8F5E9;
  386. border-radius: 16rpx;
  387. display: flex;
  388. align-items: center;
  389. justify-content: center;
  390. margin-bottom: 16rpx;
  391. }
  392. .service-icon text {
  393. font-size: 32rpx;
  394. color: #4CAF50;
  395. font-weight: bold;
  396. }
  397. .icon-svg {
  398. width: 48rpx;
  399. height: 48rpx;
  400. display: block;
  401. }
  402. .service-name {
  403. font-size: 28rpx;
  404. color: #666;
  405. text-align: center;
  406. }
  407. .function-list {
  408. width: 100%;
  409. }
  410. .function-item {
  411. display: flex;
  412. justify-content: space-between;
  413. align-items: center;
  414. padding: 30rpx 0;
  415. border-bottom: 1rpx solid #f5f5f5;
  416. }
  417. .function-item:last-child {
  418. border-bottom: none;
  419. }
  420. .function-item .left {
  421. display: flex;
  422. align-items: center;
  423. }
  424. .function-icon {
  425. width: 44rpx;
  426. height: 44rpx;
  427. background-color: #E8F5E9;
  428. color: #4CAF50;
  429. font-size: 28rpx;
  430. font-weight: bold;
  431. border-radius: 8rpx;
  432. display: flex;
  433. align-items: center;
  434. justify-content: center;
  435. margin-right: 20rpx;
  436. }
  437. .function-item text {
  438. font-size: 28rpx;
  439. color: #333;
  440. }
  441. .arrow {
  442. color: #999;
  443. font-size: 24rpx;
  444. }
  445. .login-text {
  446. font-size: 32rpx;
  447. font-weight: bold;
  448. color: #fff;
  449. background-color: rgba(255, 255, 255, 0.2);
  450. padding: 10rpx 30rpx;
  451. border-radius: 30rpx;
  452. }
  453. </style>