crud1.vue 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. <template>
  2. <view class="container">
  3. <view class="safe-area-inset-bottom">
  4. <rider-curd
  5. v-model="form"
  6. :option="option"
  7. :data="list"
  8. :rules="rules"
  9. :readonly.sync="readonly"
  10. formType="uniForm"
  11. @search="onSearch"
  12. @action-click="onActionClick"
  13. @submit="onSubmit"
  14. @del="onRemove"
  15. ref="crud"
  16. >
  17. <template #form>
  18. <view class="uni-form">
  19. <view class="uni-form-item">
  20. <view class="uni-form-item-label uni-form-item-required">通知标题</view>
  21. <view class="uni-form-item-input">
  22. <input
  23. class="uni-input"
  24. v-model="form.title"
  25. placeholder="通知标题"
  26. :placeholder-style="placeholderStyle"
  27. :disabled="readonly"
  28. />
  29. </view>
  30. </view>
  31. <view class="uni-form-item">
  32. <view class="uni-form-item-label uni-form-item-required">通知类型</view>
  33. <view class="uni-form-item-input">
  34. <picker
  35. @change="e => onConfirm(e, 'category')"
  36. :range="categoryList"
  37. range-key="dictValue"
  38. class="curdPicker"
  39. :disabled="readonly"
  40. >
  41. <input
  42. class="uni-input"
  43. v-model="formLabel.category"
  44. disabled
  45. placeholder="通知类型"
  46. :placeholder-style="placeholderStyle"
  47. />
  48. </picker>
  49. </view>
  50. <view class="uni-form-item-icon"><u-icon name="arrow-right" color="#999" size="28"></u-icon></view>
  51. </view>
  52. <view class="uni-form-item">
  53. <view class="uni-form-item-label uni-form-item-required">通知日期</view>
  54. <view class="uni-form-item-input">
  55. <picker
  56. @change="e => onConfirm(e, 'releaseTime')"
  57. mode="date"
  58. :value="form.releaseTime"
  59. :start="startDate"
  60. :end="endDate"
  61. class="curdPicker"
  62. :disabled="readonly"
  63. >
  64. <input
  65. class="uni-input"
  66. v-model="form.releaseTime"
  67. disabled
  68. placeholder="通知日期"
  69. :placeholder-style="placeholderStyle"
  70. />
  71. </picker>
  72. </view>
  73. <view class="uni-form-item-icon"><u-icon name="arrow-right" color="#999" size="28"></u-icon></view>
  74. </view>
  75. <view class="uni-form-item">
  76. <view class="uni-form-item-label">通知内容</view>
  77. <view v-if="!readonly" class="uni-form-item-btn" @click="onRichEdit(form.content)">去编辑</view>
  78. </view>
  79. <mp-html :content="form.content"></mp-html>
  80. </view>
  81. </template>
  82. <template #loadmore>
  83. <u-loadmore :status="loadStatus" @loadmore="getList()" />
  84. </template>
  85. </rider-curd>
  86. </view>
  87. </view>
  88. </template>
  89. <script>
  90. import riderCurd from '@/components/rider-crud/index'
  91. import mpHtml from '@/components/rider-crud/components/mp-html/mp-html'
  92. import { getList, add, update, remove } from '@/api/notice.js'
  93. import { dictionary } from '@/api/system/dict.js'
  94. import { logo } from '../../common/setting'
  95. import { getDicLabel } from '@/components/rider-crud/util/tool.js'
  96. export default {
  97. components: { riderCurd, mpHtml },
  98. data() {
  99. return {
  100. form: {
  101. content: ''
  102. },
  103. formLabel: {
  104. category: '' ,
  105. },
  106. list: [],
  107. params: {
  108. current: 1,
  109. size: 10
  110. },
  111. readonly: false, // 查看时控制表单只读
  112. labelWidth: 160,
  113. placeholderStyle: 'color:rgb(192, 196, 204); font-size:14px',
  114. option: {
  115. navTitle: 'CRUD 操作', //标题
  116. manageBtn: true, // 右上角管理按钮,默认不显示
  117. searchShow: true, //是否显示搜索,默认不显示
  118. column: [
  119. {
  120. label: '通知标题',
  121. prop: 'title'
  122. },
  123. {
  124. label: '通知类型',
  125. prop: 'categoryName'
  126. },
  127. {
  128. label: '通知日期',
  129. prop: 'releaseTime'
  130. }
  131. ],
  132. actions: [
  133. {
  134. name: '编辑', // 操作名称
  135. icon: 'edit-pen', //操作图标,有图标文字不显示
  136. color: '#333', // 字体颜色
  137. fontsize: 30, //字体大小,单位rpx
  138. width: 40, // 宽度,单位px
  139. background: '#fff' // 菜单按钮背景色
  140. },
  141. {
  142. name: '删除',
  143. icon: 'trash',
  144. color: '#333',
  145. fontsize: 30,
  146. width: 40,
  147. background: '#fff'
  148. }
  149. ]
  150. },
  151. rules: {
  152. title: [{ required: true, message: '请输入通知标题', trigger: 'blur' }],
  153. category: [{ required: true, message: '请选择通知类型', trigger: 'change', type: 'number' }],
  154. releaseTime: [{ required: true, message: '请选择通知日期', trigger: 'change' }]
  155. },
  156. loadStatus: 'loadmore', // 列表加载状态
  157. checkedSwitch: false,
  158. timeShow: false,
  159. timeParams: {
  160. year: true,
  161. month: true,
  162. day: true,
  163. hour: true,
  164. minute: true,
  165. second: true
  166. },
  167. categoryShow: false,
  168. categoryList: [], //类型
  169. category: '',
  170. pagesTotal: 0
  171. }
  172. },
  173. computed: {
  174. startDate() {
  175. return this.getDate('start')
  176. },
  177. endDate() {
  178. return this.getDate('end')
  179. }
  180. },
  181. onShow() {
  182. let that = this
  183. uni.$on('richBack', function(data) {
  184. that.$set(that.form, 'content', data)
  185. })
  186. },
  187. onReady() {
  188. this.getList(true)
  189. this.getCategoryList()
  190. },
  191. onPullDownRefresh() {
  192. this.getList(true)
  193. },
  194. onReachBottom() {
  195. if (this.loadStatus == 'nomore') return
  196. this.getList()
  197. },
  198. methods: {
  199. getDate(type) {
  200. const date = new Date()
  201. let year = date.getFullYear()
  202. let month = date.getMonth() + 1
  203. let day = date.getDate()
  204. if (type === 'start') {
  205. year = year - 60
  206. } else {
  207. year = year + 2
  208. }
  209. month = month > 9 ? month : '0' + month
  210. day = day > 9 ? day : '0' + day
  211. return `${year}-${month}-${day}`
  212. },
  213. // 获取列表
  214. getList(override = false) {
  215. if (override) {
  216. this.params.current = 1
  217. }
  218. const { size } = this.params
  219. getList(this.params)
  220. .then(res => {
  221. const { records, current } = res.data
  222. if (records.length < size) this.loadStatus = 'nomore'
  223. else this.loadStatus = 'loadmore'
  224. if (override) {
  225. this.list = records
  226. } else {
  227. this.list = this.list.concat(records)
  228. }
  229. this.params.current++
  230. uni.stopPullDownRefresh()
  231. })
  232. .catch(err => {
  233. uni.stopPullDownRefresh()
  234. })
  235. },
  236. // 通知类型
  237. getCategoryList() {
  238. dictionary({ code: 'notice' }).then(res => {
  239. this.categoryList = res.data
  240. })
  241. },
  242. // picker确定
  243. onConfirm(e, type) {
  244. if (type == 'category') {
  245. const i = this.categoryList[e.detail.value]
  246. this.$set(this.formLabel, type, i.dictValue)
  247. this.$set(this.form, type, Number(i.dictKey))
  248. } else {
  249. this.$set(this.form, type, `${e.detail.value} 12:00:00`)
  250. }
  251. },
  252. // 编辑富文本
  253. onRichEdit(e) {
  254. uni.navigateTo({
  255. url: `/pages/rich/index?data=${e}`
  256. })
  257. },
  258. // 搜索 (自行配置搜索内容)
  259. onSearch(val) {
  260. this.params = {
  261. current: 1,
  262. size: 10,
  263. title: val
  264. }
  265. this.getList(true)
  266. },
  267. // 左滑按钮点击
  268. onActionClick({ index, data, action, done }) {
  269. const { name } = action
  270. switch (name) {
  271. case '新增':
  272. this.formLabel = { category: '' }
  273. done()
  274. break
  275. case '删除':
  276. this.onRemove(data.id)
  277. break
  278. case '查看':
  279. this.readonly = true
  280. case '编辑':
  281. this.formLabel.category = getDicLabel(data.category, this.categoryList)
  282. done()
  283. break
  284. }
  285. },
  286. // 删除
  287. onRemove(id) {
  288. uni.showModal({
  289. title: '确定要删除吗?',
  290. success: action => {
  291. if (action.confirm) {
  292. remove(id).then(() => {
  293. uni.showToast({
  294. title: '删除成功',
  295. icon: 'none'
  296. })
  297. setTimeout(() => {
  298. this.$refs['crud'].allCheckHide(false)
  299. }, 100)
  300. // #ifdef APP
  301. this.getList(true)
  302. // #endif
  303. // #ifndef APP
  304. uni.startPullDownRefresh({})
  305. // #endif
  306. })
  307. }
  308. }
  309. })
  310. },
  311. // 表单提交
  312. onSubmit(form, action, unloading, done) {
  313. if (action == '新增') {
  314. add(form)
  315. .then(res => {
  316. uni.showToast({
  317. title: '新增成功',
  318. icon: 'none'
  319. })
  320. this.formLabel = { category: '' }
  321. done()
  322. uni.startPullDownRefresh({})
  323. })
  324. .catch(() => {
  325. unloading()
  326. })
  327. } else if (action == '编辑') {
  328. update(form)
  329. .then(res => {
  330. uni.showToast({
  331. title: '编辑成功',
  332. icon: 'none'
  333. })
  334. this.formLabel = { category: '' }
  335. done()
  336. uni.startPullDownRefresh({})
  337. })
  338. .catch(() => {
  339. unloading()
  340. })
  341. }
  342. }
  343. }
  344. }
  345. </script>
  346. <style>
  347. page {
  348. background: #f6f6f6;
  349. }
  350. .uni-picker-container {
  351. z-index: 99999 !important;
  352. }
  353. </style>
  354. <style lang="scss" scoped>
  355. .nav-right {
  356. color: #fff;
  357. font-size: 28rpx;
  358. font-weight: normal;
  359. padding-right: 24rpx;
  360. }
  361. .uni-form {
  362. &-item {
  363. position: relative;
  364. width: 100%;
  365. display: flex;
  366. justify-content: space-between;
  367. align-items: center;
  368. padding: 20rpx 0;
  369. font-size: 14px;
  370. color: #303133;
  371. line-height: 70rpx;
  372. box-sizing: border-box;
  373. &-label {
  374. width: 160rpx;
  375. .required {
  376. color: #fa3534;
  377. vertical-align: middle;
  378. }
  379. }
  380. &-required {
  381. position: relative;
  382. &:after {
  383. content: '*';
  384. position: absolute;
  385. left: -8px;
  386. color: #fa3534;
  387. vertical-align: middle;
  388. }
  389. }
  390. &-input {
  391. flex: 1;
  392. .uni-input {
  393. font-size: 14px;
  394. color: #303133;
  395. }
  396. }
  397. &-btn {
  398. color: #fff;
  399. width: auto;
  400. font-size: 22rpx;
  401. line-height: 50rpx;
  402. padding: 0 20rpx;
  403. border-radius: 6rpx;
  404. background: linear-gradient(rgb(95, 136, 255), rgb(105, 176, 255));
  405. }
  406. }
  407. &-item:after {
  408. content: ' ';
  409. border-bottom-width: 2rpx;
  410. position: absolute;
  411. left: 0;
  412. top: 0;
  413. pointer-events: none;
  414. box-sizing: border-box;
  415. -webkit-transform-origin: 0 0;
  416. transform-origin: 0 0;
  417. width: 199.8%;
  418. height: 199.7%;
  419. -webkit-transform: scale(0.5, 0.5);
  420. transform: scale(0.5, 0.5);
  421. border: 0 solid #e4e7ed;
  422. z-index: 2;
  423. }
  424. &-item:after {
  425. border-bottom-width: 2rpx;
  426. }
  427. }
  428. </style>