login.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <template>
  2. <view style="height: 100%;">
  3. <image mode="widthFix" src="/static/login_bg.jpg" class="full_img"></image>
  4. <u-navbar title="登录" background="{ background: '#ffffff',opacity:0.4; }" title-color="#ffffff" back-icon-color="#ffffff"></u-navbar>
  5. <view style="padding: 0 40rpx;margin-top: 90%;"><u-button open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">微信授权登录</u-button></view>
  6. </view>
  7. </template>
  8. <script>
  9. export default {
  10. data() {
  11. return {
  12. code: ''
  13. };
  14. },
  15. mounted() {},
  16. methods: {
  17. getPhoneNumber(e) {
  18. let that = this;
  19. uni.checkSession({
  20. success () {
  21. //session_key 未过期,并且在本生命周期一直有效
  22. that.putInfo(e)
  23. },
  24. fail () {
  25. // session_key 已经失效,需要重新执行登录流程
  26. uni.login({
  27. provider: 'weixin',
  28. success: function(loginRes) {
  29. that.code = loginRes.code;
  30. that.putInfo(e)
  31. }
  32. });
  33. }
  34. })
  35. },
  36. putInfo(e){
  37. let that = this;
  38. if (e.detail.encryptedData) {
  39. let inviteCode = uni.getStorageSync("inviteCode")
  40. //用户同意授权
  41. var datas = {
  42. iv: e.detail.iv,
  43. encryptedData: e.detail.encryptedData,
  44. code: that.code
  45. };
  46. if(inviteCode){
  47. datas.inviteCode = inviteCode
  48. }
  49. that.$api.login(datas).then(
  50. res => {
  51. if (res.data.code == 200) {
  52. uni.setStorageSync('union_id', res.data.data.union_id);
  53. uni.setStorageSync('token', res.data.data.token);
  54. that.$api.getInfo().then(resdata => {
  55. if(resdata.data.code == 200){
  56. uni.navigateBack();
  57. that.$store.state.userInfo = resdata.data.data;
  58. }
  59. });
  60. } else {
  61. uni.showModal({
  62. title: '提示',
  63. content: res.data.msg,
  64. showCancel: false
  65. });
  66. }
  67. },
  68. err => {
  69. console.log(err);
  70. }
  71. );
  72. }
  73. }
  74. },
  75. onLoad(option) {
  76. let that = this;
  77. this.from = option.from;
  78. uni.login({
  79. provider: 'weixin',
  80. success: function(loginRes) {
  81. that.code = loginRes.code;
  82. }
  83. });
  84. }
  85. };
  86. </script>
  87. <style>
  88. .u-border-bottom:after,
  89. .u-border-left:after,
  90. .u-border-right:after,
  91. .u-border-top-bottom:after,
  92. .u-border-top:after,
  93. .u-border:after {
  94. border: none !important;
  95. }
  96. page {
  97. background-color: #bee0ff;
  98. height: 100%;
  99. width: 100%;
  100. }
  101. </style>
  102. <style scoped>
  103. .full_img {
  104. position: absolute;
  105. left: 0;
  106. display: block;
  107. width: 100%;
  108. z-index: -999;
  109. top: 0;
  110. }
  111. .head {
  112. height: 96rpx;
  113. width: 100%;
  114. line-height: 96rpx;
  115. margin-top: 40rpx;
  116. text-align: center;
  117. display: flex;
  118. position: relative;
  119. justify-content: center;
  120. }
  121. .icon {
  122. position: absolute;
  123. left: 30rpx;
  124. }
  125. </style>