addressManage.vue 12 KB

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