login.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. const app = getApp();
  2. const appConfig = require("../config");
  3. const utils = require("../utils/util");
  4. var login = function (that) {
  5. var serverInfo = wx.getStorageSync(appConfig.storeKeys.serverInfo);
  6. if(serverInfo && typeof serverInfo == 'object'
  7. && serverInfo['openId'] && serverInfo['openId'] != ""
  8. && serverInfo['sessionId'] && serverInfo['sessionId'] != "") {
  9. //如果是切换到本地就不去发请求,直接成功
  10. if (utils.getSwitchToLocalStatus) {
  11. that.setData({
  12. motto: "登录成功",
  13. showLists: true,
  14. hasServerInfo: true,
  15. hiddenStatus: true
  16. });
  17. app.serInfoReady({
  18. code: 0,
  19. msg: "本地登录成功~",
  20. data: {
  21. sslKeys: {}
  22. }
  23. })
  24. return;
  25. }
  26. var header = {
  27. 'content-type': 'application/x-www-form-urlencoded',
  28. };
  29. wx.request({
  30. url: utils.getUrl("login"),
  31. header: header,
  32. data : {
  33. openId: serverInfo['openId'],
  34. sessionId: serverInfo['sessionId'],
  35. },
  36. method : "POST",
  37. dataType : "json",
  38. success: function(req) {
  39. //解密用户信息后登录
  40. if (!req || req.data.code > 0 || !req.data.data || !req.data.data.sslKeys) {
  41. //登录失败
  42. app.serInfoReady(req.data);
  43. setTimeout(function () {
  44. wx.hideLoading();
  45. }, 3000);
  46. return;
  47. } else {
  48. //登录成功,将用户信息加密保存到本地
  49. wx.showToast({
  50. title: '登录成功',
  51. })
  52. }
  53. wx.setStorageSync(appConfig.storeKeys.sessionId, req.header["Set-Cookie"])
  54. wx.setStorageSync(appConfig.storeKeys.serverInfo, req.data.data)
  55. that.setData({
  56. motto: "登录成功",
  57. showLists: true,
  58. hasServerInfo: true,
  59. hiddenStatus: true
  60. });
  61. if (req.data.data.sslKeys && req.data.data.sslKeys.publicKey && req.data.data.sslKeys.publicKey != "") {
  62. that.setData({
  63. hasPublicKey: true
  64. });
  65. app.globalData.hasPublicKey = true;
  66. }
  67. wx.hideToast();
  68. wx.hideLoading();
  69. app.serInfoReady(req.data)
  70. },
  71. fail: function (res) {
  72. app.serInfoReady(appConfig.networkError);
  73. }
  74. });
  75. return;
  76. }
  77. //如果有openId 和 sessionId 就直接登录,不走获取微信信息
  78. wx.getSetting({
  79. success(res) {
  80. if (res.authSetting['scope.userInfo']) {
  81. if (!app.globalData.res) {
  82. app.warning("用户信息获取失败");
  83. return;
  84. }
  85. if (app.globalData.code == null) {
  86. app.warning("登录授权码获取失败~");
  87. return;
  88. }
  89. wx.showLoading({
  90. title: '正在登录',
  91. });
  92. var header = {
  93. 'content-type': 'application/x-www-form-urlencoded',
  94. };
  95. wx.request({
  96. url: utils.getUrl("auth"),
  97. data: {
  98. code: app.globalData.code,
  99. encryptedData: app.globalData.res.encryptedData,
  100. iv: app.globalData.res.iv
  101. },
  102. header: header,
  103. method: 'POST',
  104. dataType: 'json',
  105. success: function (req) {
  106. //解密用户信息后登录
  107. if (!req || req.data.code > 0 || !req.data.data || !req.data.data.sslKeys) {
  108. //登录失败
  109. app.serInfoReady(req.data);
  110. setTimeout(function () {
  111. wx.hideLoading();
  112. }, 3000);
  113. return;
  114. } else {
  115. //登录成功,将用户信息加密保存到本地
  116. wx.showToast({
  117. title: '登录成功',
  118. })
  119. }
  120. wx.setStorageSync(appConfig.storeKeys.sessionId, req.header["Set-Cookie"])
  121. wx.setStorageSync(appConfig.storeKeys.serverInfo, req.data.data)
  122. that.setData({
  123. motto: "登录成功",
  124. showLists: true,
  125. hasServerInfo: true,
  126. hiddenStatus: true
  127. });
  128. if (req.data.data.sslKeys && req.data.data.sslKeys.publicKey && req.data.data.sslKeys.publicKey != "") {
  129. that.setData({
  130. hasPublicKey: true
  131. });
  132. app.globalData.hasPublicKey = true;
  133. }
  134. wx.hideToast();
  135. wx.hideLoading();
  136. app.serInfoReady(req.data)
  137. },
  138. fail: function (res) {
  139. app.serInfoReady(appConfig.networkError);
  140. }
  141. });
  142. }else{
  143. app.warning("请先授权此应用访问您的基本信息");
  144. }
  145. }
  146. });
  147. }
  148. module.exports = login