registerUser.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. <template>
  2. <view class="container">
  3. <!-- #ifdef MP-WEIXIN -->
  4. <image class="bg" src="https://ysrw-1395926010.cos.ap-chengdu.myqcloud.com/image/bg_login.png" mode="widthFix">
  5. </image>
  6. <view class="force-login-wrap">
  7. <view :style="{ height: menuButtonInfo.height, marginTop: menuButtonInfo.top }" class="backImg">
  8. <image @tap="goBack()" src="https://ysrw-1395926010.cos.ap-chengdu.myqcloud.com/image/back_white.png">
  9. </image>
  10. </view>
  11. <view class="top-title">
  12. <view class="title-text">欢迎注册<text class="blue">文依学术</text></view>
  13. <view class="login-notice">填写信息完成注册</view>
  14. </view>
  15. <view class="force-login__content">
  16. <view class="input-form">
  17. <view class="input-item">
  18. <input class="input-field" type="text" v-model="formData.userName" placeholder="请输入用户账号(登录名)" />
  19. </view>
  20. <view class="input-item">
  21. <input class="input-field" type="text" v-model="formData.nickName" placeholder="请输入用户名称" />
  22. </view>
  23. <view class="input-item">
  24. <input class="input-field" type="number" v-model="formData.phonenumber" placeholder="请输入手机号" maxlength="11" />
  25. </view>
  26. <view class="input-item code-input-item">
  27. <input class="input-field code-input" type="number" v-model="verifyCode" placeholder="请输入验证码" maxlength="6" />
  28. <view class="get-code-btn" @click="getVerifyCode">
  29. {{ codeText }}
  30. </view>
  31. </view>
  32. <view class="input-item">
  33. <view class="mima">
  34. <input class="input-field" type="text" :password="showPassword" v-model="formData.password" placeholder="请输入密码" />
  35. <view class="pwd-toggle" @click="closePwd">
  36. <image
  37. :src="showPassword ? 'https://ysrw-1395926010.cos.ap-chengdu.myqcloud.com/image/icon_invisible.png' : 'https://ysrw-1395926010.cos.ap-chengdu.myqcloud.com/image/icon_visible.png'"
  38. mode="aspectFill"></image>
  39. </view>
  40. </view>
  41. </view>
  42. <view class="input-item">
  43. <view class="mima">
  44. <input class="input-field" type="text" :password="showConfirmPassword" v-model="formData.confirmPassword" placeholder="请再次输入密码" />
  45. <view class="pwd-toggle" @click="closeConfirmPwd">
  46. <image
  47. :src="showConfirmPassword ? 'https://ysrw-1395926010.cos.ap-chengdu.myqcloud.com/image/icon_invisible.png' : 'https://ysrw-1395926010.cos.ap-chengdu.myqcloud.com/image/icon_visible.png'"
  48. mode="aspectFill"></image>
  49. </view>
  50. </view>
  51. </view>
  52. </view>
  53. <view class="tips-group">
  54. <view class="tips-item" @click="toLogin">已有账号?立即登录</view>
  55. </view>
  56. <view class="login-btn-wrap">
  57. <button class="login-btn" @click="handleRegister">注册</button>
  58. </view>
  59. <view class="tips">
  60. <checkbox :checked="isAgreement" @click="handleAgreement()" />
  61. <view class="tips-text" @click="handleAgreement()">已阅读并接受</view>
  62. <view class="btn" @click.stop="navTo('/pages_user/userAgreement')">《用户注册协议》</view>
  63. <view class="btn" @click.stop="navTo('/pages_user/privacyPolicy')">《隐私保护政策》</view>
  64. </view>
  65. </view>
  66. </view>
  67. <!-- #endif -->
  68. </view>
  69. </template>
  70. <script>
  71. import { sendSmsCode, smsRegister } from '@/api/user'
  72. export default {
  73. data() {
  74. return {
  75. formData: {
  76. userName: '',
  77. nickName: '',
  78. phonenumber: '',
  79. password: '',
  80. confirmPassword: ''
  81. },
  82. verifyCode: '',
  83. isAgreement: false,
  84. menuButtonInfo: {},
  85. codeText: '获取验证码',
  86. countdown: 0,
  87. countdownTimer: null,
  88. showPassword: true,
  89. showConfirmPassword: true
  90. }
  91. },
  92. onLoad(option) {
  93. this.getMenuButtonInfo()
  94. },
  95. onUnload() {
  96. // 清除倒计时
  97. if (this.countdownTimer) {
  98. clearInterval(this.countdownTimer);
  99. this.countdownTimer = null;
  100. }
  101. },
  102. methods: {
  103. navTo(path) {
  104. uni.navigateTo({
  105. url: path
  106. })
  107. },
  108. closePwd() {
  109. this.showPassword = !this.showPassword
  110. },
  111. closeConfirmPwd() {
  112. this.showConfirmPassword = !this.showConfirmPassword
  113. },
  114. // 获取胶囊按钮布局参数
  115. getMenuButtonInfo() {
  116. const menuBtn = uni.getMenuButtonBoundingClientRect();
  117. if (menuBtn) {
  118. this.menuButtonInfo = {
  119. top: menuBtn.top + 'px',
  120. height: menuBtn.height + 'px',
  121. centerY: (menuBtn.top + menuBtn.height / 2) + 'px',
  122. right: menuBtn.right + 'px'
  123. };
  124. }
  125. },
  126. // 返回
  127. goBack() {
  128. uni.navigateBack({
  129. delta: 1
  130. })
  131. },
  132. // 跳转到登录页
  133. toLogin() {
  134. uni.navigateTo({
  135. url: '/pages/auth/login'
  136. })
  137. },
  138. // 获取验证码
  139. getVerifyCode() {
  140. if (this.countdown > 0) {
  141. return;
  142. }
  143. if (!this.formData.phonenumber) {
  144. uni.showToast({
  145. icon: 'none',
  146. title: "请输入手机号",
  147. });
  148. return;
  149. }
  150. if (!/^1[3-9]\d{9}$/.test(this.formData.phonenumber)) {
  151. uni.showToast({
  152. icon: 'none',
  153. title: "请输入正确的手机号",
  154. });
  155. return;
  156. }
  157. // 调用发送验证码API
  158. uni.showLoading({
  159. title: "发送中..."
  160. });
  161. let params = {
  162. phone: this.formData.phonenumber,
  163. type: '2',
  164. }
  165. sendSmsCode(params)
  166. .then(res => {
  167. uni.hideLoading();
  168. if (res.code == 200) {
  169. uni.showToast({
  170. icon: 'success',
  171. title: "验证码已发送",
  172. });
  173. // 开始倒计时
  174. this.countdown = 60;
  175. this.countdownTimer = setInterval(() => {
  176. this.countdown--;
  177. this.codeText = this.countdown + '秒重新获取';
  178. if (this.countdown <= 0) {
  179. clearInterval(this.countdownTimer);
  180. this.countdownTimer = null;
  181. this.codeText = '获取验证码';
  182. }
  183. }, 1000);
  184. } else {
  185. uni.showToast({
  186. icon: 'none',
  187. title: res.msg || "发送验证码失败",
  188. });
  189. }
  190. })
  191. .catch(err => {
  192. uni.hideLoading();
  193. });
  194. },
  195. // 注册
  196. handleRegister() {
  197. if (!this.isAgreement) {
  198. uni.showToast({
  199. icon: 'none',
  200. title: "请先同意协议后再注册",
  201. });
  202. return;
  203. }
  204. if (!this.formData.userName) {
  205. uni.showToast({
  206. icon: 'none',
  207. title: "请输入用户账号",
  208. });
  209. return;
  210. }
  211. if (!this.formData.nickName) {
  212. uni.showToast({
  213. icon: 'none',
  214. title: "请输入用户名称",
  215. });
  216. return;
  217. }
  218. if (!this.formData.phonenumber) {
  219. uni.showToast({
  220. icon: 'none',
  221. title: "请输入手机号",
  222. });
  223. return;
  224. }
  225. if (!/^1[3-9]\d{9}$/.test(this.formData.phonenumber)) {
  226. uni.showToast({
  227. icon: 'none',
  228. title: "请输入正确的手机号",
  229. });
  230. return;
  231. }
  232. if (!this.verifyCode) {
  233. uni.showToast({
  234. icon: 'none',
  235. title: "请输入验证码",
  236. });
  237. return;
  238. }
  239. if (!this.formData.password) {
  240. uni.showToast({
  241. icon: 'none',
  242. title: "请输入密码",
  243. });
  244. return;
  245. }
  246. if (this.formData.password !== this.formData.confirmPassword) {
  247. uni.showToast({
  248. icon: 'none',
  249. title: "两次输入的密码不一致",
  250. });
  251. return;
  252. }
  253. this.register();
  254. },
  255. // 注册
  256. register() {
  257. uni.showLoading({
  258. title: "注册中..."
  259. });
  260. // 调用注册接口
  261. const data = {
  262. userId: '', // 新用户userId为空
  263. userName: this.formData.userName,
  264. code: this.verifyCode,
  265. nickName: this.formData.nickName,
  266. phonenumber: this.formData.phonenumber,
  267. password: this.formData.password,
  268. confirmPassword: this.formData.confirmPassword
  269. };
  270. smsRegister(data)
  271. .then(res => {
  272. uni.hideLoading();
  273. if (res.code == 200) {
  274. uni.showToast({
  275. icon: 'success',
  276. title: "注册成功",
  277. });
  278. uni.setStorageSync('AppToken', res.data.token);
  279. uni.setStorageSync('userInfo', res.data.user);
  280. if (!res.data.user.roles) {
  281. console.log("跳转注册")
  282. uni.navigateTo({
  283. url: '/pages/auth/register'
  284. })
  285. } else {
  286. console.log("没跳转注册")
  287. uni.$emit('refreshLogin');
  288. uni.navigateBack({
  289. delta: 1
  290. });
  291. }
  292. } else {
  293. uni.showToast({
  294. icon: 'none',
  295. title: res.msg,
  296. });
  297. }
  298. })
  299. .catch(err => {
  300. uni.hideLoading();
  301. uni.showToast({
  302. icon: 'none',
  303. title: "网络错误,请稍后重试",
  304. });
  305. });
  306. },
  307. handleAgreement() {
  308. this.isAgreement = !this.isAgreement;
  309. }
  310. }
  311. }
  312. </script>
  313. <style lang="scss">
  314. .container {
  315. flex: 1;
  316. padding: 0 24rpx;
  317. display: flex;
  318. flex-direction: column;
  319. justify-content: flex-start;
  320. position: relative;
  321. .bg {
  322. width: 100%;
  323. position: absolute;
  324. top: 0;
  325. left: 0;
  326. }
  327. .backImg {
  328. display: flex;
  329. align-items: center;
  330. image {
  331. width: 40rpx;
  332. height: 40rpx;
  333. }
  334. margin-left: 32rpx;
  335. }
  336. }
  337. .force-login-wrap {
  338. width: 100%;
  339. height: 100vh;
  340. position: relative;
  341. .top-title {
  342. padding: 0 32rpx;
  343. text-align: left;
  344. margin-top: 20rpx;
  345. .title-text {
  346. font-weight: 600;
  347. font-size: 48rpx;
  348. color: #333333;
  349. margin-bottom: 20rpx;
  350. .blue {
  351. color: #157CF8;
  352. }
  353. }
  354. .login-notice {
  355. font-weight: 400;
  356. font-size: 32rpx;
  357. color: #666666;
  358. line-height: 40rpx;
  359. }
  360. }
  361. .force-login__content {
  362. margin-top: 54rpx;
  363. padding: 0 32rpx;
  364. position: relative;
  365. .input-form {
  366. margin-bottom: 60rpx;
  367. .input-item {
  368. background: #FFFFFF;
  369. border-radius: 16rpx;
  370. padding: 30rpx 40rpx;
  371. margin-bottom: 48rpx;
  372. box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.04);
  373. &::last-child {
  374. margin-bottom: 0
  375. }
  376. .input-field {
  377. width: 100%;
  378. font-size: 30rpx;
  379. font-family: PingFang SC;
  380. color: #333333;
  381. &::placeholder {
  382. color: #CCCCCC;
  383. }
  384. }
  385. .mima {
  386. width: 100%;
  387. display: flex;
  388. align-items: center;
  389. justify-content: space-between;
  390. .pwd-toggle {
  391. width: 88rpx;
  392. display: flex;
  393. align-items: center;
  394. justify-content: center;
  395. }
  396. image {
  397. width: 32rpx;
  398. height: 32rpx;
  399. }
  400. }
  401. &.code-input-item {
  402. display: flex;
  403. align-items: center;
  404. .code-input {
  405. flex: 1;
  406. }
  407. .get-code-btn {
  408. font-size: 28rpx;
  409. font-family: PingFang SC;
  410. font-weight: 500;
  411. color: #157CF8;
  412. padding-left: 20rpx;
  413. white-space: nowrap;
  414. }
  415. }
  416. }
  417. }
  418. .tips-group {
  419. display: flex;
  420. align-items: center;
  421. justify-content: flex-end;
  422. font-weight: 400;
  423. font-size: 24rpx;
  424. color: #388BFF;
  425. line-height: 40rpx;
  426. margin: -40rpx 0 24rpx;
  427. }
  428. .login-btn-wrap {
  429. margin-bottom: 40rpx;
  430. margin-top: 140rpx;
  431. .login-btn {
  432. width: 100%;
  433. height: 88rpx;
  434. background: rgba(56, 139, 255, 1);
  435. border-radius: 44rpx;
  436. font-size: 32rpx;
  437. font-family: PingFang SC;
  438. font-weight: 500;
  439. color: #FFFFFF;
  440. border: none;
  441. display: flex;
  442. align-items: center;
  443. justify-content: center;
  444. &::after {
  445. border: none;
  446. }
  447. }
  448. }
  449. }
  450. }
  451. .tips {
  452. display: flex;
  453. justify-content: center;
  454. align-items: center;
  455. font-size: 24rpx;
  456. color: #666666;
  457. flex-wrap: nowrap;
  458. padding: 0 60rpx;
  459. checkbox {
  460. margin-right: 10rpx;
  461. flex-shrink: 0;
  462. transform: scale(0.8);
  463. }
  464. .tips-text {
  465. margin-right: 4rpx;
  466. white-space: nowrap;
  467. }
  468. .btn {
  469. color: #157CF8;
  470. margin: 0 2rpx;
  471. white-space: nowrap;
  472. }
  473. }
  474. </style>