login.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. <template>
  2. <view class="login-container">
  3. <view class="logo-section">
  4. <image class="logo" src="/static/logo.png" mode="aspectFit" @error="handleImageError"></image>
  5. <text class="app-name">知行益家</text>
  6. <text class="slogan">家庭教育任务管理系统</text>
  7. </view>
  8. <!-- 手机号登录 -->
  9. <view class="login-form">
  10. <view class="form-item">
  11. <text class="label">手机号</text>
  12. <input
  13. class="input"
  14. type="number"
  15. v-model="phone"
  16. placeholder="请输入手机号"
  17. maxlength="11"
  18. cursor-spacing="100"
  19. />
  20. </view>
  21. <view class="form-item">
  22. <text class="label">验证码</text>
  23. <view class="code-input-wrapper">
  24. <input
  25. class="input code-input"
  26. type="number"
  27. v-model="code"
  28. placeholder="请输入验证码"
  29. maxlength="6"
  30. />
  31. <button
  32. class="send-code-btn"
  33. :disabled="countdown > 0"
  34. @click="sendCode"
  35. >
  36. {{ countdown > 0 ? countdown + 's' : '发送验证码' }}
  37. </button>
  38. </view>
  39. </view>
  40. <button class="login-btn" :loading="loading" @click="phoneLogin">登录</button>
  41. <!-- 微信一键登录 -->
  42. <view class="wechat-section">
  43. <button
  44. class="wechat-btn"
  45. open-type="getPhoneNumber"
  46. @getphonenumber="handleWechatPhoneLogin"
  47. >
  48. <text class="wechat-icon">微信</text>
  49. <text>一键登录</text>
  50. </button>
  51. </view>
  52. </view>
  53. <view class="agreement">
  54. <checkbox-group @change="onAgreementChange">
  55. <label>
  56. <checkbox value="agree" :checked="agreed" />
  57. <text class="agreement-text">我已阅读并同意</text>
  58. </label>
  59. </checkbox-group>
  60. <text class="link" @click="showAgreement">《用户协议》</text>
  61. <text class="agreement-text">和</text>
  62. <text class="link" @click="showPrivacy">《隐私政策》</text>
  63. </view>
  64. </view>
  65. </template>
  66. <script>
  67. import { sendCode, phoneLogin as phoneLoginApi, wechatPhoneLogin, checkPhone } from '../../utils/api.js'
  68. export default {
  69. data() {
  70. return {
  71. phone: '',
  72. code: '',
  73. loading: false,
  74. countdown: 0,
  75. agreed: false,
  76. nickname: '',
  77. avatar: ''
  78. }
  79. },
  80. onLoad(options) {
  81. // 处理邀请码
  82. if (options.inviteCode) {
  83. uni.setStorageSync('inviteCode', options.inviteCode)
  84. uni.setStorageSync('inviteType', options.inviteType || 'family')
  85. }
  86. },
  87. methods: {
  88. handleImageError(e) {
  89. console.log('Logo 加载失败,使用默认显示')
  90. },
  91. onAgreementChange(e) {
  92. this.agreed = e.detail.value.length > 0
  93. },
  94. showAgreement() {
  95. uni.showModal({
  96. title: '用户协议',
  97. content: '这里是用户协议内容...',
  98. showCancel: false
  99. })
  100. },
  101. showPrivacy() {
  102. uni.showModal({
  103. title: '隐私政策',
  104. content: '这里是隐私政策内容...',
  105. showCancel: false
  106. })
  107. },
  108. async sendCode() {
  109. if (!this.phone) {
  110. uni.showToast({ title: '请输入手机号', icon: 'none' })
  111. return
  112. }
  113. if (!/^1[3-9]\d{9}$/.test(this.phone)) {
  114. uni.showToast({ title: '手机号格式不正确', icon: 'none' })
  115. return
  116. }
  117. try {
  118. await sendCode({ phone: this.phone, type: 'login' })
  119. uni.showToast({ title: '验证码已发送', icon: 'success' })
  120. // 开始倒计时
  121. this.countdown = 60
  122. const timer = setInterval(() => {
  123. this.countdown--
  124. if (this.countdown <= 0) {
  125. clearInterval(timer)
  126. }
  127. }, 1000)
  128. } catch (e) {
  129. uni.showToast({ title: e.message || '发送失败', icon: 'none' })
  130. }
  131. },
  132. async phoneLogin() {
  133. if (!this.agreed) {
  134. uni.showToast({ title: '请先同意用户协议', icon: 'none' })
  135. return
  136. }
  137. if (!this.phone) {
  138. uni.showToast({ title: '请输入手机号', icon: 'none' })
  139. return
  140. }
  141. if (!this.code) {
  142. uni.showToast({ title: '请输入验证码', icon: 'none' })
  143. return
  144. }
  145. this.loading = true
  146. try {
  147. const res = await phoneLoginApi({
  148. phone: this.phone,
  149. code: this.code,
  150. nickname: this.nickname || '用户',
  151. avatar: this.avatar || ''
  152. })
  153. // 保存 token 和用户信息
  154. uni.setStorageSync('token', res.data.token)
  155. uni.setStorageSync('userId', res.data.userId)
  156. uni.setStorageSync('role', res.data.role)
  157. uni.setStorageSync('familyId', res.data.familyId)
  158. // 登录后以本次登录角色为准,避免沿用上次切换视图
  159. uni.setStorageSync('currentRole', res.data.role)
  160. uni.setStorageSync('isSwitchedChild', false)
  161. uni.setStorageSync('isSwitchedTeacher', false)
  162. uni.setStorageSync('userInfo', {
  163. nickname: res.data.nickname,
  164. userId: res.data.userId
  165. })
  166. // 存储成长规划师审核状态
  167. if (res.data.teacherStatus) {
  168. uni.setStorageSync('teacherStatus', res.data.teacherStatus)
  169. }
  170. if (res.data.teacherRejectReason) {
  171. uni.setStorageSync('teacherRejectReason', res.data.teacherRejectReason)
  172. }
  173. uni.showToast({ title: '登录成功', icon: 'success' })
  174. // 根据角色跳转到对应首页,或进入信息编辑页面
  175. setTimeout(() => {
  176. if (res.data.needsRoleSelect) {
  177. // 新用户需要选择角色
  178. uni.redirectTo({
  179. url: `/pages/user-edit/user-edit?token=${res.data.token}&userId=${res.data.userId}&needsRoleSelect=true`
  180. })
  181. } else {
  182. // 老用户直接进入首页
  183. this.navigateToHome(res.data.role)
  184. }
  185. }, 1000)
  186. } catch (e) {
  187. uni.showToast({ title: e.message || '登录失败', icon: 'none' })
  188. } finally {
  189. this.loading = false
  190. }
  191. },
  192. async handleWechatPhoneLogin(e) {
  193. if (e.detail.errMsg === 'getPhoneNumber:ok') {
  194. if (!this.agreed) {
  195. uni.showToast({ title: '请先同意用户协议', icon: 'none' })
  196. return
  197. }
  198. // 测试环境:使用用户输入的手机号
  199. const inputPhone = this.phone
  200. if (!inputPhone || !/^1[3-9]\d{9}$/.test(inputPhone)) {
  201. uni.showToast({ title: '请先输入手机号', icon: 'none' })
  202. return
  203. }
  204. this.loading = true
  205. try {
  206. // 获取登录 code 用于获取 openid
  207. const loginRes = await new Promise((resolve, reject) => {
  208. uni.login({
  209. provider: 'weixin',
  210. success: resolve,
  211. fail: reject
  212. })
  213. })
  214. // 调用后端微信手机号登录接口
  215. // 测试环境:将用户输入的手机号作为 phoneCode 传递
  216. const res = await wechatPhoneLogin({
  217. code: loginRes.code, // 用于获取 openid
  218. phoneCode: inputPhone, // 测试环境:用输入的手机号
  219. nickname: this.nickname || '用户',
  220. avatar: this.avatar || ''
  221. })
  222. // 保存 token 和用户信息
  223. uni.setStorageSync('token', res.data.token)
  224. uni.setStorageSync('userId', res.data.userId)
  225. uni.setStorageSync('role', res.data.role)
  226. uni.setStorageSync('familyId', res.data.familyId)
  227. uni.setStorageSync('currentRole', res.data.role)
  228. uni.setStorageSync('isSwitchedChild', false)
  229. uni.setStorageSync('isSwitchedTeacher', false)
  230. // 保存 openid,用于后续自动登录
  231. if (res.data.openid) {
  232. uni.setStorageSync('openid', res.data.openid)
  233. }
  234. uni.setStorageSync('userInfo', {
  235. nickname: res.data.nickname,
  236. userId: res.data.userId
  237. })
  238. // 存储成长规划师审核状态
  239. if (res.data.teacherStatus) {
  240. uni.setStorageSync('teacherStatus', res.data.teacherStatus)
  241. }
  242. if (res.data.teacherRejectReason) {
  243. uni.setStorageSync('teacherRejectReason', res.data.teacherRejectReason)
  244. }
  245. uni.showToast({ title: '登录成功', icon: 'success' })
  246. // 根据角色跳转到对应首页,或进入信息编辑页面
  247. setTimeout(() => {
  248. if (res.data.needsRoleSelect) {
  249. // 新用户需要选择角色
  250. uni.redirectTo({
  251. url: `/pages/user-edit/user-edit?token=${res.data.token}&userId=${res.data.userId}&needsRoleSelect=true`
  252. })
  253. } else {
  254. // 老用户直接进入首页
  255. this.navigateToHome(res.data.role)
  256. }
  257. }, 1000)
  258. } catch (e) {
  259. uni.showToast({ title: e.message || '登录失败', icon: 'none' })
  260. } finally {
  261. this.loading = false
  262. }
  263. }
  264. },
  265. navigateToHome(role) {
  266. // 通过 TabBar 首页跳转,让 index.vue 根据角色显示不同内容
  267. uni.reLaunch({ url: '/pages/index/index' })
  268. }
  269. }
  270. }
  271. </script>
  272. <style scoped>
  273. .login-container {
  274. min-height: 100vh;
  275. background: linear-gradient(180deg, #667eea 0%, #764ba2 100%);
  276. padding: 60rpx 40rpx;
  277. }
  278. .logo-section {
  279. text-align: center;
  280. margin-bottom: 80rpx;
  281. }
  282. .logo {
  283. width: 160rpx;
  284. height: 160rpx;
  285. border-radius: 80rpx;
  286. background: #fff;
  287. }
  288. .app-name {
  289. display: block;
  290. font-size: 48rpx;
  291. font-weight: bold;
  292. color: #fff;
  293. margin-top: 20rpx;
  294. }
  295. .slogan {
  296. display: block;
  297. font-size: 26rpx;
  298. color: rgba(255, 255, 255, 0.8);
  299. margin-top: 10rpx;
  300. }
  301. .login-form {
  302. background: #fff;
  303. border-radius: 20rpx;
  304. padding: 40rpx;
  305. }
  306. .form-item {
  307. margin-bottom: 30rpx;
  308. }
  309. .label {
  310. display: block;
  311. font-size: 28rpx;
  312. color: #333;
  313. margin-bottom: 10rpx;
  314. }
  315. .input {
  316. width: 100%;
  317. height: 80rpx;
  318. background: #f5f5f5;
  319. border-radius: 10rpx;
  320. padding: 0 20rpx;
  321. font-size: 28rpx;
  322. }
  323. .code-input-wrapper {
  324. display: flex;
  325. align-items: center;
  326. gap: 20rpx;
  327. }
  328. .code-input {
  329. flex: 1;
  330. }
  331. .send-code-btn {
  332. width: 200rpx;
  333. height: 80rpx;
  334. background: #667eea;
  335. color: #fff;
  336. border-radius: 10rpx;
  337. font-size: 24rpx;
  338. line-height: 80rpx;
  339. padding: 0;
  340. }
  341. .send-code-btn[disabled] {
  342. background: #ccc;
  343. }
  344. .login-btn {
  345. width: 100%;
  346. height: 88rpx;
  347. background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  348. color: #fff;
  349. border-radius: 44rpx;
  350. font-size: 32rpx;
  351. line-height: 88rpx;
  352. margin-top: 20rpx;
  353. }
  354. /* 角色选择器样式 */
  355. .role-select {
  356. display: flex;
  357. gap: 40rpx;
  358. padding: 20rpx 0;
  359. }
  360. .role-option {
  361. display: flex;
  362. align-items: center;
  363. gap: 10rpx;
  364. }
  365. .picker-input {
  366. width: 100%;
  367. height: 80rpx;
  368. background: #f5f5f5;
  369. border-radius: 10rpx;
  370. padding: 0 20rpx;
  371. font-size: 28rpx;
  372. line-height: 80rpx;
  373. color: #333;
  374. }
  375. .wechat-section {
  376. margin-top: 40rpx;
  377. text-align: center;
  378. }
  379. .wechat-btn {
  380. display: flex;
  381. align-items: center;
  382. justify-content: center;
  383. gap: 10rpx;
  384. width: 100%;
  385. height: 88rpx;
  386. background: #07c160;
  387. color: #fff;
  388. border-radius: 44rpx;
  389. font-size: 32rpx;
  390. }
  391. .wechat-icon {
  392. font-size: 36rpx;
  393. }
  394. .agreement {
  395. margin-top: 40rpx;
  396. text-align: center;
  397. font-size: 24rpx;
  398. color: #fff;
  399. }
  400. .agreement-text {
  401. color: rgba(255, 255, 255, 0.8);
  402. }
  403. .link {
  404. color: #ffd700;
  405. text-decoration: underline;
  406. }
  407. </style>