LoginModal.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 } 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. getOpen.value = false;
  92. emit('success', res);
  93. } else {
  94. uni.showToast({
  95. title: res?.msg || '登录失败,请重试',
  96. icon: 'none'
  97. });
  98. }
  99. } catch (error) {
  100. console.error('登录失败:', error);
  101. uni.showToast({
  102. title: '登录失败,请重试',
  103. icon: 'none'
  104. });
  105. }
  106. },
  107. fail: () => {
  108. uni.showToast({
  109. title: '获取登录凭证失败',
  110. icon: 'none'
  111. });
  112. }
  113. });
  114. } catch (error) {
  115. console.error('获取手机号失败:', error);
  116. uni.showToast({
  117. title: '授权失败,请重试',
  118. icon: 'none'
  119. });
  120. }
  121. };
  122. // 处理隐私协议
  123. const handlePrivacy = () => {
  124. // TODO: 跳转到隐私协议页面
  125. uni.showToast({
  126. title: '隐私协议',
  127. icon: 'none'
  128. });
  129. };
  130. // 处理用户协议
  131. const handleUserAgreement = () => {
  132. // TODO: 跳转到用户协议页面
  133. uni.showToast({
  134. title: '用户协议',
  135. icon: 'none'
  136. });
  137. };
  138. </script>
  139. <style scoped lang="scss">
  140. :deep(.uni-popup) {
  141. z-index: 100 !important;
  142. }
  143. :deep(.uni-popup__wrapper) {
  144. z-index: 100 !important;
  145. }
  146. .login-modal {
  147. width: 100%;
  148. background: #FFFFFF;
  149. border-radius: 24rpx;
  150. padding: 60rpx 40rpx 50rpx;
  151. position: relative;
  152. box-sizing: border-box;
  153. &__close {
  154. position: absolute;
  155. top: 30rpx;
  156. right: 30rpx;
  157. font-size: 24rpx;
  158. color: #ccc;
  159. line-height: 1;
  160. }
  161. &__title {
  162. font-size: 44rpx;
  163. font-weight: bold;
  164. color: #010101;
  165. // text-align: center;
  166. margin-bottom: 24rpx;
  167. line-height: 1.2;
  168. }
  169. &__desc {
  170. font-size: 28rpx;
  171. color: #333333;
  172. // text-align: center;
  173. margin-bottom: 60rpx;
  174. line-height: 1.5;
  175. }
  176. &__btn {
  177. width: 100%;
  178. height: 88rpx;
  179. // background: #07C160;
  180. border-radius: 44rpx;
  181. color: #FFFFFF;
  182. font-size: 32rpx;
  183. font-weight: 500;
  184. line-height: 88rpx;
  185. text-align: center;
  186. border: none;
  187. margin-bottom: 40rpx;
  188. padding: 0;
  189. &::after {
  190. border: none;
  191. }
  192. &[disabled] {
  193. background: #CCCCCC;
  194. color: #999999;
  195. }
  196. }
  197. &__agreement {
  198. display: flex;
  199. align-items: center;
  200. justify-content: center;
  201. flex-wrap: wrap;
  202. font-size: 24rpx;
  203. line-height: 1.5;
  204. .agreement-checkbox {
  205. margin-right: 8rpx;
  206. .checkbox-icon {
  207. width: 32rpx;
  208. height: 32rpx;
  209. border: 2rpx solid #CCCCCC;
  210. border-radius: 50%;
  211. display: flex;
  212. align-items: center;
  213. justify-content: center;
  214. background: #FFFFFF;
  215. transition: all 0.3s;
  216. &.checked {
  217. background: #07C160;
  218. border-color: #07C160;
  219. .icon-check {
  220. color: #FFFFFF;
  221. font-size: 20rpx;
  222. font-weight: bold;
  223. }
  224. }
  225. }
  226. }
  227. .agreement-text {
  228. color: #333333;
  229. margin: 0 4rpx;
  230. }
  231. .agreement-link {
  232. color: #1677FF;
  233. margin: 0 4rpx;
  234. }
  235. }
  236. }
  237. </style>