uploadImgVStore.vue 7.6 KB

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