You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
2.1 KiB

3 years ago
  1. export default{
  2. onLoad() {
  3. if(this.systemInfo.platform !== 'ios'){
  4. return;
  5. }
  6. const systemVersion = +this.systemInfo.system.split('.')[0];
  7. if(systemVersion >= 13){
  8. this.canUseAppleLogin = true;
  9. }
  10. },
  11. methods: {
  12. //苹果登录
  13. async loginByApple(){
  14. /* if(!this.canUseAppleLogin){
  15. this.$util.msg('系统版本过低,无法使用苹果登录');
  16. return;
  17. } */
  18. if(!this.agreement){
  19. this.$util.msg('请阅读并同意用户服务及隐私协议');
  20. this.$refs.confirmBtn.stop();
  21. return;
  22. }
  23. uni.login({
  24. provider: 'apple',
  25. success: loginRes=> {
  26. // 登录成功
  27. uni.getUserInfo({
  28. provider: 'apple',
  29. success: async userRes=> {
  30. console.log(userRes);
  31. const response = await this.$request('user', 'loginByApple', {
  32. authorizationCode: userRes.userInfo.authorizationCode,
  33. identityToken: userRes.userInfo.identityToken
  34. }, {
  35. showLoading: true
  36. });
  37. console.log(response);
  38. //注销苹果登录
  39. this.appleLogout();
  40. console.log(response);
  41. if(response.status === 0){
  42. this.$util.msg(response.msg);
  43. return;
  44. }
  45. if(response.hasBindMobile && response.data.token){
  46. this.loginSuccessCallBack({
  47. token: response.data.token,
  48. tokenExpired: response.data.tokenExpired
  49. });
  50. }else{
  51. this.navTo('/pages/auth/bindMobile?type=apple&data='+JSON.stringify(response.data))
  52. }
  53. }
  54. })
  55. },
  56. fail: err=> {
  57. console.log(err);
  58. this.$util.msg('登录失败');
  59. this.appleLogout();
  60. }
  61. })
  62. },
  63. appleLogout(){
  64. plus.oauth.getServices(oauthRes=>{
  65. const oIndex = oauthRes.findIndex(oItem=> oItem.id === 'apple');
  66. oauthRes[oIndex].logout(loRes => {
  67. console.log('appleLogout success=> ', loRes);
  68. }, loErr => {
  69. console.log('appleLogout error=> ', loErr);
  70. })
  71. })
  72. }
  73. }
  74. }