form.vue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <template>
  2. <view class="page">
  3. <view class="content">
  4. <view class="head">
  5. <span class="title">写日志</span>
  6. </view>
  7. <view class="forms">
  8. <view class="title2">张三的金科环境项目日志</view>
  9. <view class="title-sub">日志详情</view>
  10. <uni-forms label-position="left" class="form" ref="form" :rules="rules">
  11. <view class="box" v-for="item in formData" :key="item.key">
  12. <view
  13. v-show="formData.length > 1"
  14. class="btn-remove"
  15. @click="removeFormData(item.key)"
  16. >-</view
  17. >
  18. <uni-forms-item
  19. required
  20. label="项目名称"
  21. name="project_name"
  22. class="formItem"
  23. >
  24. <template v-if="!project_id">
  25. <picker
  26. @change="(e) => selectProject(item.key, e)"
  27. :range="projectList"
  28. range-key="project_name"
  29. >
  30. <view class="proNameEdit" v-if="item.project_name">
  31. {{ item.project_name }}
  32. </view>
  33. <view class="selectPlaceholder" v-else>请选择项目</view>
  34. </picker>
  35. </template>
  36. <view class="proName" v-else>
  37. {{ getProjectName(project_id) }}
  38. </view>
  39. </uni-forms-item>
  40. <uni-forms-item
  41. required
  42. label="日志概述"
  43. name="title"
  44. class="formItem"
  45. >
  46. <input
  47. v-model="item.title"
  48. class="input"
  49. placeholder="请输入日志概述"
  50. />
  51. </uni-forms-item>
  52. <uni-forms-item
  53. required
  54. label="日志详情"
  55. name="content"
  56. class="formItem"
  57. >
  58. <textarea
  59. v-model="item.content"
  60. class="textarea"
  61. placeholder="请输入日志详情"
  62. />
  63. </uni-forms-item>
  64. </view>
  65. </uni-forms>
  66. <view class="btn-row">
  67. <view class="btn-add" @click="addFormData">+ 新增</view>
  68. <view class="btn-add" @click="deleteLog" v-if="log_id">删除</view>
  69. </view>
  70. </view>
  71. <view class="button" @click="submit">提 交</view>
  72. </view>
  73. </view>
  74. </template>
  75. <script>
  76. import {
  77. createDaily,
  78. approvalLogDaily,
  79. approvalDelLog,
  80. approvalEditLog,
  81. } from "@/services/daily";
  82. import { mapState, mapActions } from "vuex";
  83. import { formatDate } from "../../uni_modules/uni-dateformat/components/uni-dateformat/date-format";
  84. export default {
  85. data() {
  86. return {
  87. scrollTop: 0,
  88. // projectList: [],
  89. project_id: "",
  90. log_id: "",
  91. formData: [
  92. {
  93. key: +new Date(),
  94. project_name: "",
  95. code_id: "",
  96. title: "",
  97. content: "",
  98. },
  99. ],
  100. rules: {
  101. project_name: {
  102. rules: [
  103. {
  104. required: true,
  105. errorMessage: "请选择项目名称",
  106. },
  107. ],
  108. },
  109. },
  110. };
  111. },
  112. onLoad(options) {
  113. this.queryProjectList();
  114. if (options.id) {
  115. this.initDate(options.id);
  116. this.log_id = options.id;
  117. }
  118. if (options.project_id) {
  119. this.project_id = Number(options.project_id);
  120. }
  121. },
  122. computed: {
  123. ...mapState(["projectList"]),
  124. },
  125. methods: {
  126. ...mapActions(["queryProjectList"]),
  127. getProjectName(id) {
  128. const item = this.projectList.find((item) => item.id == id);
  129. return item ? item.project_name : "-";
  130. },
  131. selectProject(key, e) {
  132. console.log("---------befor----------", this.formData);
  133. const select = this.projectList[e.target.value];
  134. const item = this.formData.find((item) => item.key == key);
  135. item.code_id = select.id;
  136. item.project_name = select.project_name;
  137. console.log("---------after----------", this.formData);
  138. // this.formData = [...this.formData];
  139. },
  140. addFormData() {
  141. this.formData.push({
  142. key: +new Date(),
  143. project_name: "",
  144. });
  145. console.log("---------addFormData----------", this.formData);
  146. },
  147. removeFormData(key) {
  148. let index = this.formData.findIndex((item) => item.key == key);
  149. this.formData.splice(index, 1);
  150. },
  151. async submit() {
  152. //编辑
  153. if (this.log_id) {
  154. this.editLog();
  155. return;
  156. }
  157. // 从项目列表页进入的日志界面无须选择项目
  158. if (this.project_id) {
  159. this.formData.forEach((item) => (item.code_id = this.project_id));
  160. }
  161. await createDaily(this.formData);
  162. uni.showToast({
  163. title: "添加成功",
  164. });
  165. setTimeout(() => {
  166. uni.hideToast();
  167. uni.navigateBack();
  168. }, 1800);
  169. },
  170. async initDate(id) {
  171. const res = await approvalLogDaily({ id });
  172. if (res.data) {
  173. this.formData = res.data;
  174. }
  175. },
  176. async deleteLog() {
  177. await approvalDelLog(this.log_id);
  178. uni.showToast({
  179. title: "删除成功",
  180. });
  181. setTimeout(() => {
  182. uni.hideToast();
  183. uni.navigateBack();
  184. }, 1800);
  185. },
  186. async editLog() {
  187. await approvalEditLog({ log_id: this.log_id, formData: this.formData });
  188. uni.showToast({
  189. title: "编辑成功",
  190. duration: 1800,
  191. });
  192. },
  193. },
  194. };
  195. </script>
  196. <style lang="less" scoped>
  197. .head {
  198. width: calc(100% - 60rpx);
  199. padding: 40rpx;
  200. position: fixed;
  201. background: url("~@/static/app-plus/menu-title-bg.png") no-repeat center;
  202. background-size: 100% 100%;
  203. background-color: #fff;
  204. display: flex;
  205. justify-content: space-between;
  206. align-items: center;
  207. z-index: 1;
  208. .title {
  209. font: 18px bold;
  210. color: #fff;
  211. }
  212. }
  213. .forms {
  214. padding: 40rpx 34rpx;
  215. padding-top: 168rpx;
  216. .titleContent {
  217. display: flex;
  218. justify-content: space-between;
  219. }
  220. .title2 {
  221. font-size: 28rpx;
  222. font-weight: 400;
  223. color: #4a90e2;
  224. line-height: 40rpx;
  225. padding-bottom: 18rpx;
  226. border-bottom: 1px solid #e8e8e8;
  227. }
  228. .title-sub {
  229. font-size: 28rpx;
  230. font-weight: 400;
  231. color: #4a4a4a;
  232. line-height: 40rpx;
  233. margin-top: 20rpx;
  234. margin-bottom: 20rpx;
  235. }
  236. .formItem {
  237. }
  238. .proNameEdit,
  239. .input,
  240. .textarea,
  241. .selectPlaceholder {
  242. font-size: 28rpx;
  243. padding-left: 24rpx;
  244. border: 1rpx solid #c0c0c0;
  245. }
  246. .proName,
  247. .proNameEdit,
  248. .input,
  249. .selectPlaceholder {
  250. line-height: 72rpx;
  251. height: 72rpx;
  252. }
  253. .textarea {
  254. padding-top: 10rpx;
  255. width: 400rpx;
  256. height: 200rpx;
  257. }
  258. .proName,
  259. .proNameEdit {
  260. width: 400rpx;
  261. white-space: nowrap;
  262. text-overflow: ellipsis;
  263. overflow: hidden;
  264. }
  265. }
  266. .form {
  267. margin: 0 20px;
  268. }
  269. .button {
  270. position: fixed;
  271. bottom: 0;
  272. left: 0;
  273. width: 100%;
  274. height: 100rpx;
  275. background: url("~@/static/btn-bg.png") no-repeat center;
  276. background-size: 100% 100%;
  277. font-size: 38rpx;
  278. font-weight: 500;
  279. color: #ffffff;
  280. line-height: 100rpx;
  281. text-align: center;
  282. }
  283. .btn-row {
  284. display: flex;
  285. justify-content: space-between;
  286. }
  287. .btn-add {
  288. width: 140rpx;
  289. height: 50rpx;
  290. border: 1rpx solid #329bfe;
  291. color: #329bfe;
  292. display: flex;
  293. justify-content: center;
  294. align-items: center;
  295. margin-left: 48rpx;
  296. }
  297. .box {
  298. position: relative;
  299. }
  300. .btn-remove {
  301. position: absolute;
  302. right: -10px;
  303. top: -12px;
  304. width: 12px;
  305. height: 12px;
  306. background: #f54444;
  307. border-radius: 50%;
  308. line-height: 20rpx;
  309. text-align: center;
  310. color: #fff;
  311. font-weight: bold;
  312. }
  313. .forms {
  314. padding-bottom: 140rpx;
  315. }
  316. </style>