add.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <template>
  2. <view class="container main">
  3. <view class="one">
  4. <uni-forms ref="baseForm" :rules="rules" :modelValue="form" label-width="80px">
  5. <uni-forms-item label="流程" required name="matchPath">
  6. <uni-data-select v-model="form.matchPath" :localdata="matchPathList"
  7. @change="matchPathChange"></uni-data-select>
  8. </uni-forms-item>
  9. <uni-forms-item label="选手" required name="sign">
  10. <uni-data-select v-model="form.sign" :localdata="signList" @change="signChange"></uni-data-select>
  11. </uni-forms-item>
  12. <uni-forms-item label="分数" required name="score">
  13. <uni-easyinput v-model="form.score" placeholder="请输入分数" />
  14. </uni-forms-item>
  15. </uni-forms>
  16. <view class="button">
  17. <button type="primary" @click="submit('baseForm')">保存</button>
  18. </view>
  19. </view>
  20. </view>
  21. </template>
  22. <script>
  23. import moment from 'moment';
  24. export default {
  25. data() {
  26. return {
  27. id: "",
  28. match: "",
  29. user: {},
  30. form: {
  31. sign: "",
  32. score: "",
  33. },
  34. matchPath: '',
  35. matchPathList: [],
  36. signList: [],
  37. // 校验规则
  38. rules: {
  39. sign: {
  40. rules: [{
  41. required: true,
  42. errorMessage: '选手不能为空'
  43. }]
  44. },
  45. score: {
  46. rules: [{
  47. required: true,
  48. errorMessage: '分数不能为空'
  49. }]
  50. },
  51. },
  52. }
  53. },
  54. onLoad: async function(e) {
  55. const that = this;
  56. that.$set(that, `id`, e && e.id || '');
  57. that.$set(that, `match`, e && e.match || '');
  58. that.$set(that.form, `matchPath`, e && e.matchPath || '');
  59. that.$set(that, `matchPath`, e && e.matchPath || '');
  60. await that.searchToken();
  61. await that.searchOther();
  62. await that.search();
  63. },
  64. methods: {
  65. // 用户信息
  66. searchToken() {
  67. const that = this;
  68. try {
  69. const res = uni.getStorageSync('token');
  70. if (res) {
  71. const user = that.$jwt(res);
  72. that.$set(that, `user`, user);
  73. }
  74. } catch (e) {}
  75. },
  76. async searchOther() {
  77. const that = this;
  78. let res;
  79. // 查询流程
  80. res = await that.$api(`/matchPath`, 'GET', {
  81. match: that.match,
  82. is_use: '0',
  83. })
  84. if (res.errcode == '0') {
  85. for (let val of res.data) {
  86. val.text = val.name
  87. val.value = val.id
  88. }
  89. that.$set(that, `matchPathList`, res.data)
  90. }
  91. // 查询选手
  92. res = await that.$api(`/sign`, 'GET', {
  93. match: that.match,
  94. status: '1',
  95. })
  96. if (res.errcode == '0') {
  97. for (let val of res.data) {
  98. val.text = val.name
  99. val.value = val.id
  100. }
  101. that.$set(that, `signList`, res.data)
  102. }
  103. },
  104. // 查询
  105. async search() {
  106. const that = this;
  107. if (that.id) {
  108. let res;
  109. res = await that.$api(`/score/${that.id}`, 'GET', {})
  110. if (res.errcode == '0') {
  111. that.$set(that, `form`, res.data)
  112. } else {
  113. uni.showToast({
  114. title: res.errmsg,
  115. });
  116. }
  117. } else {
  118. that.$set(that, `form`, {
  119. matchPath: that.matchPath
  120. })
  121. }
  122. },
  123. // 选择流程
  124. matchPathChange(e) {
  125. const that = this;
  126. let data = that.matchPathList.find(i => i.id == e);;
  127. if (data) {
  128. that.$set(that.form, `matchPath`, data.id)
  129. }
  130. },
  131. // 选择选手
  132. signChange(e) {
  133. const that = this;
  134. let data = that.signList.find(i => i.id == e);;
  135. if (data) {
  136. that.$set(that.form, `sign`, data.id)
  137. }
  138. },
  139. // 保存
  140. submit(ref) {
  141. const that = this;
  142. that.$refs[ref].validate().then(async res => {
  143. let arr;
  144. const data = {
  145. match: that.match,
  146. time: moment().format('YYYY-MM-DD'),
  147. }
  148. if (that.id) {
  149. arr = await that.$api(`/score/${that.id}`, 'POST', {
  150. ...res,
  151. ...data
  152. })
  153. } else {
  154. arr = await that.$api(`/score`, 'POST', {
  155. ...res,
  156. ...data
  157. })
  158. }
  159. if (arr.errcode == '0') {
  160. uni.showModal({
  161. content: "维护成功!",
  162. showCancel: false
  163. });
  164. uni.navigateBack({
  165. delta: 1
  166. })
  167. } else {
  168. uni.showToast({
  169. title: arr.errmsg,
  170. });
  171. }
  172. }).catch(err => {
  173. console.log('err', err);
  174. })
  175. }
  176. }
  177. }
  178. </script>
  179. <style lang="scss" scoped>
  180. .main {
  181. padding: 0 3vw;
  182. .one {
  183. .button_remark {
  184. margin: 2vw 0;
  185. text-indent: 10px;
  186. color: var(--fF0Color);
  187. font-size: var(--font12Size);
  188. }
  189. .button {
  190. margin: 2vw 0 0 0;
  191. button {
  192. background-color: var(--f3CColor);
  193. font-size: var(--font14Size);
  194. border-radius: 2vw;
  195. }
  196. }
  197. }
  198. }
  199. </style>