login.vue 12 KB

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