App.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <script>
  2. import {
  3. mapMutations,
  4. } from 'vuex';
  5. export default {
  6. methods: {
  7. ...mapMutations(['setOpenid','login','showDialog'])
  8. },
  9. onLaunch: function() {
  10. // 用户登录
  11. console.log(this.$store)
  12. let that = this;
  13. let sessionId = uni.getStorageSync('sessionId');// 用户sessinid
  14. console.log(sessionId)
  15. if(sessionId != '') {
  16. uni.checkSession({
  17. success(){
  18. console.log("登录未过期")
  19. that.setOpenid("22222");
  20. },
  21. fail(){
  22. console.log("jjjj")
  23. // 登录过期 重新登录
  24. uni.login({
  25. provider: 'weixin',
  26. success: function(res) {
  27. // 获取code
  28. that.setOpenid("22222");
  29. that.login();
  30. // 获取sessionId
  31. uni.request({
  32. url: '',
  33. data: {
  34. },
  35. success: (res) => {
  36. //this.setOpenid("dddd")
  37. }
  38. })
  39. },
  40. fail: function(res) {
  41. }
  42. })
  43. }
  44. })
  45. }else {
  46. uni.login({
  47. provider: 'weixin',
  48. success: function(res) {
  49. // 获取code
  50. that.setOpenid("22222");
  51. that.login();
  52. // 获取sessionId
  53. uni.request({
  54. url: '',
  55. data: {
  56. },
  57. success: (res) => {
  58. //this.setOpenid("dddd")
  59. }
  60. })
  61. },
  62. fail: function(res) {
  63. }
  64. })
  65. };
  66. // 用户授全
  67. uni.getSetting({
  68.   success(res) {
  69.    if (res.authSetting['scope.userInfo']) {
  70.     console.log("已授权=====")
  71. console.log(uni.getStorageSync('sessionId'));
  72.     // 已经授权,可以直接调用 getUserInfo 获取头像昵称
  73.     uni.getUserInfo({
  74.      success(res) {
  75. that.login(res.userInfo);
  76. // uni.setStorage({//缓存用户登陆状态
  77. // key: 'userInfo',
  78. // data: res.userInfo
  79. // })
  80.      },
  81.      fail(res) {
  82.       console.log("获取用户信息失败", res)
  83.      }
  84.     })
  85.    } else {
  86. console.log("触发授权")
  87. uni.showModal({
  88. title: '提示',
  89. content: '请前往授权',
  90. success: function (res) {
  91. if (res.confirm) {
  92. that.showDialog(true)
  93. } else if (res.cancel) {
  94. uni.showToast({
  95. title: '授权失败不能正常使用',
  96. duration: 2000
  97. });
  98. }
  99. }
  100. });
  101. //     console.log("未授权=====")
  102. //     that.showSettingToast("请授权")
  103.    }
  104.   }
  105.  })
  106. },
  107. onShow: function() {
  108. console.log('App Show')
  109. },
  110. onHide: function() {
  111. console.log('App Hide')
  112. }
  113. }
  114. </script>
  115. <style>
  116. /*每个页面公共css */
  117. </style>