SmsModal.vue 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <template>
  2. <u-modal
  3. :show="visible"
  4. title="推送短信"
  5. :show-cancel-button="true"
  6. :showCancelButton="true"
  7. :show-confirm-button="true"
  8. :showConfirmButton="true"
  9. confirmText="确定"
  10. cancelText="关闭"
  11. @confirm="handleSubmit"
  12. @cancel="handleCancel"
  13. @close="handleCancel"
  14. width="650rpx"
  15. >
  16. <view class="modal-content">
  17. <u-form
  18. ref="formRef"
  19. :model="form"
  20. :rules="rules"
  21. labelWidth="120"
  22. labelPosition="top"
  23. >
  24. <u-form-item label="短信类型" prop="type" required>
  25. <u-radio-group
  26. v-model="form.type"
  27. placement="column"
  28. @change="handleChangeType"
  29. >
  30. <u-radio
  31. v-for="item in smsContentList"
  32. :key="item.type"
  33. :name="item.type"
  34. :label="item.typeName"
  35. ></u-radio>
  36. </u-radio-group>
  37. </u-form-item>
  38. <u-form-item label="短信预览" prop="remark" required>
  39. <u-textarea
  40. v-model="form.remark"
  41. placeholder="请输入短信预览"
  42. :disabled="true"
  43. :rows="4"
  44. count
  45. ></u-textarea>
  46. </u-form-item>
  47. <u-form-item label="推送历史">
  48. <view class="sms-history">
  49. <view v-if="smsRecords.length === 0" class="no-data">
  50. <text>暂无推送记录</text>
  51. </view>
  52. <view v-else class="history-list">
  53. <view
  54. v-for="(record, index) in smsRecords"
  55. :key="record.id || index"
  56. class="history-item"
  57. >
  58. <view class="history-header">
  59. <text class="sender">{{ record.createName }}</text>
  60. <text class="time">{{ record.createTime }}</text>
  61. <text
  62. class="status"
  63. :class="record.status == 1 ? 'success' : 'fail'"
  64. >
  65. {{ record.status == 1 ? '成功' : '失败' }}
  66. </text>
  67. </view>
  68. <view class="history-content">
  69. <text>{{ record.smsContent }}</text>
  70. </view>
  71. </view>
  72. </view>
  73. </view>
  74. </u-form-item>
  75. </u-form>
  76. </view>
  77. </u-modal>
  78. </template>
  79. <script setup>
  80. import { ref, reactive, nextTick, getCurrentInstance } from 'vue';
  81. const { proxy } = getCurrentInstance();
  82. const emit = defineEmits(['success']);
  83. /** 弹窗是否打开 */
  84. const visible = defineModel({ type: Boolean });
  85. /** 表单引用 */
  86. const formRef = ref();
  87. /** SMS记录数据 */
  88. const smsRecords = ref([]);
  89. const form = reactive({
  90. orderId: null,
  91. type: '',
  92. remark: ''
  93. });
  94. /** 短信内容列表 */
  95. const smsContentList = ref([]);
  96. /** 短信类型改变 */
  97. const handleChangeType = (val) => {
  98. const selectedItem = smsContentList.value.find(item => item.type === val);
  99. if (selectedItem) {
  100. form.remark = selectedItem.smsContent;
  101. }
  102. };
  103. /** 表单验证规则 */
  104. const rules = reactive({
  105. type: [
  106. {
  107. required: true,
  108. message: '请选择短信类型',
  109. trigger: 'change'
  110. }
  111. ],
  112. remark: [
  113. {
  114. required: true,
  115. message: '请输入短信预览',
  116. trigger: 'blur'
  117. }
  118. ]
  119. });
  120. /** 关闭弹窗 */
  121. const handleCancel = () => {
  122. visible.value = false;
  123. resetForm();
  124. };
  125. /** 重置表单 */
  126. const resetForm = () => {
  127. form.orderId = null;
  128. form.type = '';
  129. form.remark = '';
  130. smsRecords.value = [];
  131. smsContentList.value = [];
  132. formRef.value?.resetFields();
  133. };
  134. /** 弹窗打开事件 */
  135. const handleOpen = (orderId) => {
  136. resetForm();
  137. if (orderId) {
  138. console.log('打开短信弹窗,订单ID:', orderId);
  139. visible.value = true;
  140. getSmsLogInfo(orderId);
  141. }
  142. };
  143. /** 获取短信记录信息 */
  144. const getSmsLogInfo = (orderId) => {
  145. uni.$u.http.get(`/app/orderinfo/orderSmsLog/${orderId}`).then((res) => {
  146. if (res.code === 200) {
  147. let data = res.data || {};
  148. form.orderId = data.orderId;
  149. smsRecords.value = data.smsLogList || [];
  150. smsContentList.value = data.smsContentList || [];
  151. } else {
  152. uni.$u.toast(res.msg || '获取短信记录失败');
  153. }
  154. }).catch((err) => {
  155. console.error('获取短信记录失败:', err);
  156. uni.$u.toast('获取短信记录失败');
  157. });
  158. };
  159. /** 提交表单 */
  160. const handleSubmit = () => {
  161. formRef.value?.validate().then(valid => {
  162. if (!valid) return;
  163. uni.$u.http.post('/app/orderinfo/orderSmsLogSend', {
  164. orderId: form.orderId,
  165. type: form.type
  166. }).then((res) => {
  167. if (res.code == 200) {
  168. uni.$u.toast('短信发送成功');
  169. emit('success');
  170. handleCancel();
  171. } else {
  172. uni.$u.toast(res.msg || '短信发送失败');
  173. }
  174. }).catch((err) => {
  175. console.error('短信发送失败:', err);
  176. uni.$u.toast('短信发送失败');
  177. });
  178. }).catch(err => {
  179. console.log('表单验证失败:', err);
  180. });
  181. };
  182. defineExpose({
  183. handleOpen
  184. });
  185. </script>
  186. <style lang="scss" scoped>
  187. .modal-content{
  188. max-height: 72vh;
  189. overflow: auto;
  190. }
  191. .sms-history {
  192. .no-data {
  193. text-align: center;
  194. color: #999;
  195. padding: 40rpx 0;
  196. }
  197. .history-list {
  198. .history-item {
  199. margin-bottom: 20rpx;
  200. padding: 20rpx;
  201. background-color: #f8f9fa;
  202. border-radius: 8rpx;
  203. border-left: 4rpx solid #409eff;
  204. .history-header {
  205. display: flex;
  206. justify-content: space-between;
  207. align-items: center;
  208. margin-bottom: 10rpx;
  209. .sender {
  210. font-size: 28rpx;
  211. font-weight: bold;
  212. color: #333;
  213. }
  214. .time {
  215. font-size: 24rpx;
  216. color: #666;
  217. }
  218. .status {
  219. font-size: 24rpx;
  220. padding: 4rpx 8rpx;
  221. border-radius: 4rpx;
  222. &.success {
  223. background-color: #67c23a;
  224. color: white;
  225. }
  226. &.fail {
  227. background-color: #f56c6c;
  228. color: white;
  229. }
  230. }
  231. }
  232. .history-content {
  233. font-size: 26rpx;
  234. line-height: 1.5;
  235. color: #666;
  236. }
  237. }
  238. }
  239. }
  240. /* 自定义单选框样式 */
  241. :deep(.u-radio-group) {
  242. .u-radio {
  243. margin-bottom: 20rpx;
  244. }
  245. }
  246. </style>