TXApiFun.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. import Vue from 'vue';
  2. import { getTicket } from '@/api/index';
  3. import { toastLoading } from '@/utils/commonVant';
  4. import { CJ02BD, gcj02BD } from '@/utils/index';
  5. import { jsonp } from 'vue-jsonp';
  6. // 微信JSSDK实例
  7. const wx = Vue.prototype.wx;
  8. // 腾讯位置服务 key
  9. const TxMapKey = 'WLCBZ-HRM6L-YOMPV-ME62B-AQOG6-JUBW6';
  10. // 当前设备
  11. const isDevice = localStorage.getItem('isDevice');
  12. /**
  13. * 获取当前定位 调用之前确保当前页面已经授权(getTicketFun)
  14. * @param isPermit // PC 模式下不能获取定位,是否允许跳过定位
  15. * */
  16. export function getPosition(isPermit = false) {
  17. return new Promise((resolve, reject) => {
  18. toastLoading(0, '定位中...', true);
  19. // PC端没有定位权限依然可以访问
  20. if (process.env.NODE_ENV === 'test') {
  21. // 本地开发 test 环境时跳过获取定位功能 模拟定位
  22. let resData = {
  23. latitude: 34.615684509277344,
  24. longitude: 112.4474105834961,
  25. };
  26. // 定位坐标转换 腾讯转百度
  27. let TXisBD = CJ02BD(resData.latitude, resData.longitude);
  28. localStorage.setItem('lat', TXisBD.lat);
  29. localStorage.setItem('lon', TXisBD.lon);
  30. toastLoading().clear();
  31. resolve({ TXisBD: TXisBD, resData: resData });
  32. } else if (isDevice == 'PC') {
  33. // PC状态 分允许通过和不允许通过 isPermit: tru允许,false不允许
  34. localStorage.setItem('lat', '');
  35. localStorage.setItem('lon', '');
  36. toastLoading().clear();
  37. if (isPermit) {
  38. resolve({ TXisBD: { lat: '', lon: '' }, resData: { latitude: '', longitude: '' } });
  39. } else {
  40. reject('GPS未开启');
  41. }
  42. } else {
  43. wx.ready(() => {
  44. wx.getLocation({
  45. type: 'gcj02',
  46. success: (resData) => {
  47. console.log('success');
  48. toastLoading().clear();
  49. console.log('处理前');
  50. console.log(resData.latitude, resData.longitude);
  51. // 定位坐标转换 腾讯转百度
  52. let TXisBD = CJ02BD(resData.latitude, resData.longitude);
  53. console.log('处理后');
  54. console.log(TXisBD);
  55. localStorage.setItem('lat', TXisBD.lat);
  56. localStorage.setItem('lon', TXisBD.lon);
  57. resolve({ TXisBD: TXisBD, resData: resData });
  58. },
  59. fail: () => {
  60. console.log('fail');
  61. toastLoading().clear();
  62. reject('GPS未开启');
  63. },
  64. complete: () => {
  65. console.log('complete');
  66. toastLoading().clear();
  67. },
  68. });
  69. });
  70. wx.error((err) => {
  71. console.log('wx.error');
  72. toastLoading().clear();
  73. console.log(err);
  74. localStorage.setItem('lat', '');
  75. localStorage.setItem('lon', '');
  76. reject('定位失败,请开启企微定位权限');
  77. });
  78. }
  79. });
  80. }
  81. /**
  82. *当前页面授权 一个url只需要授权一次,wx.config 调用多次,只有第一次会调用成功,后面的都会走失败
  83. * @param {*object} jsApiList //授权接口列表
  84. * @param {*String} getLocation //获取定位
  85. * @param {*String} configType //注入类型
  86. * config,agentConfig
  87. * config注入的是企业的身份与权限,
  88. * 而agentConfig注入的是应用的身份与权限。
  89. * 尤其是当调用者为第三方服务商时,通过config无法准确区分出调用者是哪个第三方应用,
  90. * 而在部分场景下,又必须严谨区分出第三方应用的身份,此时即需要通过agentConfig来注入应用的身份信息。
  91. *
  92. */
  93. export function getTicketFun(jsApiList = ['getLocation'], configType = 'config') {
  94. return new Promise((resolve, reject) => {
  95. // 当前页面
  96. let url = window.location.href;
  97. // 获取签名
  98. getTicket({ url: url }).then((response) => {
  99. console.log(response);
  100. toastLoading().clear();
  101. if (response.code == 200) {
  102. let qiyeData = response.data;
  103. if (configType == 'config') {
  104. wx.config({
  105. beta: true, // 必须这么写,否则wx.invoke调用形式的jsapi会有问题
  106. debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
  107. appId: qiyeData.appId, // 必填,企业微信的corpID
  108. timestamp: qiyeData.timestamp, // 必填,生成签名的时间戳
  109. nonceStr: qiyeData.nonceStr, // 必填,生成签名的随机串
  110. signature: qiyeData.signature, // 必填,签名,见 附录-JS-SDK使用权限签名算法
  111. jsApiList: ['ready', ...jsApiList], // 必填,需要使用的JS接口列表,凡是要调用的接口都需要传进来
  112. });
  113. console.log('获取签名成功');
  114. resolve('获取签名成功');
  115. } else if (configType == 'agentConfig') {
  116. wx.agentConfig({
  117. corpid: qiyeData.appId, // 必填,企业微信的corpid,必须与当前登录的企业一致
  118. agentid: qiyeData.agentId, // 必填,企业微信的应用id (e.g. 1000247)
  119. timestamp: qiyeData.timestamp, // 必填,生成签名的时间戳
  120. nonceStr: qiyeData.nonceStr, // 必填,生成签名的随机串
  121. signature: qiyeData.signature, // 必填,签名,见附录-JS-SDK使用权限签名算法
  122. jsApiList: [...jsApiList], //必填,传入需要使用的接口名称
  123. success: function (res) {
  124. console.log('获取签名成功');
  125. resolve('获取签名成功');
  126. },
  127. fail: function (res) {
  128. if (res.errMsg.indexOf('function not exist') > -1) {
  129. alert('版本过低请升级');
  130. }
  131. },
  132. });
  133. }
  134. } else {
  135. console.log('获取签名失败');
  136. reject('获取签名失败');
  137. alert('获取签名失败');
  138. }
  139. });
  140. });
  141. }
  142. /**
  143. * 地点搜索 获取500米范围poi点
  144. * @param {*object} location //当前位置
  145. * @param {*number} radius //搜索半径
  146. * @param {*number} auto_extend //是否自动扩大范围 0 不扩大
  147. * @returns
  148. */
  149. export function getMapPoi(location, radius = 500, auto_extend = 0) {
  150. return new Promise((resolve, reject) => {
  151. let api =
  152. 'https://apis.map.qq.com/ws/place/v1/search?page_size=10&page_index=1&orderby=_distance&output=jsonp';
  153. let boundary = `&boundary=nearby(${location.latitude},${location.longitude},${radius},${auto_extend})`;
  154. let key = `&key=${TxMapKey}`;
  155. jsonp(api + boundary + key)
  156. .then((res) => {
  157. console.log(res);
  158. resolve(res);
  159. })
  160. .catch((err) => {
  161. console.log(err);
  162. reject(err);
  163. });
  164. });
  165. }
  166. /**
  167. * 关键词搜索 在当前城市范围内搜索
  168. * @param {*object} location //当前位置
  169. * @param {*string} keywordValue //关键字
  170. * @returns
  171. */
  172. export function getkeywordPoi(location, keywordValue) {
  173. return new Promise((resolve, reject) => {
  174. let keyword = keywordValue ? '&keyword=' + encodeURI(keywordValue) : '';
  175. // 关键词搜索
  176. let api =
  177. 'https://apis.map.qq.com/ws/place/v1/suggestion?output=jsonp&page_size=10&region_fix=1';
  178. let key = `&key=${TxMapKey}`;
  179. let locationPos = `&location=${location.latitude},${location.longitude}`;
  180. jsonp(api + locationPos + keyword + key)
  181. .then((res) => {
  182. console.log(keyword);
  183. console.log(res);
  184. resolve(res);
  185. })
  186. .catch((err) => {
  187. console.log(err);
  188. reject(err);
  189. });
  190. });
  191. }