uniapp,h5

register.vue 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. <template>
  2. <view class="page">
  3. <view class="logo">
  4. <image src="../../../static/logo.png"></image>
  5. </view>
  6. <view class="input">
  7. <input v-model="register_email" placeholder="请输入邮箱地址" @input="onInputEmail" @blur="validateEmail" />
  8. <view v-if="showSuggestions" class="suggestions">
  9. <text v-for="suffix in suggestedSuffixes" :key="suffix"
  10. @click="appendSuffix(suffix)">{{ register_email + suffix }}</text>
  11. </view>
  12. </view>
  13. <view class="input">
  14. <input v-model="register_username" placeholder="请输入用户名" @blur="validateUsername" />
  15. </view>
  16. <view class="input">
  17. <input v-model="register_password" type="password" placeholder="请输入密码" @blur="checkPassword" />
  18. </view>
  19. <view class="input">
  20. <input v-model="register_password_sub" type="password" placeholder="请再次输入密码" @blur="validatePassword" />
  21. </view>
  22. <view class="underlinecontainer">
  23. <view class="buttonleft">
  24. <button @click="return2login()">返回登录</button>
  25. </view>
  26. <view class="buttonright">
  27. <button @click="register()">立即注册</button>
  28. </view>
  29. </view>
  30. </view>
  31. </template>
  32. <script>
  33. export default {
  34. data() {
  35. return {
  36. register_username: '',
  37. register_email: '',
  38. register_password: '',
  39. register_password_sub: '',
  40. passwordError: true,
  41. showSuggestions: false,
  42. emailValid: false,
  43. passwordValid: false,
  44. usernameValid: false,
  45. suggestedSuffixes: ['@qq.com']
  46. }
  47. },
  48. methods: {
  49. return2login() {
  50. uni.reLaunch({
  51. url: "./login"
  52. });
  53. },
  54. validatePassword() {
  55. // 验证两次输入的密码是否一致
  56. if (this.register_password !== this.register_password_sub) {
  57. this.passwordError = true;
  58. // 显示错误提示,这里可以是弹窗或者气泡提示
  59. uni.showToast({
  60. title: '两次输入的密码不一致!',
  61. icon: 'none'
  62. })
  63. } else {
  64. this.passwordError = false;
  65. }
  66. },
  67. validateUsername() {
  68. this.usernameValid = this.register_username;
  69. },
  70. checkPassword() {
  71. this.passwordValid = this.register_password;
  72. },
  73. validateEmail() {
  74. // 邮箱格式校验正则表达式
  75. const emailPattern = /^[a-zA-Z0-9._%+-]/;
  76. this.emailValid = emailPattern.test(this.register_email);
  77. if (!this.emailValid) {
  78. uni.showToast({
  79. title: '邮箱格式不正确',
  80. icon: 'none'
  81. });
  82. }
  83. },
  84. onInputEmail(e) {
  85. // 确保 e 不为 undefined 并且 e.target 不为 undefined
  86. if (e && e.target) {
  87. // 当输入框内容改变时,检查是否需要显示建议
  88. // this.showSuggestions = e.detail.value.includes('@') && !e.detail.value.includes('.');
  89. if (e.detail.value) {
  90. this.showSuggestions = true;
  91. } else {
  92. this.showSuggestions = false;
  93. }
  94. }
  95. },
  96. appendSuffix(suffix) {
  97. // 当用户点击建议的后缀时,将其添加到输入框中
  98. this.register_email += suffix;
  99. this.showSuggestions = false;
  100. },
  101. async register() {
  102. // 确保在注册前密码已经验证过
  103. if (!this.emailValid) {
  104. uni.showToast({
  105. title: '请先输入邮箱',
  106. icon: 'none'
  107. });
  108. return;
  109. }
  110. if (!this.usernameValid) {
  111. uni.showToast({
  112. title: '请先输入用户名',
  113. icon: 'none'
  114. });
  115. return;
  116. }
  117. if (!this.passwordValid) {
  118. uni.showToast({
  119. title: '请先输入密码',
  120. icon: 'none'
  121. });
  122. return;
  123. }
  124. if (this.passwordError) {
  125. this.passwordError = (this.register_password !== this.register_password_sub);
  126. if (this.passwordError) {
  127. uni.showToast({
  128. title: '两次输入的密码不一致',
  129. icon: 'none'
  130. });
  131. return;
  132. }
  133. }
  134. if (!this.passwordError && this.emailValid && this.usernameValid && this.passwordValid) {
  135. // 注册操作
  136. const registerData = {
  137. username: this.register_username,
  138. password: this.register_password,
  139. email: this.register_email
  140. };
  141. // 显示加载提示
  142. uni.showLoading({
  143. title: '注册中...',
  144. mask: true // 可选,显示加载遮罩
  145. });
  146. try {
  147. // 调用注册接口
  148. const res = await uni.request({
  149. url: 'https://afanai.top:8089/v1/user/register',
  150. method: 'POST',
  151. data: registerData,
  152. header: {
  153. 'content-type': 'application/json'
  154. }
  155. });
  156. // 关闭加载提示
  157. uni.hideLoading();
  158. // 成功响应处理
  159. if (res.statusCode === 201) {
  160. // 如果状态码为200,通常表示请求成功
  161. uni.showToast({
  162. title: '注册成功',
  163. duration: 2000,
  164. icon: 'success'
  165. });
  166. } else {
  167. try {
  168. // 确保 res.data 是一个对象且包含 error 键
  169. if (res.data && typeof res.data === 'object' && 'error' in res.data) {
  170. uni.showToast({
  171. title: res.data.error,
  172. duration: 2000,
  173. icon: 'none'
  174. });
  175. }
  176. } catch (e) {
  177. // 如果尝试处理 res.data 时出错,显示默认错误信息
  178. uni.showToast({
  179. title: '处理服务器响应时出错',
  180. duration: 2000,
  181. icon: 'none'
  182. });
  183. console.error('Error processing response data:', e);
  184. }
  185. }
  186. } catch (error) {
  187. // 关闭加载提示
  188. uni.hideLoading();
  189. // 错误处理
  190. uni.showToast({
  191. title: '网络错误,请检查网络',
  192. duration: 2000,
  193. icon: 'none'
  194. });
  195. console.error('注册请求出错:', error);
  196. }
  197. }
  198. }
  199. }
  200. }
  201. </script>
  202. <style lang="scss" scoped>
  203. .page {
  204. display: flex;
  205. flex-direction: column;
  206. align-items: center;
  207. min-height: 100vh;
  208. background: #f5f5f5;
  209. .logo {
  210. padding-top: 8vh;
  211. margin-bottom: 30rpx;
  212. image {
  213. width: 280rpx;
  214. height: 280rpx;
  215. }
  216. }
  217. .input {
  218. width: 660rpx;
  219. margin-bottom: 30rpx;
  220. position: relative;
  221. input {
  222. box-sizing: border-box;
  223. width: 660rpx;
  224. height: 80rpx;
  225. border-radius: 20rpx;
  226. padding: 0 40rpx;
  227. background: #fff;
  228. font-size: 14px;
  229. }
  230. .send_button {
  231. position: absolute;
  232. right: 0;
  233. top: 0;
  234. height: 80rpx;
  235. width: 220rpx;
  236. text-align: center;
  237. line-height: 80rpx;
  238. font-size: 12px;
  239. color: #777;
  240. z-index: 99;
  241. }
  242. .suggestions {
  243. position: absolute;
  244. top: 110%; // 修改这里,从bottom改为top
  245. left: 0;
  246. right: 0;
  247. background: #fff;
  248. border: 1px solid #e1e1e1;
  249. border-radius: 20rpx;
  250. z-index: 10;
  251. }
  252. .suggestions text {
  253. display: block;
  254. padding: 10px;
  255. cursor: pointer;
  256. }
  257. }
  258. .underlinecontainer {
  259. display: flex;
  260. /* 使用flex布局 */
  261. justify-content: space-between;
  262. /* 使子元素一个靠左,一个靠右 */
  263. gap: 60rpx;
  264. /* 在所有子元素之间添加间距 */
  265. .buttonleft {
  266. margin-bottom: 30rpx;
  267. button {
  268. width: 200rpx;
  269. height: 80rpx;
  270. line-height: 80rpx;
  271. border-radius: 20rpx;
  272. background: #0068B7;
  273. color: #fff;
  274. font-size: 14px;
  275. &:active {
  276. background: #005694;
  277. }
  278. }
  279. }
  280. .buttonright {
  281. margin-bottom: 30rpx;
  282. button {
  283. width: 400rpx;
  284. height: 80rpx;
  285. line-height: 80rpx;
  286. border-radius: 20rpx;
  287. background: #f0ad4e;
  288. color: #fff;
  289. font-size: 14px;
  290. &:active {
  291. background: #f09b2b;
  292. }
  293. }
  294. }
  295. }
  296. }
  297. </style>