addressManage.vue 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <template>
  2. <view class="content">
  3. <view class="row b-b">
  4. <text class="tit">联系人</text>
  5. <input class="input" type="text" v-model="addressData.name" placeholder="收货人姓名" placeholder-class="placeholder" />
  6. </view>
  7. <view class="row b-b">
  8. <text class="tit">手机号</text>
  9. <input class="input" type="number" v-model="addressData.mobile" placeholder="收货人手机号码" placeholder-class="placeholder" />
  10. </view>
  11. <view class="row b-b">
  12. <text class="tit">省市区</text>
  13. <picker mode="multiSelector" :range="areaJs" range-key="name" :value="addressIndex" @columnchange="onCityClick" @change="changeArea">
  14. <view v-if="addressDetail">{{ addressDetail }}</view>
  15. <view v-else class="font-color-gray">请选择省市区</view>
  16. </picker>
  17. </view>
  18. <view class="row b-b">
  19. <text class="tit">村镇</text>
  20. <picker mode="multiSelector" :range="cityJs" range-key="name" :value="addressIndexCity" @columnchange="onCityChange" @change="changeCity">
  21. <view v-if="addressDetailCity">{{ addressDetailCity }}</view>
  22. <view v-else class="font-color-gray">请选择村镇</view>
  23. </picker>
  24. </view>
  25. <view class="row b-b">
  26. <text class="tit">门牌号</text>
  27. <input class="input" type="text" v-model="addressData.area" placeholder="楼号、门牌" placeholder-class="placeholder" />
  28. </view>
  29. <uni-list class="margin-t-20">
  30. <uni-list-item
  31. title="设为默认"
  32. :switch-checked="addressData.default"
  33. :show-switch="true"
  34. :show-arrow="false"
  35. switch-color="#5dbc7c"
  36. @switchChange="switchChange"
  37. ></uni-list-item>
  38. </uni-list>
  39. <button class="add-btn" @click="confirm">提交</button>
  40. </view>
  41. </template>
  42. <script>
  43. import uniList from '@/components/uni-list/uni-list.vue';
  44. import uniListItem from '@/components/uni-list-item/uni-list-item.vue';
  45. import uniPopup from '@/components/uni-popup/uni-popup.vue';
  46. import { addressEdit } from '@/api/user.js';
  47. import { getAddressArea, getAddressCity } from '@/api/set.js';
  48. import { mapState, mapMutations } from 'vuex';
  49. export default {
  50. components: {
  51. uniList,
  52. uniListItem,
  53. uniPopup
  54. },
  55. data() {
  56. return {
  57. addressIndex: [0, 0, 0], //当前选中的省市区
  58. addressIndexCity: [0, 0], //当前选中的村镇
  59. addressDetail: '', //省市区名称
  60. addressDetailCity: '', //村镇名称
  61. addressData: {
  62. name: '',
  63. mobile: '',
  64. address: {
  65. province: '',
  66. city: '',
  67. district: ''
  68. },
  69. area: '',
  70. default: false
  71. }
  72. };
  73. },
  74. computed: {
  75. ...mapState('address', ['area', 'city']),
  76. // 省市区动态渲染列表
  77. areaJs() {
  78. if (this.area.length > 0) {
  79. const index1 = this.addressIndex[0];
  80. return [this.area, this.area[index1].child, this.area[index1].child[this.addressIndex[1]].child];
  81. }
  82. },
  83. // 村镇切换渲染
  84. cityJs() {
  85. if (this.city.length > 0) {
  86. const index1 = this.addressIndexCity[0];
  87. return [this.city, this.city[index1].child];
  88. }
  89. }
  90. },
  91. onLoad(option) {
  92. let title = '新增收货地址';
  93. if (option.type === 'edit') {
  94. title = '编辑收货地址';
  95. let data = JSON.parse(option.data);
  96. console.log(data);
  97. this.addressData = {
  98. name: data.real_name,
  99. mobile: data.phone,
  100. address: {
  101. province: data.province,
  102. city: data.city,
  103. district: data.district
  104. },
  105. area: data.detail,
  106. default: data.is_default == 1,
  107. id: data.id
  108. };
  109. this.addressDetail = data.province + data.city + data.district;
  110. }
  111. this.manageType = option.type;
  112. uni.setNavigationBarTitle({
  113. title
  114. });
  115. this.init();
  116. },
  117. methods: {
  118. ...mapMutations('address', ['setArea', 'setCity']),
  119. // 初始化
  120. init() {
  121. if (this.area.length <= 0) {
  122. // 获取省市区信息
  123. this.getAddressArea();
  124. }
  125. },
  126. // 获取省市区信息
  127. getAddressArea() {
  128. getAddressArea().then(e => {
  129. this.setArea(e.data);
  130. this.getAddressCity(e.data[0].child[0].child[0].city_id);
  131. });
  132. },
  133. // 获取村镇信息
  134. getAddressCity(id) {
  135. uni.showLoading({
  136. title: '村镇数据加载中....',
  137. mask: true
  138. });
  139. getAddressCity({}, id)
  140. .then(e => {
  141. uni.hideLoading();
  142. this.setCity(e.data);
  143. })
  144. .catch(e => {
  145. uni.hideLoading();
  146. uni.showToast({
  147. title: ''
  148. });
  149. });
  150. },
  151. //省市区确认后村镇数据更新
  152. changeArea(e) {
  153. if (this.area.length > 0) {
  154. const index0 = this.addressIndex[0];
  155. const index1 = this.addressIndex[1];
  156. const index2 = this.addressIndex[2];
  157. this.addressDetail = this.areaJs[0][index0].name + this.areaJs[1][index1].name + this.areaJs[2][index2].name;
  158. this.getAddressCity(this.areaJs[2][index2].city_id);
  159. } else {
  160. this.addressDetail = '';
  161. }
  162. },
  163. // 村镇切换
  164. changeCity(e) {
  165. if (this.area.length > 0) {
  166. const index0 = this.addressIndexCity[0];
  167. const index1 = this.addressIndexCity[1];
  168. this.addressDetailCity = this.cityJs[0][index0].name + this.cityJs[1][index1].name;
  169. } else {
  170. this.addressDetailCity = '';
  171. }
  172. },
  173. // 选中省市区切换
  174. onCityClick(data) {
  175. // 采用map防止直接修改无法触发数组set事件
  176. this.addressIndex = this.addressIndex.map((e, ind) => {
  177. if (data.detail.column < ind) {
  178. e = 0;
  179. }
  180. if (ind == data.detail.column) {
  181. e = data.detail.value;
  182. }
  183. return e;
  184. });
  185. },
  186. // 选中村镇切换
  187. onCityChange(data) {
  188. // 采用map防止直接修改无法触发数组set事件
  189. this.addressIndexCity = this.addressIndexCity.map((e, ind) => {
  190. if (data.detail.column < ind) {
  191. e = 0;
  192. }
  193. if (ind == data.detail.column) {
  194. e = data.detail.value;
  195. }
  196. return e;
  197. });
  198. },
  199. //地图选择地址
  200. chooseLocation() {
  201. uni.chooseLocation({
  202. success: data => {
  203. console.log(data);
  204. this.addressData.addressName = data.name;
  205. this.addressData.address = data.name;
  206. }
  207. });
  208. },
  209. // 设置是否为默认地址
  210. switchChange(e) {
  211. this.addressData.default = e.value;
  212. },
  213. //提交
  214. confirm() {
  215. let obj = this;
  216. let data = this.addressData;
  217. if (!data.name) {
  218. this.$api.msg('请填写收货人姓名');
  219. return;
  220. }
  221. if (!/(^1[3|4|5|7|8][0-9]{9}$)/.test(data.mobile)) {
  222. this.$api.msg('请输入正确的手机号码');
  223. return;
  224. }
  225. if (!data.address) {
  226. this.$api.msg('请在地图选择所在位置');
  227. return;
  228. }
  229. if (!data.area) {
  230. this.$api.msg('请填写门牌号信息');
  231. return;
  232. }
  233. //this.$api.prePage()获取上一页实例,可直接调用上页所有数据和方法,在App.vue定义
  234. addressEdit({
  235. real_name: data.name,
  236. phone: data.mobile,
  237. address: {
  238. province: data.address.province,
  239. city: data.address.city,
  240. district: data.address.district
  241. },
  242. detail: data.area,
  243. is_default: data.default,
  244. id: data.id || '',
  245. type: 1
  246. }).then(function(e) {
  247. obj.$api.prePage().refreshList();
  248. uni.showToast({
  249. title: '提交成功',
  250. duration: 2000
  251. });
  252. setTimeout(function() {
  253. uni.navigateBack();
  254. }, 800);
  255. });
  256. }
  257. }
  258. };
  259. </script>
  260. <style lang="scss">
  261. page {
  262. background: $page-color-base;
  263. padding-top: 16rpx;
  264. }
  265. .row {
  266. display: flex;
  267. align-items: center;
  268. position: relative;
  269. padding: 0 30rpx;
  270. height: 110rpx;
  271. background: #fff;
  272. .tit {
  273. flex-shrink: 0;
  274. width: 120rpx;
  275. font-size: 30rpx;
  276. color: $font-color-dark;
  277. }
  278. .input {
  279. flex: 1;
  280. font-size: 30rpx;
  281. color: $font-color-dark;
  282. }
  283. .iconlocation {
  284. font-size: 36rpx;
  285. color: $font-color-light;
  286. }
  287. }
  288. .default-row {
  289. margin-top: 16rpx;
  290. .tit {
  291. flex: 1;
  292. }
  293. switch {
  294. transform: translateX(16rpx) scale(0.9);
  295. }
  296. }
  297. .add-btn {
  298. display: flex;
  299. align-items: center;
  300. justify-content: center;
  301. width: 690rpx;
  302. height: 80rpx;
  303. margin: 60rpx auto;
  304. font-size: $font-lg;
  305. color: #fff;
  306. background-color: $base-color;
  307. border-radius: 10rpx;
  308. // box-shadow: 1px 2px 5px rgba(219, 63, 96, 0.4);
  309. }
  310. .alert-box {
  311. background-color: #ffffff;
  312. }
  313. </style>