index.vue 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <template>
  2. <view>
  3. <car-common :login="true"></car-common>
  4. </view>
  5. </template>
  6. <script>
  7. import * as m from '@/utils/mixin.js'
  8. import * as API_WeiXin from '@/apis/weixin.js'
  9. import {
  10. getUrlParam,
  11. getWeixinRedirectURI,
  12. isWeiXin
  13. } from '@/utils'
  14. export default {
  15. data() {
  16. return {
  17. }
  18. },
  19. components:{
  20. CarCommon
  21. },
  22. onReady() {
  23. console.log("my-mounted")
  24. },
  25. onShow() {
  26. console.log("my-mounted-show")
  27. console.log(m.get("wx_openId"))
  28. if(!m.get("wx_openId")){
  29. if (isWeiXin()) {
  30. this.getOpenid();
  31. }
  32. }
  33. },
  34. methods: {
  35. getOpenid(){
  36. console.log("!!!!")
  37. const code = getUrlParam('code');
  38. if (!code) {
  39. var url= document.URL;
  40. if(process.car.NODE_ENV=='test'){
  41. url="http://xpgj.xiaoxinda.com/#/"
  42. }
  43. window.location.href = getWeixinRedirectURI(process.car.VUE_APP_WXAPPID,url);
  44. }else{
  45. console.log("code"+code)
  46. API_WeiXin.getDataByCode(code).then(response => {
  47. console.log("openId+!!!!"+response.openid)
  48. m.set("wx_openId",code)
  49. var linkUrl =document.URL.replace(/\?code=(.*?)&state=STATE/g, '');
  50. window.location = linkUrl;//隐藏参数
  51. //return Promise.resolve(response.openid);
  52. }).catch(error => {
  53. console.log(error);
  54. });
  55. }
  56. }
  57. }
  58. }
  59. </script>
  60. <style>
  61. </style>