purchaseGuide.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <template>
  2. <view class="form-container">
  3. <!-- 状态开关部分 -->
  4. <view class="form-section">
  5. <view class="form-item">
  6. <text class="form-label">状态</text>
  7. <view class="form-control">
  8. <text class="status-text">{{ status ? '开启' : '关闭' }}</text>
  9. <switch
  10. :checked="status"
  11. @change="onStatusChange"
  12. color="#07c160"
  13. class="status-switch"
  14. />
  15. </view>
  16. </view>
  17. </view>
  18. <view class="form-section">
  19. <view class="form-item textarea-item">
  20. <text class="form-label">公告标题</text>
  21. <button class="admin-button-com middle blue" style="float: right;" @click="purchaseGuideText=''">清空</button>
  22. </view>
  23. <view class="textarea-wrapper">
  24. <textarea
  25. v-model="purchaseGuideText"
  26. class="textarea-input"
  27. placeholder="请输入标题,不超过70个字,留空会显示一张图片"
  28. :placeholder-style="{ color: '#999999' }"
  29. :maxlength="500"
  30. />
  31. <view class="char-count">{{ purchaseGuideText.length }}/70</view>
  32. </view>
  33. </view>
  34. <view class="form-section">
  35. <view class="form-item" @click="showPicker">
  36. <text class="form-label">公告详情</text>
  37. <view class="form-control">
  38. <text class="select-text">{{ selectedValue || '请选图文' }}</text>
  39. <text class="arrow">></text>
  40. </view>
  41. </view>
  42. </view>
  43. <view class="button-section">
  44. <button class="btn btn-cancel" @click="onCancel">返回</button>
  45. <button class="btn btn-submit" @click="onSubmit">提交</button>
  46. </view>
  47. </view>
  48. </template>
  49. <script>
  50. import { getShopPurchaseGuide, switchPurchaseGuide } from '@/api/pic-text';
  51. export default {
  52. name: 'PurchaseGuide',
  53. data() {
  54. return {
  55. purchaseGuideData: {},
  56. status: true, // 状态开关,默认开启
  57. selectedValue: '', // 选中的值
  58. saveSelectedValue: '', // 保存选中的值
  59. selectedId: 0, // 选中的id
  60. formData: {},
  61. purchaseGuideText: '' // 公告内容
  62. }
  63. },
  64. onShow() {
  65. uni.$on('updatePurchaseGuideEvent', this.updateSelectedItem);
  66. },
  67. onUnload() {
  68. uni.$off('updatePurchaseGuideEvent', this.updateSelectedItem);
  69. },
  70. methods: {
  71. init(){
  72. getShopPurchaseGuide().then(res => {
  73. this.purchaseGuideData = res.data.shopExt;
  74. this.status = res.data.shop && res.data.shop.buyNotice == 1 ? true : false;
  75. this.purchaseGuideText = res.data.shopExt && res.data.shopExt.purchaseGuideText ? res.data.shopExt.purchaseGuideText : ''
  76. // 根据 purchaseGuide 和 title 设置选中值
  77. if (this.purchaseGuideData.purchaseGuide !== "0" && this.purchaseGuideData.title) {
  78. this.selectedValue = `${this.purchaseGuideData.title} (id: ${this.purchaseGuideData.purchaseGuide})`;
  79. this.saveSelectedValue = this.selectedValue;
  80. this.selectedId = this.purchaseGuideData.purchaseGuide; // purchaseGuide 保存的是表picText 的自增Id
  81. } else {
  82. this.selectedValue = '';
  83. this.selectedId = 0;
  84. }
  85. })
  86. },
  87. // 状态开关变化
  88. onStatusChange(e) {
  89. this.status = e.detail.value
  90. if (this.status) {
  91. this.selectedValue = this.saveSelectedValue;
  92. } else {
  93. this.selectedValue = '...';
  94. }
  95. },
  96. // 显示选择器
  97. showPicker() {
  98. uni.navigateTo({ url: `/admin/picText/picTextList?selectedId=${this.selectedId}` })
  99. },
  100. // 取消按钮
  101. onCancel() {
  102. uni.navigateBack();
  103. },
  104. // 提交按钮
  105. onSubmit() {
  106. const formData = { picTextId: this.selectedId, switch: this.status ? 1 : 0, purchaseGuideText:this.purchaseGuideText }
  107. uni.showLoading()
  108. switchPurchaseGuide(formData).then(res => {
  109. uni.hideLoading();
  110. if(res.code == 1){
  111. uni.showToast({ title: '提交成功', icon: 'success' })
  112. setTimeout(() => {
  113. uni.navigateBack()
  114. }, 1500);
  115. } else {
  116. this.$msg(res.msg)
  117. }
  118. })
  119. },
  120. updateSelectedItem(data) {
  121. // 更新选中项显示
  122. if (data && data.picTextId && data.title) {
  123. this.selectedValue = `${data.title} (ID: ${data.picTextId})`;
  124. this.saveSelectedValue = this.selectedValue;
  125. this.selectedId = data.picTextId;
  126. } else if (data && data.title) {
  127. this.selectedValue = data.title;
  128. this.saveSelectedValue = this.selectedValue;
  129. this.selectedId = data.picTextId || 0;
  130. } else {
  131. this.selectedValue = '';
  132. this.saveSelectedValue = '';
  133. this.selectedId = 0;
  134. }
  135. }
  136. }
  137. }
  138. </script>
  139. <style lang="scss" scoped>
  140. .form-container {
  141. background-color: #ffffff;
  142. min-height: 100vh;
  143. padding: 0 30rpx;
  144. }
  145. .form-section {
  146. border-bottom: 1rpx solid #f0f0f0;
  147. &:last-of-type {
  148. border-bottom: none;
  149. }
  150. }
  151. .form-item {
  152. display: flex;
  153. justify-content: space-between;
  154. align-items: center;
  155. padding: 30rpx 0;
  156. min-height: 88rpx;
  157. }
  158. .form-label {
  159. font-size: 32rpx;
  160. color: #333333;
  161. font-weight: 400;
  162. }
  163. .form-control {
  164. display: flex;
  165. align-items: center;
  166. gap: 20rpx;
  167. }
  168. .status-text {
  169. font-size: 32rpx;
  170. color: #333333;
  171. }
  172. .status-switch {
  173. transform: scale(0.8);
  174. }
  175. .select-text {
  176. font-size: 32rpx;
  177. color: #333333;
  178. max-width: 430rpx;
  179. overflow: hidden;
  180. text-overflow: ellipsis;
  181. white-space: nowrap;
  182. text-align: right;
  183. }
  184. .arrow {
  185. font-size: 32rpx;
  186. color: #999999;
  187. margin-left: 4rpx;
  188. }
  189. .textarea-item {
  190. min-height: auto;
  191. padding: 30rpx 0 20rpx 0;
  192. }
  193. .textarea-wrapper {
  194. position: relative;
  195. padding: 0 0 30rpx 0;
  196. }
  197. .textarea-input {
  198. width: 100%;
  199. height: 200rpx;
  200. padding: 20rpx;
  201. border: 1rpx solid #e0e0e0;
  202. border-radius: 8rpx;
  203. font-size: 28rpx;
  204. color: #333333;
  205. box-sizing: border-box;
  206. line-height: 1.5;
  207. }
  208. .char-count {
  209. position: absolute;
  210. bottom: 40rpx;
  211. right: 20rpx;
  212. font-size: 24rpx;
  213. color: #999999;
  214. }
  215. .button-section {
  216. display: flex;
  217. gap: 30rpx;
  218. margin-top: 60rpx;
  219. padding: 0 20rpx;
  220. }
  221. .btn {
  222. flex: 1;
  223. height: 88rpx;
  224. border-radius: 12rpx;
  225. font-size: 32rpx;
  226. font-weight: 500;
  227. display: flex;
  228. align-items: center;
  229. justify-content: center;
  230. }
  231. .btn-cancel {
  232. background-color: #ffffff;
  233. color: #666666;
  234. border: 2rpx solid #e0e0e0;
  235. }
  236. .btn-submit {
  237. background-color: #007aff;
  238. color: #ffffff;
  239. border: none;
  240. }
  241. </style>