s-auth-modal.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <template>
  2. <!-- 规格弹窗 -->
  3. <su-popup :show="authType !== ''" round="10" :showClose="true" @close="closeAuthModal">
  4. <view class="login-wrap">
  5. <!-- 1. 账号密码登录 accountLogin -->
  6. <account-login v-if="authType === 'accountLogin'" :agreeStatus="state.protocol" @onConfirm="onConfirm" />
  7. <!-- 2. 短信登录 smsLogin -->
  8. <sms-login v-if="authType === 'smsLogin'" :agreeStatus="state.protocol" @onConfirm="onConfirm" />
  9. <!-- 3. 忘记密码 resetPassword-->
  10. <reset-password v-if="authType === 'resetPassword'" />
  11. <!-- 4. 绑定手机号 changeMobile -->
  12. <change-mobile v-if="authType === 'changeMobile'" />
  13. <!-- 5. 修改密码 changePassword-->
  14. <changePassword v-if="authType === 'changePassword'" />
  15. <!-- 6. 微信小程序授权 -->
  16. <mp-authorization v-if="authType === 'mpAuthorization'" />
  17. <!-- 7. 第三方登录 -->
  18. <view v-if="['accountLogin', 'smsLogin'].includes(authType)"
  19. class="auto-login-box ss-flex ss-flex-col ss-row-center ss-col-center">
  20. <!-- 7.1 微信小程序的快捷登录 -->
  21. <view v-if="sheep.$platform.name === 'WechatMiniProgram'" class="ss-flex register-box">
  22. <view class="register-title">还没有账号?</view>
  23. <button class="ss-reset-button login-btn" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">
  24. 快捷登录
  25. </button>
  26. <view class="circle" />
  27. </view>
  28. <!-- 7.2 微信的公众号、App、小程序的登录,基于 openid + code -->
  29. <button v-if="
  30. ['WechatOfficialAccount', 'WechatMiniProgram', 'App'].includes(sheep.$platform.name) &&
  31. sheep.$platform.isWechatInstalled
  32. " @tap="thirdLogin('wechat')" class="ss-reset-button auto-login-btn">
  33. <image class="auto-login-img" :src="sheep.$url.static('/static/img/shop/platform/wechat.png')" />
  34. </button>
  35. <!-- 7.3 iOS 登录 TODO 芋艿:等后面搞 App 再弄 -->
  36. <button v-if="sheep.$platform.os === 'ios' && sheep.$platform.name === 'App'" @tap="thirdLogin('apple')"
  37. class="ss-reset-button auto-login-btn">
  38. <image class="auto-login-img" :src="sheep.$url.static('/static/img/shop/platform/apple.png')" />
  39. </button>
  40. </view>
  41. <!-- 用户协议的勾选 -->
  42. <view v-if="['accountLogin', 'smsLogin'].includes(authType)" class="agreement-box ss-flex ss-row-center"
  43. :class="{ shake: currentProtocol }">
  44. <label class="radio ss-flex ss-col-center" @tap="onChange">
  45. <radio :checked="state.protocol" color="var(--ui-BG-Main)" style="transform: scale(0.8)"
  46. @tap.stop="onChange" />
  47. <view class="agreement-text ss-flex ss-col-center ss-m-l-8">
  48. 我已阅读并遵守
  49. <view class="tcp-text" @tap.stop="onProtocol('用户协议')"> 《用户协议》 </view>
  50. <view class="agreement-text">与</view>
  51. <view class="tcp-text" @tap.stop="onProtocol('隐私协议')"> 《隐私协议》 </view>
  52. </view>
  53. </label>
  54. </view>
  55. <view class="safe-box" />
  56. </view>
  57. </su-popup>
  58. </template>
  59. <script setup>
  60. import { computed, reactive, ref } from 'vue';
  61. import sheep from '@/sheep';
  62. import accountLogin from './components/account-login.vue';
  63. import smsLogin from './components/sms-login.vue';
  64. import resetPassword from './components/reset-password.vue';
  65. import changeMobile from './components/change-mobile.vue';
  66. import changePassword from './components/change-password.vue';
  67. import mpAuthorization from './components/mp-authorization.vue';
  68. import { closeAuthModal, showAuthModal } from '@/sheep/hooks/useModal';
  69. const modalStore = sheep.$store('modal');
  70. // 授权弹窗类型
  71. const authType = computed(() => modalStore.auth);
  72. const state = reactive({
  73. protocol: false,
  74. });
  75. const currentProtocol = ref(false);
  76. // 勾选协议
  77. function onChange() {
  78. state.protocol = !state.protocol;
  79. }
  80. // 查看协议
  81. function onProtocol(title) {
  82. closeAuthModal();
  83. sheep.$router.go('/pages/public/richtext', {
  84. title,
  85. });
  86. }
  87. // 点击登录 / 注册事件
  88. function onConfirm(e) {
  89. currentProtocol.value = e;
  90. setTimeout(() => {
  91. currentProtocol.value = false;
  92. }, 1000);
  93. }
  94. // 第三方授权登陆(微信小程序、Apple)
  95. const thirdLogin = async (provider) => {
  96. if (!state.protocol) {
  97. currentProtocol.value = true;
  98. setTimeout(() => {
  99. currentProtocol.value = false;
  100. }, 1000);
  101. sheep.$helper.toast('请勾选同意');
  102. return;
  103. }
  104. const loginRes = await sheep.$platform.useProvider(provider).login();
  105. if (loginRes) {
  106. const userInfo = await sheep.$store('user').getInfo();
  107. closeAuthModal();
  108. // 如果用户已经有头像和昵称,不需要再次授权
  109. if (userInfo.avatar && userInfo.nickname) {
  110. return;
  111. }
  112. // 触发小程序授权信息弹框
  113. // #ifdef MP-WEIXIN
  114. showAuthModal('mpAuthorization');
  115. // #endif
  116. }
  117. };
  118. // 微信小程序的“手机号快速验证”:https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/getPhoneNumber.html
  119. const getPhoneNumber = async (e) => {
  120. if (e.detail.errMsg !== 'getPhoneNumber:ok') {
  121. sheep.$helper.toast('快捷登录失败');
  122. return;
  123. }
  124. let result = await sheep.$platform.useProvider().mobileLogin(e.detail);
  125. if (result) {
  126. closeAuthModal();
  127. }
  128. };
  129. </script>
  130. <style lang="scss" scoped>
  131. @import './index.scss';
  132. .shake {
  133. animation: shake 0.05s linear 4 alternate;
  134. }
  135. @keyframes shake {
  136. from {
  137. transform: translateX(-10rpx);
  138. }
  139. to {
  140. transform: translateX(10rpx);
  141. }
  142. }
  143. .register-box {
  144. position: relative;
  145. justify-content: center;
  146. .register-btn {
  147. color: #999999;
  148. font-size: 30rpx;
  149. font-weight: 500;
  150. }
  151. .register-title {
  152. color: #999999;
  153. font-size: 30rpx;
  154. font-weight: 400;
  155. margin-right: 24rpx;
  156. }
  157. .or-title {
  158. margin: 0 16rpx;
  159. color: #999999;
  160. font-size: 30rpx;
  161. font-weight: 400;
  162. }
  163. .login-btn {
  164. color: var(--ui-BG-Main);
  165. font-size: 30rpx;
  166. font-weight: 500;
  167. }
  168. .circle {
  169. position: absolute;
  170. right: 0rpx;
  171. top: 18rpx;
  172. width: 8rpx;
  173. height: 8rpx;
  174. border-radius: 8rpx;
  175. background: var(--ui-BG-Main);
  176. }
  177. }
  178. .safe-box {
  179. height: calc(constant(safe-area-inset-bottom) / 5 * 3);
  180. height: calc(env(safe-area-inset-bottom) / 5 * 3);
  181. }
  182. .tcp-text {
  183. color: var(--ui-BG-Main);
  184. }
  185. .agreement-text {
  186. color: $dark-9;
  187. }
  188. </style>