index.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <template>
  2. <view class="login-container">
  3. <!-- 登录标题 -->
  4. <text class="title">智能医疗问诊系统</text>
  5. <!-- 微信登录按钮 -->
  6. <button
  7. class="wechat-btn"
  8. open-type="getPhoneNumber"
  9. @getphonenumber="handleGetPhoneNumber"
  10. @tap="handleGetUserProfile"
  11. >
  12. <text class="btn-text">微信一键登录</text>
  13. </button>
  14. <!-- 加载状态 -->
  15. <view v-if="loading" class="loading-mask">
  16. <uni-spinner type="circular" color="#07c160" size="40"></uni-spinner>
  17. </view>
  18. </view>
  19. </template>
  20. <script>
  21. export default {
  22. data() {
  23. return {
  24. loading: false,
  25. userInfo: null,
  26. rawData: null
  27. };
  28. },
  29. mounted() {
  30. this.initWechat();
  31. },
  32. methods: {
  33. initWechat() {
  34. // 这里可以添加依赖 WeixinJSBridge 的代码
  35. },
  36. handleGetUserProfile() {
  37. wx.showLoading({
  38. title: '获取信息中...', // 缩短提示文字
  39. mask: true
  40. });
  41. wx.getUserProfile({
  42. desc: '用于完善会员资料',
  43. success: (res) => {
  44. this.userInfo = res.userInfo;
  45. this.rawData = res.rawData;
  46. console.log('获取到的 rawData:', this.rawData);
  47. wx.hideLoading();
  48. },
  49. fail: (err) => {
  50. console.error('获取用户信息失败:', err);
  51. wx.showToast({ title: '请授权后再登录', icon: 'none' });
  52. wx.hideLoading();
  53. }
  54. });
  55. },
  56. // 处理获取手机号事件
  57. handleGetPhoneNumber: async function(e) {
  58. if (e.detail.errMsg === 'getPhoneNumber:ok') {
  59. if (!this.rawData) {
  60. console.error('请先授权获取用户信息');
  61. wx.showToast({ title: '请先授权获取用户信息', icon: 'none' });
  62. return;
  63. }
  64. try {
  65. wx.showLoading({
  66. title: '登录中...', // 缩短提示文字
  67. mask: true
  68. });
  69. this.loading = true;
  70. const encryptedData = e.detail.encryptedData;
  71. const iv = e.detail.iv;
  72. const { code } = await new Promise((resolve, reject) => {
  73. wx.login({
  74. success: (res) => resolve(res),
  75. fail: (err) => reject(err)
  76. });
  77. });
  78. const response = await new Promise((resolve, reject) => {
  79. wx.request({
  80. url: 'http://localhost:8080/api/auth/wechat/login',
  81. method: 'POST',
  82. data: {
  83. code,
  84. encryptedData,
  85. iv,
  86. rawData: this.rawData // 使用获取到的 rawData
  87. },
  88. success: (res) => resolve(res),
  89. fail: (err) => {
  90. if (err.errMsg.includes('request:fail url not in domain list')) {
  91. wx.showToast({
  92. title: '请求域名未配置,请联系管理员',
  93. icon: 'none',
  94. duration: 3000
  95. });
  96. }
  97. console.error('网络请求失败:', err);
  98. reject(err);
  99. }
  100. });
  101. });
  102. console.log('后端返回的完整数据:', response.data); // 打印完整的响应数据
  103. if (response.data) {
  104. if (response.data.wxNickname && response.data.wxAvatarUrl) {
  105. wx.setStorageSync('userToken', response.data.token);
  106. wx.setStorageSync('userInfo', {
  107. userId: response.data.userId,
  108. wxNickname: response.data.wxNickname,
  109. wxAvatarUrl: response.data.wxAvatarUrl
  110. });
  111. wx.navigateTo({
  112. url: '/pages/home/index'
  113. });
  114. } else {
  115. console.error('后端未返回微信昵称或头像信息:', response.data);
  116. wx.showToast({ title: '获取用户信息失败,请重试', icon: 'none' });
  117. }
  118. } else {
  119. console.error('登录响应数据为空:', response);
  120. wx.showToast({ title: '登录失败,请重试', icon: 'none' });
  121. }
  122. } catch (error) {
  123. console.error('登录过程出错:', error);
  124. wx.showToast({ title: '登录失败,请重试', icon: 'none' });
  125. } finally {
  126. this.loading = false;
  127. wx.hideLoading();
  128. }
  129. } else {
  130. console.error('用户拒绝授权', e.detail.errMsg);
  131. wx.showToast({ title: '请授权后再登录', icon: 'none' });
  132. }
  133. }
  134. }
  135. };
  136. </script>
  137. <style scoped lang="scss">
  138. .login-container {
  139. display: flex;
  140. flex-direction: column;
  141. align-items: center;
  142. justify-content: center;
  143. height: 100vh;
  144. background-color: #f0f2f5;
  145. }
  146. .title {
  147. font-size: 36rpx;
  148. color: #1a73e8;
  149. margin-bottom: 80rpx;
  150. font-weight: bold;
  151. }
  152. .wechat-btn {
  153. display: flex;
  154. align-items: center;
  155. justify-content: center;
  156. width: 600rpx;
  157. height: 120rpx;
  158. background-color: #07c160;
  159. color: white;
  160. border-radius: 60rpx;
  161. box-shadow: 0 8rpx 32rpx rgba(7, 193, 96, 0.15);
  162. font-size: 32rpx;
  163. margin-bottom: 40rpx;
  164. .wechat-icon {
  165. width: 60rpx;
  166. height: 60rpx;
  167. margin-right: 20rpx;
  168. }
  169. }
  170. .loading-mask {
  171. position: fixed;
  172. top: 0;
  173. left: 0;
  174. width: 100%;
  175. height: 100%;
  176. background-color: rgba(0, 0, 0, 0.3);
  177. display: flex;
  178. align-items: center;
  179. justify-content: center;
  180. z-index: 9999;
  181. }
  182. </style>