login.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <template>
  2. <view class="bg">
  3. <view class="p"><image src="../../static/profile.jpg" class="pic" mode="widthFix"></image></view>
  4. <view class="title">谢通门智慧旅游登录使用:</view>
  5. <view class="info">
  6. <text v-if="first">您的公开信息(昵称,头像等)</text>
  7. <text v-else>您的手机号</text>
  8. </view>
  9. <button class="btn" @click="getUserProfile()" v-if="first" :disabled="disabled">获取信息</button>
  10. <button class="btn" open-type="getPhoneNumber" :disabled="disabled" @getphonenumber="getPhoneNumber" style="background-color: #2196F3" v-else>获取手机号</button>
  11. <button class="btn" style="background-color: #9E9E9E;" @click="cancel()">取消</button>
  12. </view>
  13. </template>
  14. <script>
  15. export default {
  16. data() {
  17. return {
  18. first: true, //是否第一次使用,第一次使用需要绑定手机号
  19. disabled: false //防止重复点击
  20. };
  21. },
  22. methods: {
  23. getUserProfile() {
  24. this.disabled = true;
  25. let code = '';
  26. uni.login({
  27. provider: 'weixin',
  28. success: res => {
  29. code = res.code;
  30. }
  31. });
  32. //获取用户信息
  33. uni.getUserProfile({
  34. desc: '用于完善会员资料',
  35. lang: 'zh_CN',
  36. provider: 'weixin',
  37. success: info => {
  38. this.$http.request({
  39. url: this.$http.urls.wxLogin + code,
  40. data: info,
  41. method: 'POST',
  42. success: res => {
  43. this.disabled = false;
  44. res.data.data.memberInfo.apiToken = res.data.data.apiToken;
  45. uni.setStorageSync('user', res.data.data.memberInfo);
  46. //未绑定手机号
  47. if (res.data.data.memberInfo.mobile == null || res.data.data.memberInfo.mobile == '') {
  48. this.first = false;
  49. } else {
  50. uni.navigateBack({ delta: 1 });
  51. }
  52. }
  53. });
  54. },
  55. fail: res => {
  56. this.disabled = false;
  57. }
  58. });
  59. },
  60. // 绑定用户手机号
  61. getPhoneNumber(e) {
  62. let item = {};
  63. uni.login({
  64. provider: 'weixin',
  65. success: res => {
  66. this.disabled = false;
  67. item.code = res.code;
  68. item.encryptedData = e.target.encryptedData;
  69. item.iv = e.target.iv;
  70. this.$http.request({
  71. url: this.$http.urls.bindWxMobile,
  72. data: item,
  73. method: 'POST',
  74. success: r => {
  75. uni.navigateBack({ delta: 1 });
  76. }
  77. });
  78. }
  79. });
  80. },
  81. cancel() {
  82. uni.navigateBack();
  83. }
  84. }
  85. };
  86. </script>
  87. <style lang="less">
  88. page {
  89. background-color: white;
  90. }
  91. .bg {
  92. padding: 30px;
  93. .p {
  94. text-align: center;
  95. padding: 15px;
  96. .pic {
  97. width: 100px;
  98. height: 100px;
  99. border-radius: 50%;
  100. border: 1px solid #9e9e9e;
  101. }
  102. }
  103. .title {
  104. font-weight: bold;
  105. font-size: 16px;
  106. }
  107. .info {
  108. color: #8b918d;
  109. margin-top: 10px;
  110. .icon {
  111. padding-right: 3px;
  112. }
  113. }
  114. .btn {
  115. margin-top: 20px;
  116. width: 75%;
  117. border-radius: 20px;
  118. }
  119. }
  120. </style>