app.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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.28: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. wx.setStorageSync('token', res.data.data.token);
  63. var avatar = wx.getStorageSync('avatarUrl');
  64. var name = wx.getStorageSync('nickName');
  65. // console.log(vid);
  66. console.log('vid: '+ wx.getStorageSync('vid'));
  67. if(res.data.code == 403){
  68. wx.request({
  69. url: 'http://192.168.31.28:8084/user/register',
  70. method: 'POST',
  71. data: {
  72. avatar: avatar,
  73. name: name,
  74. vid: wx.getStorageSync('vid'),
  75. phone: '',
  76. sign: '',
  77. },
  78. success(res){
  79. console.log('注册成功');
  80. console.log(res);
  81. wx.navigateTo({
  82. url: '/pages/register/register',
  83. })
  84. wx.request({
  85. url: 'http://192.168.31.28:8084/user/login',
  86. method: 'POST',
  87. header: {
  88. 'content-type': 'application/x-www-form-urlencoded'
  89. },
  90. data:{
  91. vid: wx.getStorageSync('vid')
  92. },
  93. success(res){
  94. wx.setStorageSync('token', res.data.data.token);
  95. }
  96. })
  97. }
  98. })
  99. }
  100. }
  101. })
  102. }
  103. })
  104. }
  105. })
  106. }else{
  107. wx.request({
  108. url: 'http://192.168.31.28:8084/user/info',
  109. header:{
  110. "Authorization": wx.getStorageSync('token')
  111. },
  112. success(res){
  113. console.log(res);
  114. if(res.code == 200){
  115. console.log('token有效');
  116. }
  117. }
  118. })
  119. }
  120. },
  121. globalData: {
  122. userInfo: null
  123. }
  124. })