addressManage.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. <!-- range是你要显示的数组,range-key是数组中你要显示的那一项,range-key="{{'label'}}" ,range-key中的数据要加引号!!!!!才生效! -->
  14. <picker mode="multiSelector" :range="areaJs" range-key="name" :value="addressIndex" @columnchange="onCityClick" @change="changeArea">
  15. <view v-if="addressDetail">{{ addressDetail }}</view>
  16. <view v-else class="font-color-gray">请选择省市区</view>
  17. </picker>
  18. </view>
  19. <view class="row b-b" v-if="showl">
  20. <text class="tit">村镇</text>
  21. <picker mode="multiSelector" :range="cityJs" range-key="name" :value="addressIndexCity" @columnchange="onCityChange" @change="changeCity">
  22. <view v-if="addressDetailCity">{{ addressDetailCity }}</view>
  23. <view v-else class="font-color-gray">请选择村镇</view>
  24. </picker>
  25. </view>
  26. <view class="row b-b">
  27. <text class="tit">门牌号</text>
  28. <input class="input" type="text" v-model="addressData.area" placeholder="楼号、门牌" placeholder-class="placeholder" />
  29. </view>
  30. <uni-list class="margin-t-20">
  31. <uni-list-item
  32. title="设为默认"
  33. :switch-checked="addressData.default"
  34. :show-switch="true"
  35. :show-arrow="false"
  36. switch-color="#ff4c4c"
  37. @switchChange="switchChange"
  38. ></uni-list-item>
  39. </uni-list>
  40. <button class="add-btn" @click="confirm">提交</button>
  41. </view>
  42. </template>
  43. <script>
  44. import uniList from '@/components/uni-list/uni-list.vue';
  45. import uniListItem from '@/components/uni-list-item/uni-list-item.vue';
  46. import uniPopup from '@/components/uni-popup/uni-popup.vue';
  47. import { addressEdit } from '@/api/user.js';
  48. import { getAddressArea, getAddressCity, getAddressDetail } from '@/api/set.js';
  49. import { mapState, mapMutations } from 'vuex';
  50. export default {
  51. components: {
  52. uniList,
  53. uniListItem,
  54. uniPopup
  55. },
  56. data() {
  57. return {
  58. showl: false,//设置是否隐藏村镇选择
  59. addressIndex: [0, 0, 0], //当前选中的省市区
  60. addressIndexCity: [0, 0], //当前选中的村镇
  61. addressDetail: '', //省市区名称
  62. addressDetailCity: '', //村镇名称
  63. addressData: {
  64. name: '',
  65. mobile: '',
  66. area: '',
  67. default: false,
  68. id: ''
  69. },
  70. type: 'add', //判断当前加载类型
  71. loadCityp: false //判断是否已经初次加载过数据
  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. console.log(this.addressIndex, this.area[index1], this.area[index1].child[this.addressIndex[1]]);
  81. return [this.area, this.area[index1].child, this.area[index1].child[this.addressIndex[1]].child];
  82. }
  83. },
  84. // 村镇切换渲染
  85. cityJs() {
  86. if (this.city.length > 0) {
  87. const index1 = this.addressIndexCity[0];
  88. return [this.city, this.city[index1].child];
  89. }
  90. }
  91. },
  92. onLoad(option) {
  93. let title = '新增收货地址';
  94. this.type = option.type;
  95. if (this.type === 'edit') {
  96. title = '编辑收货地址';
  97. this.getAddressDetail(option.id);
  98. // this.addressDetail = data.province + data.city + data.district;
  99. }
  100. if (this.type === 'add') {
  101. console.log('');
  102. this.loadCityp = true;
  103. this.init();
  104. }
  105. this.manageType = option.type;
  106. uni.setNavigationBarTitle({
  107. title
  108. });
  109. },
  110. methods: {
  111. ...mapMutations('address', ['setArea', 'setCity']),
  112. // 初始化
  113. init() {
  114. if (this.area.length <= 0) {
  115. // 获取省市区信息
  116. this.getAddressArea();
  117. } else {
  118. if (this.type == 'edit' && !this.loadCityp) {
  119. this.changeArea();
  120. }
  121. }
  122. },
  123. // 获取地址详情
  124. getAddressDetail(id) {
  125. getAddressDetail({}, id)
  126. .then(e => {
  127. let address = this.addressData;
  128. let data = e.data;
  129. address.name = data.real_name; //保存用户姓名
  130. address.mobile = data.phone; //保存用户手机号
  131. address.area = data.detail; //保存用户详细地址
  132. address.default = data.is_default == 1 ? true : false; //保存是否默认地址
  133. address.id = data.id;
  134. // 开始加载村镇信息
  135. let arr = e.data.address_arr.split(',');
  136. let ar1 = [];
  137. let ar2 = [];
  138. for (var i = 0; i < 5; i++) {
  139. if (i < 3) {
  140. ar1.push(arr[i]);
  141. }
  142. if (i >= 3) {
  143. // 判断是否有值
  144. if (arr[i]) {
  145. ar2.push(arr[i]);
  146. } else {
  147. ar2.push(0);
  148. }
  149. }
  150. }
  151. this.addressIndex = ar1;
  152. this.addressIndexCity = ar2;
  153. // 开始初始化
  154. this.init();
  155. })
  156. .catch(e => {
  157. console.log(e);
  158. });
  159. },
  160. // 获取省市区信息
  161. getAddressArea() {
  162. getAddressArea().then(e => {
  163. this.setArea(e.data);
  164. this.getAddressCity(e.data[0].child[0].child[0].city_id);
  165. // 判断是否为修改
  166. if (this.type == 'edit' && !this.loadCityp) {
  167. console.log('初始化数据');
  168. this.changeArea();
  169. }
  170. });
  171. },
  172. // 获取村镇信息
  173. getAddressCity(id) {
  174. uni.showLoading({
  175. title: '村镇数据加载中....',
  176. mask: true
  177. });
  178. getAddressCity({}, id)
  179. .then(e => {
  180. uni.hideLoading();
  181. this.setCity(e.data);
  182. if(e.data.length > 0) {
  183. this.showl = true
  184. }else {
  185. this.showl = false
  186. }
  187. if (!this.loadCityp) {
  188. this.loadCityp = true;
  189. this.changeCity();
  190. }
  191. })
  192. .catch(e => {
  193. console.log(e);
  194. uni.hideLoading();
  195. });
  196. },
  197. //省市区确认后村镇数据更新
  198. changeArea() {
  199. if (this.area.length > 0) {
  200. const index0 = this.addressIndex[0];
  201. const index1 = this.addressIndex[1];
  202. const index2 = this.addressIndex[2];
  203. this.addressDetail = this.areaJs[0][index0].name + this.areaJs[1][index1].name + this.areaJs[2][index2].name;
  204. // 判断是否已经加载过修改
  205. if (this.loadCityp) {
  206. // 初始化选中的村镇
  207. this.addressIndexCity = [0, 0];
  208. this.addressDetailCity = '';
  209. }
  210. this.getAddressCity(this.areaJs[2][index2].city_id);
  211. } else {
  212. this.addressDetail = '';
  213. }
  214. },
  215. // 村镇切换
  216. changeCity(e) {
  217. if (this.city.length > 0) {
  218. const index0 = this.addressIndexCity[0];
  219. const index1 = this.addressIndexCity[1];
  220. this.addressDetailCity = this.cityJs[0][index0].name + this.cityJs[1][index1].name;
  221. } else {
  222. this.addressDetailCity = '';
  223. }
  224. },
  225. // 选中省市区切换
  226. onCityClick(data) {
  227. // 采用map防止直接修改无法触发数组set事件
  228. this.addressIndex = this.addressIndex.map((e, ind) => {
  229. if (data.detail.column < ind) {
  230. e = 0;
  231. }
  232. if (ind == data.detail.column) {
  233. e = data.detail.value;
  234. }
  235. return e;
  236. });
  237. },
  238. // 选中村镇切换
  239. onCityChange(data) {
  240. // 采用map防止直接修改无法触发数组set事件
  241. this.addressIndexCity = this.addressIndexCity.map((e, ind) => {
  242. if (data.detail.column < ind) {
  243. e = 0;
  244. }
  245. if (ind == data.detail.column) {
  246. e = data.detail.value;
  247. }
  248. return e;
  249. });
  250. },
  251. //地图选择地址
  252. chooseLocation() {
  253. uni.chooseLocation({
  254. success: data => {
  255. this.addressData.addressName = data.name;
  256. this.addressData.address = data.name;
  257. }
  258. });
  259. },
  260. // 设置是否为默认地址
  261. switchChange(e) {
  262. this.addressData.default = e.value;
  263. },
  264. //提交
  265. confirm() {
  266. let obj = this;
  267. let data = this.addressData;
  268. if (!data.name) {
  269. this.$api.msg('请填写收货人姓名');
  270. return;
  271. }
  272. if (!/(^1[3|4|5|7|8][0-9]{9}$)/.test(data.mobile)) {
  273. this.$api.msg('请输入正确的手机号码');
  274. return;
  275. }
  276. if (!data.area) {
  277. this.$api.msg('请填写门牌号信息');
  278. return;
  279. }
  280. if (!this.addressDetail) {
  281. this.$api.msg('请选择省市区');
  282. return;
  283. }
  284. // 数组
  285. let address = [];
  286. let arrObj = [];
  287. // 保存省市区信息
  288. this.areaJs.forEach((e, ind) => {
  289. arrObj.push(this.addressIndex[ind]);
  290. address.push({
  291. city_id: e[this.addressIndex[ind]].city_id,
  292. name: e[this.addressIndex[ind]].name
  293. });
  294. });
  295. // 保存村镇信息
  296. if (this.cityJs && this.cityJs.length > 0) {
  297. this.cityJs.forEach((e, ind) => {
  298. arrObj.push(this.addressIndexCity[ind]);
  299. address.push({
  300. city_id: e[this.addressIndexCity[ind]].city_id,
  301. name: e[this.addressIndexCity[ind]].name
  302. });
  303. });
  304. }
  305. console.log(address);
  306. uni.showLoading({
  307. title: '提交中....',
  308. mask: true
  309. });
  310. //this.$api.prePage()获取上一页实例,可直接调用上页所有数据和方法,在App.vue定义
  311. addressEdit({
  312. real_name: data.name,
  313. phone: data.mobile,
  314. address: {
  315. province: address[0].name,
  316. city: address[1].name,
  317. district: address[2].name,
  318. street: address[3] ? address[3].name : '', //街道
  319. village: address[4] ? address[4].name : '', //村
  320. city_id: address[address.length - 1].city_id //保存id
  321. },
  322. address_arr: arrObj,
  323. detail: data.area,
  324. is_default: data.default,
  325. id: data.id || '',
  326. type: 0
  327. })
  328. .then(function(e) {
  329. uni.hideLoading();
  330. uni.showToast({
  331. title: '提交成功',
  332. duration: 2000
  333. });
  334. obj.$api.prePage().refreshList();
  335. setTimeout(function() {
  336. uni.navigateBack();
  337. }, 800);
  338. })
  339. .catch(e => {
  340. uni.hideLoading();
  341. });
  342. }
  343. }
  344. };
  345. </script>
  346. <style lang="scss">
  347. page {
  348. background: $page-color-base;
  349. padding-top: 16rpx;
  350. }
  351. .row {
  352. display: flex;
  353. align-items: center;
  354. position: relative;
  355. padding: 0 30rpx;
  356. height: 110rpx;
  357. background: #fff;
  358. .tit {
  359. flex-shrink: 0;
  360. width: 120rpx;
  361. font-size: 30rpx;
  362. color: $font-color-dark;
  363. }
  364. .input {
  365. flex: 1;
  366. font-size: 30rpx;
  367. color: $font-color-dark;
  368. }
  369. .iconlocation {
  370. font-size: 36rpx;
  371. color: $font-color-light;
  372. }
  373. }
  374. .default-row {
  375. margin-top: 16rpx;
  376. .tit {
  377. flex: 1;
  378. }
  379. switch {
  380. transform: translateX(16rpx) scale(0.9);
  381. }
  382. }
  383. .add-btn {
  384. display: flex;
  385. align-items: center;
  386. justify-content: center;
  387. width: 690rpx;
  388. height: 80rpx;
  389. margin: 60rpx auto;
  390. font-size: $font-lg;
  391. color: #fff;
  392. background-color: $base-color;
  393. border-radius: 10rpx;
  394. // box-shadow: 1px 2px 5px rgba(219, 63, 96, 0.4);
  395. }
  396. .alert-box {
  397. background-color: #ffffff;
  398. }
  399. </style>