itmister-address-picker-other.nvue 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. <template>
  2. <div v-if="isShow">
  3. <div class="address-picker-mask" bubble='true' @click="hide"
  4. :class="[isOpen?'show-address-picker-mask':'hide-address-picker-mask']"
  5. :style="{backgroundColor:maskColor,opacity:opacity}">
  6. </div>
  7. <!-- #ifdef APP-PLUS-NVUE -->
  8. <div class="address-picker-container" ref="addressPickerEl" @click.stop="handleClick"
  9. :class="[isOpen?'show-address-picker':'hide-address-picker']">
  10. <!-- #endif -->
  11. <!-- #ifndef APP-PLUS-NVUE -->
  12. <div class="address-picker-container" @click.stop="handleClick"
  13. :class="[isOpen?'show-address-picker':'hide-address-picker']"
  14. :style="{transform:'translatey('+translatey+'px)'}">
  15. <!-- #endif -->
  16. <div class="address-picker-button row between-center">
  17. <text class="address-picker-button-text" style="color: #777777;" @click="hide">取消</text>
  18. <text class="address-picker-button-text" style="color: #007AFF;"
  19. @click="confirmChange(value)">确认</text>
  20. </div>
  21. <picker-view class="address-picker-box" v-if="visible" :indicator-style="indicatorStyle" :value="value"
  22. @change="bindChange">
  23. <picker-view-column class="center">
  24. <div class="address-picker-item center" v-for="(item,index) in provinceList" :key="index">
  25. <text class="address-picker-item-text">{{item}}</text>
  26. </div>
  27. </picker-view-column>
  28. <picker-view-column>
  29. <div class="address-picker-item center" v-for="(item,index) in cityList" :key="index">
  30. <text class="address-picker-item-text">{{item}}</text>
  31. </div>
  32. </picker-view-column>
  33. <picker-view-column v-show="showCheck">
  34. <div class="address-picker-item center" v-for="(item,index) in areaList" :key="index">
  35. <text class="address-picker-item-text">{{item}}</text>
  36. </div>
  37. </picker-view-column>
  38. </picker-view>
  39. </div>
  40. </div>
  41. </template>
  42. <script>
  43. // #ifdef APP-PLUS-NVUE
  44. const animation = weex.requireModule('animation');
  45. // #endif
  46. export default {
  47. name: "itmisterAddressPickerOther",
  48. props: {
  49. showCheck: { //是否显示区级
  50. type: Boolean,
  51. default: true
  52. },
  53. // wholeCountry:{
  54. // type: Boolean,
  55. // default: false
  56. // },
  57. maskColor: { // 模态框背景色
  58. type: String,
  59. default: 'rgba(0,0,0,0.3)'
  60. },
  61. addressIndex: { // 修改地址时使用,接收旧地址的省市区,对应的索引数组
  62. type: Array,
  63. default () {
  64. return [0, 0, 0]
  65. }
  66. }
  67. },
  68. data() {
  69. return {
  70. isShow: false, // 是否弹出
  71. isOpen: false,
  72. translatey: 500, // 弹出层高度
  73. opacity: 0, // 模态框透明度
  74. visible: true,
  75. indicatorStyle: `height: ${Math.round(uni.getSystemInfoSync().screenWidth/(750/100))}px;`,
  76. value: [], // 设置默认选中
  77. cityData: require('../../components/itmister-address-picker/city_other.json'), // 加载城市数据
  78. provinceList: [], // 省列表
  79. cityList: [], // 市列表
  80. areaList: [], // 区列表
  81. }
  82. },
  83. mounted() {
  84. this.value = this.addressIndex;
  85. // let arrays = {}
  86. // arrays = {
  87. // city: [{
  88. // area: [],
  89. // name: ''
  90. // }],
  91. // name: '全国'
  92. // }
  93. // this.cityData.splice(0, 0, arrays)
  94. this.loadProvince(this.cityData);
  95. },
  96. methods: {
  97. // 确认选中
  98. confirmChange(val) {
  99. let address = {
  100. // 当前选中的地址
  101. province: this.provinceList[val[0]],
  102. city: this.cityList[val[1]],
  103. area: this.areaList[val[2]],
  104. // 当前地址的索引,保存起来,用于日后修改地址时,加载默认
  105. provinceIndex: val[0],
  106. cityIndex: val[1],
  107. areaIndex: val[2]
  108. }
  109. this.$emit('confirmChange', address);
  110. this.hide();
  111. },
  112. // 监听选中
  113. bindChange(e) {
  114. const val = e.detail.value;
  115. this.value = [val[0], val[1], val[2]];
  116. this.loadCity(this.cityData[val[0]].city);
  117. },
  118. // 加载数据
  119. loadProvince(data) {
  120. let newList = [];
  121. data.forEach(item => {
  122. newList.push(item.name);
  123. });
  124. this.provinceList = newList;
  125. this.loadCity(this.cityData[this.value[0]].city);
  126. },
  127. loadCity(data) {
  128. let newList = [];
  129. data.forEach(item => {
  130. newList.push(item.name);
  131. });
  132. this.cityList = newList;
  133. if ((data.length - 1) >= this.value[1]) { // 解决特殊情况报错的问题
  134. this.loadArea(this.cityData[this.value[0]].city[this.value[1]].area);
  135. } else {
  136. this.loadArea(this.cityData[this.value[0]].city[0].area);
  137. }
  138. },
  139. loadArea(data) {
  140. let newList = [];
  141. data.forEach(item => {
  142. newList.push(item);
  143. });
  144. this.areaList = newList;
  145. },
  146. // 显示
  147. show() {
  148. // 渲染视图
  149. this.isShow = true;
  150. // 等待视图渲染
  151. this.$nextTick(() => {
  152. setTimeout(() => {
  153. // #ifdef APP-PLUS-NVUE
  154. // APP 端获取动画元素
  155. let ele = this.$refs.addressPickerEl;
  156. // #endif
  157. // 执行 mask 透明度
  158. this.isOpen = true;
  159. this.opacity = 1;
  160. // #ifndef APP-PLUS-NVUE
  161. // 非APP 端 执行动画
  162. this.translatey = 0;
  163. // #endif
  164. // #ifdef APP-PLUS-NVUE
  165. // APP 端执行动画
  166. animation.transition(ele, {
  167. styles: {
  168. transform: 'translate(0,0)',
  169. transformOrigin: 'center center'
  170. },
  171. duration: 250, //ms
  172. timingFunction: 'ease',
  173. delay: 0 //ms
  174. });
  175. // #endif
  176. }, 10);
  177. });
  178. },
  179. hide() {
  180. // #ifdef APP-PLUS-NVUE
  181. // APP 端获取动画元素
  182. let ele = this.$refs.addressPickerEl;
  183. // #endif
  184. // 执行 mask 透明度
  185. this.isOpen = false;
  186. this.opacity = 0;
  187. // #ifndef APP-PLUS-NVUE
  188. // 非APP 端执行动画
  189. this.translatey = 500;
  190. setTimeout(() => {
  191. this.isShow = false;
  192. }, 200);
  193. // #endif
  194. // #ifdef APP-PLUS-NVUE
  195. // APP 端执行动画
  196. animation.transition(ele, {
  197. styles: {
  198. transform: 'translate(0,550)',
  199. transformOrigin: 'center center'
  200. },
  201. duration: 250, //ms
  202. timingFunction: 'ease',
  203. delay: 0 //ms
  204. }, () => {
  205. this.isShow = false;
  206. });
  207. // #endif
  208. },
  209. // 阻止冒泡
  210. handleClick(event) {
  211. event.stopPropagation();
  212. }
  213. }
  214. }
  215. </script>
  216. <style lang="scss">
  217. .address-picker-mask {
  218. position: fixed;
  219. left: 0;
  220. right: 0;
  221. top: 0;
  222. bottom: 0;
  223. z-index: 999988;
  224. }
  225. .address-picker-container {
  226. position: fixed;
  227. left: 0;
  228. right: 0;
  229. bottom: 0;
  230. z-index: 999999;
  231. height: 640rpx;
  232. background-color: #FFFFFF;
  233. }
  234. .show-address-picker-mask {
  235. transition-property: opacity;
  236. transition-duration: 0.2s;
  237. transition-timing-function: ease;
  238. }
  239. .hide-address-picker-mask {
  240. transition-property: opacity;
  241. transition-duration: 0.2s;
  242. transition-timing-function: ease;
  243. }
  244. .show-address-picker {
  245. /* #ifndef APP-PLUS-NVUE */
  246. transition-property: transform;
  247. transition-duration: 0.2s;
  248. transition-timing-function: ease;
  249. /* #endif */
  250. /* #ifdef APP-PLUS-NVUE */
  251. transform: 'translate(0,550)',
  252. /* #endif */
  253. }
  254. .hide-address-picker {
  255. /* #ifndef APP-PLUS-NVUE */
  256. transition-property: transform;
  257. transition-duration: 0.2s;
  258. transition-timing-function: ease;
  259. /* #endif */
  260. /* #ifdef APP-PLUS-NVUE */
  261. transform: 'translate(0,550)',
  262. /* #endif */
  263. }
  264. // 内容
  265. .address-picker-box {
  266. width: 750rpx;
  267. height: 500rpx;
  268. padding: 0 20rpx;
  269. /* #ifdef H5 */
  270. box-sizing: border-box;
  271. /* #endif */
  272. background-color: #FFF;
  273. }
  274. .address-picker-item {
  275. height: 100rpx;
  276. }
  277. .address-picker-item-text {
  278. color: #444;
  279. font-size: 32rpx;
  280. }
  281. // 按钮
  282. .address-picker-button {
  283. height: 100rpx;
  284. margin-bottom: 20rpx;
  285. box-shadow: 0 1rpx 1rpx #e4e4e4;
  286. background-color: #FFFFFF;
  287. }
  288. .address-picker-button-text {
  289. padding: 10rpx 25rpx;
  290. margin: 0 30rpx;
  291. }
  292. .row {
  293. /* #ifndef APP-PLUS-NVUE */
  294. display: flex;
  295. /* #endif */
  296. flex-direction: row;
  297. }
  298. .center {
  299. /* #ifndef APP-PLUS-NVUE */
  300. display: flex;
  301. /* #endif */
  302. justify-content: center;
  303. align-items: center;
  304. }
  305. .between-center {
  306. justify-content: space-between;
  307. align-items: center;
  308. }
  309. </style>