LoginBySms.vue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. <div
  10. class="mobile-prefix"
  11. @click.stop="$emit('changeParams', { isShowMobilePrefix: !viewParams.isShowMobilePrefix })">
  12. <span class="selected-value">{{ mobilePrefix }}</span>
  13. <i class="iconfont icon-xiala1"/>
  14. </div>
  15. <input
  16. v-model="mobile"
  17. :placeholder="$t('login_register.placeholder_mobile')"
  18. readonly="readonly"
  19. onfocus="javascript:this.removeAttribute('readonly');"
  20. type="text"
  21. class="hl-input">
  22. <ul
  23. v-if="viewParams.isShowMobilePrefix"
  24. class="mobile-prefix-list">
  25. <li
  26. v-for="(item, index) in mobilePrefixArr"
  27. :key="index"
  28. class="prefix-item"
  29. @click="handleSelectPrefix(item)">
  30. {{ `(${item.code}) ${item.name}` }}
  31. </li>
  32. </ul>
  33. </div>
  34. <div class="form-item">
  35. <input
  36. v-model="smsCode"
  37. :placeholder="$t('login_register.placeholder_sms_code')"
  38. readonly="readonly"
  39. onfocus="javascript:this.removeAttribute('readonly');"
  40. maxlength="6"
  41. type="text"
  42. class="hl-input">
  43. <span
  44. class="get-code"
  45. @click="getSmsCode">
  46. {{ codeText }}
  47. </span>
  48. </div>
  49. <div class="form-item">
  50. <button
  51. :loading="isLoading"
  52. class="form-button"
  53. @click="goLogin">{{ btnLoginText }}</button>
  54. </div>
  55. <div class="skip-item">
  56. <span
  57. class="skip-button"
  58. @click="$emit('handleSkipView', 'LoginByPassword')">{{ $t('login_register.login_by_password') }}</span>
  59. <span
  60. class="skip-button"
  61. @click="$emit('handleSkipView', 'FindBySms')">{{ $t('login_register.forget_password') }}</span>
  62. </div>
  63. </div>
  64. </template>
  65. <script>
  66. import { mapState } from 'vuex'
  67. import {
  68. php_api_common_address,
  69. php_api_user_verification,
  70. php_api_user_mobile_code
  71. } from '~/common/apis.js'
  72. import { myValidator } from '~/common/utils.js'
  73. const defaultPrefix = {
  74. cn: {
  75. code: '+86',
  76. id: '317',
  77. name: '中国'
  78. },
  79. en: {
  80. code: '+86',
  81. id: '317',
  82. name: 'China'
  83. }
  84. }
  85. export default {
  86. props: {
  87. viewParams: {
  88. type: Object,
  89. default: () => ({
  90. isShowMobilePrefix: false
  91. })
  92. }
  93. },
  94. data() {
  95. return {
  96. mobilePrefixObj: null,
  97. mobilePrefix: '',
  98. mobile: '',
  99. smsCode: '',
  100. codeTimer: null,
  101. codeText: this.$t('login_register.btn_get_code'),
  102. btnLoginText: this.$t('login_register.btn_login'),
  103. tipMsg: {
  104. type: 'error', // error warn
  105. msg: ''
  106. },
  107. tipMsgStatus: false,
  108. dxLoaded: false,
  109. dxStatus: false,
  110. security_code: '',
  111. isLoading: false
  112. }
  113. },
  114. computed: {
  115. ...mapState(['locale', 'mobilePrefixArr'])
  116. },
  117. watch: {
  118. dxStatus(val) {
  119. // 验证成功
  120. if (val) {
  121. this.getSmsCode()
  122. }
  123. }
  124. },
  125. created() {
  126. // 默认china
  127. this.handleSelectPrefix(defaultPrefix[this.locale] || {})
  128. if (!this.mobilePrefixArr.length) {
  129. this.loadMobilePrefixData()
  130. }
  131. },
  132. destroyed() {
  133. this.clearTimer()
  134. },
  135. methods: {
  136. showTip(data) {
  137. this.tipMsg = { ...this.tipMsg, ...data }
  138. this.tipMsgStatus = true
  139. },
  140. handleSelectPrefix({ code, id, name }) {
  141. this.mobilePrefixObj = { code, id, name }
  142. this.mobilePrefix = `(${code}) ${name}`
  143. this.$emit('changeParams', { isShowMobilePrefix: false })
  144. },
  145. async loadMobilePrefixData() {
  146. try {
  147. const { code, data } = await this.$axios.$post(php_api_common_address, {
  148. range: 'B',
  149. prefix: 1
  150. })
  151. if (code || !data.length) return
  152. const prefixsArr = data.reduce((res, cur) => res.concat(cur.list), [])
  153. this.$store.commit('SET_MOBILE_PREFIX_ARR', prefixsArr)
  154. } catch (error) {}
  155. },
  156. checkMobile() {
  157. if (!myValidator.checkMobile(this.mobile)) {
  158. this.showTip({ msg: this.$t('login_register.check_phone') })
  159. return false
  160. }
  161. return true
  162. },
  163. checkCode() {
  164. if (!myValidator.checkCode(this.smsCode)) {
  165. this.showTip({ msg: this.$t('login_register.check_sms_code') })
  166. return false
  167. }
  168. return true
  169. },
  170. bindDxEvent() {
  171. _dx.hooliCaptcha.on('verifySuccess', security_code => {
  172. this.security_code = security_code
  173. this.dxStatus = true
  174. })
  175. },
  176. getDxCode() {
  177. if (_dx) {
  178. if (!this.dxLoaded) {
  179. this.dxLoaded = true
  180. this.bindDxEvent()
  181. }
  182. _dx.hooliCaptcha.reload()
  183. _dx.hooliCaptcha.show()
  184. } else {
  185. // dx加载失败 直接跳过验证
  186. this.dxStatus = true
  187. }
  188. },
  189. async getSmsCode() {
  190. if (this.codeTimer) return
  191. if (!this.checkMobile()) return
  192. this.tipMsgStatus = false
  193. if (!this.dxStatus) {
  194. this.getDxCode()
  195. return
  196. }
  197. const params = {
  198. countryId: this.mobilePrefixObj.id,
  199. mobile: this.mobile,
  200. token: this.security_code,
  201. status: '5'
  202. }
  203. try {
  204. const { code, data, msg } = await this.$axios.$post(
  205. php_api_user_verification,
  206. params
  207. )
  208. if (!code) {
  209. this.dealCountDown()
  210. } else {
  211. this.showTip({ msg: msg || this.$t('login_register.network_wrong') })
  212. }
  213. } catch (error) {
  214. this.showTip({ msg: this.$t('login_register.network_wrong') })
  215. }
  216. },
  217. clearTimer() {
  218. if (this.codeTimer) {
  219. window.clearTimeout(this.codeTimer)
  220. this.codeTimer = null
  221. }
  222. },
  223. dealCountDown() {
  224. let times = 60
  225. let countTime = () => {
  226. if (times > 0) {
  227. this.codeText = `${times}s`
  228. times--
  229. this.codeTimer = setTimeout(countTime, 1000)
  230. } else {
  231. this.clearTimer()
  232. this.codeText = this.$t('login_register.get_code_again')
  233. this.codeBtnDisable = false
  234. this.dxStatus = false
  235. }
  236. }
  237. countTime()
  238. },
  239. async goLogin() {
  240. if (!this.checkMobile() || !this.checkCode()) return
  241. const params = {
  242. countryId: this.mobilePrefixObj.id,
  243. mobile: this.mobile,
  244. code: this.smsCode,
  245. ...this.$hooliAnalysis.userSourceParams()
  246. }
  247. this.isLoading = true
  248. try {
  249. const { code, data, msg } = await this.$axios.$post(
  250. php_api_user_mobile_code,
  251. params
  252. )
  253. if (!code) {
  254. if (!data.loginType) {
  255. const params = {
  256. bh: this.$C.BEHAVIOR.behaviorRegister
  257. }
  258. params.et =
  259. this.$store.state.analyzeObj.ec === this.$C.ENTRANCE_CODE.register
  260. ? this.$C.EVENT_TYPE.win
  261. : this.$C.EVENT_TYPE.register
  262. this.$hooliAnalysis.upload(params)
  263. }
  264. this.btnLoginText = this.$t('login_register.btn_login_success')
  265. this.$Utils.MyCookie.setCookie(
  266. 'loginUserInfo',
  267. encodeURIComponent(JSON.stringify(data)),
  268. 7,
  269. true
  270. )
  271. location.reload()
  272. } else {
  273. this.isLoading = false
  274. this.showTip({ msg: msg || this.$t('login_register.network_wrong') })
  275. }
  276. } catch (error) {
  277. this.isLoading = false
  278. this.showTip({ msg: this.$t('login_register.network_wrong') })
  279. }
  280. }
  281. }
  282. }
  283. </script>