app.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. const util = require('./utils/util.js');
  2. App({
  3. globalData: {
  4. showAnimation: true,
  5. id: null,
  6. name: null,
  7. },
  8. onLaunch(options) {
  9. console.log(options, "onLaunch");
  10. // if (wx.getStorageSync('openId')) {
  11. // wx.request({
  12. // url: 'http://info.windd.cn:8080/test/applet/isExist',
  13. // method: "GET",
  14. // data: {
  15. // appletsId: wx.getStorageSync('openId')
  16. // },
  17. // success: (res) => {
  18. // console.log(res, "000000000000000000");
  19. // if (res.data.code == 0) {
  20. // wx.switchTab({
  21. // url: '/pages/index/index',
  22. // })
  23. // }
  24. // }
  25. // })
  26. // } else {
  27. // }
  28. // wx.login({
  29. // success: res => {
  30. // // 获取到用户的 code 之后:res.code
  31. // console.log("用户的code:" + res.code);
  32. // const appid = "wx1c015df104db7030"
  33. // const secret = "d1956c1b5d3601657c98b0dc80a006f7"
  34. // let url = 'https://api.weixin.qq.com/sns/jscode2session?appid=' + appid + '&secret=' + secret + '&js_code=' + res.code + '&grant_type=authorization_code';
  35. // wx.request({
  36. // // 自行补上自己的 APPID 和 SECRET
  37. // url: url,
  38. // success: res => {
  39. // // 获取到用户的 openid
  40. // console.log("用户的openid:" + res.data.openid);
  41. // wx.setStorageSync('openId', res.data.openid)
  42. // wx.request({
  43. // url: 'http://info.windd.cn:8080/test/applet/isExist',
  44. // method: "GET",
  45. // data: {
  46. // appletsId: wx.getStorageSync('openId')
  47. // },
  48. // success: (res) => {
  49. // console.log(res, "apppppppppppppppppppppppppp");
  50. // if (res.data.code == 0) {
  51. // if(res.data.data){
  52. // wx.setStorageSync('user', 'user');
  53. // }
  54. // }
  55. // }
  56. // })
  57. // }
  58. // });
  59. // }
  60. // });
  61. },
  62. onShow(options) {
  63. // Do something when show.
  64. },
  65. onHide() {
  66. // Do something when hide.
  67. },
  68. onError(msg) {
  69. console.log(msg)
  70. },
  71. getAuthKey: function () {
  72. return new Promise(function (resolve, reject) {
  73. // 调用登录接口
  74. wx.login({
  75. success: function (res) {
  76. if (res.code) {
  77. console.log("用户的code:" + res.code);
  78. const appid = "wx1c015df104db7030"
  79. const secret = "d1956c1b5d3601657c98b0dc80a006f7"
  80. let url = 'https://api.weixin.qq.com/sns/jscode2session?appid=' + appid + '&secret=' + secret + '&js_code=' + res.code + '&grant_type=authorization_code';
  81. wx.request({
  82. // 自行补上自己的 APPID 和 SECRET
  83. url: url,
  84. success: res => {
  85. // 获取到用户的 openid
  86. console.log("用户的openid:" + res.data.openid);
  87. wx.setStorageSync('openId', res.data.openid)
  88. wx.request({
  89. url: util.globalData.publicUrl + '/applet/isExist',
  90. method: "GET",
  91. data: {
  92. appletsId: wx.getStorageSync('openId')
  93. },
  94. success: (res) => {
  95. console.log(res, "apppppppppppppppppppppppppp");
  96. if (res.data.code == 0) {
  97. if (res.data.data) {
  98. wx.setStorageSync('user', 'user');
  99. }
  100. resolve(res);
  101. }
  102. }
  103. })
  104. }
  105. });
  106. } else {
  107. console.log('获取用户登录态失败!' + res.errMsg);
  108. var res = {
  109. status: 300,
  110. data: '错误'
  111. }
  112. reject('error');
  113. }
  114. }
  115. })
  116. });
  117. },
  118. })