itmister-address-picker.nvue 7.9 KB

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