login.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. if (res.data.code == 200) {
  40. this.disabled = false
  41. uni.setStorageSync("AppAuthorization",res.data.data.token);
  42. //成功获取code 然后需要用户点击 获取手机号
  43. if (res.data.data.hasMobile == false) {
  44. //判断是否绑定过手机号 如果false则去绑定
  45. this.showGetPhoenBtn = true
  46. }else{
  47. util.toastFunc('登录成功',()=>{
  48. // 返回上一级
  49. uni.navigateBack({
  50. delta: 1
  51. });
  52. })
  53. }
  54. }else{
  55. this.disabled = false
  56. }
  57. }else{
  58. //先去获取微信code
  59. await this.getWexinCode()
  60. }
  61. },
  62. async getWexinCode(){
  63. let that = this
  64. uni.login({
  65. "provider": "weixin",
  66. "onlyAuthorize": true, // 微信登录仅请求授权认证
  67. success: function(event){
  68. const {code} = event
  69. //客户端成功获取授权临时票据(code),向业务服务器发起登录请求。
  70. uni.setStorageSync('wxcode',code)
  71. }
  72. })
  73. },
  74. // 获取用户手机号
  75. getPhoneNumber(e){
  76. console.log(e.detail)
  77. if(e.detail.errMsg == "getPhoneNumber:ok"){ // 用户允许或去手机号
  78. this.getphone(e.detail.code)
  79. }
  80. },
  81. async getphone(code){
  82. this.disabledPhone = true
  83. let res = await bindphone(code)
  84. if (res.data.code == 200) {
  85. this.disabledPhone = false
  86. util.toastFunc('登录成功',()=>{
  87. // 返回上一级
  88. uni.navigateBack({
  89. delta: 1
  90. });
  91. })
  92. }else{
  93. this.disabledPhone = false
  94. util.toastFunc('绑定手机号失败,请重新操作')
  95. }
  96. },
  97. }
  98. }
  99. </script>
  100. <style lang="scss">
  101. .bg{
  102. width: 750rpx;
  103. height: auto;
  104. min-height: 100vh;
  105. background: #F1F1F1;
  106. border-radius: 0rpx 0rpx 0rpx 0rpx;
  107. display: flex;
  108. flex-direction: column;
  109. align-items: center;
  110. }
  111. .btn{
  112. width: 702rpx;
  113. height: 80rpx;
  114. background: #46A6FF;
  115. border-radius: 64rpx 64rpx 64rpx 64rpx;
  116. opacity: 1;
  117. margin-top: 200rpx;
  118. font-size: 28rpx;
  119. font-weight: 500;
  120. color: #FFFFFF;
  121. line-height: 80rpx;
  122. text-align: center;
  123. }
  124. </style>