en-city.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <template>
  2. <view class="box">
  3. <view class="input-box">
  4. <view class="input-box-left" :style="{'letter-spacing':labelWidth}">
  5. {{ label }}
  6. </view>
  7. <view class="input-box-right" @click="showPickerObj">
  8. <text :class="{'no-option':!optionName}">{{ optionName ? optionName : placeholder }}</text>
  9. <text class="iconfont">&#xe62b;</text>
  10. </view>
  11. </view>
  12. <uni-data-picker :popup-title="'选择'+label" :localdata="localData" ref="pickerObj" v-show="showPicker"
  13. @change="pickerChange" :border="false" :clear-icon="false" @popupclosed="setPopupClosed">
  14. </uni-data-picker>
  15. </view>
  16. </template>
  17. <script>
  18. import {getRegion} from "@/api/common";
  19. import tools from "@/service/tools";
  20. export default {
  21. name: 'en-city',
  22. props: {
  23. label: {
  24. type: String,
  25. default: '地址'
  26. },
  27. placeholder: {
  28. type: String,
  29. default: '请选择地址'
  30. },
  31. disabled: {
  32. type: Boolean,
  33. default: false
  34. },
  35. name: {
  36. type: String,
  37. default: 'text'
  38. },
  39. valueType: {
  40. type: String,
  41. default: '1'
  42. },
  43. value: {
  44. default: ''
  45. }
  46. },
  47. data() {
  48. return {
  49. localData: [],
  50. inputValue: [],
  51. optionName: '',
  52. labelWidth: 0,
  53. showPicker: false
  54. }
  55. },
  56. components: {},
  57. mounted() {
  58. this.setValue()
  59. this.setLabelWidth()
  60. this.getRegion()
  61. },
  62. watch: {
  63. 'value': function () {
  64. this.setValue()
  65. },
  66. 'inputValue': function () {
  67. console.log(this.inputValue)
  68. if (this.valueType === '1') {
  69. this.$emit('input', this.optionName)
  70. } else {
  71. this.$emit('input', this.optionName)
  72. }
  73. }
  74. },
  75. methods: {
  76. getRegion() {
  77. let localData=uni.getStorageSync('sysCityData')
  78. if(!localData){
  79. getRegion({
  80. type: 1
  81. }).then((res) => {
  82. if (res.code === 1) {
  83. this.localData = res.data;
  84. uni.setStorageSync('sysCityData', JSON.stringify(this.localData))
  85. } else {
  86. tools.error(res.msg);
  87. }
  88. })
  89. }else {
  90. this.localData=JSON.parse(localData)
  91. }
  92. },
  93. setValue() {
  94. this.optionName=this.value
  95. // let value = this.value
  96. // if (!value) {
  97. // value = []
  98. // } else if (typeof value === 'string') {
  99. // value = value.split(',')
  100. // }
  101. // if (this.inputValue !== value) {
  102. // this.inputValue = value
  103. // this.optionName = "";
  104. // this.localData.forEach((one) => {
  105. // if (one.value + '' === this.inputValue[0] + '') {
  106. // this.optionName += one.text + " ";
  107. // if (one.children) {
  108. // one.children.forEach((two) => {
  109. // if (two.value + '' === this.inputValue[1] + '') {
  110. // this.optionName += two.text + " ";
  111. // if (two.children) {
  112. // two.children.forEach((three) => {
  113. // if (three.value + '' === this.inputValue[2] + '') {
  114. // this.optionName += three.text + " ";
  115. // }
  116. // })
  117. // }
  118. // }
  119. // })
  120. // }
  121. // }
  122. // })
  123. // }
  124. },
  125. setPopupClosed() {
  126. this.showPicker = false;
  127. },
  128. showPickerObj() {
  129. this.$refs.pickerObj.show();
  130. this.showPicker = true;
  131. },
  132. pickerChange(data) {
  133. this.optionName = "";
  134. this.inputValue = [];
  135. data.detail.value.forEach((item) => {
  136. this.optionName += item.text + " ";
  137. this.inputValue.push(item.value)
  138. });
  139. this.showPicker = false;
  140. },
  141. setLabelWidth() {
  142. let differenceNum = 4 - this.label.length;
  143. if (differenceNum === 2) {
  144. this.labelWidth = '2em'
  145. } else if (differenceNum === 1) {
  146. this.labelWidth = '0.5em'
  147. }
  148. }
  149. }
  150. }
  151. </script>
  152. <style lang="scss" scoped>
  153. @import url("../../static/css/en-common.css");
  154. ::v-deep .selected-item-active {
  155. border-bottom: 2px solid #FF0000 !important;
  156. }
  157. .box {
  158. .input-box {
  159. display: flex;
  160. align-items: center;
  161. justify-content: space-between;
  162. .input-box-left {
  163. width: 210rpx;
  164. font-size: 30rpx;
  165. color: #333333;
  166. }
  167. .input-box-right {
  168. width: 100%;
  169. text {
  170. font-size: 28rpx;
  171. color: #333;
  172. }
  173. .iconfont {
  174. float: right;
  175. }
  176. .no-option {
  177. color: #999999;
  178. }
  179. }
  180. }
  181. }
  182. </style>