register.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. <template>
  2. <view class="register-container">
  3. <view class="page-header">
  4. <text class="back-icon" @click="goBack">←</text>
  5. <text class="page-title">注册账号</text>
  6. <text class="placeholder"></text>
  7. </view>
  8. <view class="register-form">
  9. <view class="form-group">
  10. <view class="input-group">
  11. <text class="input-icon">手</text>
  12. <input
  13. class="input"
  14. type="number"
  15. placeholder="请输入手机号"
  16. v-model="formData.phone"
  17. maxlength="11"
  18. />
  19. </view>
  20. </view>
  21. <view class="form-group">
  22. <view class="input-group">
  23. <text class="input-icon">验</text>
  24. <input
  25. class="input"
  26. type="number"
  27. placeholder="请输入验证码"
  28. v-model="formData.code"
  29. maxlength="6"
  30. />
  31. <text
  32. class="code-btn"
  33. :class="{ disabled: codeBtnDisabled }"
  34. @click="getVerificationCode"
  35. >
  36. {{ codeBtnText }}
  37. </text>
  38. </view>
  39. </view>
  40. <view class="form-group">
  41. <view class="input-group">
  42. <text class="input-icon">密</text>
  43. <input
  44. class="input"
  45. :type="showPassword ? 'text' : 'password'"
  46. placeholder="请设置密码"
  47. v-model="formData.password"
  48. />
  49. <text class="eye-icon" @click="togglePasswordVisibility">
  50. {{ showPassword ? '👁️' : '👁️‍🗨️' }}
  51. </text>
  52. </view>
  53. </view>
  54. <view class="form-group">
  55. <view class="input-group">
  56. <text class="input-icon">确</text>
  57. <input
  58. class="input"
  59. :type="showConfirmPassword ? 'text' : 'password'"
  60. placeholder="请确认密码"
  61. v-model="formData.confirmPassword"
  62. />
  63. <text class="eye-icon" @click="toggleConfirmPasswordVisibility">
  64. {{ showConfirmPassword ? '👁️' : '👁️‍🗨️' }}
  65. </text>
  66. </view>
  67. </view>
  68. <view class="form-group">
  69. <view class="input-group">
  70. <text class="input-icon">昵</text>
  71. <input
  72. class="input"
  73. type="text"
  74. placeholder="请输入昵称(选填)"
  75. v-model="formData.nickname"
  76. />
  77. </view>
  78. </view>
  79. <button class="register-btn" @click="handleRegister" :loading="isSubmitting">立即注册</button>
  80. <view class="login-link">
  81. <text>已有账号?</text>
  82. <text class="link" @click="navigateToLogin">立即登录</text>
  83. </view>
  84. </view>
  85. <view class="privacy-agreement">
  86. <checkbox :checked="agreed" @click="toggleAgreement"></checkbox>
  87. <text class="agreement-text">
  88. 注册即表示您已同意
  89. <text class="link" @click="navigateToTerms">用户协议</text>
  90. <text class="link" @click="navigateToPrivacy">隐私政策</text>
  91. </text>
  92. </view>
  93. <!-- #ifdef MP-WEIXIN -->
  94. <view class="wechat-register-tip">
  95. <text>推荐使用微信一键登录</text>
  96. <button class="wechat-register-btn" @click="navigateToLogin">
  97. <text class="wechat-icon">微</text>
  98. <text>一键登录</text>
  99. </button>
  100. </view>
  101. <!-- #endif -->
  102. </view>
  103. </template>
  104. <script setup>
  105. import { ref, reactive } from 'vue'
  106. import storage from "@/utils/storage.js"
  107. import { register,sendVcode, phoneRegister } from '@/api/services/connect.js'
  108. import Foundation from "@/utils/Foundation.js"
  109. // 响应式数据
  110. const formData = reactive({
  111. phone: '',
  112. code: '',
  113. scene: "REGIST",
  114. password: '',
  115. confirmPassword: '',
  116. nickname: ''
  117. })
  118. const showPassword = ref(false)
  119. const showConfirmPassword = ref(false)
  120. const agreed = ref(false)
  121. const codeBtnText = ref('获取验证码')
  122. const codeBtnDisabled = ref(false)
  123. const countdown = ref(60)
  124. const isSubmitting = ref(false)
  125. // 方法定义
  126. const goBack = () => {
  127. uni.navigateBack()
  128. }
  129. const togglePasswordVisibility = () => {
  130. showPassword.value = !showPassword.value
  131. }
  132. const toggleConfirmPasswordVisibility = () => {
  133. showConfirmPassword.value = !showConfirmPassword.value
  134. }
  135. const toggleAgreement = () => {
  136. agreed.value = !agreed.value
  137. }
  138. const navigateToLogin = () => {
  139. uni.navigateBack()
  140. }
  141. const navigateToTerms = () => {
  142. uni.navigateTo({
  143. url: '/pages/login/terms'
  144. })
  145. }
  146. const navigateToPrivacy = () => {
  147. uni.navigateTo({
  148. url: '/pages/login/privacy'
  149. })
  150. }
  151. const startCountdown = () => {
  152. codeBtnDisabled.value = true
  153. codeBtnText.value = `${countdown.value}秒`
  154. const timer = setInterval(() => {
  155. countdown.value--
  156. codeBtnText.value = `${countdown.value}秒`
  157. if (countdown.value <= 0) {
  158. clearInterval(timer)
  159. codeBtnDisabled.value = false
  160. codeBtnText.value = '获取验证码'
  161. countdown.value = 60
  162. }
  163. }, 1000)
  164. }
  165. const getVerificationCode = () => {
  166. if (!formData.phone) {
  167. uni.showToast({
  168. title: '请输入手机号',
  169. icon: 'none'
  170. })
  171. return
  172. }
  173. const phone = formData.phone
  174. const result = Foundation.validatePhoneNumber(phone)
  175. if (!result.valid) {
  176. uni.showToast({
  177. title: result.message,
  178. icon: 'none'
  179. })
  180. return
  181. }
  182. // 调用获取验证码接口
  183. uni.showLoading({ title: '发送验证码...' })
  184. sendVcode({
  185. terminal: formData.phone,
  186. scene: formData.scene
  187. }).then(res => {
  188. console.log("验证码:",res.data.code);
  189. if (res.data.code == 500 || res.data.code == 1) {
  190. uni.showToast({
  191. title: res.data.msg || '发送验证码失败,请稍后重试',
  192. icon: 'none'
  193. })
  194. return
  195. }
  196. uni.hideLoading()
  197. uni.showToast({
  198. title: '验证码已发送',
  199. icon: 'success'
  200. })
  201. startCountdown()
  202. }).catch(err => {
  203. uni.hideLoading()
  204. uni.showToast({
  205. title: '发送验证码失败,请稍后重试',
  206. icon: 'none'
  207. })
  208. console.error(err)
  209. })
  210. }
  211. const validateForm = () => {
  212. if (!formData.phone) {
  213. uni.showToast({
  214. title: '请输入手机号',
  215. icon: 'none'
  216. })
  217. return false
  218. }
  219. const phone = formData.phone // 获取用户输入的手机号
  220. const result = Foundation.validatePhoneNumber(phone)
  221. if (!result.valid) {
  222. uni.showToast({
  223. title: result.message,
  224. icon: 'none'
  225. })
  226. return false
  227. }
  228. if (!formData.password) {
  229. uni.showToast({
  230. title: '请设置密码',
  231. icon: 'none'
  232. })
  233. return false
  234. }
  235. if (formData.password.length < 6) {
  236. uni.showToast({
  237. title: '密码长度不能少于6位',
  238. icon: 'none'
  239. })
  240. return false
  241. }
  242. if (formData.password !== formData.confirmPassword) {
  243. uni.showToast({
  244. title: '两次输入的密码不一致',
  245. icon: 'none'
  246. })
  247. return false
  248. }
  249. return true
  250. }
  251. const handleRegister = () => {
  252. if (!validateForm()) return
  253. if (!agreed.value) {
  254. uni.showToast({
  255. title: '请同意用户协议和隐私政策',
  256. icon: 'none'
  257. })
  258. return
  259. }
  260. isSubmitting.value = true
  261. // 调用注册接口
  262. uni.showLoading({ title: '注册中...' })
  263. const data = {
  264. phonenumber: formData.phone,
  265. password: formData.password,
  266. userName: formData.nickname || `用户${formData.phone.substr(-4)}`,
  267. vcode: formData.code,
  268. }
  269. // 调用注册接口
  270. phoneRegister(data)
  271. .then(res => {
  272. console.log("res注册", res)
  273. if (res.data.code == 1 || res.data.code == 2) {
  274. uni.showToast({
  275. title: res.data.msg || '注册失败,请稍后重试',
  276. icon: 'none'
  277. })
  278. return
  279. }
  280. if (res.statusCode === 200) {
  281. uni.showToast({
  282. title: '注册成功',
  283. icon: 'success'
  284. })
  285. storage.setUserInfo(data)
  286. // 自动登录
  287. // if (res.data.data && res.data.data.token) {
  288. // storage.setAccessToken(res.data.data.token)
  289. // storage.setUserInfo(data)
  290. // storage.setHasLogin(true)
  291. // }
  292. // 延迟跳转到登录页或首页
  293. setTimeout(() => {
  294. uni.navigateBack({
  295. delta: 1,
  296. })
  297. }, 1500)
  298. } else {
  299. uni.showToast({
  300. title: res.data.msg || '注册失败,请稍后重试',
  301. icon: 'none'
  302. })
  303. }
  304. })
  305. .catch(err => {
  306. uni.showToast({
  307. title: '网络异常,请稍后重试',
  308. icon: 'none'
  309. })
  310. console.error(err)
  311. })
  312. .finally(() => {
  313. uni.hideLoading()
  314. isSubmitting.value = false
  315. })
  316. }
  317. </script>
  318. <style>
  319. .register-container {
  320. min-height: 100vh;
  321. padding: 0 40rpx 40rpx;
  322. background-color: #fff;
  323. display: flex;
  324. flex-direction: column;
  325. }
  326. .page-header {
  327. display: flex;
  328. justify-content: space-between;
  329. align-items: center;
  330. padding: 40rpx 0;
  331. }
  332. .back-icon {
  333. font-size: 50rpx;
  334. color: #333;
  335. width: 60rpx;
  336. }
  337. .page-title {
  338. font-size: 36rpx;
  339. font-weight: bold;
  340. color: #333;
  341. }
  342. .placeholder {
  343. width: 60rpx;
  344. }
  345. .register-form {
  346. width: 100%;
  347. margin-bottom: 30rpx;
  348. }
  349. .form-group {
  350. margin-bottom: 30rpx;
  351. }
  352. .input-group {
  353. display: flex;
  354. align-items: center;
  355. border-bottom: 1rpx solid #E0E0E0;
  356. padding: 20rpx 0;
  357. }
  358. .input-icon {
  359. width: 44rpx;
  360. height: 44rpx;
  361. background-color: #E8F5E9;
  362. color: #4CAF50;
  363. font-size: 24rpx;
  364. border-radius: 8rpx;
  365. display: flex;
  366. align-items: center;
  367. justify-content: center;
  368. margin-right: 20rpx;
  369. }
  370. .input {
  371. flex: 1;
  372. height: 44rpx;
  373. font-size: 28rpx;
  374. }
  375. .eye-icon {
  376. padding: 0 10rpx;
  377. color: #999;
  378. }
  379. .code-btn {
  380. font-size: 28rpx;
  381. color: #4CAF50;
  382. padding: 0 10rpx;
  383. }
  384. .code-btn.disabled {
  385. color: #999;
  386. }
  387. .register-btn {
  388. width: 100%;
  389. height: 90rpx;
  390. background-color: #4CAF50;
  391. color: #fff;
  392. border-radius: 45rpx;
  393. font-size: 32rpx;
  394. font-weight: bold;
  395. display: flex;
  396. align-items: center;
  397. justify-content: center;
  398. border: none;
  399. margin: 60rpx 0 30rpx;
  400. }
  401. .login-link {
  402. display: flex;
  403. justify-content: center;
  404. font-size: 28rpx;
  405. color: #666;
  406. }
  407. .link {
  408. color: #4CAF50;
  409. margin-left: 10rpx;
  410. }
  411. .privacy-agreement {
  412. display: flex;
  413. align-items: center;
  414. justify-content: center;
  415. padding: 40rpx 0;
  416. }
  417. .agreement-text {
  418. font-size: 24rpx;
  419. color: #666;
  420. margin-left: 10rpx;
  421. }
  422. /* 微信注册提示 */
  423. .wechat-register-tip {
  424. margin-top: 40rpx;
  425. display: flex;
  426. flex-direction: column;
  427. align-items: center;
  428. }
  429. .wechat-register-tip text {
  430. font-size: 28rpx;
  431. color: #666;
  432. margin-bottom: 20rpx;
  433. }
  434. .wechat-register-btn {
  435. width: 80%;
  436. height: 90rpx;
  437. background-color: #07C160;
  438. color: #fff;
  439. border-radius: 45rpx;
  440. font-size: 32rpx;
  441. font-weight: bold;
  442. display: flex;
  443. align-items: center;
  444. justify-content: center;
  445. border: none;
  446. padding: 0;
  447. }
  448. .wechat-icon {
  449. width: 40rpx;
  450. height: 40rpx;
  451. background-color: rgba(255, 255, 255, 0.2);
  452. color: #fff;
  453. border-radius: 20rpx;
  454. font-size: 20rpx;
  455. display: flex;
  456. align-items: center;
  457. justify-content: center;
  458. margin-right: 10rpx;
  459. }
  460. /* #ifdef H5 */
  461. /* H5特殊样式调整 */
  462. @media screen and (min-width: 768px) {
  463. .register-form {
  464. max-width: 600rpx;
  465. margin: 0 auto;
  466. }
  467. }
  468. /* #endif */
  469. /* #ifdef APP-PLUS || MP-HARMONY */
  470. /* APP和鸿蒙特殊样式调整 */
  471. .register-container {
  472. padding-top: 20rpx;
  473. }
  474. .input-group {
  475. background-color: #f5f5f5;
  476. border-radius: 8rpx;
  477. padding: 20rpx 15rpx;
  478. border-bottom: none;
  479. margin-bottom: 20rpx;
  480. }
  481. .input {
  482. background-color: transparent;
  483. }
  484. .register-btn {
  485. margin-top: 40rpx;
  486. }
  487. .privacy-agreement {
  488. margin-top: auto;
  489. }
  490. /* #endif */
  491. </style>