h5Templete.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. // uni.showModal({
  49. // content:JSON.stringify(req.getStorage('userInfo'))
  50. // })
  51. });
  52. }else{
  53. //未登录移除用户信息缓存
  54. req.removeStorage('userInfo')
  55. }
  56. },
  57. getwxConfig() {
  58. return new Promise((resolve, reject) => {
  59. req.getwxConfig(data => {
  60. resolve();
  61. });
  62. });
  63. },
  64. postShareMessage(title,imageUrl,path,sendBehaviorObj){
  65. if(path.indexOf('/share/home/index')>-1){
  66. }else{
  67. path = '/pages/webView/webView?url=' + encodeURIComponent(window.location.protocol + '//' + window.location.hostname + ':' + window.location.port + path)+'&userId='+req.getStorage('userInfo').id
  68. }
  69. h5util.postMsg({
  70. type: 'function', //function 直接调用方法(此时eventName必传) msg 传递参数
  71. eventName: 'setShareDate',
  72. data: {
  73. title: title?title:'',
  74. imageUrl: imageUrl?imageUrl:'',
  75. path: path?path:'',
  76. sendBehaviorObj:sendBehaviorObj
  77. }
  78. })
  79. }
  80. }
  81. };