index.vue 1.4 KB

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