wechatcode.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <template>
  2. <view class="column centerV">
  3. <view class="codeimg">
  4. <view v-if="wechatImg!=null" class="centerV">
  5. <image :src="wechatImg" style="width: 100%;" mode="aspectFit" :show-menu-by-longpress="true" ></image>
  6. <view class="centerV">点击保存图片至本地!</view>
  7. </view>
  8. <view v-else>
  9. <image src="https://hzyy.obs.cn-north-4.myhuaweicloud.com/app/shop/images/no_data.png" style="width: 100%;"
  10. mode="aspectFill"></image>
  11. <view class="centerV">暂无二维码,请点击生成二维码</view>
  12. </view>
  13. </view>
  14. <view class="centerV generate" @click="generatecode" v-if="wechatImg==null">点击生成</view>
  15. <view class="centerV generate" @click="saveImage" v-else>保存图片</view>
  16. </view>
  17. </template>
  18. <script>
  19. import {
  20. getcomapycode,
  21. generateComapycode
  22. } from '@/api/companyUser'
  23. export default {
  24. data() {
  25. return {
  26. wechatImg: null,
  27. companyId: ''
  28. }
  29. },
  30. onLoad(option) {
  31. this.companyId = option.companyId
  32. this.getcomapycodes()
  33. },
  34. methods: {
  35. saveImage() {
  36. wx.downloadFile({
  37. url: this.wechatImg, //仅为示例,并非真实的资源
  38. success (res) {
  39. console.log(res)
  40. // 只要服务器有响应数据,就会把响应内容写入文件并进入 success 回调,业务需要自行判断是否下载到了想要的内容
  41. if (res.statusCode === 200) {
  42. wx.saveImageToPhotosAlbum({
  43. filePath: res.tempFilePath,
  44. success: function(res) {
  45. wx.showToast({
  46. title: '已保存至相册',
  47. icon: 'none',
  48. duration: 1000
  49. })
  50. },
  51. fail: function(err) {
  52. console.log(err);
  53. wx.showToast({
  54. title: '保存失败',
  55. icon: 'none',
  56. duration: 1000
  57. })
  58. }
  59. })
  60. wx.playVoice({
  61. filePath: res.tempFilePath
  62. })
  63. }
  64. }
  65. })
  66. },
  67. generatecode() {
  68. const data = {
  69. companyId: this.companyId,
  70. appId: wx.getAccountInfoSync().miniProgram.appId
  71. }
  72. generateComapycode(data).then(res => {
  73. if (res.code == 200) {
  74. this.getcomapycodes()
  75. console.log(res)
  76. } else {
  77. uni.showToast({
  78. icon: 'none',
  79. title: res.msg,
  80. });
  81. }
  82. })
  83. },
  84. getcomapycodes() {
  85. const data = {
  86. companyId: this.companyId,
  87. appId: wx.getAccountInfoSync().miniProgram.appId
  88. }
  89. getcomapycode(data).then(res => {
  90. if (res.code == 200) {
  91. // 核心js代码:
  92. this.wechatImg = res.data
  93. } else {
  94. uni.showToast({
  95. icon: 'none',
  96. title: res.msg,
  97. });
  98. }
  99. })
  100. }
  101. }
  102. }
  103. </script>
  104. <style lang="scss" scoped>
  105. .centerV {
  106. display: flex;
  107. flex-direction: column;
  108. align-items: center;
  109. justify-content: center;
  110. }
  111. .codeimg {
  112. width: 80%;
  113. margin: 40rpx 0;
  114. }
  115. .generate {
  116. background-color: #2BC7B9;
  117. width: 70%;
  118. margin: 0 auto;
  119. border-radius: 40rpx;
  120. padding: 20rpx 0;
  121. color: #fff;
  122. margin-top: 30rpx;
  123. }
  124. </style>