s-auth-modal.vue 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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
  7. v-if="authType === 'accountLogin'"
  8. :agreeStatus="state.protocol"
  9. @onConfirm="onConfirm"
  10. />
  11. <!-- 2. 短信登录 smsLogin -->
  12. <sms-login
  13. v-if="authType === 'smsLogin'"
  14. :agreeStatus="state.protocol"
  15. @onConfirm="onConfirm"
  16. />
  17. <!-- 3. 忘记密码 resetPassword-->
  18. <reset-password v-if="authType === 'resetPassword'" />
  19. <!-- 4. 绑定手机号 changeMobile -->
  20. <change-mobile v-if="authType === 'changeMobile'" />
  21. <!-- 5. 修改密码 changePassword-->
  22. <changePassword v-if="authType === 'changePassword'" />
  23. <!-- 6. 微信小程序授权 -->
  24. <mp-authorization v-if="authType === 'mpAuthorization'" />
  25. <!-- 7. 第三方登录 -->
  26. <view
  27. v-if="['accountLogin', 'smsLogin'].includes(authType)"
  28. class="auto-login-box ss-flex ss-flex-col ss-row-center ss-col-center"
  29. >
  30. <!-- 7.1 微信小程序的快捷登录 -->
  31. <view v-if="sheep.$platform.name === 'WechatMiniProgram'" class="ss-flex register-box">
  32. <view class="register-title">还没有账号?</view>
  33. <button
  34. class="ss-reset-button login-btn"
  35. open-type="getPhoneNumber"
  36. @getphonenumber="getPhoneNumber"
  37. >
  38. 快捷登录
  39. </button>
  40. <view class="circle" />
  41. </view>
  42. <!-- 7.2 微信的公众号、App、小程序的登录,基于 openid + code -->
  43. <button
  44. v-if="
  45. ['WechatOfficialAccount', 'WechatMiniProgram', 'App'].includes(sheep.$platform.name) &&
  46. sheep.$platform.isWechatInstalled
  47. "
  48. @tap="thirdLogin('wechat')"
  49. class="ss-reset-button auto-login-btn"
  50. >
  51. <image
  52. class="auto-login-img"
  53. :src="sheep.$url.static('/static/img/shop/platform/wechat.png')"
  54. />
  55. </button>
  56. <!-- 7.3 iOS 登录 TODO 芋艿:等后面搞 App 再弄 -->
  57. <button
  58. v-if="sheep.$platform.os === 'ios' && sheep.$platform.name === 'App'"
  59. @tap="thirdLogin('apple')"
  60. class="ss-reset-button auto-login-btn"
  61. >
  62. <image
  63. class="auto-login-img"
  64. :src="sheep.$url.static('/static/img/shop/platform/apple.png')"
  65. />
  66. </button>
  67. <!-- 7.4 支付宝小程序登录 -->
  68. <button
  69. v-if="sheep.$platform.name === 'alipayMiniProgram'"
  70. @tap="thirdLogin('alipay')"
  71. class="ss-reset-button auto-login-btn"
  72. >
  73. <image
  74. class="auto-login-img"
  75. :src="sheep.$url.static('/static/img/shop/pay/alipay.png')"
  76. />
  77. </button>
  78. </view>
  79. <!-- 用户协议的勾选 -->
  80. <view
  81. v-if="['accountLogin', 'smsLogin'].includes(authType)"
  82. class="agreement-box ss-flex ss-flex-col ss-col-center"
  83. :class="{ shake: currentProtocol }"
  84. >
  85. <view class="agreement-title ss-m-b-20">请选择是否同意以下协议(请联网查看):</view>
  86. <view class="agreement-options-container">
  87. <!-- 同意选项 -->
  88. <view class="agreement-option ss-m-b-20">
  89. <view class="radio ss-flex ss-col-center" @tap="onAgree">
  90. <radio
  91. :checked="state.protocol === true"
  92. color="var(--ui-BG-Main)"
  93. style="transform: scale(0.8)"
  94. @tap.stop="onAgree"
  95. />
  96. <view class="agreement-text ss-flex ss-col-center ss-m-l-8">
  97. 我已阅读并同意遵守
  98. <view class="tcp-text" @tap.stop="onProtocol('用户协议')"> 《用户协议》 </view>
  99. <view class="agreement-text">与</view>
  100. <view class="tcp-text" @tap.stop="onProtocol('隐私协议')"> 《隐私协议》 </view>
  101. </view>
  102. </view>
  103. </view>
  104. <!-- 拒绝选项 -->
  105. <view class="agreement-option">
  106. <view class="radio ss-flex ss-col-center" @tap="onRefuse">
  107. <radio
  108. :checked="state.protocol === false"
  109. color="#ff4d4f"
  110. style="transform: scale(0.8)"
  111. @tap.stop="onRefuse"
  112. />
  113. <view class="agreement-text ss-flex ss-col-center ss-m-l-8">
  114. 我拒绝遵守
  115. <view class="tcp-text" @tap.stop="onProtocol('用户协议')"> 《用户协议》 </view>
  116. <view class="agreement-text">与</view>
  117. <view class="tcp-text" @tap.stop="onProtocol('隐私协议')"> 《隐私协议》 </view>
  118. </view>
  119. </view>
  120. </view>
  121. </view>
  122. </view>
  123. <view class="safe-box" />
  124. </view>
  125. </su-popup>
  126. </template>
  127. <script setup>
  128. import { computed, reactive, ref } from 'vue';
  129. import sheep from '@/sheep';
  130. import accountLogin from './components/account-login.vue';
  131. import smsLogin from './components/sms-login.vue';
  132. import resetPassword from './components/reset-password.vue';
  133. import changeMobile from './components/change-mobile.vue';
  134. import changePassword from './components/change-password.vue';
  135. import mpAuthorization from './components/mp-authorization.vue';
  136. import { closeAuthModal, showAuthModal } from '@/sheep/hooks/useModal';
  137. const modalStore = sheep.$store('modal');
  138. // 授权弹窗类型
  139. const authType = computed(() => modalStore.auth);
  140. const state = reactive({
  141. protocol: null, // null表示未选择,true表示同意,false表示拒绝
  142. });
  143. const currentProtocol = ref(false);
  144. // 同意协议
  145. function onAgree() {
  146. state.protocol = true;
  147. }
  148. // 拒绝协议
  149. function onRefuse() {
  150. state.protocol = false;
  151. }
  152. // 查看协议
  153. function onProtocol(title) {
  154. closeAuthModal();
  155. sheep.$router.go('/pages/public/richtext', {
  156. title,
  157. });
  158. }
  159. // 点击登录 / 注册事件
  160. function onConfirm(e) {
  161. currentProtocol.value = e;
  162. setTimeout(() => {
  163. currentProtocol.value = false;
  164. }, 1000);
  165. }
  166. // 第三方授权登陆(微信小程序、Apple)
  167. const thirdLogin = async (provider) => {
  168. if (state.protocol !== true) {
  169. currentProtocol.value = true;
  170. setTimeout(() => {
  171. currentProtocol.value = false;
  172. }, 1000);
  173. if (state.protocol === false) {
  174. sheep.$helper.toast('您已拒绝协议,无法继续登录');
  175. } else {
  176. sheep.$helper.toast('请选择是否同意协议');
  177. }
  178. return;
  179. }
  180. const loginRes = await sheep.$platform.useProvider(provider).login();
  181. if (loginRes) {
  182. const userInfo = await sheep.$store('user').getInfo();
  183. closeAuthModal();
  184. // 如果用户已经有头像和昵称,不需要再次授权
  185. if (userInfo.avatar && userInfo.nickname) {
  186. return;
  187. }
  188. // 触发小程序授权信息弹框
  189. // #ifdef MP-WEIXIN
  190. showAuthModal('mpAuthorization');
  191. // #endif
  192. }
  193. };
  194. // 微信小程序的“手机号快速验证”:https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/getPhoneNumber.html
  195. const getPhoneNumber = async (e) => {
  196. if (e.detail.errMsg !== 'getPhoneNumber:ok') {
  197. sheep.$helper.toast('快捷登录失败');
  198. return;
  199. }
  200. let result = await sheep.$platform.useProvider().mobileLogin(e.detail);
  201. if (result) {
  202. closeAuthModal();
  203. }
  204. };
  205. </script>
  206. <style lang="scss" scoped>
  207. @import './index.scss';
  208. .shake {
  209. animation: shake 0.05s linear 4 alternate;
  210. }
  211. @keyframes shake {
  212. from {
  213. transform: translateX(-10rpx);
  214. }
  215. to {
  216. transform: translateX(10rpx);
  217. }
  218. }
  219. .register-box {
  220. position: relative;
  221. justify-content: center;
  222. .register-btn {
  223. color: #999999;
  224. font-size: 30rpx;
  225. font-weight: 500;
  226. }
  227. .register-title {
  228. color: #999999;
  229. font-size: 30rpx;
  230. font-weight: 400;
  231. margin-right: 24rpx;
  232. }
  233. .or-title {
  234. margin: 0 16rpx;
  235. color: #999999;
  236. font-size: 30rpx;
  237. font-weight: 400;
  238. }
  239. .login-btn {
  240. color: var(--ui-BG-Main);
  241. font-size: 30rpx;
  242. font-weight: 500;
  243. }
  244. .circle {
  245. position: absolute;
  246. right: 0rpx;
  247. top: 18rpx;
  248. width: 8rpx;
  249. height: 8rpx;
  250. border-radius: 8rpx;
  251. background: var(--ui-BG-Main);
  252. }
  253. }
  254. .safe-box {
  255. height: calc(constant(safe-area-inset-bottom) / 5 * 3);
  256. height: calc(env(safe-area-inset-bottom) / 5 * 3);
  257. }
  258. .tcp-text {
  259. color: var(--ui-BG-Main);
  260. }
  261. .agreement-text {
  262. color: $dark-9;
  263. }
  264. .agreement-title {
  265. font-size: 28rpx;
  266. color: $dark-9;
  267. text-align: left;
  268. width: 100%;
  269. padding-left: 60rpx;
  270. }
  271. .agreement-options-container {
  272. width: 100%;
  273. padding-left: 100rpx;
  274. }
  275. .agreement-option {
  276. width: 100%;
  277. display: flex;
  278. justify-content: flex-start;
  279. }
  280. </style>