LoginModal.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. const props = defineProps({
  41. open: {
  42. type: Boolean,
  43. default: false
  44. }
  45. });
  46. const emit = defineEmits(['update:open', 'success', 'cancel']);
  47. const agreed = ref(false);
  48. const userStore = useUserStore();
  49. const getOpen = computed({
  50. get: () => props.open,
  51. set: (val) => emit('update:open', val)
  52. });
  53. // 切换同意状态
  54. const toggleAgree = () => {
  55. agreed.value = !agreed.value;
  56. };
  57. // 处理关闭
  58. const handleClose = () => {
  59. getOpen.value = false;
  60. emit('cancel');
  61. };
  62. // 处理获取手机号
  63. const handleGetPhoneNumber = async (e) => {
  64. if (!agreed.value) {
  65. uni.showToast({
  66. title: '请先同意用户协议和隐私协议',
  67. icon: 'none'
  68. });
  69. return;
  70. }
  71. if (!e.detail.code) {
  72. uni.showToast({
  73. title: '授权失败,请重试',
  74. icon: 'none'
  75. });
  76. return;
  77. }
  78. try {
  79. const phoneCode = e.detail?.code || "";
  80. // 登录接口:传 code、phoneCode;手机号明文传递(不加密),有缓存则一并传 phone)
  81. uni.login({
  82. success: async (loginRes) => {
  83. try {
  84. const res = await DiningUserWechatLogin({
  85. code: loginRes.code,
  86. phoneCode: phoneCode || undefined,
  87. phone: userStore.getPhone || undefined // 明文传递,有缓存则传
  88. });
  89. if (res && res.token) {
  90. userStore.login(res);
  91. try {
  92. const userRes = await GetUserInfo();
  93. const raw = userRes?.data ?? userRes?.result ?? userRes?.userInfo ?? userRes?.user ?? userRes ?? {};
  94. const userData = typeof raw === 'object' && raw !== null ? raw : {};
  95. const merged = { ...userStore.getUserInfo, ...userData };
  96. userStore.setUserInfo(merged);
  97. } catch (err) {
  98. console.warn('获取用户信息失败', err);
  99. }
  100. getOpen.value = false;
  101. emit('success', res);
  102. } else {
  103. uni.showToast({
  104. title: res?.msg || '登录失败,请重试',
  105. icon: 'none'
  106. });
  107. }
  108. } catch (error) {
  109. console.error('登录失败:', error);
  110. uni.showToast({
  111. title: '登录失败,请重试',
  112. icon: 'none'
  113. });
  114. }
  115. },
  116. fail: () => {
  117. uni.showToast({
  118. title: '获取登录凭证失败',
  119. icon: 'none'
  120. });
  121. }
  122. });
  123. } catch (error) {
  124. console.error('获取手机号失败:', error);
  125. uni.showToast({
  126. title: '授权失败,请重试',
  127. icon: 'none'
  128. });
  129. }
  130. };
  131. // 处理隐私协议
  132. const handlePrivacy = () => {
  133. // TODO: 跳转到隐私协议页面
  134. uni.showToast({
  135. title: '隐私协议',
  136. icon: 'none'
  137. });
  138. };
  139. // 处理用户协议
  140. const handleUserAgreement = () => {
  141. // TODO: 跳转到用户协议页面
  142. uni.showToast({
  143. title: '用户协议',
  144. icon: 'none'
  145. });
  146. };
  147. </script>
  148. <style scoped lang="scss">
  149. /* 优先级最高,避免被 tabbar 等挡住 */
  150. :deep(.uni-popup) {
  151. z-index: 99999 !important;
  152. }
  153. :deep(.uni-popup__wrapper) {
  154. z-index: 99999 !important;
  155. }
  156. :deep(.uni-popup__mask) {
  157. z-index: 99998 !important;
  158. }
  159. .login-modal {
  160. width: 100%;
  161. background: #FFFFFF;
  162. border-radius: 24rpx;
  163. padding: 60rpx 40rpx 50rpx;
  164. position: relative;
  165. box-sizing: border-box;
  166. &__close {
  167. position: absolute;
  168. top: 30rpx;
  169. right: 30rpx;
  170. font-size: 24rpx;
  171. color: #ccc;
  172. line-height: 1;
  173. }
  174. &__title {
  175. font-size: 44rpx;
  176. font-weight: bold;
  177. color: #010101;
  178. // text-align: center;
  179. margin-bottom: 24rpx;
  180. line-height: 1.2;
  181. }
  182. &__desc {
  183. font-size: 28rpx;
  184. color: #333333;
  185. // text-align: center;
  186. margin-bottom: 60rpx;
  187. line-height: 1.5;
  188. }
  189. &__btn {
  190. width: 100%;
  191. height: 88rpx;
  192. // background: #07C160;
  193. border-radius: 44rpx;
  194. color: #FFFFFF;
  195. font-size: 32rpx;
  196. font-weight: 500;
  197. line-height: 88rpx;
  198. text-align: center;
  199. border: none;
  200. margin-bottom: 40rpx;
  201. padding: 0;
  202. &::after {
  203. border: none;
  204. }
  205. &[disabled] {
  206. background: #CCCCCC;
  207. color: #999999;
  208. }
  209. }
  210. &__agreement {
  211. display: flex;
  212. align-items: center;
  213. justify-content: center;
  214. flex-wrap: wrap;
  215. font-size: 24rpx;
  216. line-height: 1.5;
  217. .agreement-checkbox {
  218. margin-right: 8rpx;
  219. .checkbox-icon {
  220. width: 32rpx;
  221. height: 32rpx;
  222. border: 2rpx solid #CCCCCC;
  223. border-radius: 50%;
  224. display: flex;
  225. align-items: center;
  226. justify-content: center;
  227. background: #FFFFFF;
  228. transition: all 0.3s;
  229. &.checked {
  230. background: #07C160;
  231. border-color: #07C160;
  232. .icon-check {
  233. color: #FFFFFF;
  234. font-size: 20rpx;
  235. font-weight: bold;
  236. }
  237. }
  238. }
  239. }
  240. .agreement-text {
  241. color: #333333;
  242. margin: 0 4rpx;
  243. }
  244. .agreement-link {
  245. color: #1677FF;
  246. margin: 0 4rpx;
  247. }
  248. }
  249. }
  250. </style>