mp-authorization.vue 3.7 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">授权信息</view>
  8. </view>
  9. <view class="head-subtitle">完善您的头像、昵称、手机号</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="头像">
  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="昵称">
  37. <uni-easyinput
  38. type="nickname"
  39. placeholder="请输入昵称"
  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"> 确认授权 </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. const props = defineProps({
  57. agreeStatus: {
  58. type: Boolean,
  59. default: false,
  60. },
  61. });
  62. const userInfo = computed(() => sheep.$store('user').userInfo);
  63. const accountLoginRef = ref(null);
  64. // 数据
  65. const state = reactive({
  66. model: {
  67. nickname: userInfo.value.nickname,
  68. avatar: userInfo.value.avatar,
  69. },
  70. rules: {},
  71. disabledStyle: {
  72. color: '#999',
  73. disableColor: '#fff',
  74. },
  75. });
  76. // 选择头像(来自微信)
  77. function onChooseAvatar(e) {
  78. const tempUrl = e.detail.avatarUrl || '';
  79. uploadAvatar(tempUrl);
  80. }
  81. // 选择头像(来自文件系统)
  82. async function uploadAvatar(tempUrl) {
  83. if (!tempUrl) {
  84. return;
  85. }
  86. let { data } = await FileApi.uploadFile(tempUrl);
  87. state.model.avatar = data;
  88. }
  89. // 确认授权
  90. async function onConfirm() {
  91. const { model } = state;
  92. const { nickname, avatar } = model;
  93. if (!nickname) {
  94. sheep.$helper.toast('请输入昵称');
  95. return;
  96. }
  97. if (!avatar) {
  98. sheep.$helper.toast('请选择头像');
  99. return;
  100. }
  101. // 发起更新
  102. const { code } = await UserApi.updateUser({
  103. avatar: state.model.avatar,
  104. nickname: state.model.nickname,
  105. });
  106. // 更新成功
  107. if (code === 0) {
  108. sheep.$helper.toast('授权成功');
  109. await sheep.$store('user').getInfo();
  110. closeAuthModal();
  111. }
  112. }
  113. </script>
  114. <style lang="scss" scoped>
  115. @import '../index.scss';
  116. .foot-box {
  117. width: 100%;
  118. display: flex;
  119. justify-content: center;
  120. }
  121. .authorization-btn {
  122. width: 686rpx;
  123. height: 80rpx;
  124. background-color: var(--ui-BG-Main);
  125. border-radius: 40rpx;
  126. color: #fff;
  127. }
  128. .avatar-img {
  129. width: 72rpx;
  130. height: 72rpx;
  131. border-radius: 36rpx;
  132. }
  133. .cicon-forward {
  134. font-size: 30rpx;
  135. color: #595959;
  136. }
  137. .avatar-btn {
  138. width: 100%;
  139. justify-content: space-between;
  140. }
  141. </style>