index.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <template>
  2. <view class="container">
  3. <button @click="handleLogin">登录获取code</button>
  4. <button @click="handleToken">获取access_token</button>
  5. <button @click="handleUserInfo">获取用户信息</button>
  6. <button @click="handleClick">订阅</button>
  7. <button @click="handleClick2">设置</button>
  8. <button @click="handleGo">跳转</button>
  9. </view>
  10. </template>
  11. <script>
  12. export default {
  13. data() {
  14. return {
  15. // 订单进度模版id
  16. templateOrder: 'ERU1ZY9IqwNkDxWyFJvo5VSE7ua-wey3SqhZgjqLDtU',
  17. // 小程序Id
  18. appid: 'wx2fc3f45732fae5d3',
  19. // 小程序密钥
  20. screct: '7eee4a49a4470a77f9222995e8511547',
  21. access_token: '',
  22. code: ''
  23. }
  24. },
  25. onLoad() {
  26. // uni.switchTab({
  27. // url: '/pages/home/home'
  28. // })
  29. },
  30. methods: {
  31. handleGo() {
  32. uni.navigateTo({
  33. url: '/pages/shouquan/shouquan'
  34. })
  35. },
  36. handleLogin() {
  37. uni.login({
  38. provider: 'weixin', //使用微信登录
  39. success: (loginRes) => {
  40. console.log(loginRes)
  41. this.code = loginRes.code
  42. }
  43. })
  44. },
  45. handleToken() {
  46. uni.request({
  47. url: 'https://api.weixin.qq.com/sns/jscode2session',
  48. data: {
  49. grant_type: 'authorization_code',
  50. appid: this.appid,
  51. appsecret: this.screct,
  52. js_code: this.code
  53. },
  54. success: (res) => {
  55. console.log(res)
  56. if (res.statusCode === 200) {
  57. // this.access_token = res.data.
  58. }
  59. }
  60. })
  61. },
  62. handleUserInfo() {
  63. uni.getUserProfile({
  64. desc: '用于完善用户资料',
  65. lang: 'zh_CN',
  66. success: (res) => {
  67. console.log('res', res)
  68. }
  69. })
  70. },
  71. // 订阅方法
  72. handleClick() {
  73. if (uni.requestSubscribeMessage) {
  74. uni.requestSubscribeMessage({
  75. tmplIds: [this.templateOrder],
  76. success: (res) => {
  77. console.log(1)
  78. console.log(res)
  79. for (let key in res) {
  80. console.log(key)
  81. if (res[key] === 'accept') {
  82. uni.showToast({
  83. title: '订阅成功',
  84. icon: 'success'
  85. })
  86. } else if (res[key] === 'reject') {
  87. }
  88. }
  89. },
  90. fail: (res) => {
  91. console.log(2)
  92. console.log(res)
  93. }
  94. })
  95. } else {
  96. uni.showToast({
  97. title: '请更新您的微信版本来获取订阅消息功能',
  98. icon: 'none'
  99. })
  100. }
  101. },
  102. // 设置方法
  103. handleClick2() {
  104. // 获取用户订阅消息设置
  105. uni.getSetting({
  106. withSubscriptions: true,
  107. success: (res) => {
  108. console.log(res)
  109. console.log(res.subscriptionsSetting)
  110. }
  111. })
  112. }
  113. }
  114. }
  115. </script>
  116. <style lang="scss" scoped>
  117. .container {
  118. height: 100vh;
  119. background-color: #fff;
  120. }
  121. </style>