mp-authorization.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <!-- 微信授权信息 mpAuthorization -->
  2. <template>
  3. <view>
  4. <!-- 标题栏 -->
  5. <view class="head-box ss-m-b-60 ss-flex-col">
  6. <view class="ss-flex ss-m-b-20">
  7. <view class="head-title ss-m-r-40 head-title-animation">{{ t('account.authorization_info')}}</view>
  8. </view>
  9. <view class="head-subtitle">{{ t('account.complete_profile') }}</view>
  10. </view>
  11. <!-- 表单项 -->
  12. <uni-forms
  13. ref="accountLoginRef"
  14. v-model="state.model"
  15. :rules="state.rules"
  16. validateTrigger="bind"
  17. labelWidth="140"
  18. labelAlign="center"
  19. >
  20. <!-- 获取头像昵称:https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/userProfile.html -->
  21. <uni-forms-item name="avatar" :label="t('account.avatar')">
  22. <button
  23. class="ss-reset-button avatar-btn"
  24. open-type="chooseAvatar"
  25. @chooseavatar="onChooseAvatar"
  26. >
  27. <image
  28. class="avatar-img"
  29. :src="sheep.$url.cdn(state.model.avatar)"
  30. mode="aspectFill"
  31. @tap="sheep.$router.go('/pages/user/info')"
  32. />
  33. <text class="cicon-forward" />
  34. </button>
  35. </uni-forms-item>
  36. <uni-forms-item name="nickname" :label="t('account.nickname')">
  37. <uni-easyinput
  38. type="nickname"
  39. :placeholder="t('account.enter_nickname')"
  40. v-model="state.model.nickname"
  41. :inputBorder="false"
  42. />
  43. </uni-forms-item>
  44. <view class="foot-box">
  45. <button class="ss-reset-button authorization-btn" @tap="onConfirm"> {{ t('account.confirm_authorization') }} </button>
  46. </view>
  47. </uni-forms>
  48. </view>
  49. </template>
  50. <script setup>
  51. import { computed, ref, reactive } from 'vue';
  52. import sheep from '@/sheep';
  53. import { closeAuthModal } from '@/sheep/hooks/useModal';
  54. import FileApi from '@/sheep/api/infra/file';
  55. import UserApi from '@/sheep/api/member/user';
  56. import { t } from '@/locale'
  57. const props = defineProps({
  58. agreeStatus: {
  59. type: Boolean,
  60. default: false,
  61. },
  62. });
  63. const userInfo = computed(() => sheep.$store('user').userInfo);
  64. const accountLoginRef = ref(null);
  65. // 数据
  66. const state = reactive({
  67. model: {
  68. nickname: userInfo.value.nickname,
  69. avatar: userInfo.value.avatar,
  70. },
  71. rules: {},
  72. disabledStyle: {
  73. color: '#999',
  74. disableColor: '#fff',
  75. },
  76. });
  77. // 选择头像(来自微信)
  78. function onChooseAvatar(e) {
  79. const tempUrl = e.detail.avatarUrl || '';
  80. uploadAvatar(tempUrl);
  81. }
  82. // 选择头像(来自文件系统)
  83. async function uploadAvatar(tempUrl) {
  84. if (!tempUrl) {
  85. return;
  86. }
  87. let { data } = await FileApi.uploadFile(tempUrl);
  88. state.model.avatar = data;
  89. }
  90. // 确认授权
  91. async function onConfirm() {
  92. const { model } = state;
  93. const { nickname, avatar } = model;
  94. if (!nickname) {
  95. sheep.$helper.toast(t('account.select_avatar'));
  96. return;
  97. }
  98. if (!avatar) {
  99. sheep.$helper.toast(t('account.enter_nickname'));
  100. return;
  101. }
  102. // 发起更新
  103. const { code } = await UserApi.updateUser({
  104. avatar: state.model.avatar,
  105. nickname: state.model.nickname,
  106. });
  107. // 更新成功
  108. if (code === 0) {
  109. sheep.$helper.toast(t('account.authorization_successful'));
  110. await sheep.$store('user').getInfo();
  111. closeAuthModal();
  112. }
  113. }
  114. </script>
  115. <style lang="scss" scoped>
  116. @import '../index.scss';
  117. .foot-box {
  118. width: 100%;
  119. display: flex;
  120. justify-content: center;
  121. }
  122. .authorization-btn {
  123. width: 686rpx;
  124. height: 80rpx;
  125. background-color: var(--ui-BG-Main);
  126. border-radius: 40rpx;
  127. color: #fff;
  128. }
  129. .avatar-img {
  130. width: 72rpx;
  131. height: 72rpx;
  132. border-radius: 36rpx;
  133. }
  134. .cicon-forward {
  135. font-size: 30rpx;
  136. color: #595959;
  137. }
  138. .avatar-btn {
  139. width: 100%;
  140. justify-content: space-between;
  141. }
  142. </style>