init.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. } else{
  21. alert("请使用微信打开本页面")
  22. }
  23. }else{
  24. carhelp.setOpenId(process.car.openId)
  25. }
  26. },
  27. getOpenId() {
  28. const code = getUrlParam('code');
  29. var openId = carhelp.getOpenId()
  30. if (!openId) {
  31. if (!code) {
  32. var url = document.URL;
  33. var getUrl= getWeixinRedirectURI(process.car.VUE_APP_WXAPPID, url);
  34. window.location.href = getUrl;
  35. } else {
  36. API_WeiXin.getDataByCode(code).then(response => {
  37. carhelp.setOpenId(response.data.openid)
  38. openId = response.data.openid;
  39. carhelp.set("xpgj_wx_user_info",response.data)
  40. location.reload();
  41. }).catch(error => {
  42. console.log(error);
  43. });
  44. }
  45. }
  46. }
  47. }
  48. module.exports = app