register.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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 } from '@/api/services/connect.js'
  108. import Foundation from "@/utils/Foundation.js"
  109. // 响应式数据
  110. const formData = reactive({
  111. phone: '',
  112. code: '',
  113. password: '',
  114. confirmPassword: '',
  115. nickname: ''
  116. })
  117. const showPassword = ref(false)
  118. const showConfirmPassword = ref(false)
  119. const agreed = ref(true)
  120. const codeBtnText = ref('获取验证码')
  121. const codeBtnDisabled = ref(false)
  122. const countdown = ref(60)
  123. const isSubmitting = ref(false)
  124. // 方法定义
  125. const goBack = () => {
  126. uni.navigateBack()
  127. }
  128. const togglePasswordVisibility = () => {
  129. showPassword.value = !showPassword.value
  130. }
  131. const toggleConfirmPasswordVisibility = () => {
  132. showConfirmPassword.value = !showConfirmPassword.value
  133. }
  134. const toggleAgreement = () => {
  135. agreed.value = !agreed.value
  136. }
  137. const navigateToLogin = () => {
  138. uni.navigateBack()
  139. }
  140. const navigateToTerms = () => {
  141. uni.navigateTo({
  142. url: '/pages/privacy/terms'
  143. })
  144. }
  145. const navigateToPrivacy = () => {
  146. uni.navigateTo({
  147. url: '/pages/privacy/index'
  148. })
  149. }
  150. const startCountdown = () => {
  151. codeBtnDisabled.value = true
  152. codeBtnText.value = `${countdown.value}秒`
  153. const timer = setInterval(() => {
  154. countdown.value--
  155. codeBtnText.value = `${countdown.value}秒`
  156. if (countdown.value <= 0) {
  157. clearInterval(timer)
  158. codeBtnDisabled.value = false
  159. codeBtnText.value = '获取验证码'
  160. countdown.value = 60
  161. }
  162. }, 1000)
  163. }
  164. const validateForm = () => {
  165. if (!formData.phone) {
  166. uni.showToast({
  167. title: '请输入手机号',
  168. icon: 'none'
  169. })
  170. return false
  171. }
  172. const phone = formData.phone // 获取用户输入的手机号
  173. const result = Foundation.validatePhoneNumber(phone)
  174. if (!result.valid) {
  175. uni.showToast({
  176. title: result.message,
  177. icon: 'none'
  178. })
  179. return false
  180. }
  181. if (!formData.password) {
  182. uni.showToast({
  183. title: '请设置密码',
  184. icon: 'none'
  185. })
  186. return false
  187. }
  188. if (formData.password.length < 6) {
  189. uni.showToast({
  190. title: '密码长度不能少于6位',
  191. icon: 'none'
  192. })
  193. return false
  194. }
  195. if (formData.password !== formData.confirmPassword) {
  196. uni.showToast({
  197. title: '两次输入的密码不一致',
  198. icon: 'none'
  199. })
  200. return false
  201. }
  202. return true
  203. }
  204. const handleRegister = () => {
  205. if (!validateForm()) return
  206. if (!agreed.value) {
  207. uni.showToast({
  208. title: '请同意用户协议和隐私政策',
  209. icon: 'none'
  210. })
  211. return
  212. }
  213. isSubmitting.value = true
  214. // 调用注册接口
  215. uni.showLoading({ title: '注册中...' })
  216. const data = {
  217. phoneNumber: formData.phone,
  218. password: formData.password,
  219. username: formData.nickname || `用户${formData.phone.substr(-4)}`
  220. }
  221. // 调用注册接口
  222. register(data)
  223. .then(res => {
  224. console.log("res注册", res)
  225. if (res.data.code === 200) {
  226. uni.showToast({
  227. title: '注册成功',
  228. icon: 'success'
  229. })
  230. storage.setUserInfo(data)
  231. // 自动登录
  232. // if (res.data.data && res.data.data.token) {
  233. // storage.setAccessToken(res.data.data.token)
  234. // storage.setUserInfo(data)
  235. // storage.setHasLogin(true)
  236. // }
  237. // 延迟跳转到登录页或首页
  238. setTimeout(() => {
  239. uni.navigateBack({
  240. delta: 1,
  241. })
  242. }, 1500)
  243. } else {
  244. uni.showToast({
  245. title: res.data.msg || '注册失败,请稍后重试',
  246. icon: 'none'
  247. })
  248. }
  249. })
  250. .catch(err => {
  251. uni.showToast({
  252. title: '网络异常,请稍后重试',
  253. icon: 'none'
  254. })
  255. console.error(err)
  256. })
  257. .finally(() => {
  258. uni.hideLoading()
  259. isSubmitting.value = false
  260. })
  261. }
  262. </script>
  263. <style>
  264. .register-container {
  265. min-height: 100vh;
  266. padding: 0 40rpx 40rpx;
  267. background-color: #fff;
  268. display: flex;
  269. flex-direction: column;
  270. }
  271. .page-header {
  272. display: flex;
  273. justify-content: space-between;
  274. align-items: center;
  275. padding: 40rpx 0;
  276. }
  277. .back-icon {
  278. font-size: 50rpx;
  279. color: #333;
  280. width: 60rpx;
  281. }
  282. .page-title {
  283. font-size: 36rpx;
  284. font-weight: bold;
  285. color: #333;
  286. }
  287. .placeholder {
  288. width: 60rpx;
  289. }
  290. .register-form {
  291. width: 100%;
  292. margin-bottom: 30rpx;
  293. }
  294. .form-group {
  295. margin-bottom: 30rpx;
  296. }
  297. .input-group {
  298. display: flex;
  299. align-items: center;
  300. border-bottom: 1rpx solid #E0E0E0;
  301. padding: 20rpx 0;
  302. }
  303. .input-icon {
  304. width: 44rpx;
  305. height: 44rpx;
  306. background-color: #E8F5E9;
  307. color: #4CAF50;
  308. font-size: 24rpx;
  309. border-radius: 8rpx;
  310. display: flex;
  311. align-items: center;
  312. justify-content: center;
  313. margin-right: 20rpx;
  314. }
  315. .input {
  316. flex: 1;
  317. height: 44rpx;
  318. font-size: 28rpx;
  319. }
  320. .eye-icon {
  321. padding: 0 10rpx;
  322. color: #999;
  323. }
  324. .code-btn {
  325. font-size: 28rpx;
  326. color: #4CAF50;
  327. padding: 0 10rpx;
  328. }
  329. .code-btn.disabled {
  330. color: #999;
  331. }
  332. .register-btn {
  333. width: 100%;
  334. height: 90rpx;
  335. background-color: #4CAF50;
  336. color: #fff;
  337. border-radius: 45rpx;
  338. font-size: 32rpx;
  339. font-weight: bold;
  340. display: flex;
  341. align-items: center;
  342. justify-content: center;
  343. border: none;
  344. margin: 60rpx 0 30rpx;
  345. }
  346. .login-link {
  347. display: flex;
  348. justify-content: center;
  349. font-size: 28rpx;
  350. color: #666;
  351. }
  352. .link {
  353. color: #4CAF50;
  354. margin-left: 10rpx;
  355. }
  356. .privacy-agreement {
  357. display: flex;
  358. align-items: center;
  359. justify-content: center;
  360. padding: 40rpx 0;
  361. }
  362. .agreement-text {
  363. font-size: 24rpx;
  364. color: #666;
  365. margin-left: 10rpx;
  366. }
  367. /* 微信注册提示 */
  368. .wechat-register-tip {
  369. margin-top: 40rpx;
  370. display: flex;
  371. flex-direction: column;
  372. align-items: center;
  373. }
  374. .wechat-register-tip text {
  375. font-size: 28rpx;
  376. color: #666;
  377. margin-bottom: 20rpx;
  378. }
  379. .wechat-register-btn {
  380. width: 80%;
  381. height: 90rpx;
  382. background-color: #07C160;
  383. color: #fff;
  384. border-radius: 45rpx;
  385. font-size: 32rpx;
  386. font-weight: bold;
  387. display: flex;
  388. align-items: center;
  389. justify-content: center;
  390. border: none;
  391. padding: 0;
  392. }
  393. .wechat-icon {
  394. width: 40rpx;
  395. height: 40rpx;
  396. background-color: rgba(255, 255, 255, 0.2);
  397. color: #fff;
  398. border-radius: 20rpx;
  399. font-size: 20rpx;
  400. display: flex;
  401. align-items: center;
  402. justify-content: center;
  403. margin-right: 10rpx;
  404. }
  405. /* #ifdef H5 */
  406. /* H5特殊样式调整 */
  407. @media screen and (min-width: 768px) {
  408. .register-form {
  409. max-width: 600rpx;
  410. margin: 0 auto;
  411. }
  412. }
  413. /* #endif */
  414. /* #ifdef APP-PLUS || MP-HARMONY */
  415. /* APP和鸿蒙特殊样式调整 */
  416. .register-container {
  417. padding-top: 20rpx;
  418. }
  419. .input-group {
  420. background-color: #f5f5f5;
  421. border-radius: 8rpx;
  422. padding: 20rpx 15rpx;
  423. border-bottom: none;
  424. margin-bottom: 20rpx;
  425. }
  426. .input {
  427. background-color: transparent;
  428. }
  429. .register-btn {
  430. margin-top: 40rpx;
  431. }
  432. .privacy-agreement {
  433. margin-top: auto;
  434. }
  435. /* #endif */
  436. </style>