login.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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">允许使用</button>
  10. <button class="btn" open-type="getPhoneNumber" @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. };
  20. },
  21. methods: {
  22. getUserProfile() {
  23. let code = '';
  24. uni.login({
  25. provider: 'weixin',
  26. success: res => {
  27. code = res.code;
  28. }
  29. });
  30. //获取用户信息
  31. uni.getUserProfile({
  32. desc: '用于完善会员资料',
  33. lang: 'zh_CN',
  34. provider: 'weixin',
  35. success: info => {
  36. this.$http.request({
  37. url: this.$http.urls.wxLogin + code,
  38. data: info,
  39. method: 'POST',
  40. success: res => {
  41. res.data.data.memberInfo.apiToken = res.data.data.apiToken;
  42. uni.setStorageSync('user', res.data.data.memberInfo);
  43. //未绑定手机号
  44. if (res.data.data.memberInfo.mobile == null || res.data.data.memberInfo.mobile == '') {
  45. this.first = false;
  46. } else {
  47. uni.navigateBack({ delta: 1 });
  48. }
  49. }
  50. });
  51. },
  52. fail: res => {
  53. this.click = true;
  54. }
  55. });
  56. },
  57. // 绑定用户手机号
  58. getPhoneNumber(e) {
  59. let item = {};
  60. uni.login({
  61. provider: 'weixin',
  62. success: res => {
  63. item.code = res.code;
  64. item.encryptedData = e.target.encryptedData;
  65. item.iv = e.target.iv;
  66. this.$http.request({
  67. url: this.$http.urls.bindWxMobile,
  68. data: item,
  69. method: 'POST',
  70. success: r => {
  71. uni.navigateBack({ delta: 1 });
  72. }
  73. });
  74. }
  75. });
  76. },
  77. cancel() {
  78. uni.navigateBack();
  79. }
  80. }
  81. };
  82. </script>
  83. <style lang="less">
  84. page {
  85. background-color: white;
  86. }
  87. .bg {
  88. padding: 30px;
  89. .p {
  90. text-align: center;
  91. padding: 15px;
  92. .pic {
  93. width: 100px;
  94. height: 100px;
  95. border-radius: 50%;
  96. }
  97. }
  98. .title {
  99. font-weight: bold;
  100. font-size: 16px;
  101. }
  102. .info {
  103. color: #8b918d;
  104. margin-top: 10px;
  105. .icon {
  106. padding-right: 3px;
  107. }
  108. }
  109. .btn {
  110. margin-top: 20px;
  111. }
  112. }
  113. </style>