init.js 1.3 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()) {
  13. if (checkOpenId) {
  14. if (!openId) {
  15. this.getOpenId();
  16. } else {
  17. //this.getDataByOpenId();
  18. }
  19. }
  20. } else {
  21. if (checkOpenId) {
  22. /* mui.alert('请使用微信浏览打开!', '提示', function() {
  23. //info.innerText = '你刚关闭了警告框';
  24. }); */
  25. }
  26. }
  27. },
  28. getDataByOpenId() {
  29. API_WeiXin.getDataByOpenId(openId).then(response => {
  30. var token = response ? response.token : '';
  31. carhelp.setToken(token);
  32. }).catch(error => {
  33. mui.toast(error);
  34. })
  35. },
  36. getOpenId() {
  37. const code = getUrlParam('code');
  38. var openId = carhelp.getOpenId()
  39. if (!openId) {
  40. if (!code) {
  41. var url = document.URL;
  42. window.location.href = getWeixinRedirectURI(process.car.VUE_APP_WXAPPID, url);
  43. } else {
  44. API_WeiXin.getDataByCode(code).then(response => {
  45. carhelp.setOpenId(response.data.openid)
  46. openId = response.data.openid;
  47. //this.getDataByOpenId()
  48. }).catch(error => {
  49. console.log(error);
  50. });
  51. }
  52. }
  53. }
  54. }
  55. module.exports = app