codeImage.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <template>
  2. <u-popup v-model="show" mode="center" closeable @close='close' border-radius='20'>
  3. <view class="content">
  4. <view class="title">
  5. 图片验证
  6. </view>
  7. <view class="loading" v-show="loadImage=='ok'" @click="getImage()">
  8. <image class="codeImg" @load="loadImage='ok'" @error="loadImage='err'" :src="image" mode="scaleToFill">
  9. </image>
  10. </view>
  11. <view class="loading" v-if="loadImage=='loading'">
  12. 加载中...
  13. </view>
  14. <view class="loading" v-if="loadImage=='err'" @click="getImage()">
  15. 加载失败点击刷新
  16. </view>
  17. <input class="codeInput" type="text" v-model="code" placeholder="请输入图片中的验证内容" />
  18. <view class="flex">
  19. <u-button class="buttom" size='medium' @click="getImage()">刷新</u-button>
  20. <u-button class="buttom" size='medium' type="primary" @click="openVerify">确定</u-button>
  21. </view>
  22. </view>
  23. </u-popup>
  24. </template>
  25. <script>
  26. import {
  27. verify_code,
  28. verify
  29. } from '@/api/login.js';
  30. import {
  31. mapState,
  32. mapMutations
  33. } from 'vuex';
  34. export default {
  35. props: {
  36. show: {
  37. type: Boolean,
  38. default: false,
  39. },
  40. phone: {
  41. type: Number|String,
  42. default: '',
  43. },
  44. loginType: {
  45. type: String,
  46. default: '',
  47. }
  48. },
  49. data() {
  50. return {
  51. code: '',
  52. image: '',
  53. loadImage: "loading",
  54. key: '',
  55. }
  56. },
  57. computed: {
  58. ...mapState(['baseURL'])
  59. },
  60. methods: {
  61. close() {
  62. this.$emit("close")
  63. },
  64. async getImage() {
  65. try {
  66. this.loadImage = "loading";
  67. let {
  68. data: {
  69. key
  70. }
  71. } = await verify_code();
  72. this.key = key;
  73. this.image = `${this.baseURL}/api/sms_captcha?key=${key}&time=${(new Date()).getTime()}`;
  74. } catch (e) {
  75. console.log(e, "err")
  76. //TODO handle the exception
  77. }
  78. },
  79. openVerify() {
  80. const obj = this;
  81. if(!obj.code){
  82. uni.showToast({
  83. title:"请输入图片中的内容"
  84. })
  85. }
  86. verify({
  87. key: obj.key,
  88. phone: obj.phone,
  89. type: obj.loginType,
  90. code:obj.code
  91. })
  92. .then(({
  93. data
  94. }) => {
  95. console.log(data, 'data')
  96. uni.showToast({
  97. title: '验证码已发送',
  98. duration: 2000,
  99. position: 'top',
  100. icon: 'none'
  101. });
  102. obj.close();
  103. obj.$emit("openCode")
  104. })
  105. .catch(err => {
  106. obj.code="";
  107. uni.showToast({
  108. title: err.msg,
  109. icon:"error"
  110. });
  111. obj.getImage();
  112. console.log(err, 'err');
  113. });
  114. }
  115. },
  116. }
  117. </script>
  118. <style lang="scss" scoped>
  119. .content {
  120. padding: 30rpx;
  121. .codeImg {
  122. width: 100%;
  123. height: 130rpx;
  124. }
  125. .loading {
  126. margin-top: 30rpx;
  127. background-color: #e3e3e3;
  128. border-radius: 10rpx;
  129. overflow: hidden;
  130. text-align: center;
  131. line-height: 130rpx;
  132. width: 660rpx;
  133. height: 130rpx;
  134. color: #999999;
  135. }
  136. }
  137. .codeInput {
  138. width: 100%;
  139. height: 60rpx;
  140. padding-bottom: 30rpx;
  141. border-bottom: 1px solid #e3e3e3;
  142. margin-top: 50rpx;
  143. margin-bottom: 50rpx;
  144. }
  145. </style>