wechatcode.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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://hst2-1323137866.cos.ap-chongqing.myqcloud.com/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. companyUserId:uni.getStorageSync('companyUserInfoId'),
  70. companyId: this.companyId,
  71. appId: wx.getAccountInfoSync().miniProgram.appId
  72. }
  73. generateComapycode(data).then(res => {
  74. if (res.code == 200) {
  75. this.getcomapycodes()
  76. console.log(res)
  77. } else {
  78. uni.showToast({
  79. icon: 'none',
  80. title: res.msg,
  81. });
  82. }
  83. })
  84. },
  85. getcomapycodes() {
  86. const data = {
  87. companyUserId:uni.getStorageSync('companyUserInfoId'),
  88. companyId: this.companyId,
  89. appId: wx.getAccountInfoSync().miniProgram.appId
  90. }
  91. getcomapycode(data).then(res => {
  92. if (res.code == 200) {
  93. // 核心js代码:
  94. this.wechatImg = res.data
  95. } else {
  96. uni.showToast({
  97. icon: 'none',
  98. title: res.msg,
  99. });
  100. }
  101. })
  102. }
  103. }
  104. }
  105. </script>
  106. <style lang="scss" scoped>
  107. .codeimg {
  108. width: 80%;
  109. margin: 40rpx 0;
  110. }
  111. .generate {
  112. background-color: #2BC7B9;
  113. width: 70%;
  114. margin: 0 auto;
  115. border-radius: 40rpx;
  116. padding: 20rpx 0;
  117. color: #fff;
  118. margin-top: 30rpx;
  119. }
  120. </style>