Login.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. <template>
  2. <div class="main">
  3. <a-form
  4. id="formLogin"
  5. class="user-layout-login"
  6. ref="formLogin"
  7. :form="form"
  8. @submit="handleSubmit"
  9. >
  10. <a-tabs
  11. :activeKey="customActiveKey"
  12. :tabBarStyle="{ textAlign: 'center', borderBottom: 'unset' }"
  13. @change="handleTabClick"
  14. >
  15. <a-tab-pane key="tab1" tab="用户登录">
  16. <!-- <a-alert v-if="isLoginError" type="error" style="margin-bottom: 24px;" message="账户或密码错误" /> -->
  17. <a-form-item>
  18. <a-input
  19. size="large"
  20. type="text"
  21. placeholder="账户 "
  22. v-decorator="[
  23. 'username',
  24. {initialValue: username ,rules: [{ required: true, message: '请输入帐户名' }, { validator: handleUsernameOrEmail }], validateTrigger: 'change'}
  25. ]"
  26. >
  27. <a-icon slot="prefix" type="user" :style="{ color: 'rgba(0,0,0,.25)' }"/>
  28. </a-input>
  29. </a-form-item>
  30. <a-form-item>
  31. <a-input
  32. size="large"
  33. type="password"
  34. autocomplete="false"
  35. placeholder="密码"
  36. v-decorator="[
  37. 'password',
  38. {initialValue: password ,rules: [{ required: true, message: '请输入密码' }], validateTrigger: 'blur'}
  39. ]"
  40. >
  41. <a-icon slot="prefix" type="lock" :style="{ color: 'rgba(0,0,0,.25)' }"/>
  42. </a-input>
  43. </a-form-item>
  44. </a-tab-pane>
  45. <!-- <a-tab-pane key="tab2" tab="手机号登录">
  46. <a-form-item>
  47. <a-input size="large" type="text" placeholder="手机号" v-decorator="['mobile', {rules: [{ required: true, pattern: /^1[34578]\d{9}$/, message: '请输入正确的手机号' }], validateTrigger: 'change'}]">
  48. <a-icon slot="prefix" type="mobile" :style="{ color: 'rgba(0,0,0,.25)' }"/>
  49. </a-input>
  50. </a-form-item>
  51. <a-row :gutter="16">
  52. <a-col class="gutter-row" :span="16">
  53. <a-form-item>
  54. <a-input size="large" type="text" placeholder="验证码" v-decorator="['captcha', {rules: [{ required: true, message: '请输入验证码' }], validateTrigger: 'blur'}]">
  55. <a-icon slot="prefix" type="mail" :style="{ color: 'rgba(0,0,0,.25)' }"/>
  56. </a-input>
  57. </a-form-item>
  58. </a-col>
  59. <a-col class="gutter-row" :span="8">
  60. <a-button
  61. class="getCaptcha"
  62. tabindex="-1"
  63. :disabled="state.smsSendBtn"
  64. @click.stop.prevent="getCaptcha"
  65. v-text="!state.smsSendBtn && '获取验证码' || (state.time+' s')"
  66. ></a-button>
  67. </a-col>
  68. </a-row>
  69. </a-tab-pane> -->
  70. </a-tabs>
  71. <a-form-item>
  72. <a-checkbox style="color: #ccc;" @change="rememberMe=!rememberMe" :checked="rememberMe" v-decorator="['rememberMe',{initialValue: rememberMe}]">记住密码</a-checkbox>
  73. <!-- <router-link
  74. :to="{ name: 'recover', params: { user: 'aaa'} }"
  75. class="forge-password"
  76. style="float: right;"
  77. >忘记密码</router-link> -->
  78. </a-form-item>
  79. <a-form-item style="margin-top:24px">
  80. <a-button
  81. size="large"
  82. type="primary"
  83. htmlType="submit"
  84. class="login-button"
  85. :loading="state.loginBtn"
  86. :disabled="state.loginBtn"
  87. >确定</a-button>
  88. </a-form-item>
  89. <!-- <div class="user-login-other">
  90. <span>其他登录方式</span>
  91. <a>
  92. <a-icon class="item-icon" type="alipay-circle"></a-icon>
  93. </a>
  94. <a>
  95. <a-icon class="item-icon" type="taobao-circle"></a-icon>
  96. </a>
  97. <a>
  98. <a-icon class="item-icon" type="weibo-circle"></a-icon>
  99. </a>
  100. <router-link class="register" :to="{ name: 'register' }">注册账户</router-link>
  101. </div> -->
  102. </a-form>
  103. <two-step-captcha
  104. v-if="requiredTwoStepCaptcha"
  105. :visible="stepCaptchaVisible"
  106. @success="stepCaptchaSuccess"
  107. @cancel="stepCaptchaCancel"
  108. ></two-step-captcha>
  109. <!-- ElectronApp 更新 -->
  110. <a-modal
  111. title="正在更新新版本,请稍候..."
  112. width="60%"
  113. :visible="dialogVisible"
  114. :closable="false"
  115. :maskClosable="false"
  116. centered
  117. :footer="null"
  118. >
  119. <div style="width:100%;height:20vh;line-height:20vh;text-align:center">
  120. <a-progress
  121. status="success"
  122. :strokeWidth="20"
  123. :percent="percentage"
  124. :width="strokeWidth"
  125. ></a-progress>
  126. </div>
  127. </a-modal>
  128. </div>
  129. </template>
  130. <script>
  131. import md5 from 'md5'
  132. import TwoStepCaptcha from '@/components/tools/TwoStepCaptcha'
  133. import { mapActions } from 'vuex'
  134. import { timeFix } from '@/utils/util'
  135. import { getSmsCaptcha, get2step } from '@/api/login'
  136. import { updateTheme } from '@/components/SettingDrawer/settingConfig'
  137. export default {
  138. components: {
  139. TwoStepCaptcha
  140. },
  141. data () {
  142. return {
  143. customActiveKey: 'tab1',
  144. loginBtn: false,
  145. // login type: 0 email, 1 username, 2 telephone
  146. loginType: 0,
  147. isLoginError: false,
  148. requiredTwoStepCaptcha: false,
  149. stepCaptchaVisible: false,
  150. form: this.$form.createForm(this),
  151. state: {
  152. time: 60,
  153. loginBtn: false,
  154. // login type: 0 email, 1 username, 2 telephone
  155. loginType: 0,
  156. smsSendBtn: false
  157. },
  158. rememberMe: true,
  159. username: '',
  160. password: '',
  161. // 客户端升级弹框
  162. dialogVisible: false,
  163. percentage: 0,
  164. strokeWidth: 200
  165. }
  166. },
  167. mounted () {
  168. const _this = this
  169. if (isElectronApp) {
  170. // 接收主进程版本更新消息
  171. ipcRenderer.on('message', (event, arg) => {
  172. console.log(arg)
  173. if (arg.cmd == 'update-available') {
  174. // 显示升级对话框
  175. _this.dialogVisible = true
  176. } else if (arg.cmd == 'download-progress') {
  177. console.log(arg.message.percent)
  178. const percent = Math.round(parseFloat(arg.message.percent))
  179. _this.percentage = percent
  180. } else if (arg.cmd == 'error') {
  181. _this.dialogVisible = false
  182. _this.$message.error('更新失败')
  183. } else if (arg.cmd == 'update-downloaded') {
  184. _this.dialogVisible = false
  185. _this.$info({
  186. title: '版本更新',
  187. content: '检测到新版本,是否更新安装',
  188. okText: '立即更新',
  189. onOk () {
  190. ipcRenderer.send('isUpdateNow')
  191. }
  192. })
  193. }
  194. })
  195. ipcRenderer.send('checkForUpdate')
  196. }
  197. },
  198. created () {
  199. // const userAgent = navigator.userAgent // 取得浏览器的userAgent字符串
  200. // const isIE = window.ActiveXObject || 'ActiveXObject' in window
  201. // const isIE11 = userAgent.indexOf('Trident') > -1 && userAgent.indexOf('rv:11.0') > -1
  202. // if (isIE) {
  203. // const reIE = new RegExp('MSIE (\\d+\\.\\d+);')
  204. // reIE.test(userAgent)
  205. // const fIEVersion = parseFloat(RegExp['$1'])
  206. // if (userAgent.indexOf('MSIE 6.0') != -1 || fIEVersion < 11 || isIE11) {
  207. // this.$router.push({ name: 'updateBrowser' })
  208. // }
  209. // }
  210. // get2step({ })
  211. // .then(res => {
  212. // this.requiredTwoStepCaptcha = res.result.stepCode
  213. // })
  214. // .catch(() => {
  215. // this.requiredTwoStepCaptcha = false
  216. // })
  217. // this.requiredTwoStepCaptcha = true
  218. const rem = this.$cookies.get('REMEMBER_ME')
  219. // console.log(rem)
  220. if (rem && rem.rememberMe) {
  221. this.username = rem.username
  222. this.password = rem.password
  223. this.rememberMe = rem.rememberMe
  224. }
  225. },
  226. methods: {
  227. ...mapActions(['Login', 'Logout']),
  228. // handler
  229. handleUsernameOrEmail (rule, value, callback) {
  230. const { state } = this
  231. const regex = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/
  232. if (regex.test(value)) {
  233. state.loginType = 0
  234. } else {
  235. state.loginType = 1
  236. }
  237. callback()
  238. },
  239. handleTabClick (key) {
  240. this.customActiveKey = key
  241. // this.form.resetFields()
  242. },
  243. handleSubmit (e) {
  244. e.preventDefault()
  245. const {
  246. form: { validateFields },
  247. state,
  248. customActiveKey,
  249. Login
  250. } = this
  251. state.loginBtn = true
  252. const validateFieldsKey = customActiveKey === 'tab1' ? ['username', 'password', 'rememberMe'] : ['mobile', 'captcha']
  253. validateFields(validateFieldsKey, { force: true }, (err, values) => {
  254. if (!err) {
  255. const loginParams = { ...values }
  256. delete loginParams.username
  257. loginParams[!state.loginType ? 'email' : 'username'] = values.username
  258. loginParams.password = values.password
  259. // console.log(loginParams, 'loginParams')
  260. Login(loginParams)
  261. .then((res) => this.loginSuccess(res))
  262. // .catch(err => this.requestFailed(err))
  263. .finally(() => {
  264. state.loginBtn = false
  265. })
  266. } else {
  267. setTimeout(() => {
  268. state.loginBtn = false
  269. }, 600)
  270. }
  271. })
  272. },
  273. getCaptcha (e) {
  274. e.preventDefault()
  275. const { form: { validateFields }, state } = this
  276. validateFields(['mobile'], { force: true }, (err, values) => {
  277. if (!err) {
  278. state.smsSendBtn = true
  279. const interval = window.setInterval(() => {
  280. if (state.time-- <= 0) {
  281. state.time = 60
  282. state.smsSendBtn = false
  283. window.clearInterval(interval)
  284. }
  285. }, 1000)
  286. const hide = this.$message.loading('验证码发送中..', 0)
  287. getSmsCaptcha({ mobile: values.mobile }).then(res => {
  288. setTimeout(hide, 2500)
  289. this.$notification['success']({
  290. message: '提示',
  291. description: '验证码获取成功,您的验证码为:' + res.result.captcha,
  292. duration: 8
  293. })
  294. }).catch(err => {
  295. setTimeout(hide, 1)
  296. clearInterval(interval)
  297. state.time = 60
  298. state.smsSendBtn = false
  299. // this.requestFailed(err)
  300. })
  301. }
  302. })
  303. },
  304. stepCaptchaSuccess () {
  305. this.loginSuccess()
  306. },
  307. stepCaptchaCancel () {
  308. this.Logout().then(() => {
  309. this.loginBtn = false
  310. this.stepCaptchaVisible = false
  311. })
  312. },
  313. loginSuccess (res) {
  314. // console.log(res)
  315. // check res.homePage define, set $router.push name res.homePage
  316. // Why not enter onComplete
  317. this.$router.push({ name: 'home' })
  318. this.isLoginError = false
  319. },
  320. requestFailed (err) {
  321. console.log(err, 'error')
  322. this.isLoginError = true
  323. this.$notification['error']({
  324. message: '提示',
  325. description: ((err.response || {}).data || {}).message || err.message || '请求出现错误,请稍后再试',
  326. duration: 4
  327. })
  328. }
  329. }
  330. }
  331. </script>
  332. <style lang="less" scoped>
  333. .user-layout-login {
  334. label {
  335. font-size: 14px;
  336. }
  337. .getCaptcha {
  338. display: block;
  339. width: 100%;
  340. height: 40px;
  341. }
  342. .forge-password {
  343. font-size: 14px;
  344. }
  345. button.login-button {
  346. padding: 0 15px;
  347. font-size: 16px;
  348. height: 40px;
  349. width: 100%;
  350. }
  351. .user-login-other {
  352. text-align: left;
  353. margin-top: 24px;
  354. line-height: 22px;
  355. .item-icon {
  356. font-size: 24px;
  357. color: rgba(0, 0, 0, 0.2);
  358. margin-left: 16px;
  359. vertical-align: middle;
  360. cursor: pointer;
  361. transition: color 0.3s;
  362. &:hover {
  363. color: #1890ff;
  364. }
  365. }
  366. .register {
  367. float: right;
  368. }
  369. }
  370. }
  371. </style>