index.tsx 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. import request from '@/helpers/request'
  2. import {
  3. ElButton,
  4. ElForm,
  5. ElFormItem,
  6. ElInput,
  7. ElLink,
  8. ElMessage
  9. } from 'element-plus'
  10. import { defineComponent, nextTick } from 'vue'
  11. import Cookies from 'js-cookie'
  12. import ImgCode from '../img-code'
  13. import styles from './index.module.less'
  14. import { setAuth } from '@/helpers/utils'
  15. import { checkPhone } from '@/helpers/validate'
  16. import iconSuccess from '@/common/images/icon_success.png'
  17. // const validatePassword = (
  18. // rule: any,
  19. // value: string | any[],
  20. // callback: () => void
  21. // ) => {
  22. // if (value.length < 6) {
  23. // // @ts-ignore
  24. // callback(new Error('密码必须大于六位'))
  25. // } else {
  26. // callback()
  27. // }
  28. // }
  29. export default defineComponent({
  30. name: 'loginForm',
  31. props: {
  32. type: {
  33. type: String,
  34. default: 'login' as 'login' | 'register'
  35. },
  36. loginType: {
  37. type: String,
  38. default: 'TEACHER' as 'TEACHER' | 'STUDENT'
  39. },
  40. onClose: {
  41. type: Function,
  42. default: () => {}
  43. },
  44. onChange: {
  45. type: Function,
  46. default: (type: string) => {}
  47. },
  48. onLoginTypeChange: {
  49. type: Function,
  50. default: (type: string) => {}
  51. }
  52. },
  53. data() {
  54. return {
  55. loading: false,
  56. codeDsiable: false,
  57. codeStatus: false, // 是否显示图形验证码
  58. codeTimer: 120, // 发短信时长
  59. codeInverval: null as any,
  60. form: {
  61. username: '',
  62. code: ''
  63. // password: ''
  64. },
  65. formRules: {
  66. username: [
  67. { required: true, message: '请输入手机号', trigger: 'blur' },
  68. {
  69. pattern: /^1[3456789]\d{9}$/,
  70. message: '请输入正确的手机号',
  71. trigger: 'blur'
  72. }
  73. ],
  74. code: [{ required: true, message: '请输入验证码', trigger: 'blur' }]
  75. // password: [
  76. // { required: true, trigger: 'blur', validator: validatePassword }
  77. // ]
  78. }
  79. }
  80. },
  81. unmounted() {
  82. console.log('form unmounted')
  83. clearInterval(this.codeInverval)
  84. },
  85. methods: {
  86. onSubmit() {
  87. ;(this as any).$refs.loginForm.validate(async (valid: boolean) => {
  88. if (valid) {
  89. if (this.type === 'login') {
  90. const params = {
  91. isSurportRegister: false,
  92. loginUserType: this.loginType
  93. }
  94. await this.onLogin(params)
  95. } else if (this.type === 'register') {
  96. const params = {
  97. isSurportRegister: true,
  98. loginUserType: this.loginType
  99. }
  100. await this.onLogin(params)
  101. }
  102. }
  103. })
  104. },
  105. async onLogin(params: any) {
  106. this.loading = true
  107. try {
  108. const form = this.form
  109. const res = await request.post('/api-auth/smsLogin', {
  110. requestType: 'form',
  111. data: {
  112. clientId: 'website',
  113. clientSecret: 'website',
  114. phone: form.username,
  115. smsCode: form.code,
  116. ...params
  117. }
  118. })
  119. const { authentication } = res.data
  120. const token =
  121. authentication.token_type + ' ' + authentication.access_token
  122. setAuth(
  123. JSON.stringify({
  124. token,
  125. loginUserType: params.loginUserType
  126. })
  127. )
  128. // console.log(res, 'res')
  129. if (this.type === 'login') {
  130. window.location.reload()
  131. this.onClose()
  132. } else if (this.type === 'register') {
  133. this.onChange('register-success')
  134. }
  135. } catch (e: any) {
  136. console.log(e)
  137. }
  138. this.loading = false
  139. },
  140. onResetFields() {
  141. ;(this as any).$refs.loginForm.resetFields()
  142. }
  143. },
  144. render() {
  145. return (
  146. <ElForm
  147. ref="loginForm"
  148. model={this.form}
  149. rules={this.formRules}
  150. class={[styles.formLogin, 'relative']}
  151. >
  152. <ElFormItem prop="username">
  153. <ElInput
  154. v-model={this.form.username}
  155. placeholder="请输入您的手机号码"
  156. // @ts-ignore
  157. maxlength={11}
  158. autocomplete={'off'}
  159. ></ElInput>
  160. </ElFormItem>
  161. <ElFormItem prop="code">
  162. <ElInput
  163. v-model={this.form.code}
  164. // @ts-ignore
  165. maxlength={6}
  166. minlength={6}
  167. placeholder="请输入验证码"
  168. v-slots={{
  169. suffix: () => (
  170. <div
  171. class={
  172. 'before:border-l before:border-l-[#E5E5E5] before:h-[18px] before:mr-3'
  173. }
  174. >
  175. <ElLink
  176. disabled={this.codeDsiable}
  177. class={styles.codeStyles}
  178. type="primary"
  179. underline={false}
  180. onClick={() => {
  181. if (!checkPhone(this.form.username)) {
  182. return ElMessage.error('请输入正确的手机号码')
  183. }
  184. this.codeStatus = true
  185. }}
  186. >
  187. {!this.codeDsiable ? '获取验证码' : this.codeTimer + 's'}
  188. </ElLink>
  189. </div>
  190. )
  191. }}
  192. ></ElInput>
  193. </ElFormItem>
  194. {/* {(this.type === 'teacher-register' ||
  195. this.type === 'student-register') && (
  196. <ElFormItem prop="password">
  197. <ElInput
  198. v-model={this.form.password}
  199. placeholder="请输入您的登录密码"
  200. type="password"
  201. autocomplete={'off'}
  202. ></ElInput>
  203. </ElFormItem>
  204. )} */}
  205. {this.type === 'login' ? (
  206. <div class="w-full flex justify-center mb-9 pt-6">
  207. <div
  208. class="flex items-center pr-6 cursor-pointer"
  209. onClick={() => {
  210. this.onLoginTypeChange('TEACHER')
  211. }}
  212. >
  213. {this.loginType === 'TEACHER' ? (
  214. <img src={iconSuccess} class="w-5 h-5 mr-2.5" />
  215. ) : (
  216. <div class="w-5 h-5 rounded-full box-border border border-[#CCCCCC] mr-2.5"></div>
  217. )}
  218. 老师登录
  219. </div>
  220. <div
  221. class="flex items-center cursor-pointer"
  222. onClick={() => {
  223. this.onLoginTypeChange('STUDENT')
  224. }}
  225. >
  226. {this.loginType === 'STUDENT' ? (
  227. <img src={iconSuccess} class="w-5 h-5 mr-2.5" />
  228. ) : (
  229. <div class="w-5 h-5 rounded-full box-border border border-[#CCCCCC] mr-2.5"></div>
  230. )}
  231. 学生登录
  232. </div>
  233. </div>
  234. ) : (
  235. <div class="h-12"></div>
  236. )}
  237. <ElFormItem>
  238. <ElButton
  239. type="primary"
  240. class={styles.btnStyles}
  241. onClick={this.onSubmit}
  242. disabled={this.loading}
  243. loading={this.loading}
  244. style={{ height: '48px', fontSize: '16px' }}
  245. round
  246. >
  247. {this.type === 'teacher-login' || this.type === 'student-login'
  248. ? '登 录'
  249. : '注 册'}
  250. </ElButton>
  251. </ElFormItem>
  252. {/* 图形验证码 */}
  253. {this.codeStatus && (
  254. <ImgCode
  255. phone={this.form.username}
  256. onClose={() => {
  257. this.codeStatus = false
  258. }}
  259. onSendCode={async () => {
  260. this.codeDsiable = true
  261. this.codeInverval = setInterval(() => {
  262. this.codeTimer--
  263. if (this.codeTimer === 0) {
  264. this.codeDsiable = false
  265. clearInterval(this.codeInverval)
  266. this.codeTimer = 120
  267. }
  268. }, 1000)
  269. }}
  270. />
  271. )}
  272. </ElForm>
  273. )
  274. }
  275. })