uploadImgVStorec.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <template>
  2. <div class="cameraDiv1">
  3. <!-- <div class="imgPre">-->
  4. <!-- <van-icon class="photobrowsing" name="expand-o" size="22px" color="#969696" v-if="imgArr!=''" @click="deleteImgs(imgArr)"/>-->
  5. <!-- <img :src="imgArr" alt="" v-if="imgArr!=''">-->
  6. <!-- </div>-->
  7. <p class="coverImg" @click="uploadImg">
  8. <van-icon class="photo ico" name="photograph" size="16px" color="#969696">{{
  9. imgText
  10. }}</van-icon>
  11. </p>
  12. </div>
  13. </template>
  14. <script>
  15. import { ImagePreview } from 'vant';
  16. import axios from 'axios';
  17. import { uploadImagev } from '@/api/index';
  18. export default {
  19. name: 'uploadImg',
  20. props: {
  21. uploadid: {
  22. type: String,
  23. default: '',
  24. },
  25. imgText: {
  26. type: String,
  27. default: '',
  28. },
  29. visitsId: {
  30. type: String,
  31. default: '',
  32. },
  33. taskId: {
  34. type: String,
  35. default: '',
  36. },
  37. collectionId: {
  38. type: String,
  39. default: '',
  40. },
  41. type: {
  42. type: Number,
  43. default: 1,
  44. },
  45. imgArr: {
  46. type: String,
  47. default: '',
  48. },
  49. },
  50. data() {
  51. return {
  52. shows: false,
  53. url: '',
  54. };
  55. },
  56. methods: {
  57. deleteImgs(val) {
  58. ImagePreview([val]);
  59. },
  60. uploadImg() {
  61. if (localStorage.getItem('chainName') == null) {
  62. this.$toast('请输入名称!');
  63. return;
  64. }
  65. let url = window.location.href;
  66. let that = this;
  67. let wx = this.wx;
  68. let qiyeData;
  69. const instance = axios.create();
  70. instance.defaults.headers.common['userId'] = localStorage.getItem('loginName');
  71. instance
  72. .get(process.env.VUE_APP_BASE_API + 'mobile/wx/ticket', {
  73. params: {
  74. url: url,
  75. },
  76. })
  77. .then((response) => {
  78. if (response.status == 200) {
  79. qiyeData = response.data.data;
  80. wx.config({
  81. beta: true, // 必须这么写,否则wx.invoke调用形式的jsapi会有问题
  82. debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
  83. appId: qiyeData.appId, // 必填,企业微信的corpID
  84. timestamp: qiyeData.timestamp, // 必填,生成签名的时间戳
  85. nonceStr: qiyeData.nonceStr, // 必填,生成签名的随机串
  86. signature: qiyeData.signature, // 必填,签名,见 附录-JS-SDK使用权限签名算法
  87. jsApiList: ['ready', 'chooseImage', 'uploadImage'], // 必填,需要使用的JS接口列表,凡是要调用的接口都需要传进来
  88. });
  89. wx.ready(function () {
  90. wx.chooseImage({
  91. count: 1,
  92. sizeType: ['compressed'], // 可以指定是原图还是压缩图,默认二者都有
  93. sourceType: ['camera'], // 可以指定来源是相册还是相机,默认二者都有
  94. defaultCameraMode: 'normal', //表示进入拍照界面的默认模式,目前有normal与batch两种选择,normal表示普通单拍模式,batch表示连拍模式,不传该参数则为normal模式。从3.0.26版本开始支持front和batch_front两种值,其中front表示默认为前置摄像头单拍模式,batch_front表示默认为前置摄像头连拍模式。(注:用户进入拍照界面仍然可自由切换两种模式)
  95. isSaveToAlbum: 0,
  96. success: function (res) {
  97. var localIds = '';
  98. if (res.localIds != undefined) {
  99. localIds = res.localIds[0];
  100. } else {
  101. localIds = res.localId;
  102. }
  103. // andriod中localId可以作为img标签的src属性显示图片;
  104. // iOS应当使用 getLocalImgData 获取图片base64数据,从而用于img标签的显示(在img标签内使用 wx.chooseImage 的 localid 显示可能会不成功)
  105. wx.uploadImage({
  106. localId: localIds, // 需要上传的图片的本地ID,由chooseImage接口获得
  107. isShowProgressTips: 1, // 默认为1,显示进度提示
  108. success: function (res) {
  109. that.uploadImagev(res.serverId);
  110. },
  111. });
  112. },
  113. });
  114. });
  115. }
  116. });
  117. },
  118. uploadImagev(meidaId) {
  119. var that = this;
  120. var form = {
  121. mediaId: meidaId,
  122. storeName: localStorage.getItem('chainName'),
  123. };
  124. var loind1 = that.$toast.loading({
  125. duration: 0,
  126. message: '上传中...',
  127. forbidClick: true,
  128. });
  129. uploadImagev(form).then((res) => {
  130. if (res.code == 200) {
  131. // that.imgArr = res.data.url
  132. let imgArr = [];
  133. res.data.forEach((item) => {
  134. imgArr.push(item.url);
  135. });
  136. loind1.clear();
  137. that.$toast('上传成功!');
  138. that.$emit('newimgarr', { fileUrl: imgArr.join(','), type: that.type });
  139. } else {
  140. that.$toast('上传失败!');
  141. }
  142. });
  143. },
  144. },
  145. };
  146. </script>
  147. <style scoped>
  148. .cameraDiv1 {
  149. position: relative;
  150. height: 20px;
  151. width: 100%;
  152. }
  153. .cameraDiv1 img {
  154. position: absolute;
  155. width: 100%;
  156. display: block;
  157. height: 20px;
  158. top: 0;
  159. }
  160. .imgPre {
  161. height: 20px;
  162. width: 100%;
  163. background-color: white;
  164. border-radius: 6px;
  165. overflow: hidden;
  166. }
  167. .photos1 {
  168. margin: 0px auto;
  169. left: 20%;
  170. margin-left: -14px;
  171. }
  172. .photobrowsing {
  173. position: absolute;
  174. padding: 4px;
  175. right: 0;
  176. top: 0;
  177. z-index: 99;
  178. background-color: rgba(255, 255, 255, 0.8);
  179. border-bottom-left-radius: 3px;
  180. border-top-left-radius: 3px;
  181. }
  182. .coverImg {
  183. text-align: right;
  184. position: absolute;
  185. top: 0;
  186. right: 0;
  187. height: 20px;
  188. width: 100%;
  189. margin: 0;
  190. }
  191. .coverImg .ico {
  192. top: 0%;
  193. }
  194. </style>