loading.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. var app = getApp();
  2. Page({
  3. /**
  4. * 页面的初始数据
  5. */
  6. data: {
  7. logo: '',
  8. name: '',
  9. url: app.globalData.url,
  10. is_login:true,
  11. },
  12. /**
  13. * 生命周期函数--监听页面加载
  14. */
  15. onLoad: function (options) {
  16. var that = this;
  17. that.auth();
  18. },
  19. //授权判断
  20. auth: function () {
  21. var that = this;
  22. wx.checkSession({
  23. success: function (res) {
  24. wx.getSetting({
  25. success(res) {
  26. //第一次弹窗允许授权
  27. if (!res.authSetting['scope.userInfo'] || !wx.getStorageSync('session_key')) {
  28. that.showlogin(false, 2500);
  29. } else {
  30. //第二次直接调用获取用户信息
  31. that.getUserInfoBydecryptCode();
  32. }
  33. }
  34. })
  35. },
  36. fail: function (res) {
  37. that.showlogin(false,2500);
  38. }
  39. })
  40. },
  41. //首次点击允许获取用户信息并且授权
  42. getUserInfo: function (e) {
  43. var that = this;
  44. var userInfo = e.detail.userInfo;
  45. userInfo.spid = app.globalData.spid;
  46. userInfo.spreadid = app.globalData.spreadid;//获取推广人ID 2.5.36
  47. wx.login({
  48. success: function (res) {
  49. // console.log(res);//获取code
  50. if (res.code) {
  51. userInfo.code = res.code;
  52. wx.request({
  53. url: app.globalData.url + '/routine/logins/setCode',
  54. method: 'post',
  55. dataType  : 'json',
  56. data: {
  57. info: userInfo
  58. },
  59. success: function (res) {
  60. // console.log(res);//根据code 获取openid session_key unionid(未试过用相关连应用无法获取unionid)
  61. wx.setStorageSync('session_key', res.data.session_key);//保存小程序缓存中
  62. that.showlogin(false);
  63. //解密获取用户信息
  64. that.getUserInfoBydecryptCode();
  65. },
  66. fail: function () {
  67. that.showlogin(false);
  68. },
  69. })
  70. } else {
  71. that.showlogin(false);
  72. console.log('登录失败!' + res.errMsg)
  73. }
  74. }
  75. })
  76. },
  77. //解密获取用户信息
  78. getUserInfoBydecryptCode: function (e) {
  79. var that = this;
  80. wx.getUserInfo({
  81. lang: 'zh_CN',
  82. success: function (res) {
  83. // console.log(res);//第二次获取用户信息获得iv和encryptedData
  84. var pdata = res;
  85. pdata.userInfo = res.userInfo;
  86. pdata.spid = app.globalData.spid;//获取推广人ID
  87. pdata.spreadid = app.globalData.spreadid;//获取推广人ID 2.5.36
  88. if (res.iv) {
  89. pdata.iv = encodeURI(res.iv);
  90. pdata.encryptedData = res.encryptedData;
  91. pdata.session_key = wx.getStorageSync('session_key');//获取上一步获取的session_key
  92. wx.request({
  93. url: app.globalData.url + '/routine/logins/index',
  94. method: 'post',
  95. dataType  : 'json',
  96. data: {
  97. info: pdata
  98. },
  99. success: function (res) {
  100. console.log(res);
  101. if (res.data.data == 'ILLEGAL_BUFFER'){
  102. wx.setStorageSync('session_key','');
  103. that.auth();
  104. }else{
  105. app.globalData.uid = res.data.data.uid;
  106. if (!app.globalData.uid || app.globalData.uid == 'undefined') {
  107. that.showlogin(true);
  108. } else {
  109. if (app.globalData.openPages != '' && app.globalData.openPages != undefined) {//跳转到指定页面
  110. wx.navigateTo({
  111. url: app.globalData.openPages
  112. })
  113. } else {//跳转到首页
  114. wx.reLaunch({
  115. url: '/pages/index/index'
  116. })
  117. }
  118. }
  119. }
  120. }
  121. })
  122. } else {
  123. console.log('登录失败!' + res.errMsg)
  124. }
  125. },
  126. fail: function () {
  127. },
  128. })
  129. },
  130. //是否显示登录授权框
  131. showlogin:function(ishouw,times = 1500){
  132. var that = this;
  133. setTimeout(function () {
  134. that.setData({
  135. is_login: ishouw
  136. })
  137. }, times)
  138. }
  139. })