register.vue 10 KB

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