login.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // pages/login/login.ts;
  2. const app = getApp();
  3. const {
  4. login,
  5. register
  6. } = require('./../../utils/api.js')
  7. Page({
  8. data: {
  9. nvabarData: {
  10. showCapsule: 1, //是否显示左上角图标 1表示显示 0表示不显示
  11. title: '', //导航栏 中间的标题
  12. },
  13. isYs: false,
  14. isAgree: false,
  15. },
  16. egree() {
  17. this.setData({
  18. isAgree: !this.data.isAgree
  19. })
  20. },
  21. goabout(e) {
  22. wx.navigateTo({
  23. url: './../aboutInfo/aboutInfo?type=' + e.currentTarget.dataset.index,
  24. })
  25. },
  26. getPhoneNumber(e) {
  27. if (e.detail.errMsg != "getPhoneNumber:ok") {
  28. wx.showToast({
  29. title: "获取手机号失败",
  30. icon: 'fail',
  31. duration: 2000
  32. });
  33. return;
  34. }
  35. var phoneCode = e.detail.code;
  36. if (phoneCode) {
  37. wx.login({
  38. success: res1 => {
  39. wx.showLoading({
  40. title: '加载中',
  41. });
  42. // {"openid":"oZHPJ5JcNem2och3ToaocggmUENw","unionid":null,"userId":null,"deviceUid":null,"accessToken":null,"phone":null,"headUrl":"","nickname":"","isNewUser":true}
  43. login({
  44. code: res1.code,
  45. unShowLoad: true,
  46. }).then((res2) => {
  47. register({
  48. openid: res2.openid,
  49. code: phoneCode,
  50. unShowLoad: true,
  51. }).then((res3) => {
  52. // {"openid":"oZHPJ5JcNem2och3ToaocggmUENw","userId":933625,"deviceUid":1,"accessToken":"PUK8WVnLbIFsWYjSteitmg3OhllpbUMvIPKUtfJgwhRtpl1jx4Fm1NkHQIiZYhHz","phone":"15915390284","headUrl":"https://music-play.oss-cn-shenzhen.aliyuncs.com/user/headurl/20220316194115703366576.png","nickname":"儿子的爸爸叫啥"}
  53. wx.login({
  54. success: res4 => {
  55. // {"openid":"oZHPJ5JcNem2och3ToaocggmUENw","unionid":null,"userId":933625,"deviceUid":1,"accessToken":"kaKdNAb9eO0iUGKsaLP18UE0wRwrvoCr4opjNC3AieaigH4Fu1IspQKi2zTPgLNG","phone":"15915390284","headUrl":"https://music-play.oss-cn-shenzhen.aliyuncs.com/user/headurl/20220316194115703366576.png","nickname":"儿子的爸爸叫啥","isNewUser":false}
  56. login({
  57. code: res4.code,
  58. phone: res3.phone,
  59. }).then((res5) => {
  60. wx.showToast({
  61. title: "登录成功",
  62. icon: 'success',
  63. duration: 2000
  64. });
  65. app.globalData.userInfo = res5;
  66. wx.setStorageSync("userInfo", res5);
  67. wx.setStorageSync("token", JSON.stringify({
  68. id: res5.userId,
  69. token: res5.accessToken,
  70. }));
  71. getCurrentPages()[0].onUserInfoLoad();
  72. setTimeout(() => {
  73. wx.redirectTo({
  74. url: './../deviceList/deviceList'
  75. });
  76. }, 2000);
  77. // 需要用授权登录
  78. // if ((res.isNewUser && res.isNewUser === true) || !phone) {
  79. // return;
  80. // };
  81. });
  82. },
  83. fail(fail4) {
  84. wx.hideLoading();
  85. },
  86. });
  87. });
  88. });
  89. },
  90. fail(fail1) {
  91. wx.hideLoading();
  92. },
  93. });
  94. }
  95. },
  96. notlogin() {
  97. wx.showToast({
  98. title: '请同意协议',
  99. icon: "none"
  100. })
  101. }
  102. })