itmister-address-picker.nvue 7.5 KB

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