uploadImgVStorec.vue 6.2 KB

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