MnpWechat.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <template>
  2. <div class="wechat-mnp-config">
  3. <div class="card-wrap mb10 animate-on-enter">
  4. <a-space wrap>
  5. <a-button :icon="h(ReloadOutlined)" @click="onRefresh" :loading="loading">
  6. 刷新
  7. </a-button>
  8. </a-space>
  9. </div>
  10. <div class="card-wrap mb10 animate-on-enter animate-delay-1">
  11. <a-alert :message="formState.description" type="info" show-icon />
  12. </div>
  13. <div class="card-wrap animate-on-enter animate-delay-2">
  14. <a-spin :spinning="loading">
  15. <a-form ref="formRef" :model="formState" :label-col="{ span: 4 }" :wrapper-col="{ span: 16 }" :rules="rules"
  16. @finish="onSubmit" @finishFailed="onFinishFailed">
  17. <a-form-item label="AppID" :name="['value', 'app_id']">
  18. <a-input v-model:value="formState.value.app_id" placeholder="请输入微信小程序AppID" />
  19. </a-form-item>
  20. <a-form-item label="AppSecret" :name="['value', 'secret']">
  21. <a-input-password v-model:value="formState.value.secret" placeholder="请输入微信小程序AppSecret" />
  22. </a-form-item>
  23. <a-form-item label="Token" :name="['value', 'token']">
  24. <a-input v-model:value="formState.value.token" placeholder="请输入消息推送Token" />
  25. </a-form-item>
  26. <a-form-item label="EncodingAESKey" :name="['value', 'aes_key']">
  27. <a-input v-model:value="formState.value.aes_key" placeholder="请输入消息加解密密钥" />
  28. </a-form-item>
  29. <a-form-item :wrapper-col="{ offset: 4, span: 16 }">
  30. <a-button type="primary" :icon="h(SaveOutlined)" html-type="submit" :loading="saveLoading">
  31. 保存配置
  32. </a-button>
  33. </a-form-item>
  34. </a-form>
  35. </a-spin>
  36. </div>
  37. </div>
  38. </template>
  39. <script setup>
  40. import { notifyError } from '@/utils/request.js'
  41. import { ref, reactive, onMounted, h } from 'vue'
  42. import { message } from 'ant-design-vue'
  43. import { SaveOutlined, ReloadOutlined } from '@ant-design/icons-vue'
  44. import { getPlatformConfig, saveConfig } from '@/api/config'
  45. // 响应式数据
  46. const formRef = ref()
  47. const loading = ref(false)
  48. const saveLoading = ref(false)
  49. // 表单数据
  50. const formState = reactive({
  51. id: '',
  52. name: '微信小程序配置',
  53. key: 'wechat_mnp',
  54. value: {
  55. app_id: '',
  56. secret: '',
  57. token: '',
  58. aes_key: ''
  59. },
  60. description: '微信小程序基础配置信息'
  61. })
  62. // 表单验证规则
  63. const rules = {
  64. value: {
  65. app_id: [
  66. { required: true, message: '请输入微信小程序AppID', trigger: 'change' },
  67. ],
  68. secret: [
  69. { required: true, message: '请输入微信小程序AppSecret', trigger: 'change' }
  70. ],
  71. token: [
  72. { required: true, message: '请输入消息推送Token', trigger: 'change' }
  73. ],
  74. aes_key: [
  75. { required: true, message: '请输入消息加解密密钥', trigger: 'change' }
  76. ]
  77. }
  78. }
  79. // 方法
  80. const loadConfig = async () => {
  81. loading.value = true
  82. try {
  83. const response = await getPlatformConfig({
  84. key: 'wechat_mnp'
  85. })
  86. if (response.code === 200 && response.result) {
  87. // 如果有配置数据,则填充表单
  88. const config = response.result
  89. // 处理 value,确保它是一个对象
  90. const configValue = config.value || {}
  91. Object.assign(formState, {
  92. id: config.id || '',
  93. name: config.name || '微信小程序配置',
  94. key: config.key || 'wechat_mnp',
  95. value: {
  96. app_id: configValue.app_id || '',
  97. secret: configValue.secret || '',
  98. token: configValue.token || '',
  99. aes_key: configValue.aes_key || ''
  100. },
  101. description: config.description || '微信小程序基础配置信息'
  102. })
  103. }
  104. } catch (error) {
  105. console.error('加载配置失败:', error)
  106. notifyError(error, '加载配置失败')
  107. } finally {
  108. loading.value = false
  109. }
  110. }
  111. const onSubmit = async (values) => {
  112. saveLoading.value = true
  113. try {
  114. const submitData = {
  115. name: formState.name,
  116. key: formState.key,
  117. value: values.value
  118. }
  119. const response = await saveConfig(submitData)
  120. if (response.code === 200) {
  121. message.success('保存成功')
  122. // 重新加载配置
  123. await loadConfig()
  124. } else {
  125. message.error(response.message || '保存失败')
  126. }
  127. } catch (error) {
  128. console.error('保存失败:', error)
  129. notifyError(error, '保存失败')
  130. } finally {
  131. saveLoading.value = false
  132. }
  133. }
  134. const onFinishFailed = () => {
  135. message.error('请检查表单填写是否正确')
  136. }
  137. const onRefresh = () => {
  138. loadConfig()
  139. }
  140. // 生命周期
  141. onMounted(() => {
  142. loadConfig()
  143. })
  144. </script>
  145. <style scoped>
  146. .wechat-mnp-config {
  147. .card-wrap {
  148. background: #fff;
  149. border-radius: 8px;
  150. padding: 24px;
  151. box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  152. h3 {
  153. margin-bottom: 16px;
  154. color: #333;
  155. font-weight: 600;
  156. }
  157. }
  158. .mb10 {
  159. margin-bottom: 16px;
  160. }
  161. .mt10 {
  162. margin-top: 16px;
  163. }
  164. .animate-on-enter {
  165. animation: fadeInUp 0.6s ease-out;
  166. }
  167. .animate-delay-1 {
  168. animation-delay: 0.1s;
  169. }
  170. .animate-delay-2 {
  171. animation-delay: 0.2s;
  172. }
  173. @keyframes fadeInUp {
  174. from {
  175. opacity: 0;
  176. transform: translateY(20px);
  177. }
  178. to {
  179. opacity: 1;
  180. transform: translateY(0);
  181. }
  182. }
  183. }
  184. </style>