wx-login.vue 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <template>
  2. <fs-modal v-model="visible" :showTitle="false" :showCancel="false" :showConfirm="false" :maskClickable="maskClickable">
  3. <view style="padding: 20rpx;">
  4. <view class="title bold" style="font-size:20px;margin-bottom: 60rpx;">授权微信访问</view>
  5. <!-- <view class="content" style="font-size: 15px;">
  6. 为了保护他人的隐私,授权后可查看对方的名片
  7. </view> -->
  8. <view class="wxlogin-btn">
  9. <fs-button full round type="success" @click="getUserProfile" v-if="canIUseGetUserProfile">授权微信</fs-button>
  10. <fs-button full round type="success" open-type="getUserInfo" @getuserinfo="getUserInfo" v-else>授权微信</fs-button>
  11. </view>
  12. <view class="footer">
  13. <fs-checkbox-group v-model="checkboxArray" checkedColorType="primary">
  14. <fs-checkbox value="1">
  15. <view class="sub">
  16. 同意<text @click.stop="handleRoute('/modules/common/about/agreement')">《用户服务协议》</text>和<text @click.stop="handleRoute('/modules/common/about/policy')">《隐私政策》</text>
  17. </view>
  18. </fs-checkbox>
  19. </fs-checkbox-group>
  20. </view>
  21. </view>
  22. </fs-modal>
  23. </template>
  24. <script setup>
  25. import { ref, computed } from 'vue'
  26. import { wxLogin } from '@/services/common'
  27. const props = defineProps({
  28. modelValue: Boolean,
  29. maskClickable: {
  30. type: Boolean,
  31. default: true
  32. },
  33. })
  34. const emits = defineEmits(['update:modelValue', 'success'])
  35. const visible = computed(
  36. {
  37. get: () => props.modelValue,
  38. set: value => emits('update:modelValue', value)
  39. }
  40. )
  41. const checkboxArray = ref(['1'])
  42. const canIUseGetUserProfile = ref(wx.getUserProfile ? true : false)
  43. const getUserProfile = () => {
  44. if (!checkboxArray.value.length) {
  45. return uni.showToast({
  46. title: '请勾选用户协议和隐私政策',
  47. icon: 'none'
  48. })
  49. }
  50. wx.getUserProfile({
  51. desc: '用于完善会员资料',
  52. }).then(res => {
  53. login(res)
  54. })
  55. }
  56. const getUserInfo = e => {
  57. if (!checkboxArray.value.length) {
  58. return uni.showToast({
  59. title: '请先勾选用户协议和隐私政策',
  60. icon: 'none'
  61. })
  62. }
  63. login(e)
  64. }
  65. const closeModal = () => {
  66. emits('update:modelValue', false)
  67. }
  68. const login = data => {
  69. wxLogin(data).then(() => {
  70. closeModal()
  71. emits('success')
  72. }).catch(() => {
  73. closeModal()
  74. })
  75. }
  76. const handleRoute = url => {
  77. uni.navigateTo({
  78. url
  79. })
  80. }
  81. </script>
  82. <style scoped>
  83. .wxlogin-btn{
  84. margin: 30rpx 0;
  85. }
  86. </style>