index.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <template>
  2. <view>
  3. <view v-if="showLogin">
  4. <login :ocode="ocode" :appkey="appkey" scope="snsapi_userinfo" :visible="visible" @success="loginSuccess"
  5. @fail="loginFail" @cancel="loginCancel" />
  6. </view>
  7. </view>
  8. </template>
  9. <script>
  10. export default {
  11. data() {
  12. return {
  13. // 获取用户信息
  14. ocode: '1015730314',
  15. // 商户appkey
  16. appkey: '9D6ACFE8CF9AFD07',
  17. // 是否授权可见
  18. visible: false,
  19. // 是否启动授权
  20. showLogin: false,
  21. // 获取用户信息
  22. appid: 'wxd6f090391d410534',
  23. }
  24. },
  25. onLoad() {
  26. this.hasUserInfo()
  27. },
  28. methods: {
  29. // 检查是否存在用户信息
  30. hasUserInfo() {
  31. let userInfo = uni.getStorageSync('userInfo');
  32. // console.log(userInfo);
  33. if (userInfo) {
  34. uni.reLaunch({
  35. url: "/pagesClockIn/home/home"
  36. })
  37. } else {
  38. // console.log("重新授权");
  39. this.showLogin = true
  40. this.visible = true
  41. }
  42. },
  43. // 授权成功回调
  44. loginSuccess(res) {
  45. // console.log("成功");
  46. let wxcode = res.detail.wxcode
  47. // 获取wxcode后请求登录
  48. this.login(wxcode)
  49. },
  50. // 授权失败回调
  51. loginFail() {
  52. // console.log("授权失败");
  53. uni.showModal({
  54. title: '提示',
  55. content: '授权:请先领取校园卡、并激活!',
  56. confirmText: '领取',
  57. success: (res) => {
  58. if (res.confirm) {
  59. uni.reLaunch({
  60. url: "/pages/qr_code/qr_code"
  61. });
  62. } else if (res.cancel) {
  63. uni.reLaunch({
  64. url: "/pagesClockIn/404/404"
  65. })
  66. }
  67. }
  68. });
  69. },
  70. // 授权取消回调
  71. loginCancel() {
  72. // console.log("取消");
  73. uni.showModal({
  74. title: '提示',
  75. content: '请先授权,否则无法使用该功能',
  76. confirmText: '授权',
  77. success: (res) => {
  78. if (res.confirm) {
  79. uni.reLaunch({
  80. url: "/pagesClockIn/index/index"
  81. })
  82. } else if (res.cancel) {
  83. uni.reLaunch({
  84. url: "/pagesClockIn/404/404"
  85. })
  86. }
  87. }
  88. });
  89. },
  90. // 用户登陆获取个人信息和token
  91. async login(wxcode) {
  92. let res = await this.$myRequest_clockIn({
  93. url: "/attendance/api/system/user/login/app",
  94. method: "post",
  95. header:{
  96. 'content-type': 'application/json',
  97. 'platform': 2,
  98. 'Accept-Language': 'zh-CN,zh;q=0.9'
  99. },
  100. data: {
  101. redirect_uri: `mnp://${this.appid}`,
  102. wxcode,
  103. }
  104. })
  105. // console.log(res);
  106. if (res.code == 200) {
  107. uni.setStorageSync("token", res.data.token)
  108. uni.reLaunch({
  109. url: "/pagesClockIn/home/home"
  110. })
  111. } else {
  112. uni.showToast({
  113. title: res.message,
  114. icon: 'none'
  115. })
  116. }
  117. }
  118. }
  119. }
  120. </script>
  121. <style lang="scss" scoped>
  122. </style>