LoginModal.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <template>
  2. <BasicModal type="bottom" v-model:open="getOpen" :isMack="true">
  3. <view class="login-modal">
  4. <!-- 右上角关闭按钮 -->
  5. <view class="login-modal__close" @click="handleClose">暂不登录</view>
  6. <!-- 标题 -->
  7. <view class="login-modal__title">欢迎登录U店在哪</view>
  8. <!-- 描述 -->
  9. <view class="login-modal__desc">授权手机号登录,享受更多便利和会员服务</view>
  10. <!-- 一键登录按钮 -->
  11. <button
  12. class="login-modal__btn"
  13. type="primary"
  14. open-type="getPhoneNumber"
  15. @getphonenumber="handleGetPhoneNumber"
  16. :disabled="!agreed"
  17. >
  18. 一键登录
  19. </button>
  20. <!-- 协议同意 -->
  21. <view class="login-modal__agreement">
  22. <view class="agreement-checkbox" @click="toggleAgree">
  23. <view class="checkbox-icon" :class="{ 'checked': agreed }">
  24. <text v-if="agreed" class="icon-check">✓</text>
  25. </view>
  26. </view>
  27. <text class="agreement-text">我已阅读并同意</text>
  28. <text class="agreement-link" @click="handlePrivacy">《隐私协议》</text>
  29. <text class="agreement-text">和</text>
  30. <text class="agreement-link" @click="handleUserAgreement">《用户协议》</text>
  31. </view>
  32. </view>
  33. </BasicModal>
  34. </template>
  35. <script setup>
  36. import { ref, computed } from 'vue';
  37. import BasicModal from '@/components/Modal/BasicModal.vue';
  38. import { DiningUserWechatLogin, GetUserInfo } from '@/api/dining.js';
  39. import { useUserStore } from '@/store/user.js';
  40. import { go } from '@/utils/utils.js';
  41. const props = defineProps({
  42. open: {
  43. type: Boolean,
  44. default: false
  45. }
  46. });
  47. const emit = defineEmits(['update:open', 'success', 'cancel']);
  48. const agreed = ref(false);
  49. const userStore = useUserStore();
  50. const getOpen = computed({
  51. get: () => props.open,
  52. set: (val) => emit('update:open', val)
  53. });
  54. // 切换同意状态
  55. const toggleAgree = () => {
  56. agreed.value = !agreed.value;
  57. };
  58. // 处理关闭
  59. const handleClose = () => {
  60. getOpen.value = false;
  61. emit('cancel');
  62. };
  63. // 处理获取手机号
  64. const handleGetPhoneNumber = async (e) => {
  65. if (!agreed.value) {
  66. uni.showToast({
  67. title: '请先同意用户协议和隐私协议',
  68. icon: 'none'
  69. });
  70. return;
  71. }
  72. if (!e.detail.code) {
  73. uni.showToast({
  74. title: '授权失败,请重试',
  75. icon: 'none'
  76. });
  77. return;
  78. }
  79. try {
  80. const phoneCode = e.detail?.code || "";
  81. // 登录接口:传 code、phoneCode;手机号明文传递(不加密),有缓存则一并传 phone)
  82. uni.login({
  83. success: async (loginRes) => {
  84. try {
  85. const res = await DiningUserWechatLogin({
  86. code: loginRes.code,
  87. phoneCode: phoneCode || undefined,
  88. phone: userStore.getPhone || undefined // 明文传递,有缓存则传
  89. });
  90. if (res && res.token) {
  91. userStore.login(res);
  92. try {
  93. const userRes = await GetUserInfo();
  94. const raw = userRes?.data ?? userRes?.result ?? userRes?.userInfo ?? userRes?.user ?? userRes ?? {};
  95. const userData = typeof raw === 'object' && raw !== null ? raw : {};
  96. const merged = { ...userStore.getUserInfo, ...userData };
  97. userStore.setUserInfo(merged);
  98. } catch (err) {
  99. console.warn('获取用户信息失败', err);
  100. }
  101. getOpen.value = false;
  102. emit('success', res);
  103. } else {
  104. uni.showToast({
  105. title: res?.msg || '登录失败,请重试',
  106. icon: 'none'
  107. });
  108. }
  109. } catch (error) {
  110. console.error('登录失败:', error);
  111. uni.showToast({
  112. title: '登录失败,请重试',
  113. icon: 'none'
  114. });
  115. }
  116. },
  117. fail: () => {
  118. uni.showToast({
  119. title: '获取登录凭证失败',
  120. icon: 'none'
  121. });
  122. }
  123. });
  124. } catch (error) {
  125. console.error('获取手机号失败:', error);
  126. uni.showToast({
  127. title: '授权失败,请重试',
  128. icon: 'none'
  129. });
  130. }
  131. };
  132. // 处理隐私协议:跳转 webview 打开
  133. const PRIVACY_URL = 'https://ossfile.ailien.shop/privacy/%E9%9A%90%E7%A7%81%E5%8D%8F%E8%AE%AE931648.html';
  134. const handlePrivacy = () => {
  135. go(`/pages/webview/index?url=${encodeURIComponent(PRIVACY_URL)}`);
  136. };
  137. // 处理用户协议:与隐私协议一致,跳转 webview 打开
  138. const USER_AGREEMENT_URL = 'https://ossfile.ailien.shop/privacy/U%E5%BA%97%E5%9C%A8%E8%BF%99-%E5%B9%B3%E5%8F%B0%E8%A7%84%E5%88%99456793.htm';
  139. const handleUserAgreement = () => {
  140. go(`/pages/webview/index?url=${encodeURIComponent(USER_AGREEMENT_URL)}`);
  141. };
  142. </script>
  143. <style scoped lang="scss">
  144. /* 优先级最高,避免被 tabbar 等挡住 */
  145. :deep(.uni-popup) {
  146. z-index: 99999 !important;
  147. }
  148. :deep(.uni-popup__wrapper) {
  149. z-index: 99999 !important;
  150. }
  151. :deep(.uni-popup__mask) {
  152. z-index: 99998 !important;
  153. }
  154. .login-modal {
  155. width: 100%;
  156. background: #FFFFFF;
  157. border-radius: 24rpx;
  158. padding: 60rpx 40rpx calc(50rpx + env(safe-area-inset-bottom) + 120rpx);
  159. position: relative;
  160. box-sizing: border-box;
  161. &__close {
  162. position: absolute;
  163. top: 30rpx;
  164. right: 30rpx;
  165. font-size: 24rpx;
  166. color: #ccc;
  167. line-height: 1;
  168. }
  169. &__title {
  170. font-size: 44rpx;
  171. font-weight: bold;
  172. color: #010101;
  173. // text-align: center;
  174. margin-bottom: 24rpx;
  175. line-height: 1.2;
  176. }
  177. &__desc {
  178. font-size: 28rpx;
  179. color: #333333;
  180. // text-align: center;
  181. margin-bottom: 60rpx;
  182. line-height: 1.5;
  183. }
  184. &__btn {
  185. width: 100%;
  186. height: 88rpx;
  187. // background: #07C160;
  188. border-radius: 44rpx;
  189. color: #FFFFFF;
  190. font-size: 32rpx;
  191. font-weight: 500;
  192. line-height: 88rpx;
  193. text-align: center;
  194. border: none;
  195. margin-bottom: 40rpx;
  196. padding: 0;
  197. &::after {
  198. border: none;
  199. }
  200. &[disabled] {
  201. background: #CCCCCC;
  202. color: #999999;
  203. }
  204. }
  205. &__agreement {
  206. display: flex;
  207. align-items: center;
  208. justify-content: center;
  209. flex-wrap: wrap;
  210. font-size: 24rpx;
  211. line-height: 1.5;
  212. .agreement-checkbox {
  213. margin-right: 8rpx;
  214. .checkbox-icon {
  215. width: 32rpx;
  216. height: 32rpx;
  217. border: 2rpx solid #CCCCCC;
  218. border-radius: 50%;
  219. display: flex;
  220. align-items: center;
  221. justify-content: center;
  222. background: #FFFFFF;
  223. transition: all 0.3s;
  224. &.checked {
  225. background: #07C160;
  226. border-color: #07C160;
  227. .icon-check {
  228. color: #FFFFFF;
  229. font-size: 20rpx;
  230. font-weight: bold;
  231. }
  232. }
  233. }
  234. }
  235. .agreement-text {
  236. color: #333333;
  237. margin: 0 4rpx;
  238. }
  239. .agreement-link {
  240. color: #1677FF;
  241. margin: 0 4rpx;
  242. }
  243. }
  244. }
  245. </style>