register.vue 12 KB

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