LocationDemo.vue 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <template>
  2. <view>
  3. <u--form :model="inputForm" labelWidth="100px" class="u-form" labelPosition="left"
  4. ref="inputForm">
  5. <u-form-item label="地理位置" prop="actualDrawerEmailAddress"
  6. :rules="[
  7. ]">
  8. <view class="button-container">
  9. <button @click="getLocation">获取位置</button>
  10. </view>
  11. </u-form-item>
  12. <u-form-item label="经度" prop="longitude" borderBottom>
  13. <u--input v-model="inputForm.longitude" clearable></u--input>
  14. </u-form-item>
  15. <u-form-item label="纬度" prop="latitude" borderBottom>
  16. <u--input v-model="inputForm.latitude" clearable></u--input>
  17. </u-form-item>
  18. <div class="container">
  19. <van-popup v-model="show" round position="bottom" :style="{ height: '30%' }">
  20. <button class="clickBtn" @click="goToFilm('camera')">拍摄</button>
  21. <button class="clickBtn" @click="goToFilm('album')">从相册选择</button>
  22. </van-popup>
  23. <div class="img_box">
  24. <div class="left_box" @click="boxClick">
  25. <u--image width="150" height="75" :src="img1" />
  26. </div>
  27. </div>
  28. </div>
  29. </u--form>
  30. </view>
  31. </template>
  32. <!--<script src="https://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script>-->
  33. <script>
  34. import wx from 'weixin-js-sdk';
  35. import UImage from "../../../uni_modules/uview-ui/components/u--image/u--image";
  36. export default {
  37. name: 'LocationComponent',
  38. components: {UImage},
  39. mounted() {
  40. this.getConfig()
  41. },
  42. data () {
  43. return {
  44. inputForm:{
  45. latitude:'',
  46. longitude:'',
  47. files:[]
  48. },
  49. show: false,//是否弹窗
  50. img1: '',
  51. img2: 'https://img01.yzcdn.cn/vant/cat.jpeg',
  52. imgFlag: '',//点击的是哪张图片
  53. timer: null
  54. }
  55. },
  56. methods: {
  57. getConfig() {
  58. this.initWeixin();
  59. },
  60. initWeixin() {
  61. this.inputForm.latitude = ''
  62. this.inputForm.longitude = ''
  63. // 假设这些参数是从后端获取的
  64. const appId = 'wx96885ba58675d910';
  65. const timestamp = '1724144081';
  66. const nonceStr = '1142148534';
  67. const signature = '494260AF76E8A6ECB89356A463C2031402873A3D';
  68. wx.config({
  69. debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
  70. appId: appId, // 必填,公众号的唯一标识
  71. timestamp: timestamp, // 必填,生成签名的时间戳
  72. nonceStr: nonceStr, // 必填,生成签名的随机串
  73. signature: signature, // 必填,签名
  74. jsApiList: ['getLocation','chooseImage',
  75. 'uploadImage',
  76. 'getLocalImgData',
  77. 'downloadImage'] // 必填,需要使用的JS接口列表
  78. });
  79. wx.ready(function () {
  80. // 可以在这里调用其他API
  81. wx.openLocation({
  82. latitude: 0, // 纬度,浮点数,范围为90 ~ -90
  83. longitude: 0, // 经度,浮点数,范围为180 ~ -180。
  84. name: '', // 位置名
  85. address: '', // 地址详情说明
  86. scale: 1, // 地图缩放级别,整型值,范围从1~28。默认为最大
  87. infoUrl: '' // 在查看位置界面底部显示的超链接,可点击跳转
  88. });
  89. wx.checkJsApi({
  90. jsApiList: [
  91. 'chooseImage',
  92. 'previewImage',
  93. 'uploadImage',
  94. 'downloadImage'
  95. ],
  96. success: function (res) {
  97. if (res.checkResult.getLocation == true) {
  98. wxChooseImage();
  99. }
  100. }
  101. });
  102. });
  103. wx.error(function (res) {
  104. // config信息验证失败会执行error函数,如签名过期导致验证失败,具体错误信息可以打开config的debug模式查看,也可以在返回的res参数中查看,对于SPA可以在这里更新签名。
  105. console.log('验证失败:', res);
  106. });
  107. },
  108. getLocation() {
  109. wx.getLocation({
  110. type: 'wgs84', // 默认为wgs84的gps坐标,如果要返回火星坐标,可传入'gcj02'
  111. success: (res)=> {
  112. let latitude = res.latitude; // 纬度,浮点数,范围为90 ~ -90
  113. let longitude = res.longitude; // 经度,浮点数,范围为180 ~ -180。
  114. console.log('纬度:', latitude, '经度:', longitude);
  115. // 处理你的逻辑
  116. this.inputForm.latitude = res.latitude
  117. this.inputForm.longitude = res.longitude
  118. console.log('纬度:', this.inputForm, '经度:', this.longitude);
  119. },
  120. fail: function (err) {
  121. // 调用失败
  122. console.error('获取位置失败:', err);
  123. }
  124. });
  125. },
  126. chooseImage(){
  127. wx.chooseImage({
  128. count: 1, // 最多可以选择的图片张数,默认9
  129. sizeType: ['original', 'compressed'], // original 原图,compressed 压缩图,默认二者都有
  130. sourceType: ['album', 'camera'], // album 从相册选图,camera 使用相机,默认二者都有
  131. success: function (res) {
  132. let localIds = res.localIds; // 返回选定照片的本地ID列表(手机上操作就是手机端的ID列表,是一个数组),localId可以作为img标签的src属性显示图片
  133. wx.uploadImage({
  134. localId: localIds[0], // 需要上传的图片的本地ID,由chooseImage接口获得
  135. isShowProgressTips: 1, // 默认为1,显示进度提示
  136. success: function (result) {
  137. let serverId = result.serverId; // 返回图片的服务器端ID
  138. // 可以将serverId传给后台,用于存放在自己服务器上
  139. }
  140. });
  141. },
  142. fail: function() {},
  143. complete: function() {}
  144. });
  145. },
  146. wxChooseImage() {
  147. wx.chooseImage({
  148. success: function(res) {
  149. var localId = res.localIds;
  150. function upload() {
  151. //图片上传
  152. wx.uploadImage({
  153. localId: localId[i],
  154. success: function(res) {
  155. var serverId = res.serverIdl;
  156. //图片上传完成之后,进行图片的下载,图片上传完成之后会返回一个在腾讯服务器的存放的图片的ID--->serverId
  157. wx.downloadImage({
  158. serverId: res.serverId, // 需要下载的图片的服务器端ID,由uploadImage接口获得
  159. isShowProgressTips: 1, // 默认为1,显示进度提示
  160. success: function (res) {
  161. var localId = res.localId; // 返回图片下载后的本地ID
  162. //通过下载的本地的ID获取的图片的base64数据,通过对数据的转换进行图片的保存
  163. wx.getLocalImgData({
  164. localId: localId, // 图片的localID
  165. success: function (res) {
  166. var localData = res.localData; // localData是图片的base64数据,可以用img标签显示
  167. //通过ajax来将base64数据转换成图片保存在本地
  168. $.ajax({
  169. });
  170. }
  171. });
  172. }
  173. });
  174. },
  175. fail: function(res) {
  176. }
  177. });
  178. }
  179. }
  180. });
  181. },
  182. boxClick () {
  183. this.show = true
  184. },
  185. goToFilm (sourceType) {
  186. this.show = false
  187. console.log(sourceType);
  188. wx.chooseImage({
  189. count: 1, // 默认9
  190. sizeType: ['compressed'], // 可以指定是原图还是压缩图,默认二者都有 ['original', 'compressed']
  191. sourceType: sourceType, // 可以指定来源是相册还是相机,默认二者都有 ['album', 'camera']
  192. success: (res) => {
  193. var localIds = res.localIds;// 返回选定照片的本地ID列表,localId可以作为img标签的src属性显示图片
  194. var localId=localIds[0];
  195. wx.uploadImage({
  196. localId: localId,// 需要上传的图片的本地ID,由chooseImage接口获得
  197. isShowProgressTips: 1,// 默认为1,显示进度提示
  198. success: function (res) {
  199. var serverId = res.serverId;// 返回图片的服务器端ID
  200. wx.getLocalImgData({
  201. localId: localId,// 图片的localID
  202. success: function (res) {
  203. var localData = res.localData;// localData是图片的base64数据,可以用img标签显示
  204. this.img1 = localData
  205. console.log('img1',this.img1)
  206. }
  207. });
  208. }
  209. })
  210. // this.timer = window.setTimeout(() => {
  211. // wx.uploadImage({
  212. // localId: localId, // 需要上传的图片的本地ID,由chooseImage接口获得
  213. // isShowProgressTips: 1, // 默认为1,显示进度提示
  214. // success: (res) => {
  215. // let serverId = res.serverId; // 返回图片的服务器端ID
  216. //
  217. //
  218. // }
  219. // })
  220. // }, 100);
  221. },
  222. fail: (err) => {
  223. console.log('chooseImage失败', err);
  224. },
  225. complete: () => {
  226. console.log('chooseImage接口调用完成');
  227. }
  228. });
  229. },
  230. }
  231. }
  232. </script>
  233. <style>
  234. </style>