verify-code.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. <template>
  2. <view class="login-bg">
  3. <view class="login-center">
  4. <view class="login-card">
  5. <!-- 顶部Tab切换 -->
  6. <view class="login-tabs">
  7. <view :class="['tab', loginType==='code' ? 'active' : '']" @click="loginType='code'">验证码登录</view>
  8. <view :class="['tab', loginType==='password' ? 'active' : '']" @click="loginType='password'">密码登录</view>
  9. </view>
  10. <!-- 验证码登录表单 -->
  11. <view v-if="loginType==='code'" class="form-area">
  12. <input class="input" type="number" v-model="phone" maxlength="11" placeholder="请输入手机号" />
  13. <view class="input-group">
  14. <input class="input" type="number" v-model="verifyCode" maxlength="6" placeholder="请输入验证码" />
  15. <text class="send-code" :class="{disabled: counting}" @click="sendVerifyCode">
  16. {{ counting ? `${countdown}s后重发` : '获取验证码' }}
  17. </text>
  18. </view>
  19. </view>
  20. <!-- 密码登录表单 -->
  21. <view v-else class="form-area">
  22. <input class="input" type="text" v-model="account" maxlength="32" placeholder="请输入账号/手机号" />
  23. <view class="input-group">
  24. <input class="input" :type="showPassword ? 'text' : 'password'" v-model="password" password maxlength="20" placeholder="请输入密码" />
  25. <text class="iconfont" :class="showPassword ? 'icon-eye-open' : 'icon-eye-close'" @click="showPassword=!showPassword"></text>
  26. </view>
  27. </view>
  28. <!-- 返回微信登录链接 -->
  29. <view class="back-to-wechat" @click="goToWechatLogin">
  30. <text class="back-link">返回微信登录</text>
  31. </view>
  32. <!-- 登录按钮 -->
  33. <button class="login-btn" :disabled="!isFormValid" @click="handleLogin">
  34. 登录/注册
  35. </button>
  36. <!-- 协议勾选 -->
  37. <view class="agreement">
  38. <checkbox-group @change="handleAgreementChange">
  39. <label class="agreement-label">
  40. <checkbox :checked="isAgreed" color="#07C160" />
  41. <text class="agreement-text">
  42. 我已阅读并同意
  43. <text class="link" @click.stop="showServiceAgreement = true">《用户协议》</text>
  44. <text class="link" @click.stop="showPrivacyPolicy = true">《隐私政策》</text>
  45. </text>
  46. </label>
  47. </checkbox-group>
  48. </view>
  49. </view>
  50. <!-- 协议弹窗 -->
  51. <!-- <uni-popup ref="popup" :show="showServiceAgreement || showPrivacyPolicy" type="center" @close="closePopup">
  52. <view class="popup-content">
  53. <view class="popup-title">{{ showServiceAgreement ? '服务协议' : '隐私政策' }}</view>
  54. <scroll-view scroll-y class="popup-scroll">
  55. <text v-if="showServiceAgreement">这里是服务协议内容...</text>
  56. <text v-else>这里是隐私政策内容...</text>
  57. </scroll-view>
  58. <button class="popup-btn" @click="closePopup">我已知晓</button>
  59. </view>
  60. </uni-popup> -->
  61. </view>
  62. </view>
  63. </template>
  64. <script>
  65. export default {
  66. data() {
  67. return {
  68. loginType: 'code',
  69. phone: '',
  70. verifyCode: '',
  71. account: '',
  72. password: '',
  73. showPassword: false,
  74. counting: false,
  75. countdown: 60,
  76. isAgreed: false,
  77. showServiceAgreement: false,
  78. showPrivacyPolicy: false
  79. }
  80. },
  81. computed: {
  82. isFormValid() {
  83. if (!this.isAgreed) return false
  84. if (this.loginType === 'code') {
  85. return this.phone.length === 11 && this.verifyCode.length === 6
  86. } else {
  87. return this.account.length > 0 && this.password.length >= 6
  88. }
  89. }
  90. },
  91. methods: {
  92. handleAgreementChange(e) {
  93. this.isAgreed = e.detail.value.length > 0
  94. },
  95. async sendVerifyCode() {
  96. if (this.counting) return
  97. if (this.phone.length !== 11) {
  98. uni.showToast({ title: '请输入正确的手机号', icon: 'none' })
  99. return
  100. }
  101. try {
  102. // 调用获取验证码接口
  103. const res = await uni.request({
  104. url: 'http://localhost:9527/api/getCode',
  105. method: 'POST',
  106. data: {
  107. phone: this.phone
  108. },
  109. header: {
  110. 'content-type': 'application/json'
  111. },
  112. });
  113. console.log(res.statusCode);
  114. if (res.statusCode === 200 ) {
  115. console.log('验证码接口返回:', res);
  116. uni.showToast({
  117. title: '验证码已发送',
  118. icon: 'success'
  119. });
  120. // 开始倒计时
  121. this.counting = true
  122. this.countdown = 60
  123. this.startCountdown()
  124. } else {
  125. throw new Error(res.data.msg || '获取验证码失败')
  126. }
  127. } catch (error) {
  128. console.error('获取验证码失败:', error);
  129. uni.showToast({
  130. title: error.message || '获取验证码失败,请重试',
  131. icon: 'none'
  132. });
  133. }
  134. },
  135. startCountdown() {
  136. const timer = setInterval(() => {
  137. if (this.countdown > 0) {
  138. this.countdown--
  139. } else {
  140. this.counting = false
  141. clearInterval(timer)
  142. }
  143. }, 1000)
  144. },
  145. async handleLogin() {
  146. if (!this.isFormValid) return
  147. if (!this.isAgreed) {
  148. uni.showToast({ title: '请先同意服务协议和隐私政策', icon: 'none' })
  149. return
  150. }
  151. // TODO: 调用后端登录接口
  152. const res = await uni.request({
  153. url: "http://localhost:9527/api/login",
  154. method:'POST',
  155. data:{
  156. phone: this.phone,
  157. password: this.password,
  158. code: this.verifyCode
  159. },
  160. header: {
  161. 'content-type': 'application/json'
  162. }
  163. })
  164. console.log(res.data)
  165. if(res.data.code===200){
  166. // 登录成功,保存token
  167. uni.setStorageSync('token', res.data.data.token);
  168. uni.switchTab({
  169. url: '/pages/discover/index' // 替换为实际页面路径
  170. });
  171. }else{
  172. uni.showToast({
  173. title: '登录失败', icon: 'none'
  174. })
  175. }
  176. },
  177. closePopup() {
  178. this.showServiceAgreement = false
  179. this.showPrivacyPolicy = false
  180. },
  181. goToWechatLogin() {
  182. uni.navigateTo({
  183. url: '/pages/login/index'
  184. })
  185. }
  186. }
  187. }
  188. </script>
  189. <style lang="scss">
  190. .login-bg {
  191. min-height: 100vh;
  192. width: 100vw;
  193. background: linear-gradient(135deg, #f5f7fa 0%, #e4e8f0 100%);
  194. display: flex;
  195. align-items: center;
  196. justify-content: center;
  197. }
  198. .login-center {
  199. width: 100vw;
  200. min-height: 100vh;
  201. display: flex;
  202. align-items: center;
  203. justify-content: center;
  204. padding: 40rpx;
  205. }
  206. .login-card {
  207. width: 100%;
  208. max-width: 750rpx;
  209. background: rgba(255, 255, 255, 0.98);
  210. border-radius: 40rpx;
  211. box-shadow: 0 20rpx 60rpx rgba(0, 0, 0, 0.08);
  212. padding: 80rpx 60rpx;
  213. display: flex;
  214. flex-direction: column;
  215. align-items: center;
  216. backdrop-filter: blur(10px);
  217. transform: translateY(0);
  218. transition: transform 0.3s ease;
  219. margin: 0 auto;
  220. &:hover {
  221. transform: translateY(-5rpx);
  222. }
  223. }
  224. .login-tabs {
  225. width: 100%;
  226. display: flex;
  227. justify-content: center;
  228. margin-bottom: 60rpx;
  229. position: relative;
  230. &::after {
  231. content: '';
  232. position: absolute;
  233. bottom: 0;
  234. left: 50%;
  235. transform: translateX(-50%);
  236. width: 60%;
  237. height: 1px;
  238. background: linear-gradient(90deg, transparent, rgba(0, 0, 0, 0.1), transparent);
  239. }
  240. .tab {
  241. flex: 1;
  242. text-align: center;
  243. font-size: 34rpx;
  244. color: #666;
  245. padding-bottom: 20rpx;
  246. border-bottom: 4rpx solid transparent;
  247. font-weight: 500;
  248. transition: all 0.3s ease;
  249. position: relative;
  250. &.active {
  251. color: #2c3e50;
  252. font-weight: 700;
  253. &::after {
  254. content: '';
  255. position: absolute;
  256. bottom: -4rpx;
  257. left: 50%;
  258. transform: translateX(-50%);
  259. width: 40%;
  260. height: 4rpx;
  261. background: linear-gradient(90deg, #2c3e50, #3498db);
  262. border-radius: 2rpx;
  263. }
  264. }
  265. }
  266. }
  267. .form-area {
  268. width: 100%;
  269. margin-bottom: 50rpx;
  270. .input {
  271. width: 100%;
  272. height: 100rpx;
  273. border: none;
  274. border-bottom: 2rpx solid rgba(0, 0, 0, 0.1);
  275. font-size: 34rpx;
  276. margin-bottom: 40rpx;
  277. background: transparent;
  278. outline: none;
  279. padding: 0 16rpx;
  280. transition: all 0.3s ease;
  281. &:focus {
  282. border-bottom-color: #07C160;
  283. }
  284. }
  285. .input-group {
  286. display: flex;
  287. align-items: center;
  288. position: relative;
  289. .input {
  290. flex: 1;
  291. margin-bottom: 0;
  292. }
  293. .send-code {
  294. font-size: 30rpx;
  295. color: #ffffff;
  296. margin-left: 24rpx;
  297. padding: 16rpx 32rpx;
  298. background: linear-gradient(45deg, #07C160, #0a9d4e);
  299. border-radius: 36rpx;
  300. transition: all 0.3s ease;
  301. box-shadow: 0 4rpx 12rpx rgba(7, 193, 96, 0.2);
  302. &.disabled {
  303. color: #ffffff;
  304. background: #e0e0e0;
  305. box-shadow: none;
  306. }
  307. &:active:not(.disabled) {
  308. transform: scale(0.95);
  309. box-shadow: 0 2rpx 6rpx rgba(7, 193, 96, 0.15);
  310. }
  311. }
  312. .iconfont {
  313. font-size: 44rpx;
  314. color: #666;
  315. margin-left: 20rpx;
  316. transition: all 0.3s ease;
  317. &:active {
  318. transform: scale(0.9);
  319. }
  320. }
  321. }
  322. }
  323. .login-btn {
  324. width: 100%;
  325. height: 100rpx;
  326. background: linear-gradient(45deg, #07C160, #0a9d4e);
  327. color: #fff;
  328. border-radius: 50rpx;
  329. font-size: 36rpx;
  330. font-weight: 600;
  331. border: none;
  332. margin: 30rpx 0 50rpx;
  333. box-shadow: 0 8rpx 20rpx rgba(7, 193, 96, 0.2);
  334. transition: all 0.3s ease;
  335. &:active {
  336. transform: scale(0.98);
  337. box-shadow: 0 4rpx 10rpx rgba(7, 193, 96, 0.15);
  338. }
  339. &:disabled {
  340. background: #e0e0e0;
  341. color: #aaa;
  342. box-shadow: none;
  343. }
  344. }
  345. .agreement {
  346. margin-top: 30rpx;
  347. text-align: center;
  348. .agreement-label {
  349. display: inline-flex;
  350. align-items: center;
  351. font-size: 26rpx;
  352. color: #666;
  353. }
  354. .agreement-text {
  355. margin-left: 10rpx;
  356. }
  357. .link {
  358. color: #07C160;
  359. text-decoration: none;
  360. font-weight: 500;
  361. position: relative;
  362. &::after {
  363. content: '';
  364. position: absolute;
  365. bottom: -2rpx;
  366. left: 0;
  367. width: 100%;
  368. height: 1px;
  369. background: #07C160;
  370. transform: scaleX(0);
  371. transition: transform 0.3s ease;
  372. }
  373. &:active::after {
  374. transform: scaleX(1);
  375. }
  376. }
  377. }
  378. .back-to-wechat {
  379. margin-top: 50rpx;
  380. text-align: center;
  381. .back-link {
  382. color: #3498db;
  383. font-size: 30rpx;
  384. font-weight: 500;
  385. text-decoration: none;
  386. position: relative;
  387. padding: 10rpx 0;
  388. &::after {
  389. content: '';
  390. position: absolute;
  391. bottom: 0;
  392. left: 0;
  393. width: 100%;
  394. height: 1px;
  395. background: #3498db;
  396. transform: scaleX(0);
  397. transition: transform 0.3s ease;
  398. }
  399. &:active::after {
  400. transform: scaleX(1);
  401. }
  402. }
  403. }
  404. // 协议弹窗样式
  405. .popup-content {
  406. width: 650rpx;
  407. background: #fff;
  408. border-radius: 40rpx;
  409. padding: 60rpx 50rpx 40rpx;
  410. display: flex;
  411. flex-direction: column;
  412. align-items: center;
  413. box-shadow: 0 20rpx 60rpx rgba(0, 0, 0, 0.1);
  414. }
  415. .popup-title {
  416. font-size: 40rpx;
  417. font-weight: bold;
  418. background: linear-gradient(45deg, #07C160, #0a9d4e);
  419. -webkit-background-clip: text;
  420. color: transparent;
  421. margin-bottom: 40rpx;
  422. }
  423. .popup-scroll {
  424. max-height: 400rpx;
  425. width: 100%;
  426. margin-bottom: 40rpx;
  427. font-size: 30rpx;
  428. color: #444;
  429. line-height: 1.6;
  430. }
  431. .popup-btn {
  432. width: 100%;
  433. height: 88rpx;
  434. background: linear-gradient(45deg, #07C160, #0a9d4e);
  435. color: #fff;
  436. border-radius: 44rpx;
  437. font-size: 32rpx;
  438. font-weight: 600;
  439. border: none;
  440. box-shadow: 0 8rpx 20rpx rgba(7, 193, 96, 0.2);
  441. transition: all 0.3s ease;
  442. &:active {
  443. transform: scale(0.98);
  444. box-shadow: 0 4rpx 10rpx rgba(7, 193, 96, 0.15);
  445. }
  446. }
  447. </style>