logion.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <template>
  2. <view>
  3. <view v-if='!hasLogin'>
  4. <view style="text-align: center;margin: 100upx;font-size: 36upx;">欢迎回来</view>
  5. <view style="width: 50%;margin: 0 auto;">
  6. <button @click="wxlogion" type="primary" open-type='getUserInfo'>
  7. 授权登录
  8. </button>
  9. </view>
  10. </view>
  11. <view v-if='hasLogin&& !successlogion'>
  12. <view style="text-align: center;margin: 100upx;font-size: 36upx;">获取您手机号码权限</view>
  13. <view style="width: 50%;margin: 0 auto;">
  14. <button type="primary" open-type="getPhoneNumber" @getphonenumber="decryptPhoneNumber">手机号登录</button>
  15. </view>
  16. </view>
  17. <!-- <van-dialog title="请登录"
  18. :show-cancel-button="ishow"
  19. :show="show"
  20. @cancel="cancelDialog" @confirm="confirmOrder"
  21. confirm-button-open-type="getUserInfo"
  22. confirm-button-color = "#323232"
  23. >
  24. </van-dialog> -->
  25. <!-- <button @click="wxlogion" open-type="getUserInfo">请点击授权</button> -->
  26. </view>
  27. </template>
  28. <script>
  29. import {
  30. mapMutations,mapState
  31. } from 'vuex'
  32. export default {
  33. props:{
  34. showdialog:{
  35. default: false,
  36. type: Boolean
  37. },
  38. ishow:{
  39. default: true,
  40. type: Boolean
  41. },
  42. },
  43. data() {
  44. return{
  45. show: false
  46. }
  47. },
  48. computed:{
  49. ...mapState([
  50. 'hasLogin', // 用户是否同意授权
  51. 'userInfo', // 同意授权用户头像
  52. 'successlogion', // 同意手机号授权 登录成功
  53. 'sessionId', // 登录成功的token
  54. 'getcode', // logion时候获取的code session_key oppen_id
  55. ]),
  56. },
  57. onLoad(){
  58. console.log(this.successlogion);
  59. },
  60. methods:{
  61. ...mapMutations(['login','showDialog', 'setOpenid', 'setcode','islogionsuccess']),
  62. // 授权用户信息头像
  63. wxlogion() {
  64. let that = this;
  65.     uni.getUserInfo({
  66.      success: (res) => {
  67. console.log(res.userInfo);
  68. that.login(res.userInfo);
  69. that.showDialog(true); // 改变haslogion的值
  70. that.$msg("授权成功");
  71. if(this.successlogion) {
  72. uni.navigateBack();
  73. }
  74.      },
  75.      fail(res) {
  76. that.showDialog(false);
  77. that.$msg("授权失败")
  78.      }
  79.     })
  80. },
  81. // 获取手机号码
  82. decryptPhoneNumber(e) {
  83. console.log(e);
  84. if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
  85. console.log(e);
  86. } else {
  87. console.log('用户同意提供手机号');
  88. console.log(this.getcode);
  89. let encryptedData = e.detail.encryptedData;
  90. let iv = e.detail.iv;
  91. // 调取注册账号
  92. this.request({
  93. url: '/v1/entry/mp_account',
  94. method:'POST',
  95. data: {
  96. code: this.getcode.code,
  97. encryptedData: encryptedData,
  98. iv: iv
  99. },
  100. success: (res) => {
  101. let token = res.data.data.token;
  102. this.setOpenid(token);
  103. this.islogionsuccess(true);
  104. uni.navigateBack();
  105. }
  106. })
  107. }
  108. },
  109. }
  110. }
  111. </script>
  112. <style>
  113. </style>