h5WxLogin.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <template>
  2. <view class="content">
  3. <view class="force-login-wrap">
  4. <view class="force-login__content y-f">
  5. <view class="logo">
  6. <view class="logo-img">
  7. <image src="/static/logo.png"></image>
  8. </view>
  9. <view class="title">直播</view>
  10. </view>
  11. <view class="login-notice">为了提供更优质的服务,请先登录</view>
  12. <view class="btns">
  13. <view class="author-btn" @click="loginByMp()">微信授权登录</view>
  14. </view>
  15. </view>
  16. </view>
  17. </view>
  18. </template>
  19. <script>
  20. const isWechat = () => {
  21. return String(navigator.userAgent.toLowerCase().match(/MicroMessenger/i)) === "micromessenger";
  22. }
  23. // import { loginByMp } from '@/api/user'
  24. import { loginByWeChat } from '@/api/login'
  25. export default {
  26. data() {
  27. return {
  28. code:null,
  29. }
  30. },
  31. onLoad(option) {
  32. this.getWechatCode();
  33. // setTimeout(()=>{
  34. // this.goToLanch();
  35. // },2000);
  36. },
  37. onUnload() {
  38. },
  39. mounted() {
  40. },
  41. methods: {
  42. loginByMp(){
  43. if(this.code==null){
  44. return;
  45. }
  46. uni.showLoading({
  47. title:"处理中..."
  48. });
  49. let that=this;
  50. var data={code:this.code}
  51. loginByWeChat(data).then(res => {
  52. uni.hideLoading();
  53. if(res.code==200){
  54. uni.setStorageSync('AppToken',res.token);
  55. uni.setStorageSync('userInfo',JSON.stringify(res.user));
  56. uni.$emit('refreshIM');
  57. that.goToLanch();
  58. }
  59. else{
  60. uni.showToast({title: res.msg,icon: 'none'});
  61. }
  62. },
  63. err => {
  64. }
  65. );
  66. },
  67. getWechatCode() {
  68. if (isWechat) {
  69. let appid = "wx9ea36eecd281bcd3"; //微信APPid
  70. let code = this.getUrlCode().code; //是否存在code
  71. let local = window.location.href;
  72. if (code == null || code === "") {
  73. //不存在就打开上面的地址进行授权
  74. window.location.href =
  75. "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" +
  76. appid +
  77. "&redirect_uri=" +
  78. encodeURIComponent(local) +
  79. "&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
  80. } else {
  81. this.code = code;
  82. }
  83. }
  84. },
  85. getUrlCode(){
  86. // 截取url中的code方法
  87. var url = location.search;
  88. var theRequest = new Object();
  89. if (url.indexOf("?") != -1) {
  90. var str = url.substr(1);
  91. var strs = str.split("&");
  92. for (var i = 0; i < strs.length; i++) {
  93. theRequest[strs[i].split("=")[0]] = strs[i].split("=")[1];
  94. }
  95. }
  96. console.log(theRequest);
  97. return theRequest;
  98. },
  99. goToLanch(){
  100. let beforLoginUrl=uni.getStorageSync('beforLoginPage');
  101. console.log("beforLoginUrl:"+beforLoginUrl);
  102. uni.reLaunch({
  103. url:beforLoginUrl
  104. });
  105. }
  106. },
  107. }
  108. </script>
  109. <style lang="scss">
  110. .container {
  111. flex: 1;
  112. display: flex;
  113. flex-direction: column;
  114. justify-content: flex-start;
  115. position: relative;
  116. }
  117. .force-login-wrap {
  118. width: 100%;
  119. height: 100%;
  120. overflow: hidden;
  121. z-index: 11111;
  122. top: 0;
  123. .force-login__content {
  124. position: absolute;
  125. left: 50%;
  126. top: 40%;
  127. transform: translate(-50%, -50%);
  128. .logo{
  129. display: flex;
  130. flex-direction: column;
  131. justify-content: center;
  132. align-items: center;
  133. .logo-img{
  134. border: 4upx solid #FFFFFF;
  135. box-shadow: 0px 5px 15px 2px rgba(0,0,0,0.1);
  136. border-radius: 50%;
  137. width: 80px;
  138. height: 80px;
  139. image{
  140. border-radius: 50%;
  141. width: 100%;
  142. height: 100%;
  143. overflow: hidden;
  144. }
  145. }
  146. .title{
  147. margin-top: 20rpx;
  148. font-size: 35rpx;
  149. font-family: PingFang SC;
  150. font-weight: bold;
  151. color: #000;
  152. margin-bottom: 30rpx;
  153. }
  154. }
  155. .login-notice {
  156. font-size: 28rpx;
  157. font-family: PingFang SC;
  158. font-weight: 400;
  159. color: #000;
  160. line-height: 44rpx;
  161. width: 500rpx;
  162. text-align: center;
  163. margin-bottom: 80rpx;
  164. }
  165. .btns{
  166. position: relative;
  167. width: 630rpx;
  168. height: 80rpx;
  169. .author-btn{
  170. display: flex;
  171. justify-content: center;
  172. align-items: center;
  173. z-index:100;
  174. position: absolute;
  175. width: 630rpx;
  176. height: 80rpx;
  177. background: linear-gradient(to right, #FF5C03 0%, #E2C99E 100%);
  178. background: -moz-linear-gradient(to right, #FF5C03 0%, #E2C99E 100%);
  179. // box-shadow: 0px 7rpx 6rpx 0px rgba(229, 138, 0, 0.22);
  180. border-radius: 40rpx;
  181. font-size: 30rpx;
  182. font-family: PingFang SC;
  183. font-weight: 500;
  184. color: rgba(255, 255, 255, 1);
  185. }
  186. }
  187. }
  188. }
  189. </style>