index.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <template>
  2. <view class="container">
  3. <!-- 顶部导航 -->
  4. <view class="nav-bar">
  5. <view class="back" @click="goBack">
  6. <text class="iconfont">←</text>
  7. </view>
  8. <!-- <text class="title">登录2</text> -->
  9. <view class="more-options">
  10. <text class="iconfont">→</text>
  11. </view>
  12. </view>
  13. <!-- 头像授权 -->
  14. <view class="avatar-section">
  15. <button class="avatar-button" open-type="chooseAvatar" @chooseavatar="onChooseAvatar">
  16. <image class="avatar" :src="userInfo.avatarUrl || '/static/icons/user_icon.png'" mode="aspectFill"></image>
  17. </button>
  18. <text class="avatar-text" v-if="isShow">授权头像</text>
  19. </view>
  20. <!-- 昵称输入 -->
  21. <view class="form">
  22. <view class="form-item">
  23. <text class="label">昵称</text>
  24. <input name="nickName" type="nickname" placeholder="请填写昵称" class="input" v-model="userInfo.nickName" />
  25. </view>
  26. </view>
  27. <!-- 微信一键登录按钮 -->
  28. <button class="wx-login-btn" @tap="loginWithWeChat" :loading="loading" :disabled="loading">
  29. <image class="wx-icon" src="/static/wechat-icon.png" mode="widthFix" />
  30. 微信一键授权登录
  31. </button>
  32. <!-- 提示文字 -->
  33. <text class="tip-text">微信授权登录后才可进行更多操作哦</text>
  34. </view>
  35. </template>
  36. <script>
  37. import {
  38. uploadInfo
  39. } from "@/api/services/connect.js";
  40. import storage from "@/utils/storage.js";
  41. export default {
  42. data() {
  43. return {
  44. isShow: true,
  45. userInfo: {
  46. avatarUrl: '',
  47. nickName: '',
  48. openId: ''
  49. },
  50. loading: false,
  51. };
  52. },
  53. methods: {
  54. //获取微信头像
  55. onChooseAvatar(e) {
  56. this.userInfo.avatarUrl = e.detail.avatarUrl;
  57. console.log("eeee", e);
  58. },
  59. goBack() {
  60. uni.navigateBack();
  61. },
  62. async requestProfile() {
  63. try {
  64. const res = await uni.getUserProfile({
  65. desc: '用于完善会员资料',
  66. });
  67. this.userInfo = res.userInfo;
  68. } catch (err) {
  69. uni.showToast({
  70. icon: 'none',
  71. title: '授权失败',
  72. });
  73. }
  74. },
  75. async loginWithWeChat() {
  76. // 防止进入后重复点击(双保险)
  77. if (this.loading) return;
  78. if (!this.userInfo.nickName) {
  79. uni.showToast({
  80. title: '请先授权头像昵称',
  81. icon: 'none'
  82. });
  83. return;
  84. }
  85. this.loading = true;
  86. try {
  87. // TODO: 调用后端接口完成注册/登录
  88. const res = await uploadInfo(this.userInfo);
  89. console.log("res", res);
  90. if (res.data.code == 200) {
  91. storage.setUserInfo(res.data.data);
  92. }
  93. uni.showToast({
  94. title: '登录成功',
  95. });
  96. // 成功跳转页面
  97. wx.switchTab({
  98. url: '/pages/user/index'
  99. });
  100. } catch (err) {
  101. console.log("err", err);
  102. uni.showToast({
  103. icon: 'none',
  104. title: '登录失败,请重试',
  105. });
  106. } finally {
  107. this.loading = false;
  108. }
  109. },
  110. },
  111. onLoad(options) {
  112. console.log("options", options);
  113. this.userInfo.openId = options.openId || '';
  114. }
  115. };
  116. </script>
  117. <style>
  118. .container {
  119. background-color: #fff;
  120. height: 100vh;
  121. padding: 0 20rpx;
  122. box-sizing: border-box;
  123. }
  124. .nav-bar {
  125. display: flex;
  126. align-items: center;
  127. justify-content: space-between;
  128. padding: 40rpx 0 20rpx;
  129. }
  130. .back,
  131. .more-options {
  132. width: 60rpx;
  133. text-align: center;
  134. }
  135. .title {
  136. font-size: 36rpx;
  137. font-weight: bold;
  138. }
  139. .avatar-section {
  140. display: flex;
  141. flex-direction: column;
  142. align-items: center;
  143. margin-top: 60rpx;
  144. }
  145. .avatar-button {
  146. padding: 0;
  147. margin: 0;
  148. border: none;
  149. background-color: transparent;
  150. border-radius: 50%;
  151. overflow: hidden;
  152. }
  153. .avatar {
  154. width: 120rpx;
  155. height: 120rpx;
  156. border-radius: 50%;
  157. background-color: #f5f5f5;
  158. }
  159. .avatar-text {
  160. margin-top: 20rpx;
  161. font-size: 28rpx;
  162. color: #888;
  163. }
  164. .form {
  165. margin: 60rpx 0 40rpx;
  166. }
  167. .form-item {
  168. display: flex;
  169. align-items: center;
  170. border-bottom: 1px solid #eee;
  171. padding-bottom: 20rpx;
  172. }
  173. .label {
  174. width: 100rpx;
  175. font-size: 28rpx;
  176. color: #333;
  177. }
  178. .input {
  179. flex: 1;
  180. font-size: 28rpx;
  181. padding-left: 20rpx;
  182. }
  183. .wx-login-btn {
  184. display: flex;
  185. align-items: center;
  186. justify-content: center;
  187. background-color: #1aad19;
  188. color: #fff;
  189. border-radius: 100rpx;
  190. height: 88rpx;
  191. font-size: 30rpx;
  192. margin-top: 40rpx;
  193. }
  194. .wx-icon {
  195. width: 40rpx;
  196. margin-right: 20rpx;
  197. }
  198. .tip-text {
  199. margin-top: 20rpx;
  200. font-size: 24rpx;
  201. color: #999;
  202. text-align: center;
  203. }
  204. </style>