login.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <template>
  2. <view class="login">
  3. <view>
  4. <view>
  5. <view class="header">
  6. <image src="/static/img/tabbar/logo.jpg"></image>
  7. <text>郑州新明一中学生资料库</text>
  8. </view>
  9. <view class="content">
  10. <view>申请获取以下权限</view>
  11. <text>获得你的公开信息(手机号,昵称,头像等)</text>
  12. </view>
  13. <!-- <button class='bottom' type='primary' open-type="getUserInfo" lang="zh_CN" @getuserinfo="getUserInfo">
  14. 授权登录
  15. </button> -->
  16. <u-button v-if="!show" class="bottom" type="success" open-type="getUserInfo" @click="getUserProfile">授权登录</u-button>
  17. <u-button v-if="show" class="bottom" type="success" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">手机号绑定</u-button>
  18. </view>
  19. </view>
  20. </view>
  21. </template>
  22. <script>
  23. // import store from '@/store/index.js'
  24. export default {
  25. data() {
  26. return {
  27. // SessionKey: '',
  28. // OpenId: '',
  29. // nickName: null,
  30. // avatarUrl: null,
  31. userInfo: {},
  32. // 手机号授权
  33. show: false,
  34. info: {}
  35. };
  36. },
  37. onLoad() {},
  38. onShow() {},
  39. computed: {},
  40. methods: {
  41. getPhoneNumber(e) {
  42. if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
  43. this.$api.toast('您已拒绝提供手机号');
  44. } else {
  45. let that = this;
  46. uni.login({
  47. provider: 'weixin',
  48. success: res2 => {
  49. // console.log(this.userInfo)
  50. let params = {
  51. token: that.info.token,
  52. code: res2.code,
  53. iv: e.detail.iv,
  54. encryptedData: e.detail.encryptedData
  55. };
  56. that.$api.request('user/bindPhone', params).then(res => {
  57. console.log("手机号登录授权",res);
  58. if (res.code == 200) {
  59. that.$api.toast('手机号绑定成功!');
  60. uni.setStorageSync('us_token', res.data.token);
  61. this.$store.dispatch('getUserInfo');
  62. uni.reLaunch({
  63. url: '/pages/index/index'
  64. });
  65. } else {
  66. that.$api.toast(res.msg);
  67. }
  68. });
  69. },
  70. fail: () => {
  71. uni.showToast({
  72. title: '微信登录授权失败',
  73. icon: 'none'
  74. });
  75. }
  76. });
  77. }
  78. },
  79. // 获取用户信息
  80. getUserProfile() {
  81. let that = this;
  82. wx.getUserProfile({
  83. desc: '用于完善会员资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
  84. success: res => {
  85. console.log(res);
  86. // 用户信息
  87. that.userInfo = res.userInfo;
  88. // 获取code
  89. uni.login({
  90. provider: 'weixin',
  91. success: res2 => {
  92. that.$api
  93. .request('login/toLogin', {
  94. code: res2.code,
  95. avatar: that.userInfo.avatarUrl,
  96. nickname: that.userInfo.nickName,
  97. yqm: uni.getStorageSync('yqm')||'',
  98. })
  99. .then(res3 => {
  100. console.log("单纯授权",res3);
  101. //是否绑定手机号: 0= 否 1=是
  102. if (res3.data.is_bind_phone == 0) {
  103. that.$api.toast('请绑定手机号');
  104. that.show = true;
  105. that.info = res3.data;
  106. } else {
  107. uni.setStorageSync('us_token', res3.data.token);
  108. uni.reLaunch({
  109. url: '/pages/index/index'
  110. });
  111. this.$store.dispatch('getUserInfo');
  112. }
  113. });
  114. },
  115. fail: () => {
  116. uni.showToast({
  117. title: '微信登录授权失败',
  118. icon: 'none'
  119. });
  120. }
  121. });
  122. }
  123. });
  124. }
  125. }
  126. };
  127. </script>
  128. <style lang="scss" scoped>
  129. .header {
  130. margin: 90rpx 0 90rpx 50rpx;
  131. border-bottom: 2rpx solid #ccc;
  132. text-align: center;
  133. width: 650rpx;
  134. height: 300rpx;
  135. // line-height: 450rpx;
  136. }
  137. .header image {
  138. width: 200rpx;
  139. height: 200rpx;
  140. margin: 0 auto;
  141. }
  142. .header text{
  143. margin-top: 25rpx;
  144. }
  145. .content {
  146. margin-left: 50rpx;
  147. margin-bottom: 90rpx;
  148. }
  149. .content text {
  150. display: block;
  151. color: #9d9d9d;
  152. margin-top: 40rpx;
  153. }
  154. .bottom {
  155. border-radius: 80rpx;
  156. margin: 70rpx 50rpx;
  157. font-size: 35rpx;
  158. }
  159. </style>