pcLogin.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <template>
  2. <view class="pcLogins">
  3. <u-navbar
  4. :is-back="false"
  5. title="登录"
  6. :border-bottom="false"
  7. title-color="#333333"
  8. back-icon-color="#ffffff"
  9. >
  10. </u-navbar>
  11. <u-line color="#D6D6DB" />
  12. <view class="contents">
  13. <!-- 正文内容 -->
  14. <image
  15. class="logo"
  16. :src="SCAN_LOGO"
  17. style="width: 360rpx; height: 72rpx"
  18. ></image>
  19. <text>登录后您可在网页端继续浏览课程</text>
  20. <!-- <view class="login_bt" @click="pcLogin()">微信登录</view> -->
  21. <button
  22. type="default"
  23. open-type="getPhoneNumber"
  24. @getphonenumber="wxLogin"
  25. class="login_bt"
  26. >
  27. 微信登录
  28. </button>
  29. </view>
  30. </view>
  31. </template>
  32. <script>
  33. import config from "@/common/config";
  34. export default {
  35. data() {
  36. return {
  37. SCAN_LOGO: config.SCAN_LOGO,
  38. scanCode: "", // 获取扫码的路径最后面的6位标识码
  39. code: "",
  40. };
  41. },
  42. onLoad(query) {
  43. const q = decodeURIComponent(query.q);
  44. this.scanCode = q.substring(q.length - 6);
  45. console.log("扫描后得:", q, this.scanCode);
  46. // 扫描二维码后调用,小程序已扫码
  47. if (this.scanCode) {
  48. this.$api.scanhasCode({ scanCode: this.scanCode }).then((res) => {
  49. console.log("调用扫码接口返回的:", res);
  50. if (res.data.code == 200) {
  51. console.log();
  52. }
  53. });
  54. }
  55. },
  56. onShow() {
  57. // 获取code
  58. this.getwxCode();
  59. },
  60. methods: {
  61. getwxCode() {
  62. uni.login({
  63. provider: "weixin",
  64. success: (loginRes) => {
  65. this.code = loginRes.code;
  66. console.log("获取的code:", this.code);
  67. },
  68. });
  69. },
  70. wxLogin(e) {
  71. this.$api
  72. .wxLogin({
  73. code: this.code,
  74. encryptedData: e.detail.encryptedData,
  75. iv: e.detail.iv,
  76. })
  77. .then((res) => {
  78. if (res.data.code == 200) {
  79. this.loginCallback(res);
  80. } else {
  81. if (res.data.code == 666) {
  82. this.$u.toast(res.data.msg);
  83. setTimeout(() => {
  84. uni.navigateTo({
  85. url: "/pages2/register/register" ,
  86. });
  87. }, 500);
  88. return;
  89. }
  90. this.getwxCode(); // code用完一次就会过期,防止用户点击取消后再点微信登录后code过期
  91. }
  92. });
  93. },
  94. // 微信登录成功的回调
  95. loginCallback(res) {
  96. // console.log('登录后的回调',res)
  97. // if(res.data.data && res.data.data.full_info){
  98. //信息完善,直接进入页面
  99. uni.setStorageSync("user_account", res.data.data.user_account);
  100. uni.setStorageSync("token", res.data.data.token);
  101. // /app/user/getInfo 登录用户信息// fromPlat来源平台 1小程序 2PC网站
  102. this.$api.getInfo({ fromPlat: 1 }).then((resdata) => {
  103. if (resdata.data.code == 200) {
  104. this.$store.state.userInfo = resdata.data.data;
  105. this.submitCode();
  106. }
  107. });
  108. // } else {
  109. // //未完善信息,存为临时信息
  110. // uni.setStorageSync('user_account_temp', res.data.data.user_account);
  111. // uni.setStorageSync('token_temp', res.data.data.token);
  112. // this.$navTo.togo('/pages2/register/bind', {scanCode: this.scanCode})
  113. // }
  114. },
  115. // scanLoginCheck小程序校验PC登录二维码,执行登录获取到令牌,然后把扫码的路径最后面的6位标识码提交给后台就行
  116. submitCode() {
  117. this.$api
  118. .scanLoginCheck({
  119. scanCode: this.scanCode,
  120. })
  121. .then((res) => {
  122. if (res.data.code == 200) {
  123. uni.navigateTo({
  124. url: "/pages4/login/pcLoginSuccess",
  125. });
  126. } else {
  127. this.$u.toast(res.data.msg);
  128. }
  129. });
  130. },
  131. },
  132. };
  133. </script>
  134. <style lang="scss" scoped>
  135. .contents {
  136. width: 100%;
  137. height: 100%;
  138. text-align: center;
  139. display: flex;
  140. flex-direction: column;
  141. align-items: center;
  142. .logo {
  143. margin: 90rpx 0rpx 250rpx;
  144. }
  145. > text {
  146. font-size: 22rpx;
  147. color: #666;
  148. }
  149. .login_bt {
  150. width: 400rpx;
  151. height: 70rpx;
  152. line-height: 70rpx;
  153. text-align: center;
  154. background: #09ba08;
  155. color: #fff;
  156. font-size: 28rpx;
  157. border-radius: 35rpx;
  158. margin-top: 20rpx;
  159. }
  160. }
  161. </style>