login.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  1. <template>
  2. <view class="login-page">
  3. <view class="login-content">
  4. <!-- Logo 区域 -->
  5. <view class="logo-section">
  6. <view class="logo">
  7. <view class="logo-icon">
  8. <view class="logo-leaf"></view>
  9. <view class="logo-leaf right"></view>
  10. </view>
  11. </view>
  12. <text class="brand-name">哈哈运营平台</text>
  13. </view>
  14. <!-- 登录表单 -->
  15. <view class="login-card">
  16. <view class="form-group">
  17. <view class="input-wrapper">
  18. <view class="input-icon user"></view>
  19. <input
  20. class="form-input"
  21. type="text"
  22. v-model="formData.username"
  23. placeholder="请输入用户名"
  24. placeholder-class="input-placeholder"
  25. />
  26. </view>
  27. </view>
  28. <view class="form-group">
  29. <view class="input-wrapper">
  30. <view class="input-icon lock"></view>
  31. <input
  32. class="form-input"
  33. :password="!showPassword"
  34. v-model="formData.password"
  35. placeholder="请输入密码"
  36. placeholder-class="input-placeholder"
  37. />
  38. <view class="input-action" @click="showPassword = !showPassword">
  39. <view :class="['eye-icon', { active: showPassword }]"></view>
  40. </view>
  41. </view>
  42. </view>
  43. <button class="login-btn" :loading="loading" :disabled="loading" @click="handleLogin">
  44. <text v-if="!loading">立即登录</text>
  45. <text v-else>登录中...</text>
  46. </button>
  47. <!-- 分隔线 -->
  48. <view class="login-divider">
  49. <view class="divider-line"></view>
  50. <text class="divider-text">其他登录方式</text>
  51. <view class="divider-line"></view>
  52. </view>
  53. <!-- 微信登录 -->
  54. <button class="wechat-btn" :loading="wechatLoading" :disabled="wechatLoading" @click="handleWechatLogin">
  55. <view class="wechat-icon"></view>
  56. <text>补货员微信一键登录</text>
  57. </button>
  58. <!-- 绑定码登录 -->
  59. <view class="bind-section" v-if="showBindInput">
  60. <view class="divider-line"></view>
  61. <text class="bind-hint">该微信未绑定补货员账号,请扫码或输入管理员提供的一次性绑定码:</text>
  62. <view class="bind-input-wrapper">
  63. <input
  64. class="bind-input"
  65. type="text"
  66. v-model="bindingCode"
  67. placeholder="请输入24位绑定码"
  68. placeholder-class="input-placeholder"
  69. maxlength="24"
  70. />
  71. <view class="scan-bind-btn" @click="handleScanBindCode">
  72. <text class="scan-icon-text">扫码</text>
  73. </view>
  74. </view>
  75. <button class="bind-btn" :loading="bindLoading" :disabled="bindLoading || !bindingCode" @click="handleBindWechat">
  76. <text v-if="!bindLoading">绑定微信并登录</text>
  77. <text v-else>绑定中...</text>
  78. </button>
  79. </view>
  80. <text v-else class="bind-toggle" @click="showBindInput = true">已有绑定码?点击绑定微信</text>
  81. <view class="login-tips">
  82. <view class="tips-icon"></view>
  83. <text class="tips-label">测试账号</text>
  84. <text class="tips-value">admin / admin123</text>
  85. </view>
  86. </view>
  87. </view>
  88. <!-- 底部 -->
  89. <view class="login-footer">
  90. <text>© 2025 哈哈零售 All Rights Reserved</text>
  91. </view>
  92. </view>
  93. </template>
  94. <script setup lang="ts">
  95. import { ref, reactive, onMounted } from 'vue';
  96. import { login } from '@/api/auth';
  97. import { isLoggedIn } from '@/utils/auth';
  98. const formData = reactive({
  99. username: '',
  100. password: ''
  101. });
  102. const showPassword = ref(false);
  103. const loading = ref(false);
  104. const wechatLoading = ref(false);
  105. const showBindInput = ref(false);
  106. const bindingCode = ref('');
  107. const bindLoading = ref(false);
  108. onMounted(() => {
  109. if (isLoggedIn()) {
  110. uni.switchTab({ url: '/pages/index/index' });
  111. }
  112. });
  113. const handleLogin = async () => {
  114. if (!formData.username) {
  115. uni.showToast({ title: '请输入用户名', icon: 'none' });
  116. return;
  117. }
  118. if (!formData.password) {
  119. uni.showToast({ title: '请输入密码', icon: 'none' });
  120. return;
  121. }
  122. loading.value = true;
  123. try {
  124. await login(formData);
  125. uni.showToast({ title: '登录成功', icon: 'success' });
  126. setTimeout(() => {
  127. uni.switchTab({ url: '/pages/index/index' });
  128. }, 500);
  129. } catch (error: any) {
  130. uni.showToast({ title: error.message || '登录失败', icon: 'none' });
  131. } finally {
  132. loading.value = false;
  133. }
  134. };
  135. const handleWechatLogin = async () => {
  136. wechatLoading.value = true;
  137. try {
  138. // 1. 微信授权获取code
  139. const loginRes = await new Promise<any>((resolve, reject) => {
  140. uni.login({
  141. provider: 'weixin',
  142. success: (res) => resolve(res),
  143. fail: (err) => reject(err)
  144. });
  145. });
  146. if (!loginRes.code) {
  147. uni.showToast({ title: '微信授权失败', icon: 'none' });
  148. return;
  149. }
  150. // 2. 调用后端登录接口
  151. const { loginByWechat } = await import('@/api/replenish');
  152. await loginByWechat({ code: loginRes.code });
  153. uni.showToast({ title: '登录成功', icon: 'success' });
  154. setTimeout(() => {
  155. uni.reLaunch({ url: '/pages/replenish/index' });
  156. }, 500);
  157. } catch (error: any) {
  158. // 如果是未绑定的账号,显示绑定码输入
  159. if (error.message && error.message.includes('未绑定')) {
  160. showBindInput.value = true;
  161. uni.showToast({ title: '该微信未绑定账号,请输入绑定码', icon: 'none' });
  162. } else {
  163. uni.showToast({ title: error.message || '登录失败', icon: 'none' });
  164. }
  165. } finally {
  166. wechatLoading.value = false;
  167. }
  168. };
  169. /**
  170. * 扫码识别绑定码
  171. */
  172. const handleScanBindCode = () => {
  173. uni.scanCode({
  174. onlyFromCamera: false,
  175. scanType: ['qrCode'],
  176. success: (res: any) => {
  177. const result = (res.result || '').trim();
  178. // 绑定码为24位大写字母数字组合
  179. if (/^[A-Z0-9]{24}$/.test(result)) {
  180. bindingCode.value = result;
  181. uni.showToast({ title: '已识别绑定码', icon: 'success' });
  182. } else {
  183. uni.showToast({ title: '无效的绑定码二维码', icon: 'none' });
  184. }
  185. },
  186. fail: () => {
  187. // 用户取消扫码
  188. }
  189. });
  190. };
  191. /**
  192. * 补货员绑定微信
  193. */
  194. const handleBindWechat = async () => {
  195. if (!bindingCode.value || bindingCode.value.length < 4) {
  196. uni.showToast({ title: '请输入有效的绑定码', icon: 'none' });
  197. return;
  198. }
  199. bindLoading.value = true;
  200. try {
  201. // 1. 微信授权获取code
  202. const loginRes = await new Promise<any>((resolve, reject) => {
  203. uni.login({
  204. provider: 'weixin',
  205. success: (res) => resolve(res),
  206. fail: (err) => reject(err)
  207. });
  208. });
  209. if (!loginRes.code) {
  210. uni.showToast({ title: '微信授权失败', icon: 'none' });
  211. return;
  212. }
  213. // 2. 调用绑定接口
  214. const { bindWechat } = await import('@/api/replenish');
  215. await bindWechat({
  216. bindingCode: bindingCode.value.trim().toUpperCase(),
  217. code: loginRes.code
  218. });
  219. uni.showToast({ title: '绑定成功', icon: 'success' });
  220. setTimeout(() => {
  221. uni.reLaunch({ url: '/pages/replenish/index' });
  222. }, 500);
  223. } catch (error: any) {
  224. uni.showToast({ title: error.message || '绑定失败', icon: 'none' });
  225. } finally {
  226. bindLoading.value = false;
  227. }
  228. };
  229. </script>
  230. <style lang="scss" scoped>
  231. .login-page {
  232. min-height: 100vh;
  233. background: $bg-color-page;
  234. display: flex;
  235. flex-direction: column;
  236. padding-top: constant(safe-area-inset-top);
  237. padding-top: env(safe-area-inset-top);
  238. }
  239. .login-content {
  240. flex: 1;
  241. display: flex;
  242. flex-direction: column;
  243. justify-content: center;
  244. padding: 0 40rpx;
  245. }
  246. /* Logo 区域 */
  247. .logo-section {
  248. text-align: center;
  249. margin-bottom: 48rpx;
  250. .logo {
  251. width: 120rpx;
  252. height: 120rpx;
  253. background: $primary-color;
  254. border-radius: 32rpx;
  255. margin: 0 auto 28rpx;
  256. display: flex;
  257. align-items: center;
  258. justify-content: center;
  259. position: relative;
  260. .logo-icon {
  261. position: relative;
  262. width: 48rpx;
  263. height: 48rpx;
  264. .logo-leaf {
  265. position: absolute;
  266. width: 24rpx;
  267. height: 40rpx;
  268. background: $bg-color-card;
  269. border-radius: 50% 50% 50% 50% / 60% 60% 40% 40%;
  270. left: 0;
  271. top: 4rpx;
  272. transform: rotate(-20deg);
  273. &.right {
  274. left: auto;
  275. right: 0;
  276. transform: rotate(20deg);
  277. }
  278. }
  279. }
  280. }
  281. .brand-name {
  282. display: block;
  283. font-size: 44rpx;
  284. font-weight: 700;
  285. color: $text-color-primary;
  286. margin-bottom: 12rpx;
  287. letter-spacing: 2rpx;
  288. }
  289. .brand-subtitle {
  290. display: block;
  291. font-size: 28rpx;
  292. color: $text-color-tertiary;
  293. }
  294. }
  295. /* 登录卡片 */
  296. .login-card {
  297. background: $bg-color-card;
  298. border: 1rpx solid $border-color;
  299. border-radius: 24rpx;
  300. padding: 40rpx 32rpx;
  301. }
  302. .form-group {
  303. margin-bottom: 28rpx;
  304. .form-label {
  305. display: block;
  306. font-size: 26rpx;
  307. color: $text-color-secondary;
  308. margin-bottom: 12rpx;
  309. font-weight: 500;
  310. }
  311. }
  312. .input-wrapper {
  313. display: flex;
  314. align-items: center;
  315. background: $bg-color-page;
  316. border: 2rpx solid $border-color;
  317. border-radius: 16rpx;
  318. padding: 0 20rpx;
  319. transition: all 0.2s;
  320. &:focus-within {
  321. border-color: $primary-color;
  322. background: $bg-color-card;
  323. box-shadow: 0 0 0 4rpx rgba(255, 193, 7, 0.1);
  324. }
  325. .input-icon {
  326. width: 40rpx;
  327. height: 40rpx;
  328. margin-right: 12rpx;
  329. flex-shrink: 0;
  330. &.user {
  331. position: relative;
  332. &::before {
  333. content: '';
  334. position: absolute;
  335. top: 6rpx;
  336. left: 50%;
  337. transform: translateX(-50%);
  338. width: 16rpx;
  339. height: 16rpx;
  340. background: $text-color-muted;
  341. border-radius: 50%;
  342. }
  343. &::after {
  344. content: '';
  345. position: absolute;
  346. bottom: 4rpx;
  347. left: 50%;
  348. transform: translateX(-50%);
  349. width: 26rpx;
  350. height: 14rpx;
  351. background: $text-color-muted;
  352. border-radius: 14rpx 14rpx 0 0;
  353. }
  354. }
  355. &.lock {
  356. position: relative;
  357. &::before {
  358. content: '';
  359. position: absolute;
  360. top: 4rpx;
  361. left: 50%;
  362. transform: translateX(-50%);
  363. width: 18rpx;
  364. height: 14rpx;
  365. border: 3rpx solid $text-color-muted;
  366. border-bottom: none;
  367. border-radius: 16rpx 16rpx 0 0;
  368. }
  369. &::after {
  370. content: '';
  371. position: absolute;
  372. bottom: 4rpx;
  373. left: 50%;
  374. transform: translateX(-50%);
  375. width: 22rpx;
  376. height: 18rpx;
  377. background: $text-color-muted;
  378. border-radius: 4rpx;
  379. }
  380. }
  381. }
  382. .form-input {
  383. flex: 1;
  384. height: 100rpx;
  385. font-size: 30rpx;
  386. color: $text-color-primary;
  387. }
  388. .input-action {
  389. padding: 16rpx;
  390. margin: -16rpx;
  391. margin-left: 0;
  392. .eye-icon {
  393. width: 40rpx;
  394. height: 28rpx;
  395. border: 3rpx solid $text-color-muted;
  396. border-radius: 50%;
  397. position: relative;
  398. &::before {
  399. content: '';
  400. position: absolute;
  401. top: 50%;
  402. left: 50%;
  403. transform: translate(-50%, -50%);
  404. width: 12rpx;
  405. height: 12rpx;
  406. background: $text-color-muted;
  407. border-radius: 50%;
  408. }
  409. &.active::before {
  410. background: $primary-color;
  411. }
  412. &.active {
  413. border-color: $primary-color;
  414. }
  415. }
  416. }
  417. }
  418. .input-placeholder {
  419. color: $text-color-placeholder;
  420. }
  421. /* 登录按钮 */
  422. .login-btn {
  423. width: 100%;
  424. height: 100rpx;
  425. line-height: 100rpx;
  426. padding: 0;
  427. background: $primary-color;
  428. color: #fff;
  429. font-size: 32rpx;
  430. font-weight: 600;
  431. border-radius: 16rpx;
  432. margin-top: 20rpx;
  433. border: none;
  434. transition: all 0.2s;
  435. display: flex;
  436. align-items: center;
  437. justify-content: center;
  438. &::after {
  439. border: none;
  440. }
  441. &:active {
  442. background: $primary-color-dark;
  443. transform: scale(0.98);
  444. }
  445. &[disabled] {
  446. opacity: 0.6;
  447. }
  448. }
  449. .login-tips {
  450. display: flex;
  451. align-items: center;
  452. justify-content: center;
  453. margin-top: 28rpx;
  454. padding: 20rpx;
  455. background: $success-color-bg;
  456. border-radius: 12rpx;
  457. .tips-icon {
  458. width: 32rpx;
  459. height: 32rpx;
  460. background: $primary-color;
  461. border-radius: 50%;
  462. margin-right: 12rpx;
  463. position: relative;
  464. &::before {
  465. content: '';
  466. position: absolute;
  467. top: 50%;
  468. left: 50%;
  469. transform: translate(-50%, -50%);
  470. width: 12rpx;
  471. height: 4rpx;
  472. background: #fff;
  473. border-radius: 2rpx;
  474. }
  475. &::after {
  476. content: '';
  477. position: absolute;
  478. top: 50%;
  479. left: 50%;
  480. transform: translate(-50%, -50%);
  481. width: 4rpx;
  482. height: 12rpx;
  483. background: #fff;
  484. border-radius: 2rpx;
  485. }
  486. }
  487. .tips-label {
  488. font-size: 24rpx;
  489. color: $text-color-tertiary;
  490. margin-right: 12rpx;
  491. }
  492. .tips-value {
  493. font-size: 24rpx;
  494. color: $primary-color;
  495. font-weight: 600;
  496. }
  497. }
  498. /* 分隔线 */
  499. .login-divider {
  500. display: flex;
  501. align-items: center;
  502. margin-top: 32rpx;
  503. margin-bottom: 28rpx;
  504. .divider-line {
  505. flex: 1;
  506. height: 1rpx;
  507. background: $border-color;
  508. }
  509. .divider-text {
  510. padding: 0 24rpx;
  511. font-size: 24rpx;
  512. color: $text-color-muted;
  513. }
  514. }
  515. /* 微信登录按钮 */
  516. .wechat-btn {
  517. width: 100%;
  518. height: 100rpx;
  519. line-height: 100rpx;
  520. padding: 0;
  521. background: $bg-color-card;
  522. color: $text-color-primary;
  523. font-size: 30rpx;
  524. font-weight: 500;
  525. border: 2rpx solid $border-color;
  526. border-radius: 16rpx;
  527. display: flex;
  528. align-items: center;
  529. justify-content: center;
  530. transition: all 0.2s;
  531. &::after {
  532. border: none;
  533. }
  534. &:active {
  535. background: $bg-color-page;
  536. border-color: $primary-color;
  537. }
  538. &[disabled] {
  539. opacity: 0.6;
  540. }
  541. .wechat-icon {
  542. width: 36rpx;
  543. height: 36rpx;
  544. background: #07c160;
  545. border-radius: 8rpx;
  546. margin-right: 12rpx;
  547. position: relative;
  548. &::before {
  549. content: '';
  550. position: absolute;
  551. top: 8rpx;
  552. left: 50%;
  553. transform: translateX(-50%);
  554. width: 14rpx;
  555. height: 10rpx;
  556. border: 3rpx solid #fff;
  557. border-top: none;
  558. border-right: none;
  559. transform: translateX(-50%) rotate(-45deg);
  560. }
  561. &::after {
  562. content: '';
  563. position: absolute;
  564. bottom: 8rpx;
  565. left: 50%;
  566. transform: translateX(-50%);
  567. width: 16rpx;
  568. height: 10rpx;
  569. border: 3rpx solid #fff;
  570. border-bottom: none;
  571. border-right: none;
  572. transform: translateX(-50%) rotate(45deg);
  573. }
  574. }
  575. }
  576. /* 绑定码切换链接 */
  577. .bind-toggle {
  578. display: block;
  579. text-align: center;
  580. margin-top: 20rpx;
  581. font-size: 24rpx;
  582. color: $primary-color;
  583. }
  584. /* 绑定码区域 */
  585. .bind-section {
  586. margin-top: 20rpx;
  587. .divider-line {
  588. height: 1rpx;
  589. background: $border-color;
  590. margin-bottom: 20rpx;
  591. }
  592. .bind-hint {
  593. display: block;
  594. font-size: 24rpx;
  595. color: $text-color-tertiary;
  596. margin-bottom: 16rpx;
  597. line-height: 1.5;
  598. }
  599. .bind-input-wrapper {
  600. display: flex;
  601. align-items: center;
  602. background: $bg-color-page;
  603. border: 2rpx solid $border-color;
  604. border-radius: 16rpx;
  605. padding: 0 20rpx;
  606. margin-bottom: 16rpx;
  607. transition: all 0.2s;
  608. &:focus-within {
  609. border-color: $primary-color;
  610. background: $bg-color-card;
  611. box-shadow: 0 0 0 4rpx rgba(255, 193, 7, 0.1);
  612. }
  613. .bind-input {
  614. flex: 1;
  615. height: 100rpx;
  616. font-size: 30rpx;
  617. color: $text-color-primary;
  618. letter-spacing: 4rpx;
  619. }
  620. .scan-bind-btn {
  621. display: flex;
  622. align-items: center;
  623. justify-content: center;
  624. height: 72rpx;
  625. padding: 0 24rpx;
  626. margin-right: 8rpx;
  627. background: $primary-color;
  628. border-radius: 12rpx;
  629. .scan-icon-text {
  630. font-size: 24rpx;
  631. color: #fff;
  632. font-weight: 600;
  633. }
  634. &:active {
  635. opacity: 0.85;
  636. }
  637. }
  638. }
  639. .bind-btn {
  640. width: 100%;
  641. height: 90rpx;
  642. line-height: 90rpx;
  643. padding: 0;
  644. background: $primary-color;
  645. color: #fff;
  646. font-size: 30rpx;
  647. font-weight: 600;
  648. border-radius: 16rpx;
  649. border: none;
  650. display: flex;
  651. align-items: center;
  652. justify-content: center;
  653. &::after {
  654. border: none;
  655. }
  656. &:active {
  657. background: $primary-color-dark;
  658. }
  659. &[disabled] {
  660. opacity: 0.6;
  661. }
  662. }
  663. }
  664. /* 底部 */
  665. .login-footer {
  666. padding: 40rpx 0;
  667. text-align: center;
  668. text {
  669. font-size: 24rpx;
  670. color: $text-color-placeholder;
  671. }
  672. }
  673. </style>