app.js 3.5 KB

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