h5Templete.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. const req = require('../utils/request.js');
  2. const h5util = require('../utils/h5util.js');
  3. export default {
  4. data() {
  5. return {
  6. appId: '',
  7. token: ''
  8. };
  9. },
  10. async onLoad(options) {
  11. // 同步appId和AUTH_TOKEN
  12. this.appId = req.getStorage('appId');
  13. this.token = req.getStorage('AUTH_TOKEN');
  14. let updata = false
  15. if (options.token && this.token != options.token) {
  16. req.setStorage('AUTH_TOKEN', options.token);
  17. updata = true
  18. }
  19. if (options.appId && this.appId != options.appId) {
  20. req.setStorage('appId', options.appId);
  21. updata = true
  22. }
  23. this.getLoginState()
  24. await this.getwxConfig();
  25. if (updata) window.location.reload();
  26. h5util.postMsg({
  27. type: 'function', //function 直接调用方法(此时eventName必传) msg 传递参数
  28. eventName: 'setPageTitle',
  29. data: {
  30. title: document.title
  31. }
  32. })
  33. },
  34. onShow() {
  35. },
  36. onHide() {
  37. },
  38. onUnload() {
  39. },
  40. methods: {
  41. // 获取登陆状态
  42. async getLoginState(){
  43. let isLogin = await req.isAuthFn(false)
  44. if(isLogin){
  45. // 登陆了获取用户信息缓存
  46. req.getRequest('/api/user/info', {}, data => {
  47. req.setStorage('userInfo', data);
  48. });
  49. }else{
  50. //未登录移除用户信息缓存
  51. req.removeStorage('userInfo')
  52. }
  53. },
  54. getwxConfig() {
  55. return new Promise((resolve, reject) => {
  56. req.getwxConfig(data => {
  57. resolve();
  58. });
  59. });
  60. },
  61. postShareMessage(title,imageUrl,path,sendBehaviorObj){
  62. if(path.indexOf('/share/home/index')>-1){
  63. }else{
  64. path = '/pages/webView/webView?url=' + encodeURIComponent(window.location.protocol + '//' + window.location.hostname + ':' + window.location.port + path)
  65. }
  66. h5util.postMsg({
  67. type: 'function', //function 直接调用方法(此时eventName必传) msg 传递参数
  68. eventName: 'setShareDate',
  69. data: {
  70. title: title?title:'',
  71. imageUrl: imageUrl?imageUrl:'',
  72. path: path?path:'',
  73. sendBehaviorObj:sendBehaviorObj
  74. }
  75. })
  76. }
  77. }
  78. };