itmister-address-picker.nvue 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. arrays = {
  83. city:[{area:[],name:''}],
  84. name:'全国'
  85. }
  86. this.cityData.splice(0,0,arrays)
  87. }
  88. this.loadProvince(this.cityData);
  89. },
  90. methods: {
  91. // 确认选中
  92. confirmChange(val){
  93. let address = {
  94. // 当前选中的地址
  95. province:this.provinceList[val[0]],
  96. city:this.cityList[val[1]],
  97. area:this.areaList[val[2]],
  98. // 当前地址的索引,保存起来,用于日后修改地址时,加载默认
  99. provinceIndex:val[0],
  100. cityIndex:val[1],
  101. areaIndex:val[2]
  102. }
  103. this.$emit('confirmChange',address);
  104. this.hide();
  105. },
  106. // 监听选中
  107. bindChange(e) {
  108. const val = e.detail.value;
  109. this.value = [val[0],val[1],val[2]];
  110. this.loadCity(this.cityData[val[0]].city);
  111. },
  112. // 加载数据
  113. loadProvince(data){
  114. let newList = [];
  115. data.forEach(item => {
  116. newList.push(item.name);
  117. });
  118. this.provinceList = newList;
  119. this.loadCity(this.cityData[this.value[0]].city);
  120. },
  121. loadCity(data){
  122. let newList = [];
  123. data.forEach(item => {
  124. newList.push(item.name);
  125. });
  126. this.cityList = newList;
  127. if((data.length - 1) >= this.value[1]){ // 解决特殊情况报错的问题
  128. this.loadArea(this.cityData[this.value[0]].city[this.value[1]].area);
  129. }else{
  130. this.loadArea(this.cityData[this.value[0]].city[0].area);
  131. }
  132. },
  133. loadArea(data){
  134. let newList = [];
  135. data.forEach(item => {
  136. newList.push(item);
  137. });
  138. this.areaList = newList;
  139. },
  140. // 显示
  141. show() {
  142. // 渲染视图
  143. this.isShow = true;
  144. // 等待视图渲染
  145. this.$nextTick(() => {
  146. setTimeout(() => {
  147. // #ifdef APP-PLUS-NVUE
  148. // APP 端获取动画元素
  149. let ele = this.$refs.addressPickerEl;
  150. // #endif
  151. // 执行 mask 透明度
  152. this.isOpen = true;
  153. this.opacity = 1;
  154. // #ifndef APP-PLUS-NVUE
  155. // 非APP 端 执行动画
  156. this.translatey = 0;
  157. // #endif
  158. // #ifdef APP-PLUS-NVUE
  159. // APP 端执行动画
  160. animation.transition(ele, {
  161. styles: {
  162. transform: 'translate(0,0)',
  163. transformOrigin: 'center center'
  164. },
  165. duration: 250, //ms
  166. timingFunction: 'ease',
  167. delay: 0 //ms
  168. });
  169. // #endif
  170. }, 10);
  171. });
  172. },
  173. hide() {
  174. // #ifdef APP-PLUS-NVUE
  175. // APP 端获取动画元素
  176. let ele = this.$refs.addressPickerEl;
  177. // #endif
  178. // 执行 mask 透明度
  179. this.isOpen = false;
  180. this.opacity = 0;
  181. // #ifndef APP-PLUS-NVUE
  182. // 非APP 端执行动画
  183. this.translatey = 500;
  184. setTimeout(() => {
  185. this.isShow = false;
  186. }, 200);
  187. // #endif
  188. // #ifdef APP-PLUS-NVUE
  189. // APP 端执行动画
  190. animation.transition(ele, {
  191. styles: {
  192. transform: 'translate(0,550)',
  193. transformOrigin: 'center center'
  194. },
  195. duration: 250, //ms
  196. timingFunction: 'ease',
  197. delay: 0 //ms
  198. },() => {
  199. this.isShow = false;
  200. });
  201. // #endif
  202. },
  203. // 阻止冒泡
  204. handleClick(event) {
  205. event.stopPropagation();
  206. }
  207. }
  208. }
  209. </script>
  210. <style lang="scss">
  211. .address-picker-mask {
  212. position: fixed;
  213. left: 0;
  214. right: 0;
  215. top: 0;
  216. bottom: 0;
  217. z-index: 999988;
  218. }
  219. .address-picker-container {
  220. position: fixed;
  221. left: 0;
  222. right: 0;
  223. bottom: 0;
  224. z-index: 999999;
  225. height: 640rpx;
  226. background-color: #FFFFFF;
  227. }
  228. .show-address-picker-mask {
  229. transition-property: opacity;
  230. transition-duration: 0.2s;
  231. transition-timing-function: ease;
  232. }
  233. .hide-address-picker-mask {
  234. transition-property: opacity;
  235. transition-duration: 0.2s;
  236. transition-timing-function: ease;
  237. }
  238. .show-address-picker {
  239. /* #ifndef APP-PLUS-NVUE */
  240. transition-property: transform;
  241. transition-duration: 0.2s;
  242. transition-timing-function: ease;
  243. /* #endif */
  244. /* #ifdef APP-PLUS-NVUE */
  245. transform: 'translate(0,550)',
  246. /* #endif */
  247. }
  248. .hide-address-picker {
  249. /* #ifndef APP-PLUS-NVUE */
  250. transition-property: transform;
  251. transition-duration: 0.2s;
  252. transition-timing-function: ease;
  253. /* #endif */
  254. /* #ifdef APP-PLUS-NVUE */
  255. transform: 'translate(0,550)',
  256. /* #endif */
  257. }
  258. // 内容
  259. .address-picker-box{
  260. width: 750rpx;
  261. height: 500rpx;
  262. padding: 0 20rpx;
  263. /* #ifdef H5 */
  264. box-sizing: border-box;
  265. /* #endif */
  266. background-color: #FFF;
  267. }
  268. .address-picker-item{
  269. height: 100rpx;
  270. }
  271. .address-picker-item-text{
  272. color: #444;
  273. font-size: 32rpx;
  274. }
  275. // 按钮
  276. .address-picker-button{
  277. height: 100rpx;
  278. margin-bottom: 20rpx;
  279. box-shadow: 0 1rpx 1rpx #e4e4e4;
  280. background-color: #FFFFFF;
  281. }
  282. .address-picker-button-text{
  283. padding: 10rpx 25rpx;
  284. margin: 0 30rpx;
  285. }
  286. .row{
  287. /* #ifndef APP-PLUS-NVUE */
  288. display: flex;
  289. /* #endif */
  290. flex-direction: row;
  291. }
  292. .center{
  293. /* #ifndef APP-PLUS-NVUE */
  294. display: flex;
  295. /* #endif */
  296. justify-content: center;
  297. align-items: center;
  298. }
  299. .between-center{
  300. justify-content: space-between;
  301. align-items: center;
  302. }
  303. </style>