login.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <template>
  2. <view>
  3. <view class="login-wrap">
  4. <view class="logo"><image src="../../static/logo.png"></image></view>
  5. <view class="wechat-title">微信授权登录</view>
  6. <view class="wechat-desc">获得您的公开信息(昵称、头像等),以便为您提供更好的服务</view>
  7. <view class="wechat-login-btn" @click="onWxMiniLogin">授权登录</view>
  8. <view class="wechat-go-home" @click="onGoHome">暂不登录</view>
  9. <view class="agreement-box" style="width: 80%;">
  10. <view class="agreement-text agree-text">
  11. <checkbox-group @change="checkboxChange">
  12. <checkbox value="1" class="protocol-check"></checkbox>我已阅读并遵守
  13. <view class="agree-text">
  14. <view>《用户协议》</view>
  15. <view>《隐私协议》</view>
  16. </view>
  17. </checkbox-group>
  18. </view>
  19. </view>
  20. </view>
  21. <loading v-if="isSubmit" :layer="true"></loading>
  22. </view>
  23. </template>
  24. <script>
  25. export default {
  26. data() {
  27. return {
  28. isSubmit:false,
  29. protocol: false,
  30. options:null
  31. }
  32. },
  33. onLoad(option) {
  34. this.options=option.item
  35. },
  36. methods: {
  37. onWxMiniLogin(){
  38. // if(!this.protocol){
  39. // this.isSubmit = false;
  40. // this.$utils.msg("请同意用户协议");
  41. // return ;
  42. // }
  43. var that = this;
  44. this.isSubmit = true;
  45. wx.getUserProfile({
  46. desc: '授权',
  47. success: (res) => {
  48. let userInfo = JSON.parse(res.rawData);
  49. wx.login({
  50. success: function (resData){
  51. let params = {};
  52. params.userInfo = userInfo;
  53. // params.code = resData.code;
  54. const userCode={code:resData.code}
  55. that.$http.wxgetCode(userCode).then(result=>{
  56. if(result.code === 0){
  57. const wxuserinfo={...result.data,...userInfo}
  58. console.log(wxuserinfo)
  59. that.$store.commit("UPDATEUSERS",wxuserinfo);
  60. //有token就存储token
  61. if(wxuserinfo.token){
  62. that.$store.commit("UPDATETOKEN",wxuserinfo.token);
  63. }
  64. //有手机号就存储手机号
  65. if(wxuserinfo.mobile){
  66. that.$store.commit("UPDATEMOBILE",wxuserinfo.mobile);
  67. }
  68. if(that.options === 'sancode'){
  69. that.$utils.switchTab('index/index');
  70. }
  71. if(that.options === 'ucenter'){
  72. that.$utils.switchTab('ucenter/index');
  73. }
  74. that.isSubmit = false;
  75. }
  76. else if(result.code === 1){
  77. that.$utils.msg(result.msg);
  78. that.isSubmit = false;
  79. }
  80. else{
  81. that.$utils.msg(result.message);
  82. that.isSubmit = false;
  83. }
  84. }).catch(error=>{
  85. that.isSubmit = false;
  86. that.$utils.msg(error);
  87. });
  88. },
  89. fail:function (err){
  90. that.isSubmit = false;
  91. that.$utils.msg(err);
  92. }
  93. })
  94. },
  95. fail: res=>{
  96. that.$utils.msg("您无权限请求该接口");
  97. }
  98. })
  99. },
  100. onGoHome(){
  101. this.$utils.switchTab("index/index");
  102. },
  103. checkboxChange(e){
  104. this.protocol = e.detail.value[0] != undefined;
  105. },
  106. }
  107. }
  108. </script>
  109. <style lang="scss" scoped>
  110. .agreement-box {
  111. width: 100%;
  112. margin: 30rpx auto 0;
  113. flex-direction: row;
  114. align-items: center;
  115. justify-content: center;
  116. .protocol-check {
  117. position: relative;top:-2px;transform:scale(0.6)
  118. }
  119. .agreement-text {
  120. width: 100%;
  121. font-size: 26rpx;
  122. font-weight: 500;
  123. color: #999999;
  124. .agree-text {
  125. display: inline-block;
  126. color: #2841c2;
  127. view{
  128. display: inline;
  129. }
  130. }
  131. }
  132. }
  133. .login-wrap {
  134. .logo {
  135. text-align: center;
  136. padding-top: 180rpx;
  137. image {
  138. width: 180rpx;
  139. height: 180rpx;
  140. }
  141. }
  142. .wechat-title {
  143. font-size: 35rpx;
  144. font-weight: 500;
  145. color: #333;
  146. margin-top: 24rpx;
  147. text-align: center;
  148. }
  149. .wechat-desc {
  150. font-size: 28rpx;
  151. font-weight: 500;
  152. color: #999;
  153. margin-top: 24rpx;
  154. text-align: center;
  155. padding: 10rpx 50rpx;
  156. }
  157. .wechat-login-btn {
  158. height: 80rpx; line-height: 80rpx;
  159. color: #fff; background-color: #58be6b;
  160. text-align: center; border-radius: 50rpx;
  161. margin: 50rpx; font-size: 32rpx;
  162. }
  163. .wechat-go-home { text-align: center; font-size: 30rpx; color:#666; }
  164. }
  165. </style>