VerifyEmail.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <template>
  2. <div class="VerifyEmail">
  3. <div class="label">
  4. {{ label }}
  5. </div>
  6. <div class="contents">
  7. <q-input v-model="textInput"
  8. :label="label"
  9. :disable="disable"
  10. :loading="loading"
  11. :class="{'verified': state === 'verified'}"
  12. class="VerifyEmail-input" />
  13. <q-btn v-if="state === 'input-email-address'"
  14. label="دریافت کد"
  15. outline
  16. color="primary"
  17. class="VerifyEmail-btn"
  18. :loading="loading"
  19. @click="sendOtp" />
  20. <q-btn v-if="state === 'send-verify-number'"
  21. label="تایید"
  22. color="primary"
  23. class="VerifyEmail-btn"
  24. :loading="loading"
  25. @click="verify" />
  26. <div class="VerifyEmail-message">
  27. {{message}}
  28. <svg v-if="state === 'send-verify-number'"
  29. xmlns="http://www.w3.org/2000/svg"
  30. width="19.5"
  31. height="18.94"
  32. viewBox="0 0 19.5 18.94"
  33. @click="updateEmailAddress">
  34. <g id="Edit"
  35. transform="translate(0)">
  36. <path id="Combined_Shape"
  37. data-name="Combined Shape"
  38. d="M11.5,18.94a.75.75,0,0,1,0-1.5h7.253a.75.75,0,0,1,0,1.5Zm-9.884,0a.749.749,0,0,1-.731-.577L.064,14.891a2.373,2.373,0,0,1,.461-2.037L9.944,1.073l.007-.009,0,0A3.006,3.006,0,0,1,14.116.653l1.7,1.326.018.014A2.8,2.8,0,0,1,17.1,3.767a2.87,2.87,0,0,1-.368,2.2c-.018.028-.036.055-1.865,2.347a.747.747,0,0,1-.131.164c-1.366,1.712-3.677,4.6-7.59,9.5a2.445,2.445,0,0,1-1.886.914l-3.639.046ZM1.7,13.792a.877.877,0,0,0-.171.755l.68,2.884,3.039-.038a.944.944,0,0,0,.733-.352c1.933-2.418,5.04-6.306,7.178-8.982L8.9,4.788Zm12.4-6.9c.82-1.027,1.36-1.7,1.41-1.769a1.351,1.351,0,0,0,.141-1,1.406,1.406,0,0,0-.652-.887c-.059-.04-1.265-.976-1.779-1.375l-.029-.022a1.512,1.512,0,0,0-.942-.331,1.473,1.473,0,0,0-1.13.519l-1.279,1.6Z"
  39. fill="#475f4a" />
  40. </g>
  41. </svg>
  42. </div>
  43. <div v-if="state === 'send-verify-number'"
  44. class="VerifyEmail-timer">
  45. {{formatedTimer}}
  46. </div>
  47. <div v-if="state === 'verified'"
  48. class="VerifyEmail-verified-mark">
  49. <svg xmlns="http://www.w3.org/2000/svg"
  50. width="24"
  51. height="23.999"
  52. viewBox="0 0 24 23.999">
  53. <g id="check_circle-24px_1_"
  54. data-name="check_circle-24px (1)"
  55. transform="translate(0 0)">
  56. <path id="Path_797"
  57. data-name="Path 797"
  58. d="M0,0H24V24H0Z"
  59. transform="translate(0 0)"
  60. fill="none" />
  61. <path id="Path_798"
  62. data-name="Path 798"
  63. d="M12,2A10,10,0,1,0,22,12,10,10,0,0,0,12,2ZM9.29,16.289,5.7,12.7a1,1,0,0,1,1.41-1.41L10,14.169l6.88-6.879a1,1,0,0,1,1.41,1.41L10.7,16.289a1,1,0,0,1-1.41,0Z"
  64. transform="translate(0.001 0)"
  65. fill="#10d078" />
  66. </g>
  67. </svg>
  68. </div>
  69. </div>
  70. </div>
  71. </template>
  72. <script>
  73. import { APIGateway } from 'src/api/APIGateway.js'
  74. export default {
  75. name: 'VerifyEmail',
  76. props: {
  77. value: {
  78. default: null,
  79. type: [String, Number]
  80. },
  81. btnClass: {
  82. default: '',
  83. type: String
  84. },
  85. color: {
  86. default: 'primary',
  87. type: String
  88. },
  89. atClick: {
  90. type: Function,
  91. default: () => {}
  92. }
  93. },
  94. emits: ['update:value', 'input', 'change'],
  95. data () {
  96. return {
  97. state: 'input-email-address',
  98. textInput: null,
  99. label: 'ایمیل',
  100. loading: false,
  101. emailAddress: null,
  102. inputData: null,
  103. otpInterval: null,
  104. otpValue: null,
  105. message: '',
  106. otpTimer: 120,
  107. disable: false
  108. }
  109. },
  110. computed: {
  111. formatedTimer () {
  112. const second = this.otpTimer % 60
  113. const minute = Math.floor(this.otpTimer / 60)
  114. return minute + ':' + second
  115. }
  116. },
  117. watch: {
  118. value () {
  119. this.inputData = this.value
  120. this.updateState()
  121. }
  122. },
  123. created () {
  124. this.inputData = this.value
  125. this.updateState()
  126. },
  127. methods: {
  128. updateState () {
  129. if (this.inputData !== null && this.inputData.length > 0) {
  130. this.disable = true
  131. this.textInput = this.inputData
  132. this.setState('verified')
  133. return
  134. }
  135. this.emailAddress = this.inputData
  136. },
  137. setState (newState) {
  138. this.state = newState
  139. if (newState === 'input-email-address') {
  140. this.label = 'ایمیل'
  141. } else if (newState === 'send-verify-number') {
  142. this.label = 'کد تایید را وارد کنید'
  143. } else if (newState === 'verified') {
  144. this.label = 'ایمیل'
  145. }
  146. },
  147. sendOtp () {
  148. if (!this.textInput) {
  149. this.$q.notify({
  150. type: 'negative',
  151. message: 'ایمیل خود را وارد کنید.'
  152. })
  153. return
  154. }
  155. this.emailAddress = this.textInput
  156. this.loading = true
  157. APIGateway.auth.sendOtpVerify({ input: this.emailAddress })
  158. .then(() => {
  159. this.loading = false
  160. this.message = 'کد تایید به ایمیل ' + this.emailAddress + ' ارسال شد.'
  161. this.textInput = ''
  162. this.restartOtpInterval()
  163. this.setState('send-verify-number')
  164. })
  165. .catch(() => {
  166. this.loading = false
  167. this.updateEmailAddress()
  168. })
  169. },
  170. updateEmailAddress () {
  171. this.message = ''
  172. this.setState('input-email-address')
  173. },
  174. verify () {
  175. this.loading = true
  176. this.message = ''
  177. this.otpValue = this.textInput
  178. this.textInput = ''
  179. APIGateway.auth.verify({
  180. input: this.emailAddress,
  181. otp: this.otpValue
  182. })
  183. .then(() => {
  184. this.loading = false
  185. this.disable = true
  186. this.setState('verified')
  187. this.change(this.emailAddress)
  188. })
  189. .catch(() => {
  190. this.loading = false
  191. this.updateEmailAddress()
  192. })
  193. },
  194. restartOtpInterval () {
  195. this.clearOtpInterval()
  196. this.otpInterval = setInterval(() => {
  197. if (this.otpTimer === 0) {
  198. clearInterval(this.otpInterval)
  199. } else {
  200. this.otpTimer--
  201. }
  202. }, 1000)
  203. },
  204. clearOtpInterval () {
  205. this.otpTimer = 120
  206. if (this.otpInterval) {
  207. clearInterval(this.otpInterval)
  208. }
  209. },
  210. change (val) {
  211. this.$emit('update:value', val)
  212. }
  213. }
  214. }
  215. </script>
  216. <style lang="scss" scoped>
  217. .VerifyEmail {
  218. .contents {
  219. display: flex;
  220. flex-flow: row;
  221. justify-content: center;
  222. align-items: center;
  223. position: relative;
  224. .VerifyEmail-input {
  225. width: calc(100% - 130px);
  226. &.verified {
  227. width: 100%;
  228. }
  229. }
  230. .VerifyEmail-btn {
  231. width: 118px;
  232. height: 48px;
  233. margin-left: 12px;
  234. }
  235. .VerifyEmail-message {
  236. position: absolute;
  237. right: 0;
  238. bottom: -30px;
  239. svg {
  240. cursor: pointer;
  241. }
  242. }
  243. .VerifyEmail-timer {
  244. position: absolute;
  245. right: 135px;
  246. bottom: 13px;
  247. }
  248. .VerifyEmail-verified-mark {
  249. position: absolute;
  250. right: 15px;
  251. bottom: 12px;
  252. display: flex;
  253. align-items: center;
  254. justify-content: center;
  255. }
  256. }
  257. }
  258. </style>