index.vue 5.4 KB

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