init.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import carhelp from '@/utils/mixin.js'
  2. import * as API_WeiXin from '@/apis/weixin.js'
  3. import {
  4. getUrlParam,
  5. getWeixinRedirectURI,
  6. isWeiXin
  7. } from '@/utils'
  8. var checkOpenId = true; //是否需要获取openId
  9. var openId = carhelp.getOpenId();
  10. var app = {
  11. init: function() {
  12. if(isWeiXin()|| !(process.env.NODE_ENV === "development") ){
  13. if (isWeiXin()) {
  14. if (checkOpenId) {
  15. if (!openId) {
  16. this.getOpenId();
  17. } else {
  18. }
  19. }
  20. }
  21. }else{
  22. carhelp.setOpenId(process.car.openId)
  23. }
  24. },
  25. getOpenId() {
  26. const code = getUrlParam('code');
  27. var openId = carhelp.getOpenId()
  28. if (!openId) {
  29. if (!code) {
  30. var url = document.URL;
  31. var getUrl= getWeixinRedirectURI(process.car.VUE_APP_WXAPPID, url);
  32. window.location.href = getUrl;
  33. } else {
  34. API_WeiXin.getDataByCode(code).then(response => {
  35. carhelp.setOpenId(response.data.openid)
  36. openId = response.data.openid;
  37. location.reload();
  38. }).catch(error => {
  39. console.log(error);
  40. });
  41. }
  42. }
  43. }
  44. }
  45. module.exports = app