addressManage.vue 10 KB

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