phone-login.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <template>
  2. <!-- 手机登录 -->
  3. <view class="page-container">
  4. <!-- 顶部标题区域 -->
  5. <image class="bg-image " src="@/static/images/bg.png" mode="aspectFill"></image>
  6. <!-- 登录表单区域 -->
  7. <view class="login-form">
  8. <!-- 账号输入框 -->
  9. <view class="input-group">
  10. <text class="input-label">账号</text>
  11. <u-input v-model="account" placeholder="请输入手机号码" :border="false" class="input-field" type="number"
  12. maxlength="11"></u-input>
  13. </view>
  14. <!-- 密码输入框 -->
  15. <view class="input-group">
  16. <text class="input-label">密码</text>
  17. <u-input v-model="password" placeholder="请输入密码" :border="false" class="input-field" type="password"
  18. :password-icon="true"></u-input>
  19. </view>
  20. <!-- 登录按钮 -->
  21. <button class="login-btn" @tap="handleLogin">
  22. <text>确定</text>
  23. </button>
  24. </view>
  25. </view>
  26. </template>
  27. <script>
  28. import {
  29. phoneLogin,
  30. wxLogin
  31. } from '@/config/api.js';
  32. export default {
  33. data() {
  34. return {
  35. account: '',
  36. password: ''
  37. }
  38. },
  39. methods: {
  40. // 处理登录
  41. async handleLogin() {
  42. if (!this.account) {
  43. uni.showToast({
  44. title: '请输入账号',
  45. icon: 'none'
  46. });
  47. return;
  48. }
  49. if (!this.password) {
  50. uni.showToast({
  51. title: '请输入密码',
  52. icon: 'none'
  53. });
  54. return;
  55. }
  56. uni.showLoading({
  57. title: '登录中...',
  58. mask: true
  59. });
  60. try {
  61. console.log('登录信息:', {
  62. account: this.account,
  63. password: this.password
  64. });
  65. // 第一步:手机号登录 (system/info/login)
  66. let params = {
  67. "username": this.account,
  68. "password": this.password,
  69. "wxid": "3"
  70. }
  71. const phoneRes = await phoneLogin(params);
  72. console.log(phoneRes, "手机号登录成功")
  73. // 保存手机号登录返回的token
  74. uni.setStorageSync('access_token', phoneRes.token);
  75. // 保存用户信息到缓存
  76. if (phoneRes.user) {
  77. uni.setStorageSync('user', phoneRes.user);
  78. }
  79. // 第二步:微信登录 (system/link/login)
  80. const loginRes = await this.wxLoginPromise();
  81. console.log('微信登录凭证code:', loginRes.code);
  82. const wxRes = await wxLogin({
  83. code: loginRes.code
  84. });
  85. console.log('微信登录响应:', wxRes);
  86. // 保存微信登录返回的openid
  87. if (wxRes.openid) {
  88. uni.setStorageSync('openid', wxRes.openid);
  89. }
  90. // 如果微信登录返回了新的token,也保存
  91. const wxToken = wxRes.token || wxRes.access_token;
  92. if (wxToken) {
  93. uni.setStorageSync('access_token', wxToken);
  94. uni.setStorageSync('token', wxToken);
  95. }
  96. // 如果微信登录返回了用户信息,也保存
  97. if (wxRes.user) {
  98. uni.setStorageSync('user', wxRes.user);
  99. }
  100. // 保存完整的微信登录数据
  101. if (wxRes) {
  102. uni.setStorageSync('loginData', wxRes);
  103. }
  104. // 更新store状态
  105. this.$store.commit('isLogin', true);
  106. uni.hideLoading();
  107. uni.showToast({
  108. title: '登录成功',
  109. icon: 'success',
  110. duration: 1000
  111. });
  112. // 登录成功后的跳转
  113. setTimeout(() => {
  114. uni.switchTab({
  115. url: '/pages/index/index'
  116. });
  117. }, 1000);
  118. } catch (error) {
  119. console.error('登录错误:', error);
  120. uni.hideLoading();
  121. uni.showToast({
  122. title: error.message || '登录失败,请重试',
  123. icon: 'none'
  124. });
  125. }
  126. },
  127. // 微信登录 Promise 封装
  128. wxLoginPromise() {
  129. return new Promise((resolve, reject) => {
  130. uni.login({
  131. provider: 'weixin',
  132. success: (res) => resolve(res),
  133. fail: (err) => reject(new Error('获取登录凭证失败'))
  134. });
  135. });
  136. }
  137. },
  138. }
  139. </script>
  140. <style>
  141. .page-container {
  142. min-height: 100vh;
  143. background: #F5F5F5;
  144. }
  145. /* 背景图片 */
  146. .bg-image {
  147. top: 0;
  148. left: 0;
  149. width: 100%;
  150. height: 300rpx;
  151. z-index: 0;
  152. }
  153. .login-form {
  154. background: #FFFFFF;
  155. border-radius: 24rpx;
  156. padding: 32rpx;
  157. margin: 32rpx;
  158. box-shadow: 0 4rpx 16rpx rgba(41, 44, 53, 0.1);
  159. .input-group {
  160. margin-bottom: 32rpx;
  161. .input-label {
  162. display: block;
  163. font-size: 28rpx;
  164. color: #292C35;
  165. margin-bottom: 16rpx;
  166. }
  167. .input-field {
  168. background: #F5F5F5;
  169. border-radius: 12rpx;
  170. height: 88rpx;
  171. padding: 0 24rpx;
  172. font-size: 28rpx;
  173. }
  174. }
  175. .login-btn {
  176. width: 100%;
  177. height: 88rpx;
  178. background: linear-gradient(135deg, #FF5B05 0%, #FF8E3C 100%);
  179. border-radius: 44rpx;
  180. color: #FFFFFF;
  181. font-size: 32rpx;
  182. font-weight: 500;
  183. display: flex;
  184. align-items: center;
  185. justify-content: center;
  186. margin-top: 48rpx;
  187. border: none;
  188. box-shadow: 0 4rpx 16rpx rgba(255, 91, 5, 0.3);
  189. &:active {
  190. transform: translateY(2rpx);
  191. box-shadow: 0 2rpx 8rpx rgba(255, 91, 5, 0.4);
  192. }
  193. }
  194. }
  195. </style>