LoginByPassword.vue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <template>
  2. <div>
  3. <p
  4. v-show="tipMsgStatus"
  5. :class="`form-msg ${tipMsg.type}`">
  6. <i :class="`iconfont ${tipMsg.type == 'error' ? 'icon-Shapex' : 'icon-all_tips'}`"/>{{ tipMsg.msg }}
  7. </p>
  8. <div class="form-item">
  9. <input
  10. v-model="account"
  11. :placeholder="$t('login_register.placeholder_account')"
  12. readonly="readonly"
  13. onfocus="javascript:this.removeAttribute('readonly');"
  14. type="text"
  15. class="hl-input"
  16. @input="handleListenAccountInput"
  17. @focus="handleListenAccountInput"
  18. @blur="handleBlurAccountInput"
  19. @keyup.enter="handleAccountInputKey('enter')"
  20. @keyup.up="handleAccountInputKey('up')"
  21. @keyup.down="handleAccountInputKey('down')">
  22. <ul
  23. v-if="emailArr.length"
  24. class="email-tip-list">
  25. <li
  26. v-for="(item,index) in emailArr"
  27. :key="index"
  28. :class="{'email-item':true,'active':item.isActive}"
  29. @mousedown="handleSelectEmail(index)">{{ item.email }}</li>
  30. </ul>
  31. </div>
  32. <div class="form-item">
  33. <input
  34. v-model="password"
  35. :type="showPassword ? 'text' : 'password'"
  36. :placeholder="$t('login_register.placeholder_password')"
  37. readonly="readonly"
  38. onfocus="javascript:this.removeAttribute('readonly');"
  39. class="hl-input">
  40. <i
  41. :class="`pwd-eye iconfont ${showPassword ? 'icon-zhengyan' : 'icon-biyan'}`"
  42. @click="showPassword = !showPassword"/>
  43. </div>
  44. <div class="form-item">
  45. <button
  46. :loading="isLoading"
  47. class="form-button"
  48. @click="goLogin">{{ btnLoginText }}</button>
  49. </div>
  50. <div class="skip-item">
  51. <span
  52. class="skip-button"
  53. @click="$emit('handleSkipView', 'LoginBySms')">{{ $t('login_register.login_by_sms') }}</span>
  54. <span
  55. class="skip-button"
  56. @click="$emit('handleSkipView', 'FindBySms')">{{ $t('login_register.forget_password') }}</span>
  57. </div>
  58. </div>
  59. </template>
  60. <script>
  61. import { php_api_user_login } from '~/common/apis.js'
  62. import { myValidator } from '~/common/utils.js'
  63. export default {
  64. data() {
  65. return {
  66. emailSuffixArr: [
  67. '@qq.com',
  68. '@gmail.com',
  69. '@163.com',
  70. '@yahoo.com',
  71. '@hotmail.com'
  72. ],
  73. emailArr: [],
  74. account: '',
  75. password: '',
  76. showPassword: false,
  77. btnLoginText: this.$t('login_register.btn_login'),
  78. tipMsg: {
  79. type: 'error',
  80. msg: ''
  81. },
  82. tipMsgStatus: false,
  83. isLoading: false
  84. }
  85. },
  86. created() {},
  87. methods: {
  88. showTip(data) {
  89. this.tipMsg = { ...this.tipMsg, ...data }
  90. this.tipMsgStatus = true
  91. },
  92. handleListenAccountInput() {
  93. if (this.account.trim().length === 0) {
  94. this.emailArr = []
  95. return false
  96. }
  97. if (this.account.indexOf('@') !== -1) {
  98. let accountA = this.account.split('@')[0]
  99. if (accountA.length > 0) {
  100. let emailArr = []
  101. this.emailSuffixArr.forEach((val, i, arr) => {
  102. let tmp = {}
  103. tmp.email = `${accountA}${val}`
  104. if (this.account === tmp.email) {
  105. tmp.isActive = true
  106. } else {
  107. tmp.isActive = false
  108. }
  109. emailArr.push(tmp)
  110. })
  111. this.emailArr = emailArr
  112. } else {
  113. this.emailArr = []
  114. }
  115. } else {
  116. let reg = /^\d+$/
  117. if (reg.test(this.account.trim())) {
  118. this.emailArr = []
  119. } else {
  120. let emailArr = []
  121. this.emailSuffixArr.forEach((val, i, arr) => {
  122. let tmp = {
  123. email: `${this.account}${val}`,
  124. isActive: false
  125. }
  126. emailArr.push(tmp)
  127. })
  128. this.emailArr = emailArr
  129. }
  130. }
  131. },
  132. handleBlurAccountInput() {
  133. this.emailArr = []
  134. },
  135. handleAccountInputKey(which) {
  136. switch (which) {
  137. case 'enter':
  138. this.emailArr.some((val, i, arr) => {
  139. if (val.isActive) {
  140. this.account = val.email
  141. return true
  142. }
  143. })
  144. this.emailArr = []
  145. break
  146. case 'up':
  147. this.keySelectEmail(which)
  148. break
  149. case 'down':
  150. this.keySelectEmail(which)
  151. break
  152. default:
  153. break
  154. }
  155. },
  156. handleSelectEmail(index) {
  157. this.account = this.emailArr[index].email
  158. this.emailArr = []
  159. },
  160. keySelectEmail(which) {
  161. let count = this.emailArr.length
  162. if (count === 0) {
  163. return false
  164. }
  165. let last = count - 1
  166. let curIndex = -1
  167. this.emailArr.some((val, i, arr) => {
  168. if (val.isActive) {
  169. curIndex = i
  170. return true
  171. }
  172. })
  173. if (which === 'up') {
  174. if (curIndex === -1) {
  175. curIndex = 0
  176. } else if (curIndex === 0) {
  177. curIndex = last
  178. } else {
  179. --curIndex
  180. }
  181. } else if (which === 'down') {
  182. if (curIndex === -1) {
  183. curIndex = 0
  184. } else if (curIndex === last) {
  185. curIndex = 0
  186. } else {
  187. ++curIndex
  188. }
  189. }
  190. let emailArr = []
  191. this.emailArr.forEach((val, i, arr) => {
  192. if (curIndex === i) {
  193. val.isActive = true
  194. } else {
  195. val.isActive = false
  196. }
  197. emailArr.push({ ...val })
  198. })
  199. this.emailArr = emailArr
  200. },
  201. async goLogin() {
  202. if (
  203. !myValidator.checkMobile(this.account) &&
  204. !myValidator.checkEmail(this.account)
  205. ) {
  206. this.showTip({ msg: this.$t('login_register.check_account') })
  207. return false
  208. }
  209. if (this.password.trim().length < 3) {
  210. this.showTip({ msg: this.$t('login_register.check_pwd') })
  211. return false
  212. }
  213. this.tipMsgStatus = false
  214. const params = {
  215. string: this.account,
  216. password: this.password
  217. }
  218. this.isLoading = true
  219. try {
  220. const { code, data, msg } = await this.$axios.$post(
  221. php_api_user_login,
  222. params
  223. )
  224. if (!code) {
  225. if (data && data.status === 1) {
  226. // 密码异常
  227. this.$emit('handleSkipView', 'PasswordAbnormal')
  228. return false
  229. }
  230. this.btnLoginText = this.$t('login_register.btn_login_success')
  231. this.$Utils.MyCookie.setCookie(
  232. 'loginUserInfo',
  233. encodeURIComponent(JSON.stringify(data.userInfo)),
  234. 7,
  235. true
  236. )
  237. location.reload()
  238. } else {
  239. this.isLoading = false
  240. this.showTip({ msg: msg || this.$t('login_register.network_wrong') })
  241. }
  242. } catch (error) {
  243. this.isLoading = false
  244. this.showTip({ msg: this.$t('login_register.network_wrong') })
  245. }
  246. }
  247. }
  248. }
  249. </script>
  250. <style lang="less">
  251. .form-item {
  252. .pwd-eye {
  253. right: 15px;
  254. color: #999;
  255. font-size: 20px;
  256. padding: 5px;
  257. cursor: pointer;
  258. }
  259. }
  260. </style>