phoneLogin.vue 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <template>
  2. <view class="container">
  3. <u-image src="@/static/logo.png"></u-image>
  4. <view class="login-form-wrap">
  5. <u--form labelPosition="left" :model="loginForm" :rules="rules" ref="loginForm">
  6. <u-form-item prop="mobile" borderBottom label="账号">
  7. <u-input v-model="loginForm.mobile" maxlength="11" border="none" focus placeholder="请输入账号/手机号">
  8. </u-input>
  9. </u-form-item>
  10. <u-form-item prop="captcha" label="密码" borderBottom>
  11. <u-input v-model="loginForm.password" maxlength="6" border="none" placeholder="请输入密码">
  12. </u-input>
  13. </u-form-item>
  14. <u-checkbox shape="circle" @change="checkboxChange" :checked="isChecked"></u-checkbox>
  15. <view @click="forgetPassword">记住密码</view>
  16. <view class="phone-question" @click="forgetPassword">忘记密码?</view>
  17. <u-form-item>
  18. <u-button type="primary" :disabled="isDisabled" :loading="loading" shape="circle" @click="login">登录</u-button>
  19. </u-form-item>
  20. <u-form-item>
  21. <u-button :loading="loading" shape="circle" @click="register">注册</u-button>
  22. </u-form-item>
  23. </u--form>
  24. <view class="user-agreement">
  25. <u-checkbox-group>
  26. <u-checkbox shape="circle" @change="checkboxChange" :checked="isChecked"></u-checkbox>
  27. </u-checkbox-group>
  28. <text class="user-agreement-text">
  29. 我已阅读并同意
  30. <text class="terms">《用户协议》</text>
  31. <text class="terms">《隐私政策》</text>
  32. <text class="terms">《儿童/青少年个人信息保护规则》</text>
  33. </text>
  34. </view>
  35. </view>
  36. <view>
  37. <u-popup :show="show" @close="close" mode="center">
  38. <view style="
  39. height: 100rpx;
  40. width: 700rpx;
  41. align-items: center;
  42. display: flex;
  43. justify-content: center;
  44. ">
  45. <text style="color: gray; font-size: 14px">我已阅读并同意</text>
  46. <text style="font-weight: bold">《用户协议》</text>
  47. <text style="font-weight: bold">《隐私政策》</text>
  48. </view>
  49. <u-button @click="confirmShow" type="primary" :loading="loading">确认</u-button>
  50. </u-popup>
  51. </view>
  52. </view>
  53. </template>
  54. <script>
  55. import {
  56. getSmsCodeByPhone,
  57. loginByPhoneAndSmsCode,
  58. maintainSmsCaptcha
  59. } from '@/api/login';
  60. export default {
  61. data() {
  62. return {
  63. loginForm: {
  64. client_id: 'chelvc_client',
  65. client_secret: 'qWBe6jD%GCuPPTkP',
  66. grant_type: 'sms',
  67. token: '',
  68. mobile: '18380313545', //手机号
  69. captcha: '363636', //验证码
  70. password: '1', //密码
  71. },
  72. rules: {
  73. mobile: [{
  74. required: true,
  75. trigger: ['change', 'blur'],
  76. message: '请输入您的手机号',
  77. },
  78. {
  79. validator: (rule, value, cb) => {
  80. return uni.$u.test.mobile(value);
  81. },
  82. message: '手机号码格式不正确',
  83. trigger: ['change', 'blur'],
  84. },
  85. ],
  86. captcha: [{
  87. required: true,
  88. trigger: ['change', 'blur'],
  89. message: '请输入六位数验证码',
  90. }, ],
  91. },
  92. seconds: 120,
  93. tips: '',
  94. isChecked: true,
  95. show: false,
  96. loading: false,
  97. };
  98. },
  99. onReady() {
  100. this.$refs.loginForm.setRules(this.rules);
  101. },
  102. computed: {
  103. isDisabled() {
  104. return !(this.loginForm.mobile && this.loginForm.captcha.length === 6);
  105. },
  106. },
  107. methods: {
  108. getCode() {
  109. if (!this.loginForm.mobile) return uni.$u.toast('请输入您的手机号!');
  110. if (!uni.$u.test.mobile(this.loginForm.mobile))
  111. return uni.$u.toast('手机号码格式不正确!');
  112. // TODO: 处理验证码倒计时
  113. if (this.$refs.uCode.canGetCode) {
  114. getSmsCodeByPhone(this.loginForm.mobile)
  115. .then(res => {
  116. if (res.code === 200) {
  117. uni.$u.toast('验证码已发送');
  118. this.loginForm.token = res.data.token;
  119. this.$refs.uCode.start();
  120. }
  121. })
  122. .catch(err => {
  123. uni.$u.toast('无法发送验证码');
  124. this.$refs.uCode.start();
  125. });
  126. }
  127. },
  128. //点击登录
  129. async login() {
  130. if (!this.isChecked) {
  131. this.show = true;
  132. return;
  133. }
  134. this.loading = true;
  135. let mobile = '18380313545'
  136. maintainSmsCaptcha({
  137. mobile: mobile
  138. }).then(res => {
  139. console.log('res', res);
  140. // this.loading = false;
  141. // if (res.access_token) {
  142. // uni.$u.toast('登录成功');
  143. // this.$store.commit('SET_ACCESS_TOKEN', res.access_token);
  144. // this.$store.commit('SET_REFRESH_TOKEN', res.refresh_token);
  145. // setTimeout(() => {
  146. // this.$Router.pushTab('/pages/tabbar/home/index');
  147. // }, 1500);
  148. // }
  149. // })
  150. // .catch(err => {
  151. // this.loading = false;
  152. // uni.$u.toast('登录失败-' + err.data.error_description);
  153. // this.loading = false;
  154. // 下方登陆代码调通 token不知道放哪 不知道为什么要存两个token 上方代码没动 loginByPhoneAndSmsCode 这个是之前的接口
  155. if (res.code == 200) {
  156. uni.$u.toast('登录成功');
  157. // this.$store.commit('SET_ACCESS_TOKEN', res.data.token);
  158. // this.$store.commit('SET_REFRESH_TOKEN', res.data.token);
  159. setTimeout(() => {
  160. this.$Router.pushTab('/pages/tabbar/home/index');
  161. }, 1500);
  162. }
  163. })
  164. .catch(err => {
  165. this.loading = false;
  166. uni.$u.toast('登录失败-' + err.data.error_description);
  167. });
  168. },
  169. //点击注册
  170. register() {
  171. this.$Router.pushTab('/pages/login/register');
  172. },
  173. //点击我已阅读
  174. checkboxChange(v) {
  175. this.isChecked = v;
  176. },
  177. //点击手机号无法使用?
  178. noPhone() {
  179. this.$message('要跳转到解决方案哦 还没写');
  180. },
  181. //点击弹框确认
  182. confirmShow() {
  183. this.isChecked = true;
  184. this.show = false;
  185. setTimeout(() => {
  186. this.login();
  187. }, 250);
  188. },
  189. //关闭弹框f
  190. close() {
  191. this.show = false;
  192. },
  193. },
  194. };
  195. </script>
  196. <style scoped lang="scss">
  197. .container {
  198. padding: 0 40rpx;
  199. margin: 0 auto;
  200. }
  201. .login-form-wrap {
  202. margin-top: 200rpx;
  203. .phone-prefix {
  204. display: flex;
  205. align-items: center;
  206. padding-right: 20rpx;
  207. border-right: 2rpx solid $uni-text-color-grey;
  208. margin-right: 20rpx;
  209. }
  210. .phone-question {
  211. text-align: right;
  212. font-size: 24rpx;
  213. padding: 20rpx 0 20rpx 0;
  214. }
  215. .user-agreement {
  216. display: flex;
  217. padding: 0 10rpx;
  218. .user-agreement-text {
  219. font-size: 24rpx;
  220. color: $uni-text-color-grey;
  221. margin-top: 20rpx;
  222. margin-left: 20rpx;
  223. .terms {
  224. color: $uni-text-color;
  225. font-weight: 400;
  226. }
  227. }
  228. }
  229. }
  230. </style>