app.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. // 登录
  28. wx.login({
  29. success: (res) => {
  30. //获取用户昵称和头像
  31. //和微信接口服务校验
  32. console.log("code: "+res.code);
  33. var url = 'https://api.weixin.qq.com/sns/jscode2session?appid=' + wxInfo.appid + '&secret=' + wxInfo.appSecret + '&js_code=' + res.code + '&grant_type=authorization_code'
  34. wx.request({
  35. url: url,
  36. success(res){
  37. wx.setStorageSync('vid', res.data.openid);
  38. console.log(wx.getStorageSync('vid'));
  39. wx.getUserProfile({
  40. desc: '登录',
  41. success: (res) => {
  42. wx.setStorageSync('nickName', res.userInfo.nickuName);
  43. wx.setStorageSync('avatarUrl', res.userInfo.avatarUrl);
  44. }
  45. });
  46. //登录
  47. wx.request({
  48. url: 'http://192.168.31.28:8084/user/login',
  49. method:'POST',
  50. header: {
  51. 'content-type': 'application/x-www-form-urlencoded'
  52. },
  53. data:{
  54. vid: wx.getStorageSync('vid')
  55. },
  56. success(res){
  57. console.log(res);
  58. var avatar = wx.getStorageSync('avatarUrl');
  59. var name = wx.getStorageSync('nickName');
  60. // console.log(vid);
  61. console.log('vid: '+ wx.getStorageSync('vid'));
  62. if(res.data.code == 403){
  63. wx.request({
  64. url: 'http://192.168.31.28:8084/user/register',
  65. method: 'POST',
  66. data: {
  67. avatar: avatar,
  68. name: name,
  69. vid: wx.getStorageSync('vid'),
  70. phone: '',
  71. sign: '',
  72. },
  73. success(res){
  74. console.log('注册成功');
  75. console.log(res);
  76. wx.request({
  77. url: 'http://192.168.31.28:8084/user/login',
  78. method: 'POST',
  79. header: {
  80. 'content-type': 'application/x-www-form-urlencoded'
  81. },
  82. data:{
  83. vid: wx.getStorageSync('vid')
  84. },
  85. success(res){
  86. console.log(res);
  87. }
  88. })
  89. }
  90. })
  91. }
  92. }
  93. })
  94. }
  95. })
  96. }
  97. })
  98. },
  99. globalData: {
  100. userInfo: null
  101. }
  102. })