phone-login.vue 5.7 KB

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