My_input.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. <template>
  2. <view>
  3. <u-modal
  4. ref="uModal"
  5. :show="show"
  6. @confirm="confirm"
  7. :asyncClose="true"
  8. :showCancelButton="true"
  9. @cancel="onCancel"
  10. :confirmText="confirmText"
  11. >
  12. <view class="content">
  13. <!-- 整数类型 -->
  14. <block v-if="item.inputType == 'int'">
  15. <view class="title u-line-1">
  16. {{ item.funcname }}{{ item.params.unit || "" }}
  17. </view>
  18. <block v-if="item.inputType == 'int'">
  19. <u--input
  20. :focus="intFocus"
  21. v-model="value"
  22. :type="item.num_scale == 0 ? 'number' : 'digit'"
  23. :placeholder="item.showValue || item.params.lastvalue"
  24. border="surround"
  25. />
  26. </block>
  27. <view class="tips" v-if="item.paramValue">
  28. <u-icon name="info-circle-fill" color="#E2201A" />
  29. <text style="margin-left: 4px">
  30. 有一条待更新记录,待更新值为:{{ item.paramValue
  31. }}{{ item.params.unit || "" }}
  32. </text>
  33. </view>
  34. </block>
  35. <!-- 布尔开关 -->
  36. <block v-else-if="item.inputType == 'switch'">
  37. <view class="title u-line-1">
  38. {{ item.funcname || "" }}{{ item.params.unit || "" }}
  39. </view>
  40. {{ switchTips }}
  41. </block>
  42. <!-- 步进器 -->
  43. <block v-else-if="item.inputType == 'step'">
  44. <view class="title u-line-1">
  45. {{ item.funcname }}{{ item.params.unit || "" }}
  46. </view>
  47. <!-- if 和 else 是因为有时候decimal-length设置为0不能点击加减 -->
  48. <u-number-box
  49. v-if="item.params.num_scale"
  50. :asyncChange="true"
  51. v-model="value"
  52. :min="item.params.num_minvalue || -2147483648"
  53. :max="item.params.num_maxvalue || 2147483647"
  54. :step="item.params.num_step"
  55. :decimal-length="item.params.num_scale"
  56. @change="stepChange"
  57. @focus="stepFocus"
  58. @blur="stepBlue"
  59. inputWidth="100"
  60. />
  61. <u-number-box
  62. v-else
  63. :asyncChange="true"
  64. v-model="value"
  65. :min="item.params.num_minvalue || -2147483648"
  66. :max="item.params.num_maxvalue || 2147483647"
  67. :step="item.params.num_step"
  68. @change="stepChange"
  69. @focus="stepFocus"
  70. @blur="stepBlue"
  71. inputWidth="100"
  72. />
  73. <view class="tips" v-if="item.paramValue">
  74. <u-icon name="info-circle-fill" color="#E2201A" />
  75. <text style="margin-left: 4px">
  76. 有一条待更新记录,待更新值为:{{ item.paramValue
  77. }}{{ item.params.unit || "" }}
  78. </text>
  79. </view>
  80. <view class="tips">
  81. <u-icon name="info-circle-fill" color="#E2201A" />
  82. <text style="margin-left: 4px">
  83. 受步长限制,手动输入会计算为最接近的合法值
  84. </text>
  85. </view>
  86. </block>
  87. <!-- 时段 -->
  88. <block v-else-if="item.inputType == 'dayParting'">
  89. <view class="title u-line-1">
  90. {{ item.funcname }}{{ item.params[item.key + "P"].unit || "" }}
  91. </view>
  92. <view class="day-parting">
  93. <picker
  94. mode="time"
  95. :value="item.params[item.key + 'T'].lastvalue"
  96. @change="changeT_T"
  97. >
  98. <view class="row">
  99. <view class="label"> 时间: </view>
  100. <view class="day-parting-row">
  101. {{ item.params[item.key + "T"].lastvalue || "请选择时间" }}
  102. </view>
  103. </view>
  104. </picker>
  105. <view class="row" style="margin-top: 20px">
  106. <view class="label">压力:</view>
  107. <u--input
  108. :focus="dayPartingFocus"
  109. v-model="value"
  110. :type="
  111. item.params[item.key + 'P'].num_scale == 0
  112. ? 'number'
  113. : 'digit'
  114. "
  115. :placeholder="item.params[item.key + 'P'].lastvalue || '请输入'"
  116. border="surround"
  117. />
  118. </view>
  119. </view>
  120. </block>
  121. <!-- 选择 -->
  122. <block v-else-if="item.inputType == 'radio'">
  123. <view class="title u-line-1">
  124. {{ item.funcname }}{{ item.params.unit || "" }}
  125. </view>
  126. <view class="select-box">
  127. <view
  128. class="item"
  129. @click="onSelected(op, item.inputType)"
  130. :class="op.value == item.params.lastvalue ? 'active' : ''"
  131. v-for="op in item.params.options"
  132. :key="op.value"
  133. hover-class="navigator-hover"
  134. >
  135. {{ op.label }}
  136. </view>
  137. </view>
  138. </block>
  139. <!-- 特殊选择 -->
  140. <block v-else-if="item.inputType == 'radioNum'">
  141. <view class="title u-line-1">
  142. {{ item.funcname }}{{ item.params.unit || "" }}
  143. </view>
  144. <view class="select-box">
  145. <view
  146. class="item"
  147. @click="onSelected(op, item.inputType)"
  148. :class="op.value == 1 ? 'active' : ''"
  149. v-for="op in item.params.options"
  150. :key="op.key"
  151. hover-class="navigator-hover"
  152. >
  153. {{ op.label }}
  154. </view>
  155. </view>
  156. </block>
  157. <!-- slot -->
  158. <block v-else-if="isSlot">
  159. <view class="title u-line-1">
  160. {{ item.funcname || "" }}
  161. </view>
  162. <slot />
  163. </block>
  164. <!-- 其他类型 -->
  165. <block v-else> 其他类型 </block>
  166. <view class="tips" v-if="toBeUpdated">
  167. <u-icon name="info-circle-fill" color="#E2201A" />
  168. <text style="margin-left: 4px">
  169. {{ toBeUpdated }}
  170. </text>
  171. </view>
  172. <view class="tips" v-if="tips">
  173. <u-icon name="info-circle-fill" color="#55AAFF" />
  174. <text style="margin-left: 4px">
  175. {{ tips }}
  176. </text>
  177. </view>
  178. </view>
  179. </u-modal>
  180. </view>
  181. </template>
  182. <script>
  183. import currency from "../../utils/currency";
  184. export default {
  185. name: "My_input",
  186. props: {
  187. customMethod: {
  188. type: Function,
  189. },
  190. mode: {
  191. type: [Number || String],
  192. default: 0,
  193. },
  194. sendMsg: {
  195. type: Function,
  196. },
  197. },
  198. data() {
  199. return {
  200. show: false,
  201. item: {},
  202. tips: "",
  203. toBeUpdated: "",
  204. intFocus: false,
  205. dayPartingFocus: false,
  206. value: "",
  207. confirmText: "确定",
  208. loading: false,
  209. stepIsCalculate: false,
  210. stagingMethod: null,
  211. isSlot: false,
  212. switchTips: "",
  213. checkBeforeSending: null,
  214. };
  215. },
  216. methods: {
  217. /**
  218. * @param item.inputType int:数字 step:步进器 radio:单选 switch:开关 dayParting:分时段
  219. */
  220. openInput(item, isSlot = false, replenish = {}) {
  221. this.replenish = replenish;
  222. try {
  223. this.item = JSON.parse(JSON.stringify(item));
  224. } catch (error) {
  225. console.log("item JSON.parse 失败");
  226. this.item = item;
  227. }
  228. this.switchTips = "";
  229. this.show = true;
  230. let tips = "",
  231. toBeUpdated = "";
  232. this.confirmText = "确定";
  233. this.isSlot = isSlot;
  234. if (!isSlot) {
  235. let params = item.params;
  236. if (item.inputType == "int") {
  237. this.value = "";
  238. this.intFocus = false;
  239. if (params.num_minvalue || params.num_maxvalue)
  240. tips += `输入范围:${params.num_minvalue || 0} ~ ${
  241. params.num_maxvalue || "∞"
  242. }`;
  243. if (params.num_scale) tips += ` 保留${params.num_scale}位小数`;
  244. if (params.num_step) tips += ` 增量${params.num_step || 0}`;
  245. if (tips && params.unit) tips += ` 单位${params.unit}`;
  246. setTimeout(() => {
  247. this.intFocus = true;
  248. }, 300);
  249. } else if (item.inputType == "switch") {
  250. let paramValue = item.paramValue + "";
  251. if (paramValue.length && paramValue != item.params.lastvalue) {
  252. this.confirmText = "取消修改";
  253. this.switchTips = `查询到“${
  254. item.funcname
  255. }”有待更新记录,待更新值为“${
  256. params.options.find((v) => v.value == item.paramValue).label
  257. }”;您可通过“${this.confirmText}”按钮取消待更新请求`;
  258. } else {
  259. this.confirmText = "切换";
  260. console.log("paramValue", item.paramValue);
  261. this.switchTips = `是否将“${item.funcname}”${this.confirmText}为:“${
  262. params.options.find(
  263. (v) =>
  264. v.value !=
  265. (String(item.paramValue).length
  266. ? item.paramValue
  267. : item.params.lastvalue)
  268. ).label
  269. }”`;
  270. }
  271. } else if (item.inputType == "dayParting") {
  272. this.value = item.params[item.key + "p"] || "";
  273. this.dayPartingFocus = false;
  274. toBeUpdated =
  275. item.paramValue.time || item.paramValue.value
  276. ? `查询到一条待更新指令,更新内容为:时间${item.paramValue.time
  277. .split("_")
  278. .join(":")},压力${item.paramValue.value}MPA`
  279. : "";
  280. let p = item.params[item.key + "P"];
  281. if (p.num_minvalue || p.num_maxvalue)
  282. tips += `输入范围:${p.num_minvalue || 0} ~ ${
  283. p.num_maxvalue || "∞"
  284. }`;
  285. if (p.num_scale) tips += ` 保留${p.num_scale}位小数`;
  286. if (p.num_step) tips += ` 增量${p.num_step || 0}`;
  287. if (tips && p.unit) tips += ` 单位${p.unit}`;
  288. setTimeout(() => {
  289. this.dayPartingFocus = true;
  290. }, 300);
  291. } else if (item.inputType == "radio") {
  292. let paramValue = item.paramValue + "";
  293. if (paramValue.length && paramValue != item.params.lastvalue) {
  294. toBeUpdated = `查询到“${item.funcname}”有待更新记录,待更新值为“${
  295. params.options.find((v) => v.value == item.paramValue).label
  296. }”`;
  297. }
  298. }
  299. if (item.inputType == "radioNum") {
  300. if (item.paramValue)
  301. toBeUpdated = `查询到“${item.funcname}”有待更新记录,待更新值为“${item.paramValue}”`;
  302. } else if (item.inputType == "step") {
  303. this.value = params.lastvalue;
  304. /* if (this.value > params.num_maxvalue) this.value = params.num_maxvalue;
  305. if (this.value < params.num_minvalue) this.value = params.num_minvalue; */
  306. if (params.num_minvalue || params.num_maxvalue)
  307. tips += `输入范围:${params.num_minvalue || 0} ~ ${
  308. params.num_maxvalue || "∞"
  309. }`;
  310. if (params.num_scale) tips += ` 保留${params.num_scale}位小数`;
  311. if (params.num_step) tips += ` 增量${params.num_step || 0}`;
  312. if (tips && params.unit) tips += ` 单位${params.unit}`;
  313. setTimeout(() => {
  314. this.intFocus = true;
  315. }, 300);
  316. } else if (item.inputType == "step") {
  317. this.isSlot = true;
  318. } else {
  319. console.log(item);
  320. }
  321. }
  322. this.tips = tips;
  323. this.toBeUpdated = toBeUpdated;
  324. },
  325. onSelected(option, mode) {
  326. if (mode == "radio") {
  327. this.item.params.lastvalue = option.value;
  328. } else if (mode == "radioNum") {
  329. this.item.params.options = this.item.params.options.map((v) => {
  330. v.value = v.key == option.key ? 1 : 0;
  331. return v;
  332. });
  333. this.$Http.changeOptions &&
  334. this.$Http.changeOptions(this.item.params.options);
  335. }
  336. },
  337. stepChange(e) {
  338. if (!this.stepIsCalculate) this.value = e.value;
  339. },
  340. stepFocus(e) {
  341. this.stepIsCalculate = true;
  342. },
  343. stepBlue(e) {
  344. const { num_step, num_scale, num_maxvalue, num_minvalue } =
  345. this.item.params;
  346. this.value = currency(
  347. currency(e.value, {
  348. increment: num_step,
  349. precision: num_scale,
  350. }).format()
  351. ).value;
  352. if (num_minvalue != "" && this.value < num_minvalue)
  353. this.value = num_minvalue - 0;
  354. if (num_maxvalue != "" && this.value > num_maxvalue)
  355. this.value = num_maxvalue - 0;
  356. this.stepIsCalculate = false;
  357. if (this.stagingMethod) this.confirm();
  358. },
  359. /* 时段修改 */
  360. changeT_T(e) {
  361. this.item.params[this.item.key + "T"].lastvalue = e.detail.value;
  362. },
  363. submitBreak(content) {
  364. uni.showToast({
  365. title: content + "",
  366. icon: "none",
  367. });
  368. this.$refs.uModal.loading = false;
  369. },
  370. confirm() {
  371. if (this.loading) return;
  372. if (this.isSlot) return this.$emit("customMethod", "confirm");
  373. let item = this.item,
  374. params = item.params,
  375. value = "";
  376. if (item.inputType == "int" || item.inputType == "step") {
  377. value = this.value - 0;
  378. // if (value == 0) return this.submitBreak("还未输入值");
  379. if (params.num_scale != 0) value = value.toFixed(params.num_scale);
  380. if (params.num_minvalue && value - 0 < params.num_minvalue - 0)
  381. return this.submitBreak("输入值小于最低范围");
  382. if (params.num_maxvalue && value - 0 > params.num_maxvalue - 0)
  383. return this.submitBreak("输入值大于最大范围");
  384. } else if (item.inputType == "switch") {
  385. let value = item.paramName;
  386. try {
  387. if (item.changeItem.length) {
  388. }
  389. } catch (error) {
  390. item.changeItem = [];
  391. }
  392. if (item.changeItem.length) {
  393. item.changeItem.forEach(
  394. (key) => (value[key] = value[key] == 1 ? 0 : 1)
  395. );
  396. } else {
  397. for (const key in value) {
  398. value[key] = value[key] == 1 ? 0 : 1;
  399. }
  400. }
  401. // value = params.options.find(v => v.value != (String(item.paramValue).length ? item.paramValue : item.params.lastvalue)).value
  402. return this.submit(item.w_functionid, value);
  403. } else if (item.inputType == "dayParting") {
  404. value = this.value - 0;
  405. // if (value == 0) return this.submitBreak("还未输入值");
  406. let p = item.params[item.key + "P"];
  407. if (p.num_scale != 0) value = value.toFixed(p.num_scale);
  408. if (p.num_minvalue && value - 0 > p.num_minvalue - 0)
  409. return this.submitBreak("输入值小于最低范围");
  410. if (p.num_maxvalue && value - 0 < p.num_maxvalue - 0)
  411. return this.submitBreak("输入值大于最大范围");
  412. return this.submit(item.w_functionid, {
  413. [item.key + "P"]: value,
  414. [item.key + "T"]: params[item.key + "T"].lastvalue
  415. .split(":")
  416. .join("_"),
  417. });
  418. } else if (item.inputType == "radio") {
  419. value = params.lastvalue;
  420. } else if (item.inputType == "radioNum") {
  421. value = { ...this.replenish };
  422. params.options.forEach((v) => (value[`${v.key}`] = v.value));
  423. return this.submit(item.w_functionid, value);
  424. }
  425. this.submit(item.w_functionid, {
  426. [item.paramName]: value,
  427. });
  428. },
  429. onCancel() {
  430. this.$refs.uModal.loading = false;
  431. this.show = false;
  432. this.checkBeforeSending = null;
  433. },
  434. async submit(w_functionid, params) {
  435. console.log("提交", w_functionid, params);
  436. if (this.checkBeforeSending) {
  437. this.$refs.uModal.loading = false;
  438. this.checkBeforeSending(w_functionid, params);
  439. return;
  440. }
  441. if (this.stepIsCalculate) {
  442. this.stagingMethod = true;
  443. return;
  444. } else {
  445. this.stagingMethod = false;
  446. }
  447. this.loading = true;
  448. if (this.mode == 1) {
  449. this.$emit("sendMsg", w_functionid, params);
  450. } else {
  451. let res = await this.$Http.setControlItem(w_functionid, params);
  452. this.loading = false;
  453. if (res) {
  454. this.show = false;
  455. this.stepIsCalculate = null;
  456. setTimeout(() => {
  457. // this.item = null;
  458. }, 200);
  459. } else {
  460. this.$refs.uModal.loading = false;
  461. }
  462. }
  463. },
  464. },
  465. };
  466. </script>
  467. <style lang="scss" scoped>
  468. .content {
  469. width: 100%;
  470. /* 边框 */
  471. .day-parting-row {
  472. display: flex;
  473. align-items: center;
  474. flex: 1;
  475. width: 0;
  476. border: 1px solid #dadbde;
  477. border-radius: 4px;
  478. padding: 6px 9px;
  479. height: 35px;
  480. font-size: 14px;
  481. box-sizing: border-box;
  482. }
  483. .title {
  484. text-align: center;
  485. font-size: 16px;
  486. font-weight: bold;
  487. color: #333;
  488. margin-bottom: 20px;
  489. }
  490. .tips {
  491. display: flex;
  492. color: #666;
  493. font-size: 10px;
  494. margin-top: 6px;
  495. align-items: center;
  496. /deep/.u-icon {
  497. .u-icon__icon {
  498. font-size: 12px !important;
  499. }
  500. }
  501. }
  502. /* 分时控制 */
  503. .day-parting {
  504. .row {
  505. display: flex;
  506. align-items: center;
  507. .label {
  508. width: 50px;
  509. }
  510. }
  511. }
  512. /* 选择样式 */
  513. .select-box {
  514. display: flex;
  515. flex-wrap: wrap;
  516. .item {
  517. font-size: 12px;
  518. padding: 2px 6px;
  519. border-radius: 4px;
  520. border: 1px solid #333;
  521. margin-right: 5px;
  522. margin-bottom: 5px;
  523. }
  524. .active {
  525. // font-weight: bold;
  526. color: #fff;
  527. background: #052e5d;
  528. }
  529. }
  530. /deep/.u-input {
  531. .uni-input-input {
  532. font-size: 12px;
  533. }
  534. }
  535. /* 步进器样式 */
  536. /deep/ .u-number-box__minus,
  537. /deep/.u-number-box__plus {
  538. height: 30px !important;
  539. width: 40px !important;
  540. }
  541. /deep/ .u-number-box__input {
  542. height: 30px !important;
  543. width: 100px !important;
  544. }
  545. }
  546. </style>