app.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. loginOption.data = {
  52. vid: wx.getStorageSync('vid'),
  53. }
  54. console.log(loginOption.data);
  55. login(loginOption).then(res => {
  56. console.log(res);
  57. wx.setStorageSync('token', res.data.data.token);
  58. wx.setStorageSync('uid', res.data.data.user_info.uid);
  59. wx.setStorageSync('avatarUrl', res.data.data.user_info.avatar);
  60. wx.setStorageSync('nickName', res.data.data.user_info.name);
  61. }).catch(err => {
  62. console.log(err);
  63. wx.navigateTo({
  64. url: '/pages/register/register',
  65. })
  66. })
  67. }
  68. })
  69. }
  70. })
  71. }else{
  72. getUserInfo(infoOption).then(res => {
  73. console.log(res);
  74. console.log('token有效');
  75. wx.setStorageSync('uid', res.data.data.uid);
  76. wx.setStorageSync('avatarUrl', res.data.data.avatar);
  77. wx.setStorageSync('nickName', res.data.data.name);
  78. wx.setStorageSync('personalSignatrue', res.data.data.sign);
  79. console.log(wx.getStorageSync('uid'));
  80. }).catch(err => {
  81. console.log('token已失效,请重新登录');
  82. wx.login({
  83. success: (res) => {
  84. //和微信接口服务校验
  85. console.log("code: "+res.code);
  86. var url = 'https://api.weixin.qq.com/sns/jscode2session?appid=' + wxInfo.appid + '&secret=' + wxInfo.appSecret + '&js_code=' + res.code + '&grant_type=authorization_code'
  87. wx.request({
  88. url:url,
  89. success: (res) => {
  90. console.log(res);
  91. console.log(res.data.openid);
  92. wx.setStorageSync('vid', res.data.openid);
  93. console.log(wx.getStorageSync('vid'));
  94. loginOption.data = {
  95. vid: wx.getStorageSync('vid'),
  96. }
  97. login(loginOption).then(res => {
  98. console.log(res);
  99. wx.setStorageSync('token', res.data.data.token);
  100. wx.setStorageSync('uid', res.data.data.user_info.uid);
  101. wx.setStorageSync('avatarUrl', res.data.data.user_info.avatar);
  102. wx.setStorageSync('nickName', res.data.data.user_info.name);
  103. }).catch(err => {
  104. console.log(err);
  105. wx.navigateTo({
  106. url: '/pages/register/register',
  107. })
  108. })
  109. }
  110. })
  111. }
  112. })
  113. })
  114. }
  115. },
  116. globalData: {
  117. userInfo: null
  118. }
  119. })