index.vue 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <template>
  2. <view><!-- 授权页面 --></view>
  3. </template>
  4. <script>
  5. export default {
  6. data() {
  7. return {
  8. // 获取用户信息
  9. ocode: '1015730314',
  10. // 商户appkey
  11. appkey: '9D6ACFE8CF9AFD07',
  12. // 跳转地址
  13. URL: 'https://chtech.ncjti.edu.cn/campusclock/app/'
  14. }
  15. },
  16. onLoad() {
  17. //检查是否存在用户信息
  18. this.loginFilter()
  19. },
  20. methods: {
  21. loginFilter() {
  22. const clockIn_token = uni.getStorageSync('clockIn_token')
  23. if (!clockIn_token) {
  24. let wxcode = this.getQueryString('wxcode')
  25. if (!wxcode) {
  26. window.location.href = `https://open.wecard.qq.com/connect/oauth/authorize?app_key=${this.appkey}&response_type=code&scope=snsapi_base&ocode=${
  27. this.ocode
  28. }&redirect_uri=${this.URL}&state=wxcode`
  29. } else {
  30. // 获取wxcode后请求登录
  31. this.login(wxcode)
  32. }
  33. } else {
  34. uni.reLaunch({
  35. url: '/pages/home/home'
  36. })
  37. }
  38. },
  39. // 用户登陆获取个人信息和token
  40. async login(wxcode) {
  41. let res = await this.$myRequest_clockIn({
  42. url: '/attendance/api/system/user/login/app',
  43. method: 'post',
  44. header: {
  45. 'content-type': 'application/json',
  46. platform: 2,
  47. 'Accept-Language': 'zh-CN,zh;q=0.9'
  48. },
  49. data: {
  50. redirect_uri: this.URL,
  51. wxcode
  52. }
  53. })
  54. // console.log(res)
  55. if (res.code == 200) {
  56. uni.setStorageSync('clockIn_token', res.data.token)
  57. uni.reLaunch({
  58. url: '/pages/home/home'
  59. })
  60. }
  61. },
  62. //获取当前URL指定参数
  63. getQueryString(name) {
  64. // 获取URL
  65. let url = window.location.href
  66. // 正则匹配URL
  67. let pattern = new RegExp('[?&]' + name + '=([^&]+)', 'i')
  68. let matcher = pattern.exec(url)
  69. if (matcher == null || matcher.length < 1) {
  70. return false
  71. }
  72. // 输出指定的参数值
  73. return decodeURIComponent(matcher[1])
  74. }
  75. }
  76. }
  77. </script>
  78. <style></style>