addressManage.vue 10 KB

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