register.vue 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <template>
  2. <view class="container">
  3. <view class="header">
  4. <img src="@/static/logo.png" class="logo" />
  5. </view>
  6. <view class="login-form-wrap">
  7. <u--form
  8. labelPosition="left"
  9. :model="loginForm"
  10. :rules="rules"
  11. ref="loginForm"
  12. >
  13. <u-form-item prop="mobile" borderBottom>
  14. <u-input
  15. v-model="loginForm.mobile"
  16. maxlength="11"
  17. border="none"
  18. focus
  19. placeholder="请输入手机号"
  20. @focus="showKeyboard"
  21. >
  22. <template slot="prefix">
  23. <view class="phone-prefix">
  24. <text>+86</text>
  25. <!-- <u-icon name="arrow-down-fill"></u-icon> -->
  26. </view>
  27. </template>
  28. </u-input>
  29. </u-form-item>
  30. <u-form-item prop="captcha" borderBottom label="密码">
  31. <u-input
  32. v-model="loginForm.captcha"
  33. maxlength="6"
  34. border="none"
  35. placeholder="请输入密码"
  36. >
  37. </u-input>
  38. </u-form-item>
  39. <u-form-item prop="captcha" borderBottom>
  40. <u-input
  41. v-model="loginForm.captcha"
  42. maxlength="6"
  43. border="none"
  44. placeholder="请输入短信验证码"
  45. >
  46. <template slot="suffix">
  47. <u-code
  48. ref="uCode"
  49. :seconds="seconds"
  50. changeText="X秒后重新获取"
  51. end-text="重新获取"
  52. start-text="获取验证码"
  53. @change="codeChange"
  54. :keep-running="true"
  55. ></u-code>
  56. <u--text :text="tips" @tap="getCode" type="primary"></u--text>
  57. </template>
  58. </u-input>
  59. </u-form-item>
  60. <view class="phone-question" @click="noPhone">手机号无法使用?</view>
  61. <u-form-item>
  62. <u-button
  63. type="primary"
  64. :disabled="isDisabled"
  65. :loading="loading"
  66. shape="circle"
  67. @click="login"
  68. >注册</u-button
  69. >
  70. </u-form-item>
  71. </u--form>
  72. <view class="user-agreement">
  73. <u-checkbox-group style="margin-top: 17rpx">
  74. <u-checkbox
  75. shape="circle"
  76. @change="checkboxChange"
  77. :checked="isChecked"
  78. ></u-checkbox>
  79. </u-checkbox-group>
  80. <text class="user-agreement-text">
  81. 我已阅读并同意
  82. <text class="terms">《用户协议》</text>
  83. <text class="terms">《隐私政策》</text>
  84. </text>
  85. </view>
  86. </view>
  87. <view>
  88. <u-popup :show="show" @close="close" mode="center">
  89. <view
  90. style="
  91. height: 100rpx;
  92. width: 700rpx;
  93. align-items: center;
  94. display: flex;
  95. justify-content: center;
  96. "
  97. >
  98. <text style="color: gray; font-size: 14px">我已阅读并同意</text>
  99. <text style="font-weight: bold">《用户协议》</text>
  100. <text style="font-weight: bold">《隐私政策》</text>
  101. </view>
  102. <u-button @click="confirmShow" type="primary" :loading="loading"
  103. >确认</u-button
  104. >
  105. </u-popup>
  106. </view>
  107. </view>
  108. </template>
  109. <script>
  110. import { getSmsCodeByPhone, loginByPhoneAndSmsCode } from '@/api/login';
  111. export default {
  112. data() {
  113. return {
  114. loginForm: {
  115. client_id: 'chelvc_client',
  116. client_secret: 'qWBe6jD%GCuPPTkP',
  117. grant_type: 'sms',
  118. token: '',
  119. mobile: '18888888888',
  120. captcha: '363636',
  121. },
  122. rules: {
  123. mobile: [
  124. {
  125. required: true,
  126. trigger: ['change', 'blur'],
  127. message: '请输入您的手机号',
  128. },
  129. {
  130. validator: (rule, value, cb) => {
  131. return uni.$u.test.mobile(value);
  132. },
  133. message: '手机号码格式不正确',
  134. trigger: ['change', 'blur'],
  135. },
  136. ],
  137. captcha: [
  138. {
  139. required: true,
  140. trigger: ['change', 'blur'],
  141. message: '请输入六位数验证码',
  142. },
  143. ],
  144. },
  145. seconds: 120,
  146. tips: '',
  147. isChecked: true,
  148. show: false,
  149. loading: false,
  150. };
  151. },
  152. onReady() {
  153. this.$refs.loginForm.setRules(this.rules);
  154. },
  155. computed: {
  156. isDisabled() {
  157. return !(this.loginForm.mobile && this.loginForm.captcha.length === 6);
  158. },
  159. },
  160. methods: {
  161. getCode() {
  162. if (!this.loginForm.mobile) return uni.$u.toast('请输入您的手机号!');
  163. if (!uni.$u.test.mobile(this.loginForm.mobile))
  164. return uni.$u.toast('手机号码格式不正确!');
  165. // TODO: 处理验证码倒计时
  166. if (this.$refs.uCode.canGetCode) {
  167. getSmsCodeByPhone(this.loginForm.mobile)
  168. .then(res => {
  169. if (res.code === 200) {
  170. uni.$u.toast('验证码已发送');
  171. this.loginForm.token = res.data.token;
  172. this.$refs.uCode.start();
  173. }
  174. })
  175. .catch(err => {
  176. uni.$u.toast('无法发送验证码');
  177. this.$refs.uCode.start();
  178. });
  179. }
  180. },
  181. //点击登录
  182. async login() {
  183. if (!this.isChecked) {
  184. this.show = true;
  185. return;
  186. }
  187. this.loading = true;
  188. loginByPhoneAndSmsCode(this.loginForm)
  189. .then(res => {
  190. this.loading = false;
  191. if (res.access_token) {
  192. uni.$u.toast('登录成功');
  193. this.$store.commit('SET_ACCESS_TOKEN', res.access_token);
  194. this.$store.commit('SET_REFRESH_TOKEN', res.refresh_token);
  195. setTimeout(() => {
  196. this.$Router.pushTab('/pages/tabbar/home/index');
  197. }, 1500);
  198. }
  199. })
  200. .catch(err => {
  201. this.loading = false;
  202. uni.$u.toast('登录失败-' + err.data.error_description);
  203. });
  204. },
  205. //点击注册
  206. register() {
  207. this.$Router.pushTab('/pages/login/register');
  208. },
  209. codeChange(text) {
  210. this.tips = text;
  211. },
  212. checkboxChange(v) {
  213. this.isChecked = v;
  214. },
  215. //自动调出键盘
  216. showKeyboard() {
  217. return;
  218. if (this.$q.platform.is.cordova) {
  219. cordova.plugins.Keyboard.show();
  220. }
  221. },
  222. //点击手机号无法使用?
  223. noPhone() {
  224. this.$message('要跳转到解决方案哦 还没写');
  225. },
  226. //点击弹框确认
  227. confirmShow() {
  228. this.isChecked = true;
  229. this.show = false;
  230. setTimeout(() => {
  231. this.login();
  232. }, 250);
  233. },
  234. //关闭弹框f
  235. close() {
  236. this.show = false;
  237. },
  238. },
  239. };
  240. </script>
  241. <style scoped lang="scss">
  242. .container {
  243. padding: 0 40rpx;
  244. margin: 0 auto;
  245. text-align: center; /* 设置容器内文本水平居中 */
  246. }
  247. .header {
  248. width: 100%; /* 头部宽度占据整个屏幕 */
  249. max-width: 700px; /* 设置最大宽度 */
  250. margin: 20px auto; /* 上下居中 */
  251. }
  252. .logo {
  253. display: block;
  254. margin: 0 auto; /* 实现水平居中对齐 */
  255. }
  256. .login-form-wrap {
  257. margin-top: 200rpx;
  258. .phone-prefix {
  259. display: flex;
  260. align-items: center;
  261. padding-right: 20rpx;
  262. border-right: 2rpx solid $uni-text-color-grey;
  263. margin-right: 20rpx;
  264. }
  265. .phone-question {
  266. text-align: right;
  267. font-size: 24rpx;
  268. padding: 20rpx 0 20rpx 0;
  269. margin-right: -22rpx;
  270. }
  271. .user-agreement {
  272. display: flex;
  273. padding: 0 10rpx;
  274. .user-agreement-text {
  275. font-size: 26rpx;
  276. color: $uni-text-color-grey;
  277. margin-top: 20rpx;
  278. margin-left: 20rpx;
  279. .terms {
  280. color: $uni-text-color;
  281. font-weight: 400;
  282. }
  283. }
  284. }
  285. }
  286. </style>