FindBySms.vue 7.7 KB

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