wxlogin.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <template>
  2. <view class="">
  3. <!-- 顶部导航 -->
  4. <fa-navbar title="授权登录"></fa-navbar>
  5. <view class="content">
  6. <view>
  7. <view class="login-item">
  8. <view class="logo"><open-data type="userAvatarUrl"></open-data></view>
  9. </view>
  10. <view class="login-tip">
  11. <view>申请获取以下权限</view>
  12. <view>获得你的公开信息 (昵称、头像等)</view>
  13. </view>
  14. </view>
  15. <view class="u-flex u-row-between">
  16. <!-- #ifdef MP-WEIXIN -->
  17. <u-button hover-class="none" shape="circle" @click="handleRefuse" class="u-flex-6" :custom-style="{ width: '95%' }">拒绝</u-button>
  18. <u-button
  19. hover-class="none"
  20. type="primary"
  21. shape="circle"
  22. class="u-flex-6"
  23. :custom-style="{ width: '95%', backgroundColor: theme.bgColor, color: theme.color }"
  24. @click="getUserInfo"
  25. >
  26. 允许
  27. </u-button>
  28. <!-- #endif -->
  29. <!-- #ifdef MP-ALIPAY -->
  30. <u-button hover-class="none" shape="circle" @click="getALICode">授权登录</u-button>
  31. <!-- #endif -->
  32. </view>
  33. <u-top-tips ref="uTips"></u-top-tips>
  34. </view>
  35. </view>
  36. </template>
  37. <script>
  38. import { loginfunc } from '@/common/fa.mixin.js';
  39. export default {
  40. mixins: [loginfunc],
  41. onLoad(e) {
  42. this.index = parseInt(e.index) || 2;
  43. },
  44. data() {
  45. return {
  46. index: 2
  47. };
  48. },
  49. methods: {
  50. handleRefuse() {
  51. this.$u.toast('未授权');
  52. setTimeout(() => {
  53. this.$Router.back(1);
  54. }, 1000);
  55. },
  56. getCode: async function() {
  57. return new Promise((resolve, reject) => {
  58. uni.login({
  59. // #ifdef MP-ALIPAY
  60. scopes: 'auth_user',
  61. // #endif
  62. success: function(res) {
  63. if (res.code) {
  64. resolve(res.code);
  65. } else {
  66. //login成功,但是没有取到code
  67. reject('未取得code');
  68. }
  69. },
  70. fail: function(res) {
  71. reject('用户授权失败wx.login');
  72. }
  73. });
  74. });
  75. },
  76. //用户授权得到用户的信息
  77. // #ifdef MP-WEIXIN
  78. getUserInfo: function(e) {
  79. let that = this;
  80. wx.getUserProfile({
  81. lang: 'zh',
  82. desc: '用户信息绑定',
  83. success: async function(e) {
  84. console.log(e);
  85. try {
  86. let code = await that.getCode();
  87. let data = {
  88. code: code,
  89. rawData: e.userInfo,
  90. __token__: that.vuex__token__
  91. };
  92. //有推荐码的话,带上
  93. if (that.vuex_invitecode) {
  94. data.invitecode = that.vuex_invitecode;
  95. }
  96. that.toLogin(data);
  97. } catch (e) {
  98. that.$u.toast(e);
  99. }
  100. },
  101. fail: function(e) {
  102. console.log(e);
  103. that.$u.toast(JSON.stringify(e));
  104. }
  105. });
  106. },
  107. // #endif
  108. //实际的去登陆
  109. toLogin: async function(data) {
  110. let res = await this.$api.gowxLogin(data);
  111. if (!res.code) {
  112. this.$u.toast(res.msg);
  113. return;
  114. }
  115. if (res.data.user) {
  116. this.$u.vuex('vuex_token', res.data.user.token);
  117. this.success(this.index);
  118. return;
  119. }
  120. this.$u.vuex('vuex_third', res.data.third);
  121. //授权成功到登录或绑定页面
  122. this.$Router.push({ path: '/pages/login/register', query: { bind: 'bind' } });
  123. },
  124. // #ifdef MP-ALIPAY
  125. getALICode() {
  126. let that = this;
  127. uni.login({
  128. scopes: 'auth_user',
  129. success: res => {
  130. if (res.authCode) {
  131. uni.getUserInfo({
  132. provider: 'alipay',
  133. success: function(infoRes) {
  134. if (infoRes.errMsg == 'getUserInfo:ok') {
  135. let user_info = {
  136. nickname: infoRes.nickName,
  137. avatar: infoRes.avatar
  138. };
  139. that.aLiLoginStep1(res.authCode, user_info);
  140. }
  141. },
  142. fail: function(errorRes) {
  143. this.$u.toast('未取得用户昵称头像信息');
  144. }
  145. });
  146. } else {
  147. this.$u.toast('未取得code');
  148. }
  149. },
  150. fail: function(res) {
  151. this.$u.toast('用户授权失败my.login');
  152. }
  153. });
  154. },
  155. aLiLoginStep1: async function(code, user_info) {
  156. let data = {
  157. code: code,
  158. user_info: user_info
  159. };
  160. let res = await this.$api.alilogin1(data);
  161. if (!res.code) {
  162. this.$u.toast(res.msg);
  163. }
  164. }
  165. // #endif
  166. }
  167. };
  168. </script>
  169. <style lang="scss">
  170. .content {
  171. background-color: #fff;
  172. height: 100vh;
  173. padding: 100rpx 60rpx 0;
  174. }
  175. .login-item {
  176. display: flex;
  177. justify-content: center;
  178. padding-bottom: 40rpx;
  179. border-bottom: 1rpx solid #dddddd;
  180. }
  181. .logo {
  182. display: block;
  183. width: 180rpx;
  184. height: 180rpx;
  185. border-radius: 50%;
  186. overflow: hidden;
  187. border: 2px solid #fff;
  188. box-shadow: 3px 3px 10px rgba(0, 0, 0, 0.2);
  189. }
  190. .login-tip {
  191. padding: 60rpx 0;
  192. &-big {
  193. font-size: 28rpx;
  194. line-height: 80rpx;
  195. }
  196. &-small {
  197. font-size: 12px;
  198. color: #9e9e9e;
  199. }
  200. }
  201. </style>