您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

form-picker.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. const reqInterface = require("../../../api/models");
  2. const {
  3. findidx,
  4. isValidJson
  5. } = require("../../../utils/dataHelper");
  6. Component({
  7. /**
  8. * 组件的属性列表
  9. */
  10. properties: {
  11. label: {
  12. type: String,
  13. value: ''
  14. },
  15. dict: {
  16. type: String,
  17. value: ''
  18. },
  19. list: {
  20. type: Array,
  21. value: []
  22. },
  23. cols: {
  24. type: Number,
  25. value: 1
  26. },
  27. value: {
  28. type: "any",
  29. value: ""
  30. },
  31. transmitData: Array,
  32. position: {
  33. type: String,
  34. value: 'bottom'
  35. },
  36. requiredMark: {
  37. type: Boolean,
  38. value: true
  39. },
  40. activeMode: {
  41. type: Boolean,
  42. value: false
  43. },
  44. },
  45. observers: {
  46. 'cols': function (val) {
  47. let self = this;
  48. self.setData({
  49. columns: val
  50. })
  51. },
  52. list(val) {
  53. let self = this;
  54. if (val && val.length > 0) {
  55. self.setData({
  56. pickerList: val,
  57. oneValue: val[0].dictKey
  58. })
  59. self.triggerEvent('getvalue', {
  60. val: self.data.oneValue
  61. })
  62. }
  63. },
  64. 'dict': async function (val) {
  65. let self = this;
  66. if (val) {
  67. if (self.data.columns == 1) {
  68. self.setData({
  69. dictionary: val,
  70. })
  71. } else {
  72. self.getData(val)
  73. }
  74. }
  75. },
  76. 'label': function (val) {
  77. let self = this;
  78. self.setData({
  79. placeholder: '请选择' + val
  80. })
  81. },
  82. 'value': function (val) {
  83. let self = this;
  84. // JSON.parse(val)有效
  85. if ((val && isValidJson(JSON.parse(val))) || (val == 0 && isValidJson(JSON.parse(val)))) {
  86. if (typeof (JSON.parse(val)) == 'string') {
  87. self.setData({
  88. pickerTxt: val ? JSON.parse(val) : '',
  89. oneValue: val ? JSON.parse(val) : ''
  90. })
  91. self.triggerEvent('getvalue', {
  92. val: self.data.oneValue
  93. })
  94. } else {
  95. if (self.data.pickerList.length > 0) {
  96. console.log(self.data.pickerList)
  97. let idx = findidx(self.data.pickerList, val)
  98. self.data.pickerTxt = (val || val == 0) ? self.data.pickerList[idx].dictValue : ''
  99. self.setData({
  100. index: idx,
  101. pickerTxt: self.data.pickerTxt
  102. })
  103. } else {
  104. if (self.data.dictionary) {
  105. self.getReq(self.data.dictionary).then(res => {
  106. self.setData({
  107. pickerList: res
  108. })
  109. let idx = findidx(self.data.pickerList, val)
  110. self.data.pickerTxt = (val || val == 0) ? self.data.pickerList[idx].dictValue : ''
  111. self.setData({
  112. index: idx,
  113. pickerTxt: self.data.pickerTxt
  114. })
  115. })
  116. }
  117. }
  118. }
  119. } else if ((val && isValidJson(JSON.parse(val))) || (val == 0 && isValidJson(JSON.parse(val)))) {
  120. if (typeof (JSON.parse(val)) == 'string') {
  121. self.setData({
  122. pickerTxt: val ? JSON.parse(val) : ''
  123. })
  124. } else {
  125. self.data.pickerTxt = JSON.parse(val).map(item => item.dictValue ? item.dictValue + '/' : '').filter(part => part);
  126. if (self.data.pickerTxt.length > 0) {
  127. self.data.pickerTxt[self.data.pickerTxt.length - 1] = self.data.pickerTxt[self.data.pickerTxt.length - 1].slice(0, -1);
  128. }
  129. self.data.pickerTxt = self.data.pickerTxt.join('');
  130. self.setData({
  131. pickerTxt: self.data.pickerTxt
  132. })
  133. }
  134. } else {
  135. self.setData({
  136. index: 0,
  137. pickerTxt: ''
  138. })
  139. }
  140. },
  141. },
  142. lifetimes: {
  143. attached() {
  144. let self = this;
  145. if (self.data.dictionary) {
  146. self.getReq(self.data.dictionary).then(res => {
  147. self.setData({
  148. pickerList: res,
  149. oneValue: res[0].dictKey
  150. })
  151. })
  152. }
  153. }
  154. },
  155. /**
  156. * 组件的初始数据
  157. */
  158. data: {
  159. styleCss: 'padding: 20rpx 0; line-height: 36rpx font-size: 28rpx; border-bottom: 1rpx dashed #f0f0f0;',
  160. dictionary: '',
  161. pickerList: [],
  162. index: 0,
  163. oneValue: 0,
  164. pickerTxt: '',
  165. // 多列, 最多四列吧
  166. columns: 1,
  167. totalArray: [],
  168. firstArray: [],
  169. secondArray: [],
  170. thirdArray: [],
  171. fourthArray: [],
  172. multiIndex: [],
  173. multiValue: []
  174. },
  175. methods: {
  176. onePickerChange(e) {
  177. let self = this;
  178. console.log(e)
  179. self.setData({
  180. index: e.detail.value,
  181. pickerTxt: self.data.pickerList[e.detail.value].dictValue,
  182. oneValue: self.data.pickerList[e.detail.value].dictKey
  183. })
  184. self.triggerEvent('getvalue', {
  185. val: self.data.oneValue
  186. })
  187. },
  188. multiPickerChange(e) {
  189. let self = this;
  190. self.data.multiValue = [];
  191. for (let i = 0; i < self.data.multiIndex.length; i++) {
  192. self.data.multiValue.push(self.data.pickerList[i][self.data.multiIndex[i]] ? self.data.pickerList[i][self.data.multiIndex[i]].dictKey : 0)
  193. }
  194. self.data.pickerTxt = self.data.multiIndex.map((item, i) => {
  195. let name = self.data.pickerList[i][self.data.multiIndex[i]] ? self.data.pickerList[i][self.data.multiIndex[i]].dictValue : '';
  196. return name ? name + '/' : '';
  197. }).filter(part => part);
  198. if (self.data.pickerTxt.length > 0) {
  199. self.data.pickerTxt[self.data.pickerTxt.length - 1] = self.data.pickerTxt[self.data.pickerTxt.length - 1].slice(0, -1);
  200. }
  201. self.data.pickerTxt = self.data.pickerTxt.join('')
  202. self.setData({
  203. multiValue: self.data.multiValue,
  204. pickerTxt: self.data.pickerTxt
  205. })
  206. self.triggerEvent('getvalue', {
  207. val: self.data.multiValue,
  208. label: self.data.pickerTxt
  209. })
  210. },
  211. // 列改变
  212. bindMultiPickerColumnChange(e) {
  213. let self = this;
  214. // 第几列
  215. let col = e.detail.column;
  216. // 第几个下标
  217. let idx = e.detail.value;
  218. self.data.multiIndex[col] = idx;
  219. self.setData({
  220. multiIndex: self.data.multiIndex
  221. })
  222. if (col == 0 && self.data.columns == 2) {
  223. self.data.multiIndex[1] = 0;
  224. self.data.pickerList[1] = [];
  225. self.setData({
  226. secondArray: [],
  227. multiIndex: self.data.multiIndex,
  228. pickerList: self.data.pickerList
  229. })
  230. // 滑动第一列,请求数据
  231. self.getReq(self.data.firstArray[idx].dictKey).then(res => {
  232. self.setData({
  233. secondArray: res,
  234. })
  235. self.data.pickerList[1] = self.data.secondArray;
  236. self.setData({
  237. pickerList: self.data.pickerList
  238. })
  239. })
  240. }
  241. // 四列
  242. if (self.data.columns == 4) {
  243. // 滑动第一列,请求数据
  244. switch (col) {
  245. case 0:
  246. self.data.multiIndex[1] = 0;
  247. self.data.pickerList[1] = [];
  248. self.data.multiIndex[2] = 0;
  249. self.data.pickerList[2] = [];
  250. self.data.multiIndex[3] = 0;
  251. self.data.pickerList[3] = [];
  252. self.setData({
  253. secondArray: [],
  254. multiIndex: self.data.multiIndex,
  255. pickerList: self.data.pickerList
  256. })
  257. self.getReq(self.data.firstArray[idx].dictKey).then(res => {
  258. self.setData({
  259. secondArray: res,
  260. })
  261. self.data.pickerList[1] = self.data.secondArray;
  262. self.setData({
  263. pickerList: self.data.pickerList,
  264. })
  265. })
  266. break;
  267. case 1:
  268. self.getReq(self.data.secondArray[idx].dictKey).then(res => {
  269. self.data.multiIndex[2] = 0;
  270. self.data.pickerList[2] = [];
  271. self.data.multiIndex[3] = 0;
  272. self.data.pickerList[3] = [];
  273. self.setData({
  274. thirdArray: [],
  275. multiIndex: self.data.multiIndex,
  276. pickerList: self.data.pickerList
  277. })
  278. if (res) {
  279. self.setData({
  280. thirdArray: res,
  281. })
  282. self.data.pickerList[2] = self.data.thirdArray;
  283. self.setData({
  284. pickerList: self.data.pickerList,
  285. })
  286. } else {
  287. self.setData({
  288. thirdArray: [],
  289. })
  290. self.data.pickerList[2] = self.data.thirdArray;
  291. self.setData({
  292. pickerList: self.data.pickerList,
  293. })
  294. }
  295. })
  296. break;
  297. case 2:
  298. self.getReq(self.data.thirdArray[idx].dictKey).then(res => {
  299. self.data.multiIndex[3] = 0;
  300. self.data.pickerList[3] = [];
  301. self.setData({
  302. fourArray: [],
  303. multiIndex: self.data.multiIndex,
  304. pickerList: self.data.pickerList
  305. })
  306. if (res) {
  307. self.setData({
  308. fourArray: res,
  309. })
  310. self.data.pickerList[3] = self.data.fourArray;
  311. self.setData({
  312. pickerList: self.data.pickerList,
  313. })
  314. } else {
  315. self.setData({
  316. fourArray: [],
  317. })
  318. self.data.pickerList[3] = self.data.fourArray;
  319. self.setData({
  320. pickerList: self.data.pickerList,
  321. })
  322. }
  323. })
  324. break;
  325. }
  326. }
  327. },
  328. async getData(val) {
  329. let self = this;
  330. let res = null,
  331. res2 = null,
  332. res3 = null,
  333. res4 = null;
  334. res = await self.getReq(val)
  335. if (self.data.columns == 2) {
  336. self.setData({
  337. firstArray: res
  338. })
  339. res2 = await self.getReq(res[0].dictKey)
  340. self.setData({
  341. secondArray: res2,
  342. })
  343. self.data.pickerList[0] = self.data.firstArray;
  344. self.data.pickerList[1] = self.data.secondArray;
  345. self.setData({
  346. pickerList: self.data.pickerList,
  347. multiIndex: [0, 0]
  348. })
  349. } else if (self.data.columns != 1) {
  350. self.setData({
  351. firstArray: res
  352. })
  353. res2 = await self.getReq(res[0].dictKey)
  354. self.setData({
  355. secondArray: res2 ? res2 : []
  356. })
  357. if (res2) {
  358. res3 = await self.getReq(res2[0].dictKey)
  359. self.setData({
  360. thirdArray: res3 ? res3 : [],
  361. })
  362. } else {
  363. self.setData({
  364. thirdArray: [],
  365. })
  366. }
  367. if (res3) {
  368. res4 = await self.getReq(res3[0].dictKey)
  369. self.setData({
  370. fourthArray: res4 ? res4 : [],
  371. })
  372. } else {
  373. self.setData({
  374. fourthArray: [],
  375. })
  376. }
  377. self.data.pickerList[0] = self.data.firstArray;
  378. self.data.pickerList[1] = self.data.secondArray;
  379. self.data.pickerList[2] = self.data.thirdArray;
  380. self.data.pickerList[3] = self.data.fourthArray;
  381. self.setData({
  382. pickerList: self.data.pickerList,
  383. multiIndex: [0, 0, 0, 0]
  384. })
  385. }
  386. },
  387. getReq(val) {
  388. let self = this;
  389. return new Promise((resolve, reject) => {
  390. reqInterface.GetBladeSystemDictDictionary({
  391. code: val
  392. }).then(res => {
  393. resolve(res)
  394. }).catch(error => {
  395. console.error("GetDictDetail请求错误:", error);
  396. reject(error);
  397. });
  398. })
  399. }
  400. }
  401. })