userCenter.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. <template>
  2. <view class="bg-box">
  3. <view class="placehold"></view>
  4. <view>
  5. <uni-list>
  6. <uni-list-item :showArrow="true">
  7. <template v-slot:body>
  8. <view class="avatar-box">
  9. <view class="avatar">
  10. <view>
  11. <Avatar :username="userInfo.nickname" color="#FFFFFF" backgroundColor="#BDE0FF"></Avatar>
  12. </view>
  13. <span class="nickname">{{ userInfo.nickname }}</span>
  14. </view>
  15. <view class="avatar-func" v-if="loginStatus" @click="modifyNickname">修改</view>
  16. <view class="avatar-func" v-else @click="login()">登录</view>
  17. </view>
  18. </template>
  19. </uni-list-item>
  20. </uni-list>
  21. </view>
  22. <view class="mt24 stat-board" v-if="loginStatus">
  23. <view class="stat-board-box">
  24. <view class="stat-item">
  25. <view class="stat-title">积分</view>
  26. </view>
  27. <view class="stat-item">
  28. <view class="stat-item-label">待使用</view>
  29. <view class="stat-item-value">{{ userInfo.unusedPoints }}</view>
  30. </view>
  31. <view class="stat-item">
  32. <view class="stat-item-label">已使用</view>
  33. <view class="stat-item-value">{{ userInfo.usedPoints }}</view>
  34. </view>
  35. <view class="stat-item">
  36. <view class="stat-item-label">总积分</view>
  37. <view class="stat-item-value">{{ userInfo.totalPoints }}</view>
  38. </view>
  39. </view>
  40. <view class="stat-board-box stat-mt16">
  41. <view class="stat-item">
  42. <view class="stat-title">彩票</view>
  43. </view>
  44. <view class="stat-item">
  45. <view class="stat-item-label">购买</view>
  46. <view class="stat-item-value">{{ userInfo.totalConsumeAmount }}元</view>
  47. </view>
  48. <view class="stat-item">
  49. <view class="stat-item-label">中奖</view>
  50. <view class="stat-item-value">{{ userInfo.totalWinAmount }}元</view>
  51. </view>
  52. <view class="stat-item">
  53. <view class="stat-item-label"></view>
  54. <view class="stat-item-value"></view>
  55. </view>
  56. </view>
  57. </view>
  58. <view class="mt24" v-if="loginStatus">
  59. <uni-list :border="true">
  60. <uni-list-item title="积分结算记录" :showArrow="true" :showExtraIcon="true"
  61. :extraIcon="{ color: '#000000', size: 22, type: 'info' }" link="navigateTo" :to="router.SETTLE_POINTS_PAGE">
  62. </uni-list-item>
  63. <uni-list-item title="积分兑换记录" :showArrow="true" :showExtraIcon="true" clickable
  64. :extraIcon="{ color: '#000000', size: 22, type: 'minus' }" link="navigateTo"
  65. :to="router.REWARD_EXCHANGE_HISTORY_PAGE">
  66. </uni-list-item>
  67. <uni-list-item title="彩票消费与中奖记录" :showArrow="true" :showExtraIcon="true"
  68. :extraIcon="{ color: '#000000', size: 22, type: 'info' }" link="navigateTo"
  69. :to="router.ITEM_CONSUME_WIN_HISTORY_PAGE">
  70. </uni-list-item>
  71. <uni-list-item title="账户转账记录" :showArrow="true" :showExtraIcon="true" clickable
  72. :extraIcon="{ color: '#000000', size: 22, type: 'minus' }" link="navigateTo"
  73. :to="router.ACCOUNT_TRANSFER_HISTORY_PAGE">
  74. </uni-list-item>
  75. </uni-list>
  76. </view>
  77. <view class="mt24" v-if="loginStatus">
  78. <uni-list :border="true">
  79. <uni-list-item title="积分账户" :showArrow="true" :showExtraIcon="true"
  80. :extraIcon="{ color: '#000000', size: 22, type: 'info' }" link="navigateTo" :to="router.ACCOUNT_LIST_PAGE">
  81. </uni-list-item>
  82. <uni-list-item title="兑换列表" :showArrow="true" :showExtraIcon="true" clickable
  83. :extraIcon="{ color: '#000000', size: 22, type: 'minus' }" link="navigateTo" :to="router.REWARD_LIST_PAGE">
  84. </uni-list-item>
  85. </uni-list>
  86. </view>
  87. <view class="mt24">
  88. <uni-list :border="true">
  89. <uni-list-item title="关于我们" :showArrow="true" :showExtraIcon="true"
  90. :extraIcon="{ color: '#000000', size: 22, type: 'info' }" link="navigateTo" :to="router.ABOUT_US_PAGE">
  91. </uni-list-item>
  92. <uni-list-item title="免责声明" :showArrow="true" :showExtraIcon="true" clickable
  93. :extraIcon="{ color: '#000000', size: 22, type: 'minus' }" link="navigateTo" :to="router.DISCLAIMER_PAGE">
  94. </uni-list-item>
  95. </uni-list>
  96. </view>
  97. <view class="mt24" v-if="loginStatus">
  98. <uni-list :border="true">
  99. <uni-list-item title="退出登录" :showArrow="true" :showExtraIcon="true"
  100. :extraIcon="{ color: 'red', size: 22, type: 'closeempty' }" :clickable="true" @click="logout()">
  101. </uni-list-item>
  102. </uni-list>
  103. </view>
  104. <!-- 修改昵称弹出框 -->
  105. <uni-popup ref="nicknameInputDialog" type="dialog" :is-mask-click="false">
  106. <uni-popup-dialog mode="input" :before-close="true" title="修改昵称" confirmText="保存" @confirm="nicknameFormConfirm"
  107. @close="nicknameFormClose">
  108. <view class=".nickname-dialog">
  109. <input class=".nickname" v-model="nicknameFormData.nickname" placeholder="请输入昵称" />
  110. </view>
  111. <view style="display: none">
  112. <uni-forms ref="nicknameForm" :modelValue="nicknameFormData" :rules="nicknameFormRules" label-position="top"
  113. :label-width="150" err-show-type="modal">
  114. <uni-forms-item name="nickname">
  115. <uni-easyinput type="text" placeholder="请输入昵称" v-model="nicknameFormData.nickname"></uni-easyinput>
  116. </uni-forms-item>
  117. </uni-forms>
  118. </view>
  119. </uni-popup-dialog>
  120. </uni-popup>
  121. </view>
  122. </template>
  123. <script setup>
  124. import { ref } from 'vue';
  125. import { onShow } from '@dcloudio/uni-app';
  126. import cacheKey from "@/common/cache.js";
  127. import router from "@/common/router.js";
  128. import { userApi } from "@/apis/apis.js";
  129. import Avatar from 'vue-avatar/src/Avatar.vue';
  130. // 组件
  131. /**
  132. * 昵称弹出框
  133. */
  134. const nicknameInputDialog = ref(null);
  135. /**
  136. * 昵称表单
  137. */
  138. const nicknameForm = ref(null);
  139. // 属性
  140. /**
  141. * 登录状态:false-未登录,true-已登录
  142. */
  143. const loginStatus = ref(false);
  144. /**
  145. * 用户信息
  146. */
  147. const userInfo = ref({
  148. nickname: ''
  149. });
  150. /**
  151. * 昵称表单数据
  152. */
  153. const nicknameFormData = ref({
  154. nickname: null,
  155. });
  156. /**
  157. * 昵称表单验证规则
  158. */
  159. const nicknameFormRules = ref({
  160. nickname: {
  161. rules: [
  162. {
  163. required: true,
  164. errorMessage: "昵称不能为空",
  165. },
  166. {
  167. maxLength: 30,
  168. errorMessage: "昵称不能超过{maxLength}个字符",
  169. },
  170. ],
  171. },
  172. });
  173. // 方法
  174. /**
  175. * 登录
  176. */
  177. const login = () => {
  178. uni.navigateTo({
  179. url: router.LOGIN_PAGE,
  180. });
  181. };
  182. /**
  183. * 注销登录
  184. */
  185. const logout = () => {
  186. uni.showModal({
  187. title: "注销提示",
  188. content: "确认退出登录?",
  189. success: function (res) {
  190. // 如果取消,则不执行后续代码
  191. if (res.cancel) {
  192. return;
  193. }
  194. // 移除用户信息
  195. uni.removeStorageSync(cacheKey.TOKEN);
  196. uni.removeStorageSync(cacheKey.NICKNAME);
  197. // 跳转到主页
  198. uni.reLaunch({
  199. url: router.TASK_TODO_PAGE
  200. });
  201. }
  202. });
  203. };
  204. /**
  205. * 获取用户信息
  206. */
  207. const fetchUserInfo = async () => {
  208. userInfo.value = await userApi.queryUserInfo();
  209. };
  210. /**
  211. * 修改昵称
  212. */
  213. const modifyNickname = (e) => {
  214. nicknameFormData.value.nickname = userInfo.value.nickname;
  215. nicknameInputDialog.value.open();
  216. };
  217. /**
  218. * 昵称修改表单 提交
  219. */
  220. const nicknameFormConfirm = async () => {
  221. let data = await nicknameForm.value.validate();
  222. await userApi.modifyNickname(data);
  223. userInfo.value.nickname = nicknameFormData.value.nickname;
  224. nicknameInputDialog.value.close();
  225. uni.showToast({
  226. title: "修改成功",
  227. icon: "success",
  228. });
  229. };
  230. /**
  231. * 昵称修改表单 关闭
  232. */
  233. const nicknameFormClose = async () => {
  234. nicknameInputDialog.value.close();
  235. };
  236. // 生命周期
  237. onShow(() => {
  238. // 根据登录凭据判断登录状态
  239. loginStatus.value = uni.getStorageSync(cacheKey.TOKEN) ? true : false;
  240. if (loginStatus.value) {
  241. fetchUserInfo();
  242. } else {
  243. userInfo.value.nickname = '游客';
  244. }
  245. });
  246. </script>
  247. <style lang="scss" scoped>
  248. .bg-box {
  249. background-color: #f5f5f5;
  250. height: 100vh;
  251. }
  252. .mt24 {
  253. margin-top: 16rpx;
  254. }
  255. .placehold {
  256. width: 100%;
  257. height: 24rpx;
  258. }
  259. .avatar-box {
  260. display: flex;
  261. align-items: center;
  262. justify-content: space-between;
  263. width: 100%;
  264. .avatar {
  265. display: flex;
  266. align-items: center;
  267. }
  268. .nickname {
  269. margin-left: 16rpx;
  270. }
  271. .avatar-func {
  272. font-size: 24rpx;
  273. font-weight: 400;
  274. letter-spacing: 0rpx;
  275. line-height: 34.75rpx;
  276. color: #000000;
  277. }
  278. }
  279. .nickname-dialog {
  280. width: 100%;
  281. height: 100rpx;
  282. border-radius: 8px;
  283. background: #ffffff;
  284. // 阴影
  285. border: 0.5px solid #e4e4e4;
  286. input {
  287. width: 100%;
  288. height: 100%;
  289. font-size: 36rpx;
  290. font-weight: 700;
  291. letter-spacing: 0rpx;
  292. line-height: 52.13rpx;
  293. color: #000000;
  294. border-radius: 24rpx;
  295. text-align: center;
  296. }
  297. }
  298. .stat-board {
  299. background-color: #ffffff;
  300. padding: 16rpx 64rpx 16rpx 24rpx;
  301. .stat-mt16 {
  302. margin-top: 16rpx;
  303. }
  304. .stat-board-box {
  305. display: flex;
  306. .stat-item {
  307. flex: 1;
  308. display: inline-block;
  309. .stat-title {
  310. width: 100%;
  311. height: 100%;
  312. font-size: 32rpx;
  313. font-weight: 400;
  314. letter-spacing: 0rpx;
  315. line-height: 46.34rpx;
  316. color: #000000;
  317. text-align: center;
  318. display: flex;
  319. /* 启用flex布局 */
  320. justify-content: center;
  321. /* 水平居中 */
  322. align-items: center;
  323. /* 垂直居中 */
  324. }
  325. .stat-item-label {
  326. font-size: 24rpx;
  327. font-weight: 400;
  328. letter-spacing: 0rpx;
  329. line-height: 34.75rpx;
  330. color: #000000;
  331. text-align: center;
  332. vertical-align: middle;
  333. }
  334. .stat-item-value {
  335. font-size: 32rpx;
  336. font-weight: 400;
  337. letter-spacing: 0rpx;
  338. line-height: 46.34rpx;
  339. color: #000000;
  340. text-align: center;
  341. vertical-align: middle;
  342. }
  343. }
  344. }
  345. }
  346. </style>