login.js 3.4 KB

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