LoginPopup.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. <template>
  2. <view class="login-popup" :class="{ 'show': loginPopupVisible }">
  3. <view class="mask" @tap="handleClose"></view>
  4. <view class="popup-content">
  5. <view class="popup-header">
  6. <text class="title">登录</text>
  7. <text class="close-btn" @tap="handleClose">×</text>
  8. </view>
  9. <!-- 微信登录按钮 -->
  10. <!-- <button class="wechat-btn" @tap="handleWechatLogin">
  11. <image src="/static/icon/wechat.png" mode="aspectFit" class="wechat-icon"></image>
  12. 微信一键登录
  13. </button> -->
  14. <!-- 分割线 -->
  15. <!-- <view class="divider">
  16. <text class="divider-text">或</text>
  17. </view> -->
  18. <!-- 账号密码登录 -->
  19. <view class="account-login">
  20. <view class="input-item">
  21. <input type="text" v-model="form.username" placeholder="用户名" placeholder-class="placeholder"
  22. />
  23. </view>
  24. <view class="input-item">
  25. <input :password="!showPassword" type="text" v-model="form.password" placeholder="密码"
  26. placeholder-class="placeholder" />
  27. <view class="eye-icon" @tap.stop="showPassword = !showPassword">
  28. <image :src="showPassword ? '/static/icon/eye-open.png' : '/static/icon/eye-close.png'"
  29. mode="aspectFit" class="eye-image"></image>
  30. </view>
  31. </view>
  32. <button
  33. class="account-btn"
  34. :class="{ 'disabled': isLogging }"
  35. :disabled="isLogging"
  36. @tap="handleAccountLogin"
  37. >
  38. {{ isLogging ? '登录中...' : '登录' }}
  39. </button>
  40. </view>
  41. <!-- 用户协议 -->
  42. <view class="agreement">
  43. <checkbox :checked="agreed" @tap="handleAgreementConfirm" color="#40ac6d" />
  44. <text class="agreement-text">
  45. 我已阅读并同意
  46. <text class="link" @tap="openAgreement('user')">《用户协议》</text>
  47. <text class="link" @tap="openAgreement('privacy')">《隐私协议》</text>
  48. </text>
  49. </view>
  50. </view>
  51. </view>
  52. <agreement-popup v-model:visible="showAgreement" :type="agreementType" />
  53. </template>
  54. <script setup>
  55. import AgreementPopup from '../agreement/AgreementPopup.vue'
  56. import { ref } from 'vue'
  57. import { useUserStore } from '@/store/modules/user'
  58. import { storeToRefs } from 'pinia'
  59. import { userApi } from '@/api/user'
  60. const userStore = useUserStore()
  61. const { loginPopupVisible } = storeToRefs(userStore)
  62. const loginType = ref('wechat')
  63. const form = ref({
  64. username: '',
  65. password: ''
  66. })
  67. const showPassword = ref(false)
  68. const agreed = ref(false)
  69. const showAgreement = ref(false)
  70. const agreementType = ref('user')
  71. // 防重复点击状态
  72. const isLogging = ref(false)
  73. // 点击协议链接
  74. const openAgreement = (type) => {
  75. agreementType.value = type
  76. showAgreement.value = true
  77. }
  78. // 确认阅读协议
  79. const handleAgreementConfirm = () => {
  80. agreed.value = true
  81. }
  82. const handleClose = () => {
  83. userStore.hideLoginPopup()
  84. form.value.username = ''
  85. form.value.password = ''
  86. agreed.value = false
  87. isLogging.value = false // 重置登录状态
  88. uni.hideLoading() // 确保隐藏 loading
  89. }
  90. // 微信登录
  91. const handleWechatLogin = async () => {
  92. if (isLogging.value) {
  93. console.log('正在登录中,请勿重复点击')
  94. return
  95. }
  96. if (!agreed.value) {
  97. uni.showToast({
  98. title: '请先同意用户协议和隐私协议',
  99. icon: 'none'
  100. })
  101. return
  102. }
  103. try {
  104. isLogging.value = true
  105. // 显示获取微信授权的提示
  106. uni.showLoading({
  107. title: '获取微信授权...',
  108. mask: true
  109. })
  110. const loginRes = await uni.login()
  111. // 切换到登录中的 loading
  112. uni.showLoading({
  113. title: '微信登录中...',
  114. mask: true
  115. })
  116. const res = await userApi.wechatLogin({
  117. code: loginRes.code
  118. }, () => {
  119. // 请求完成回调,无论成功失败都会执行
  120. isLogging.value = false
  121. uni.hideLoading() // 隐藏 loading
  122. })
  123. console.log("登录成功", res.data)
  124. userStore.setUserInfo(res.data)
  125. uni.$emit('login', res.data)
  126. handleClose()
  127. } catch (e) {
  128. console.error('登录失败:', e)
  129. uni.showToast({
  130. title: '登录失败',
  131. icon: 'none'
  132. })
  133. // 确保隐藏 loading 和重置状态
  134. isLogging.value = false
  135. uni.hideLoading()
  136. }
  137. }
  138. // 账号密码登录
  139. const handleAccountLogin = async () => {
  140. if (isLogging.value) {
  141. console.log('正在登录中,请勿重复点击')
  142. return
  143. }
  144. if (!agreed.value) {
  145. uni.showToast({
  146. title: '请先同意用户协议和隐私协议',
  147. icon: 'none'
  148. })
  149. return
  150. }
  151. if (!form.value.username || !form.value.password) {
  152. uni.showToast({
  153. title: '请输入用户名和密码',
  154. icon: 'none'
  155. })
  156. return
  157. }
  158. try {
  159. isLogging.value = true
  160. // 显示获取微信授权的提示
  161. uni.showLoading({
  162. title: '获取微信授权...',
  163. mask: true
  164. })
  165. uni.login({
  166. provider: 'weixin',
  167. success: async function (loginRes) {
  168. try {
  169. console.log("wxcode", loginRes.code);
  170. // 切换到登录中的 loading
  171. uni.showLoading({
  172. title: '登录中...',
  173. mask: true
  174. })
  175. try {
  176. const res = await userApi.accountLogin({
  177. yhm: form.value.username,
  178. mm: form.value.password,
  179. wdConfirmationCaptchaService: "0",
  180. wechatCode: loginRes.code
  181. })
  182. uni.hideLoading()
  183. console.log("account登录成功", res.data)
  184. uni.$emit('login', res.data)
  185. userStore.setUserInfo(res.data)
  186. handleClose()
  187. } catch (error) {
  188. console.error('登录失败:', error)
  189. uni.showToast({
  190. title: '登录失败',
  191. icon: 'none'
  192. })
  193. } finally {
  194. // 无论成功失败,都重置状态
  195. isLogging.value = false
  196. uni.hideLoading()
  197. }
  198. } catch (outerError) {
  199. console.error('登录过程出错:', outerError)
  200. uni.showToast({
  201. title: '登录失败',
  202. icon: 'none'
  203. })
  204. isLogging.value = false
  205. uni.hideLoading()
  206. }
  207. },
  208. fail: function (error) {
  209. console.error('获取微信授权失败:', error)
  210. uni.showToast({
  211. title: '获取微信授权失败',
  212. icon: 'none'
  213. })
  214. isLogging.value = false
  215. uni.hideLoading()
  216. }
  217. });
  218. } catch (e) {
  219. console.error('登录过程出错:', e)
  220. uni.showToast({
  221. title: '登录失败',
  222. icon: 'none'
  223. })
  224. isLogging.value = false
  225. uni.hideLoading()
  226. }
  227. }
  228. const handleImageError = (e) => {
  229. console.error('图片加载失败:', e)
  230. }
  231. </script>
  232. <style scoped>
  233. .login-popup {
  234. position: fixed;
  235. left: 0;
  236. right: 0;
  237. top: 0;
  238. bottom: 0;
  239. z-index: 9999;
  240. visibility: hidden;
  241. }
  242. .login-popup.show {
  243. visibility: visible;
  244. }
  245. .login-popup.show .popup-content {
  246. transform: translateY(0);
  247. }
  248. .mask {
  249. position: absolute;
  250. left: 0;
  251. right: 0;
  252. top: 0;
  253. bottom: 0;
  254. background: rgba(0, 0, 0, 0.6);
  255. }
  256. .popup-content {
  257. position: absolute;
  258. left: 0;
  259. right: 0;
  260. bottom: 0;
  261. background: #fff;
  262. border-radius: 12rpx 12rpx 0 0;
  263. padding: 40rpx 30rpx;
  264. transform: translateY(100%);
  265. transition: transform 0.3s ease-out;
  266. }
  267. .popup-header {
  268. display: flex;
  269. justify-content: space-between;
  270. align-items: center;
  271. margin-bottom: 40rpx;
  272. }
  273. .title {
  274. font-size: 36rpx;
  275. font-weight: bold;
  276. color: #333;
  277. }
  278. .close-btn {
  279. font-size: 48rpx;
  280. color: #999;
  281. padding: 20rpx;
  282. margin: -20rpx;
  283. }
  284. /* 微信登录按钮 */
  285. .wechat-btn {
  286. display: flex;
  287. align-items: center;
  288. justify-content: center;
  289. width: 100%;
  290. height: 88rpx;
  291. background: #40ac6d;
  292. color: #fff;
  293. border-radius: 44rpx;
  294. font-size: 32rpx;
  295. border: none;
  296. }
  297. .wechat-icon {
  298. width: 48rpx;
  299. height: 48rpx;
  300. margin-right: 16rpx;
  301. }
  302. /* 分割线 */
  303. .divider {
  304. position: relative;
  305. text-align: center;
  306. margin: 40rpx 0;
  307. }
  308. .divider::before,
  309. .divider::after {
  310. content: '';
  311. position: absolute;
  312. top: 50%;
  313. width: calc(50% - 40rpx);
  314. height: 1px;
  315. background: #eee;
  316. }
  317. .divider::before {
  318. left: 0;
  319. }
  320. .divider::after {
  321. right: 0;
  322. }
  323. .divider-text {
  324. display: inline-block;
  325. padding: 0 20rpx;
  326. color: #999;
  327. font-size: 28rpx;
  328. background: #fff;
  329. position: relative;
  330. z-index: 1;
  331. }
  332. /* 账号密码登录 */
  333. .account-login {
  334. margin-bottom: 30rpx;
  335. }
  336. .input-item {
  337. position: relative;
  338. margin-bottom: 20rpx;
  339. }
  340. .input-item input {
  341. width: 100%;
  342. height: 88rpx;
  343. background: #f5f5f5;
  344. border-radius: 44rpx;
  345. padding: 0 40rpx;
  346. font-size: 28rpx;
  347. box-sizing: border-box;
  348. }
  349. .eye-icon {
  350. position: absolute;
  351. right: 20rpx;
  352. /* 调整位置 */
  353. top: 50%;
  354. transform: translateY(-50%);
  355. padding: 20rpx;
  356. /* 增加点击区域 */
  357. display: flex;
  358. align-items: center;
  359. justify-content: center;
  360. z-index: 1000;
  361. }
  362. .eye-image {
  363. width: 40rpx;
  364. height: 40rpx;
  365. display: block;
  366. pointer-events: none;
  367. }
  368. .placeholder {
  369. color: #999;
  370. }
  371. .account-btn {
  372. width: 100%;
  373. height: 88rpx;
  374. line-height: 88rpx;
  375. background: #666666;
  376. color: #fff;
  377. border-radius: 44rpx;
  378. font-size: 32rpx;
  379. border: none;
  380. margin-top: 30rpx;
  381. transition: all 0.3s ease;
  382. }
  383. .account-btn.disabled {
  384. background: #cccccc !important;
  385. color: #999999 !important;
  386. opacity: 0.6;
  387. cursor: not-allowed;
  388. }
  389. /* 用户协议 */
  390. .agreement {
  391. display: flex;
  392. align-items: center;
  393. justify-content: center;
  394. margin-top: 30rpx;
  395. }
  396. .agreement-text {
  397. font-size: 24rpx;
  398. color: #999;
  399. margin-left: 8rpx;
  400. }
  401. .link {
  402. color: #40ac6d;
  403. }
  404. /* 按钮点击效果 */
  405. .wechat-btn:active,
  406. .account-btn:active {
  407. opacity: 0.8;
  408. }
  409. </style>