h5Templete.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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){
  62. h5util.postMsg({
  63. type: 'function', //function 直接调用方法(此时eventName必传) msg 传递参数
  64. eventName: 'setShareDate',
  65. data: {
  66. title: title?title:'',
  67. imageUrl: imageUrl?imageUrl:'',
  68. path: path?path:'',
  69. }
  70. })
  71. }
  72. }
  73. };