app.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // app.js
  2. const {http} = require('./utils/util')
  3. const {login,getUserInfo} = require('./API/appraise')
  4. App({
  5. onLaunch() {
  6. // 展示本地存储能力
  7. const logs = wx.getStorageSync('logs') || []
  8. logs.unshift(Date.now())
  9. wx.setStorageSync('logs', logs)
  10. //开发者信息
  11. const wxInfo = {
  12. appid: 'wxc25f00d0fc026ae7',
  13. appSecret: '9e94011d65b0f4307b1cab14022ea060'
  14. }
  15. const ui_w = 360;
  16. //把vid存到缓存
  17. wx.setStorageSync('vid', '')
  18. // 获取屏幕的宽度
  19. var clientWidth = wx.getSystemInfoSync().windowWidth;
  20. var loginOption = {
  21. header :{
  22. "content-type": 'application/x-www-form-urlencoded',
  23. },
  24. data:{
  25. vid: wx.getStorageSync('vid')
  26. },
  27. }
  28. var infoOption = {
  29. header:{
  30. "Authorization": wx.getStorageSync('token')
  31. },
  32. }
  33. //检测缓存是否有token
  34. // console.log(wx.getStorageSync('token'));
  35. if(wx.getStorageSync('token') == ''){
  36. console.log('token不存在');
  37. // 登录
  38. wx.login({
  39. success: (res) => {
  40. //获取用户昵称和头像
  41. //和微信接口服务校验
  42. console.log("code: "+res.code);
  43. var url = 'https://api.weixin.qq.com/sns/jscode2session?appid=' + wxInfo.appid + '&secret=' + wxInfo.appSecret + '&js_code=' + res.code + '&grant_type=authorization_code'
  44. wx.request({
  45. url:url,
  46. success: (res) => {
  47. console.log(res);
  48. console.log(res.data.openid);
  49. wx.setStorageSync('vid', res.data.openid);
  50. console.log(wx.getStorageSync('vid'));
  51. login(loginOption).then(res => {
  52. console.log(res);
  53. wx.setStorageSync('token', res.token);
  54. wx.setStorageSync('uid', res.uid);
  55. wx.setStorageSync('avatarUrl', res.user_info.avatar);
  56. wx.setStorageSync('nickName', res.user_info.name);
  57. }).catch(err => {
  58. wx.navigateTo({
  59. url: '/pages/register/register',
  60. })
  61. })
  62. }
  63. })
  64. }
  65. })
  66. }else{
  67. getUserInfo(infoOption).then(res => {
  68. console.log(res);
  69. console.log('token有效');
  70. wx.setStorageSync('uid', res.uid);
  71. }).catch(err => {
  72. console.log(err);
  73. console.log('token已失效,请重新登录');
  74. wx.login({
  75. success: (res) => {
  76. //和微信接口服务校验
  77. console.log("code: "+res.code);
  78. var url = 'https://api.weixin.qq.com/sns/jscode2session?appid=' + wxInfo.appid + '&secret=' + wxInfo.appSecret + '&js_code=' + res.code + '&grant_type=authorization_code'
  79. wx.request({
  80. url:url,
  81. success: (res) => {
  82. console.log(res);
  83. console.log(res.data.openid);
  84. wx.setStorageSync('vid', res.data.openid);
  85. console.log(wx.getStorageSync('vid'));
  86. login(loginOption).then(res => {
  87. console.log(res);
  88. wx.setStorageSync('token', res.token);
  89. wx.setStorageSync('uid', res.user_info.uid);
  90. wx.setStorageSync('avatarUrl', res.user_info.avatar);
  91. wx.setStorageSync('nickName', res.user_info.name);
  92. }).catch(err => {
  93. wx.navigateTo({
  94. url: '/pages/register/register',
  95. })
  96. })
  97. }
  98. })
  99. }
  100. })
  101. })
  102. }
  103. },
  104. globalData: {
  105. userInfo: null
  106. }
  107. })