msgList.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <template>
  2. <view class="page-view">
  3. <top-return :color="'#FFF'" text="个人通知"></top-return>
  4. <view class="bg-box2"></view>
  5. <view class="card-view" style="margin-top: 140rpx;">
  6. <view class="card-item" style="width: 100%;" v-for="(item,index) in userData.msgList" :key="index">
  7. <view class="card-title" style="margin-right: 10rpx;">
  8. <view class="t-icon t-icon-tongzhihuotixing"></view>
  9. <view class="title" style="margin-left: 20rpx;font-weight: 400;">{{item.title}}</view>
  10. <view class="subtitle" style="margin-left: auto;font-size: 24rpx;">{{item.msgTime}}</view>
  11. </view>
  12. <view class="title" style="margin: 0 30rpx;font-size: 28rpx;">{{item.content}}</view>
  13. <image class="msg-poster" :src="item.image" mode="scaleToFill"></image>
  14. </view>
  15. </view>
  16. <view>
  17.  <button type="btn" @click="openAuth()">
  18. <view class="btn-text">发送消息</view>
  19. </button>
  20. </view>
  21. </view>
  22. </template>
  23. <script>
  24. import {
  25. mapState,
  26. mapMutations
  27. } from 'vuex';
  28. export default {
  29. computed: {
  30. ...mapState('m_parent', ['userData'])
  31. },
  32. data() {
  33. return {
  34. tmplIds: ['tmMsJJSh3bVhbCh34x15pTqNwPXIL52mk7r64Z8BnZs'],
  35. };
  36. },
  37. onLoad() {
  38. this.sendMsg()
  39. },
  40. methods: {
  41. ...mapMutations('m_parent', ['updateUserData']),
  42. //删除信息
  43. deleteMsg(index) {
  44. let msgList = this.userData.msgList
  45. let data = msgList.splice(index, 1)
  46. this.updateUserData(data)
  47. },
  48. openAuth() {
  49. let templateId = this.tmplIds[0]
  50. uni.requestSubscribeMessage({
  51. tmplIds: this.tmplIds,
  52. success: res => {
  53. console.log('确认按钮', res);
  54. this.sendMsg()
  55. }
  56. })
  57. },
  58. // 获取access_token
  59. getAccessToken() {
  60. return new Promise((resolve, reject) => {
  61. uni.request({
  62. url: 'https://api.weixin.qq.com/cgi-bin/token',
  63. data: {
  64. appid: 'wx5705da8747c77cfe',
  65. secret: 'd5adf260a866ca43e74fbb40cec00799',
  66. grant_type: 'client_credential'
  67. },
  68. success: (res) => {
  69. console.log('返回access_token',res);
  70. resolve(res.data.access_token)
  71. },
  72. fail: (err) => {
  73. reject(err)
  74. }
  75. })
  76. })
  77. },
  78. async sendMsg() {
  79. const openid = uni.getStorageSync('token').miniappData.openid
  80. const access_token = await this.getAccessToken();
  81. uni.request({
  82. url: 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=' +
  83. access_token,
  84. method: 'POST',
  85. data: {
  86. touser: openid,
  87. template_id: this.tmplIds[0], // 第一个模板id
  88. page: "pages/init/init", // 点击消息卡片跳转地址
  89. data: { // data是模板内容,属性名为模板中所给,value值是需要传递的。
  90. name1: {
  91. value: '测试用户'
  92. },
  93. time2: {
  94. value: '2022-07-11 20:33:44'
  95. },
  96. thing3: {
  97. value: '测试测评'
  98. },
  99. number4: {
  100. value: 123
  101. }
  102. }
  103. },
  104. success: (res) => {
  105. console.log('发送模板消息',res);
  106. }
  107. })
  108. },
  109. }
  110. }
  111. </script>
  112. <style lang="scss">
  113. .msg-poster {
  114. margin: 20rpx auto;
  115. width: 94%;
  116. height: 300rpx;
  117. border-radius: 20rpx;
  118. }
  119. .t-icon {
  120. width: 40rpx;
  121. height: 40rpx;
  122. }
  123. .btn {
  124. display: flex;
  125. align-items: center;
  126. justify-content: center;
  127. height: 80rpx;
  128. margin: 0 20rpx 20rpx 20rpx;
  129. background-color: #4169E1;
  130. border-radius: $border-radius;
  131. box-shadow: $box-shadow;
  132. .btn-text {
  133. line-height: 38rpx;
  134. font-size: 38rpx;
  135. color: #FFF;
  136. font-family: YSfont;
  137. }
  138. }
  139. </style>