msgList.vue 3.6 KB

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