app.js 2.5 KB

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