index.vue 3.1 KB

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