LoginModal.vue 5.7 KB

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