uploadImgVStorec.vue 6.0 KB

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