Advertisement
Guest User

Untitled

a guest
Feb 17th, 2020
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.21 KB | None | 0 0
  1. <template>
  2. <div class="container">
  3. <Error v-model="hasError" :message="messageError" />
  4. <b-input-group v-if="ehInformacaoCEP">
  5. <b-form-input ref="option" v-model.trim="response" v-on:keydown.enter="sendResponse"></b-form-input>
  6. <b-input-group-append>
  7. <b-button variant="primary" v-on:click="sendResponse">Enviar</b-button>
  8. </b-input-group-append>
  9. </b-input-group>
  10. <div v-else class="text-right">
  11. <b-button v-on:click="confirm(true)" variant="primary" class="m-1">Sim</b-button>
  12. <b-button v-on:click="confirm(false)" variant="primary" class="m-1">Não</b-button>
  13. </div>
  14. </div>
  15. </template>
  16.  
  17. <script>
  18. import formatter from "@/javascript/formatter"
  19. import step from '@/javascript/constants/step'
  20. import Error from "@/components/Step/Error"
  21.  
  22. export default {
  23. name: "CepPernoite",
  24. components: { Error },
  25. created() {
  26. this.$store.dispatch("chat/addContent", {
  27. isResponse: false,
  28. description: "Me informe o CEP de onde o veículo passa a noite."
  29. })
  30. },
  31. mounted() {
  32. this.$refs.option.focus()
  33. },
  34. data() {
  35. return {
  36. response: "",
  37. hasError: false,
  38. messageError: "",
  39. consultouCEP: false,
  40. ehInformacaoCEP: true,
  41. endereco: {
  42. tipologradouro: "",
  43. logradouro: "",
  44. bairro: "",
  45. numero: "",
  46. cep:"",
  47. cidade: "",
  48. estado: "",
  49. enderecocompleto: false
  50. }
  51. }
  52. },
  53. methods: {
  54. showError(message) {
  55. this.messageError = message;
  56. this.hasError = true;
  57. },
  58. sendResponse() {
  59. if (this.consultouCEP) {
  60. this.confirmacaoEndereco();
  61. } else {
  62. this.obterDadosEndereco();
  63. }
  64. },
  65. confirmacaoEndereco() {
  66. this.$store.dispatch("chat/addResponse", {
  67. id: step.AUTOCEPPERNOITE,
  68. description: this.response + " - " + this.endereco.cidade + "/" + this.endereco.uf + " - CEP " + this.endereco.cep
  69. })
  70.  
  71. this.$store.dispatch("chat/addContent", {
  72. isResponse: true,
  73. description: this.response + " - " + this.endereco.cidade + "/" + this.endereco.uf + " - CEP " + this.endereco.cep
  74. })
  75.  
  76. this.$store.dispatch("chat/nextStep", step.CONTATO)
  77. },
  78. confirm(value) {
  79. this.ehInformacaoCEP = true;
  80.  
  81. if (value)
  82. this.agreed();
  83. else
  84. this.disagreed();
  85. },
  86. agreed() {
  87. this.$store.dispatch("chat/addContent", {
  88. isResponse: true,
  89. description: "Sim"
  90. })
  91.  
  92. this.$store.dispatch("chat/addContent", {
  93. isResponse: false,
  94. description: "Informe o endereço, indicando, bairro, complemento e número!"
  95. })
  96.  
  97. if (this.endereco.enderecocompleto)
  98. this.response = this.endereco.tipologradouro + " " + this.endereco.logradouro + ", " + this.endereco.bairro;
  99. },
  100. disagreed() {
  101. this.ehInformacaoCEP = true;
  102. this.$store.dispatch("chat/addContent", {
  103. isResponse: true,
  104. description: "Não"
  105. })
  106.  
  107. this.$store.dispatch("chat/addContent", {
  108. isResponse: false,
  109. description: "Então me informe o CEP correto de onde o veículo passa a noite."
  110. })
  111. },
  112. async obterDadosEndereco() {
  113. const cep = formatter.cep(this.response)
  114.  
  115. if (cep == "") {
  116. this.showError("Informe um CEP válido.")
  117. } else {
  118. if (this.buscarEnderecoAPI(cep)) {
  119. this.$store.dispatch("chat/addContent", {
  120. isResponse: true,
  121. description: this.endereco.cep + " - " + this.endereco.cidade + "/" + this.endereco.estado
  122. })
  123.  
  124. this.$store.dispatch("chat/addContent", {
  125. isResponse: false,
  126. description: "O CEP e a cidade estão corretos?"
  127. })
  128.  
  129. this.response = "";
  130. this.consultouCEP = true;
  131. this.ehInformacaoCEP = false;
  132. }
  133. }
  134. },
  135. async buscarEnderecoAPI(cep) {
  136. try {
  137. var response = await this.$http.get("http://cep.republicavirtual.com.br/web_cep.php?formato=json&cep=" + cep)
  138.  
  139. console.log(response.data)
  140. if (!response.data || !response.data.resultado) {
  141. this.erroBuscarEnderecoAPI();
  142. return false;
  143. }
  144.  
  145. if (response.data.resultado == 0) {
  146. this.showError("Endereço não encontrado para o CEP informado!")
  147. return false;
  148. }
  149.  
  150. this.endereco.enderecocompleto = response.data.resultado == 1;
  151. this.endereco.tipologradouro = response.data.tipo_logradouro || "";
  152. this.endereco.logradouro = response.data.logradouro || "";
  153. this.endereco.bairro = response.data.bairro || "";
  154. this.endereco.cidade = response.data.cidade || "";
  155. this.endereco.estado = response.data.uf || "";
  156. this.endereco.cep = cep;
  157.  
  158. return true;
  159.  
  160. } catch (error) {
  161. this.erroBuscarEnderecoAPI();
  162. return false;
  163. }
  164. },
  165. erroBuscarEnderecoAPI() {
  166. this.consultouCEP = true;
  167.  
  168. this.$store.dispatch("chat/addContent", {
  169. isResponse: false,
  170. description: "Houve um erro ao consultar o CEP. Informe o endereço manualmente.(CEP, cidade, estado, rua, bairro e numero)"
  171. });
  172. },
  173.  
  174. }
  175. }
  176. </script>
  177.  
  178. <style>
  179. </style>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement