index.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <template>
  2. <view style="width: auto; display: inline-block; padding: 0; margin: 0;">
  3. <template v-if="canIUseGetUserProfile">
  4. <button :style="styleObj" :class="className" @click="getUserProfile">{{btnText}}</button>
  5. </template>
  6. <template v-else>
  7. <button :style="styleObj" :class="className" open-type="getUserInfo" @getuserinfo="agreeGetUser"
  8. lang="zh_CN">{{btnText}}</button>
  9. </template>
  10. </view>
  11. </template>
  12. <script>
  13. const app = getApp();
  14. import api from '@/utils/api'
  15. import util from '@/utils/util'
  16. import __config from '@/config/env';
  17. export default {
  18. props: {
  19. btnText: {
  20. type: String,
  21. default: '微信授权登录'
  22. },
  23. className: {
  24. type: String,
  25. default: 'cu-btn bg-blue'
  26. },
  27. styleObj: {
  28. type: Object,
  29. default: () => {
  30. }
  31. },
  32. },
  33. data() {
  34. return {
  35. canIUseGetUserProfile: false,
  36. };
  37. },
  38. mounted() {
  39. if (uni.getUserProfile) {
  40. this.canIUseGetUserProfile = true
  41. }
  42. },
  43. methods: {
  44. getWxSession() {
  45. return new Promise((resolve, reject) => {
  46. uni.login({
  47. success: function(res) {
  48. if (res.code) {
  49. let params = {
  50. jsCode: res.code
  51. }
  52. api.doGet('/openapi/ma/common/login', params).then(res => {
  53. let session = res.data;
  54. resolve(session)
  55. });
  56. }
  57. },
  58. fail: function(error) {
  59. console.error('小程序wxSession信息出错: ' + error)
  60. resolve(error)
  61. }
  62. });
  63. })
  64. },
  65. agreeGetUser(e) {
  66. let _this = this
  67. if (e.detail.errMsg == 'getUserInfo:ok') {
  68. let eDetail = e.detail
  69. _this.getWxSession().then(wxSession => {
  70. let params = Object.assign(eDetail, wxSession, {
  71. accountId: app.getAccountId()
  72. })
  73. console.log('agreeGetUser', eDetail)
  74. api.doPost('/openapi/ma/common/decryptUserInfo', params).then(res => {
  75. if (res.code == 200) {
  76. let wxMemberInfo = util.parseUser(res.data)
  77. uni.setStorageSync(__config.loginWxUserInfoKey, wxMemberInfo)
  78. /* 授权完成触发回调 done 方法 */
  79. _this.$emit("done", wxMemberInfo)
  80. } else {
  81. uni.showToast({
  82. title: res.msg,
  83. })
  84. }
  85. });
  86. })
  87. }
  88. },
  89. getUserProfile(e) {
  90. // 推荐使用wx.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认
  91. // 开发者妥善保管用户快速填写的头像昵称,避免重复弹窗
  92. let _this = this
  93. wx.getUserProfile({
  94. desc: '用于完善会员资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
  95. success: (detail) => {
  96. let eDetail = detail
  97. _this.getWxSession().then(wxSession => {
  98. let params = Object.assign(eDetail, wxSession, {
  99. accountId: app.getAccountId()
  100. })
  101. console.log('getUserProfile', eDetail)
  102. api.doPost('/openapi/ma/common/decryptUserInfo', params).then(res => {
  103. if (res.code == 200) {
  104. let wxMemberInfo = util.parseUser(res.data)
  105. uni.setStorageSync(__config.loginWxUserInfoKey,
  106. wxMemberInfo)
  107. /* 授权完成触发回调 done 方法 */
  108. _this.$emit("done", wxMemberInfo)
  109. } else {
  110. uni.showToast({
  111. title: res.msg,
  112. })
  113. }
  114. });
  115. })
  116. }
  117. })
  118. },
  119. }
  120. }
  121. </script>
  122. <style lang="scss">
  123. </style>