relation-list.vue 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. <template>
  2. <view class="rel-page">
  3. <view class="rel-nav" :style="{ paddingTop: statusBar + 'px' }">
  4. <view class="rel-nav-row">
  5. <view class="rel-back" @tap="goBack">
  6. <text class="rel-back-icon">←</text>
  7. </view>
  8. <text class="rel-nav-title">{{ title }}</text>
  9. <view class="rel-nav-placeholder" />
  10. </view>
  11. </view>
  12. <scroll-view
  13. scroll-y
  14. class="rel-scroll"
  15. :style="{ height: scrollHeight + 'px' }"
  16. :lower-threshold="120"
  17. @scrolltolower="onScrollToLower"
  18. >
  19. <view class="rel-body" :style="{ paddingBottom: safeBottom + 'px' }">
  20. <view v-if="loading && !rows.length" class="rel-state">
  21. <text class="rel-state-txt">{{ labels.loading }}</text>
  22. </view>
  23. <empty
  24. v-else-if="loaded && !rows.length"
  25. src="find_default3x"
  26. :text="labels.empty"
  27. />
  28. <view v-else class="rel-list">
  29. <view
  30. v-for="(item, index) in rows"
  31. :key="rowKey(item, index)"
  32. class="rel-card"
  33. @tap="openUser(item)"
  34. >
  35. <image
  36. class="rel-avatar"
  37. :src="avatarUrl(item)"
  38. mode="aspectFill"
  39. />
  40. <view class="rel-main">
  41. <view class="rel-info">
  42. <view class="rel-name-row">
  43. <text v-if="item.shop" class="rel-tag">{{ labels.shopTag }}</text>
  44. <text class="rel-name">{{ displayName(item) }}</text>
  45. </view>
  46. <text class="rel-bio">{{ displayBio(item) }}</text>
  47. </view>
  48. <button
  49. class="rel-follow-btn"
  50. :class="{ 'rel-follow-btn--on': item.isFollow === 1 }"
  51. :disabled="followBusyIndex === index"
  52. @tap.stop="toggleFollow(index)"
  53. >
  54. {{ item.isFollow === 1 ? labels.followed : labels.follow }}
  55. </button>
  56. </view>
  57. </view>
  58. </view>
  59. <uni-load-more
  60. v-if="rows.length"
  61. :status="loadStatus"
  62. :content-text="loadMoreContent"
  63. />
  64. </view>
  65. </scroll-view>
  66. </view>
  67. </template>
  68. <script>
  69. import { fetchRelationPage, toggleProfileFollow } from '@/features/profile/profile-gateway'
  70. import { stripRichText } from '@/features/profile/catalog-normalize'
  71. export default {
  72. name: 'ProfileRelationList',
  73. props: {
  74. userNo: {
  75. type: [String, Number],
  76. default: ''
  77. },
  78. listType: {
  79. type: String,
  80. required: true
  81. },
  82. title: {
  83. type: String,
  84. default: ''
  85. },
  86. emptyText: {
  87. type: String,
  88. default: ''
  89. },
  90. followLabel: {
  91. type: String,
  92. default: ''
  93. },
  94. unfollowLabel: {
  95. type: String,
  96. default: ''
  97. },
  98. defaultBio: {
  99. type: String,
  100. default: ''
  101. },
  102. loadingText: { type: String, default: '' },
  103. noMoreText: { type: String, default: '' },
  104. loadMoreHint: { type: String, default: '' },
  105. loadFailedText: { type: String, default: '' },
  106. actionFailedText: { type: String, default: '' },
  107. shopTag: { type: String, default: '' }
  108. },
  109. data() {
  110. return {
  111. statusBar: 0,
  112. scrollHeight: 600,
  113. safeBottom: 0,
  114. rows: [],
  115. page: 1,
  116. lastPage: 1,
  117. loading: false,
  118. loaded: false,
  119. loadStatus: 'more',
  120. followBusyIndex: -1
  121. }
  122. },
  123. computed: {
  124. labels() {
  125. const t = this.$t.bind(this)
  126. return {
  127. loading: this.loadingText || t('relation.loading'),
  128. noMore: this.noMoreText || t('relation.noMore'),
  129. loadMore: this.loadMoreHint || t('relation.loadMore'),
  130. empty: this.emptyText || t('relation.emptyData'),
  131. follow: this.followLabel || t('mine.follow'),
  132. followed: this.unfollowLabel || t('mine.followed'),
  133. defaultBio: this.defaultBio || t('findUser.defaultBio'),
  134. shopTag: this.shopTag || t('relation.shopTag'),
  135. loadFailed: this.loadFailedText || t('relation.loadFailed'),
  136. actionFailed: this.actionFailedText || t('relation.actionFailed')
  137. }
  138. },
  139. loadMoreContent() {
  140. return {
  141. contentdown: this.labels.loadMore,
  142. contentrefresh: this.labels.loading,
  143. contentnomore: this.labels.noMore
  144. }
  145. }
  146. },
  147. watch: {
  148. userNo: {
  149. immediate: true,
  150. handler(val) {
  151. if (val) this.reload()
  152. }
  153. }
  154. },
  155. methods: {
  156. initLayout() {
  157. try {
  158. const info = uni.getSystemInfoSync()
  159. this.statusBar = info.statusBarHeight || 0
  160. this.safeBottom = (info.safeAreaInsets && info.safeAreaInsets.bottom) || 0
  161. const navH = this.statusBar + uni.upx2px(88)
  162. this.scrollHeight = (info.windowHeight || info.screenHeight || 600) - navH
  163. } catch (_) {}
  164. },
  165. rowKey(item, index) {
  166. return item.id || item.user_no || `row-${index}`
  167. },
  168. avatarUrl(item) {
  169. const path = item.shop
  170. ? item.shop.avatar
  171. : (item.user && item.user.avatar) || ''
  172. return path ? this.$appKit.oss(path, 88, 88, 2, 'avatar') : ''
  173. },
  174. displayName(item) {
  175. if (item.shop && item.shop.shopname) return item.shop.shopname
  176. return (item.user && item.user.nickname) || '—'
  177. },
  178. displayBio(item) {
  179. const raw = item.shop
  180. ? item.shop.bio
  181. : (item.user && item.user.bio) || ''
  182. const text = stripRichText(raw)
  183. return text || this.labels.defaultBio
  184. },
  185. goBack() {
  186. uni.navigateBack({
  187. fail: () => uni.switchTab({ url: '/pages/mine/index' })
  188. })
  189. },
  190. openUser(item) {
  191. if (!item || !item.user_no) return
  192. uni.navigateTo({
  193. url: `/pages/profile/show?id=${encodeURIComponent(String(item.user_no))}`
  194. })
  195. },
  196. async reload() {
  197. if (!this.userNo) return
  198. this.page = 1
  199. this.lastPage = 1
  200. this.loadStatus = 'more'
  201. this.loaded = false
  202. this.rows = []
  203. await this.loadPage(true)
  204. },
  205. onScrollToLower() {
  206. if (this.loadStatus === 'loading' || this.loadStatus === 'noMore') return
  207. this.loadPage(false)
  208. },
  209. async loadPage(reset) {
  210. if (!this.userNo || this.loading) return
  211. if (!reset && this.page >= this.lastPage) {
  212. this.loadStatus = 'noMore'
  213. return
  214. }
  215. const page = reset ? 1 : this.page + 1
  216. this.loading = true
  217. this.loadStatus = 'loading'
  218. try {
  219. const pack = await fetchRelationPage({
  220. userNo: this.userNo,
  221. listType: this.listType,
  222. page
  223. })
  224. this.page = pack.currentPage
  225. this.lastPage = pack.lastPage
  226. if (reset) {
  227. this.rows = pack.rows
  228. } else {
  229. this.rows = this.rows.concat(pack.rows)
  230. }
  231. this.loadStatus =
  232. pack.currentPage >= pack.lastPage || !pack.rows.length ? 'noMore' : 'more'
  233. this.loaded = true
  234. } catch (err) {
  235. this.loadStatus = 'more'
  236. if (reset) this.rows = []
  237. this.$appKit && this.$appKit.msg(err.message || this.labels.loadFailed)
  238. this.loaded = true
  239. } finally {
  240. this.loading = false
  241. }
  242. },
  243. async toggleFollow(index) {
  244. if (!this.$appKit || !this.$appKit.requireLogin()) return
  245. const row = this.rows[index]
  246. if (!row || !row.user_no) return
  247. if (this.followBusyIndex === index) return
  248. this.followBusyIndex = index
  249. try {
  250. const flag = await toggleProfileFollow(row.user_no)
  251. if (flag === 0 || flag === '0') row.isFollow = 0
  252. else if (flag === 1 || flag === '1') row.isFollow = 1
  253. } catch (err) {
  254. this.$appKit.msg(err.message || this.labels.actionFailed)
  255. } finally {
  256. this.followBusyIndex = -1
  257. }
  258. }
  259. },
  260. mounted() {
  261. this.initLayout()
  262. }
  263. }
  264. </script>
  265. <style scoped>
  266. .rel-page {
  267. min-height: 100vh;
  268. background-color: #f4f6fb;
  269. }
  270. .rel-nav {
  271. background-color: #ffffff;
  272. border-bottom: 1rpx solid #eef1f6;
  273. }
  274. .rel-nav-row {
  275. display: flex;
  276. flex-direction: row;
  277. align-items: center;
  278. height: 88rpx;
  279. padding: 0 24rpx;
  280. box-sizing: border-box;
  281. }
  282. .rel-back {
  283. width: 64rpx;
  284. height: 64rpx;
  285. display: flex;
  286. align-items: center;
  287. justify-content: center;
  288. }
  289. .rel-back-icon {
  290. font-size: 40rpx;
  291. color: #1a1d26;
  292. line-height: 1;
  293. }
  294. .rel-nav-title {
  295. flex: 1;
  296. text-align: center;
  297. font-size: 32rpx;
  298. font-weight: 600;
  299. color: #1a1d26;
  300. }
  301. .rel-nav-placeholder {
  302. width: 64rpx;
  303. }
  304. .rel-body {
  305. padding: 24rpx 28rpx 0;
  306. box-sizing: border-box;
  307. }
  308. .rel-state {
  309. padding: 120rpx 0;
  310. text-align: center;
  311. }
  312. .rel-state-txt {
  313. font-size: 28rpx;
  314. color: #8a8d9e;
  315. }
  316. .rel-list {
  317. display: flex;
  318. flex-direction: column;
  319. }
  320. .rel-card {
  321. display: flex;
  322. flex-direction: row;
  323. align-items: center;
  324. padding: 24rpx;
  325. margin-bottom: 20rpx;
  326. background-color: #ffffff;
  327. border-radius: 24rpx;
  328. box-shadow: 0 6rpx 24rpx rgba(74, 140, 255, 0.06);
  329. box-sizing: border-box;
  330. }
  331. .rel-avatar {
  332. width: 100rpx;
  333. height: 100rpx;
  334. border-radius: 50rpx;
  335. flex-shrink: 0;
  336. background-color: #e8edf5;
  337. }
  338. .rel-main {
  339. flex: 1;
  340. min-width: 0;
  341. margin-left: 20rpx;
  342. display: flex;
  343. flex-direction: row;
  344. align-items: center;
  345. justify-content: space-between;
  346. }
  347. .rel-info {
  348. flex: 1;
  349. min-width: 0;
  350. margin-right: 16rpx;
  351. }
  352. .rel-name-row {
  353. display: flex;
  354. flex-direction: row;
  355. align-items: center;
  356. margin-bottom: 8rpx;
  357. }
  358. .rel-tag {
  359. flex-shrink: 0;
  360. margin-right: 8rpx;
  361. padding: 2rpx 10rpx;
  362. border-radius: 8rpx;
  363. background: linear-gradient(135deg, #4a8cff 0%, #6ec8ff 100%);
  364. font-size: 20rpx;
  365. color: #ffffff;
  366. }
  367. .rel-name {
  368. font-size: 30rpx;
  369. font-weight: 600;
  370. color: #1a1d26;
  371. overflow: hidden;
  372. text-overflow: ellipsis;
  373. white-space: nowrap;
  374. }
  375. .rel-bio {
  376. display: block;
  377. font-size: 24rpx;
  378. color: #8a8d9e;
  379. line-height: 1.4;
  380. overflow: hidden;
  381. text-overflow: ellipsis;
  382. white-space: nowrap;
  383. }
  384. .rel-follow-btn {
  385. flex-shrink: 0;
  386. margin: 0;
  387. padding: 0 28rpx;
  388. height: 56rpx;
  389. line-height: 56rpx;
  390. font-size: 24rpx;
  391. color: #ffffff;
  392. background: linear-gradient(135deg, #4a8cff 0%, #6ec8ff 100%);
  393. border-radius: 999rpx;
  394. border: none;
  395. }
  396. .rel-follow-btn--on {
  397. color: #8a8d9e;
  398. background: #f4f6fb;
  399. border: 1rpx solid #e8edf5;
  400. }
  401. .rel-follow-btn::after {
  402. border: none;
  403. }
  404. </style>