login.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <template>
  2. <view class="bg">
  3. <button
  4. class="btn"
  5. hover-class="button-hover"
  6. @click="login"
  7. :disabled="disabled"
  8. v-show="!showGetPhoenBtn"
  9. >
  10. 微信登录
  11. </button>
  12. <button class="btn" :disabled="disabledPhone" open-type="getPhoneNumber" v-show="showGetPhoenBtn"
  13. @getphonenumber="getPhoneNumber">授权手机号</button>
  14. </view>
  15. </template>
  16. <script>
  17. import {
  18. login,bindphone
  19. } from '@/http/api/common.js'
  20. import env from "@/http/config/config.js"
  21. import * as util from '@/pages/util/util.js'
  22. export default {
  23. data() {
  24. return {
  25. showGetPhoenBtn:false,
  26. disabled:false,
  27. disabledPhone:false,
  28. }
  29. },
  30. onLoad(e) {
  31. },
  32. methods: {
  33. async login(){
  34. //先去获取微信code
  35. await this.getWexinCode()
  36. if (uni.getStorageSync('wxcode')) {
  37. this.disabled = true
  38. let res = await login(uni.getStorageSync('wxcode'))
  39. console.log('获取微信登录',res);
  40. if (res.data.code == 200) {
  41. this.disabled = false
  42. uni.setStorageSync("AppAuthorization",res.data.data.token);
  43. //成功获取code 然后需要用户点击 获取手机号
  44. if (res.data.data.hasMobile == false) {
  45. util.toastFunc('第一次登录请绑定手机号')
  46. //判断是否绑定过手机号 如果false则去绑定
  47. this.showGetPhoenBtn = true
  48. }else{
  49. util.toastFunc('登录成功',()=>{
  50. // 返回上一级
  51. uni.navigateBack({
  52. delta: 1
  53. });
  54. })
  55. }
  56. }else{
  57. this.disabled = false
  58. }
  59. }else{
  60. //先去获取微信code
  61. await this.getWexinCode()
  62. }
  63. },
  64. async getWexinCode(){
  65. let that = this
  66. uni.login({
  67. "provider": "weixin",
  68. "onlyAuthorize": true, // 微信登录仅请求授权认证
  69. success: function(event){
  70. const {code} = event
  71. //客户端成功获取授权临时票据(code),向业务服务器发起登录请求。
  72. uni.setStorageSync('wxcode',code)
  73. }
  74. })
  75. },
  76. // 获取用户手机号
  77. getPhoneNumber(e){
  78. console.log(e.detail)
  79. if(e.detail.errMsg == "getPhoneNumber:ok"){ // 用户允许或去手机号
  80. this.getphone(e.detail.code)
  81. }
  82. },
  83. async getphone(code){
  84. this.disabledPhone = true
  85. let res = await bindphone(code)
  86. console.log('获取手机号',res);
  87. if (res.data.code == 200) {
  88. this.disabledPhone = false
  89. util.toastFunc('登录成功',()=>{
  90. // 返回上一级
  91. uni.navigateBack({
  92. delta: 1
  93. });
  94. })
  95. }else{
  96. this.disabledPhone = false
  97. util.toastFunc('绑定手机号失败,请重新操作')
  98. }
  99. },
  100. }
  101. }
  102. </script>
  103. <style lang="scss">
  104. .bg{
  105. width: 750rpx;
  106. height: auto;
  107. min-height: 100vh;
  108. background: #F1F1F1;
  109. border-radius: 0rpx 0rpx 0rpx 0rpx;
  110. display: flex;
  111. flex-direction: column;
  112. align-items: center;
  113. }
  114. .btn{
  115. width: 702rpx;
  116. height: 80rpx;
  117. background: #46A6FF;
  118. border-radius: 64rpx 64rpx 64rpx 64rpx;
  119. opacity: 1;
  120. margin-top: 200rpx;
  121. font-size: 28rpx;
  122. font-weight: 500;
  123. color: #FFFFFF;
  124. line-height: 80rpx;
  125. text-align: center;
  126. }
  127. </style>