register.vue 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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">立即注册</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. </view>
  94. </template>
  95. <script>
  96. import { register, sendVerificationCode } from '@/api/services/auth.js';
  97. export default {
  98. data() {
  99. return {
  100. formData: {
  101. phone: '',
  102. code: '',
  103. password: '',
  104. confirmPassword: '',
  105. nickname: ''
  106. },
  107. showPassword: false,
  108. showConfirmPassword: false,
  109. agreed: true,
  110. codeBtnText: '获取验证码',
  111. codeBtnDisabled: false,
  112. countdown: 60
  113. }
  114. },
  115. methods: {
  116. goBack() {
  117. uni.navigateBack()
  118. },
  119. togglePasswordVisibility() {
  120. this.showPassword = !this.showPassword
  121. },
  122. toggleConfirmPasswordVisibility() {
  123. this.showConfirmPassword = !this.showConfirmPassword
  124. },
  125. toggleAgreement() {
  126. this.agreed = !this.agreed
  127. },
  128. navigateToLogin() {
  129. uni.navigateBack()
  130. },
  131. navigateToTerms() {
  132. uni.navigateTo({
  133. url: '/pages/privacy/terms'
  134. })
  135. },
  136. navigateToPrivacy() {
  137. uni.navigateTo({
  138. url: '/pages/privacy/index'
  139. })
  140. },
  141. getVerificationCode() {
  142. if (this.codeBtnDisabled) return
  143. if (!this.formData.phone) {
  144. uni.showToast({
  145. title: '请输入手机号',
  146. icon: 'none'
  147. })
  148. return
  149. }
  150. if (!/^1\d{10}$/.test(this.formData.phone)) {
  151. uni.showToast({
  152. title: '请输入正确的手机号',
  153. icon: 'none'
  154. })
  155. return
  156. }
  157. // 发送验证码请求
  158. uni.showLoading({ title: '发送中...' })
  159. // 使用授权服务发送验证码
  160. sendVerificationCode({
  161. phone: this.formData.phone,
  162. type: 'register'
  163. })
  164. .then(() => {
  165. uni.showToast({
  166. title: '验证码已发送',
  167. icon: 'success'
  168. });
  169. this.startCountdown();
  170. })
  171. .catch(error => {
  172. uni.showToast({
  173. title: error,
  174. icon: 'none'
  175. });
  176. })
  177. .finally(() => {
  178. uni.hideLoading();
  179. });
  180. },
  181. startCountdown() {
  182. this.codeBtnDisabled = true
  183. this.codeBtnText = `${this.countdown}秒`
  184. const timer = setInterval(() => {
  185. this.countdown--
  186. this.codeBtnText = `${this.countdown}秒`
  187. if (this.countdown <= 0) {
  188. clearInterval(timer)
  189. this.codeBtnDisabled = false
  190. this.codeBtnText = '获取验证码'
  191. this.countdown = 60
  192. }
  193. }, 1000)
  194. },
  195. validateForm() {
  196. if (!this.formData.phone) {
  197. uni.showToast({
  198. title: '请输入手机号',
  199. icon: 'none'
  200. })
  201. return false
  202. }
  203. if (!/^1\d{10}$/.test(this.formData.phone)) {
  204. uni.showToast({
  205. title: '请输入正确的手机号',
  206. icon: 'none'
  207. })
  208. return false
  209. }
  210. if (!this.formData.code) {
  211. uni.showToast({
  212. title: '请输入验证码',
  213. icon: 'none'
  214. })
  215. return false
  216. }
  217. if (!this.formData.password) {
  218. uni.showToast({
  219. title: '请设置密码',
  220. icon: 'none'
  221. })
  222. return false
  223. }
  224. if (this.formData.password.length < 6) {
  225. uni.showToast({
  226. title: '密码长度不能少于6位',
  227. icon: 'none'
  228. })
  229. return false
  230. }
  231. if (this.formData.password !== this.formData.confirmPassword) {
  232. uni.showToast({
  233. title: '两次输入的密码不一致',
  234. icon: 'none'
  235. })
  236. return false
  237. }
  238. return true
  239. },
  240. handleRegister() {
  241. if (!this.validateForm()) return
  242. if (!this.agreed) {
  243. uni.showToast({
  244. title: '请同意用户协议和隐私政策',
  245. icon: 'none'
  246. })
  247. return
  248. }
  249. // 调用注册接口
  250. uni.showLoading({ title: '注册中...' })
  251. // 使用授权服务注册
  252. register({
  253. phone: this.formData.phone,
  254. code: this.formData.code,
  255. password: this.formData.password,
  256. nickname: this.formData.nickname || `用户${this.formData.phone.substr(-4)}`
  257. })
  258. .then(() => {
  259. uni.showToast({
  260. title: '注册成功',
  261. icon: 'success'
  262. });
  263. // 延迟跳转到登录页
  264. setTimeout(() => {
  265. uni.navigateBack();
  266. }, 1500);
  267. })
  268. .catch(error => {
  269. uni.showToast({
  270. title: error,
  271. icon: 'none'
  272. });
  273. })
  274. .finally(() => {
  275. uni.hideLoading();
  276. });
  277. }
  278. }
  279. }
  280. </script>
  281. <style>
  282. .register-container {
  283. min-height: 100vh;
  284. padding: 0 40rpx 40rpx;
  285. background-color: #fff;
  286. display: flex;
  287. flex-direction: column;
  288. }
  289. .page-header {
  290. display: flex;
  291. justify-content: space-between;
  292. align-items: center;
  293. padding: 40rpx 0;
  294. }
  295. .back-icon {
  296. font-size: 50rpx;
  297. color: #333;
  298. width: 60rpx;
  299. }
  300. .page-title {
  301. font-size: 36rpx;
  302. font-weight: bold;
  303. color: #333;
  304. }
  305. .placeholder {
  306. width: 60rpx;
  307. }
  308. .register-form {
  309. width: 100%;
  310. margin-bottom: 30rpx;
  311. }
  312. .form-group {
  313. margin-bottom: 30rpx;
  314. }
  315. .input-group {
  316. display: flex;
  317. align-items: center;
  318. border-bottom: 1rpx solid #E0E0E0;
  319. padding: 20rpx 0;
  320. }
  321. .input-icon {
  322. width: 44rpx;
  323. height: 44rpx;
  324. background-color: #E8F5E9;
  325. color: #4CAF50;
  326. font-size: 24rpx;
  327. border-radius: 8rpx;
  328. display: flex;
  329. align-items: center;
  330. justify-content: center;
  331. margin-right: 20rpx;
  332. }
  333. .input {
  334. flex: 1;
  335. height: 44rpx;
  336. font-size: 28rpx;
  337. }
  338. .eye-icon {
  339. padding: 0 10rpx;
  340. color: #999;
  341. }
  342. .code-btn {
  343. font-size: 28rpx;
  344. color: #4CAF50;
  345. padding: 0 10rpx;
  346. }
  347. .code-btn.disabled {
  348. color: #999;
  349. }
  350. .register-btn {
  351. width: 100%;
  352. height: 90rpx;
  353. background-color: #4CAF50;
  354. color: #fff;
  355. border-radius: 45rpx;
  356. font-size: 32rpx;
  357. font-weight: bold;
  358. display: flex;
  359. align-items: center;
  360. justify-content: center;
  361. border: none;
  362. margin: 60rpx 0 30rpx;
  363. }
  364. .login-link {
  365. display: flex;
  366. justify-content: center;
  367. font-size: 28rpx;
  368. color: #666;
  369. }
  370. .link {
  371. color: #4CAF50;
  372. margin-left: 10rpx;
  373. }
  374. .privacy-agreement {
  375. display: flex;
  376. align-items: center;
  377. justify-content: center;
  378. margin-top: auto;
  379. padding: 40rpx 0;
  380. }
  381. .agreement-text {
  382. font-size: 24rpx;
  383. color: #666;
  384. margin-left: 10rpx;
  385. }
  386. </style>