app.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. //app.js
  2. App({
  3. onLaunch: function () {
  4. var openid;
  5. var _this=this;
  6. wx.getStorage({
  7. key: 'openid',
  8. success: function(res) {
  9. openid=res.data
  10. _this.globalData.openid = openid
  11. },
  12. fail:res=>{
  13. // 登录
  14. wx.login({
  15. success: res => {
  16. console.log(res)
  17. // 发送 res.code 到后台换取 openId, sessionKey, unionId
  18. wx.request({
  19. url: 'https://www.scxjc.club/api/wx/openid',
  20. method: 'GET',
  21. data: {
  22. code: res.code
  23. },
  24. success: res => {
  25. if (res.data.data.openid){
  26. wx.setStorage({
  27. key: 'openid',
  28. data: res.data.data.openid,
  29. })
  30. }
  31. }
  32. })
  33. }
  34. })
  35. }
  36. })
  37. // 获取用户信息
  38. wx.getSetting({
  39. success: res => {
  40. if (res.authSetting['scope.userInfo']) {
  41. // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
  42. wx.getUserInfo({
  43. success: res => {
  44. // 可以将 res 发送给后台解码出 unionId
  45. this.globalData.userInfo = res.userInfo
  46. // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
  47. // 所以此处加入 callback 以防止这种情况
  48. if (this.userInfoReadyCallback) {
  49. this.userInfoReadyCallback(res)
  50. }
  51. }
  52. })
  53. }
  54. }
  55. })
  56. // wx.request({
  57. // url:'https://www.scxjc.club/api/wx/sujectitem',
  58. // success: res => {
  59. // this.globalData.categoryArray = res.data.data
  60. // }
  61. // })
  62. },
  63. globalData: {
  64. userInfo: null,
  65. host:'https://www.scxjc.club',
  66. categoryArray:[]
  67. }
  68. })