addEditAddress.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. <template>
  2. <view class="container">
  3. <navbar :config="config" backColor="#666666"></navbar>
  4. <form @submit="formSubmit">
  5. <view class="u-bg-fff">
  6. <view class="u-plr30">
  7. <view class="u-flex-center border u-border-one-one ht104">
  8. <view class="wt140 u-font28 u-1A1A1A">收货人:</view>
  9. <input class="u-ml10" maxlength="8" name="name" :value="dataform.name"
  10. placeholder-style="color: #d9d9d9;font-weight: 400;font-size: 26rpx; "
  11. placeholder="请填写收货人姓名" />
  12. </view>
  13. <view class="u-flex-center border u-border-one-one ht104">
  14. <view class="wt140 u-font28 u-1A1A1A">手机号码:</view>
  15. <input class="u-ml10" type="number" name="phone" maxlength="11" :value="dataform.phone"
  16. placeholder-style="color: #d9d9d9;font-weight: 400;font-size: 26rpx; "
  17. placeholder="请输入手机号码" />
  18. </view>
  19. <!-- <picker class="pickerList" mode="multiSelector" :range="newProvinceDataList" range-key="label"
  20. :value="multiIndex" @change="pickerChange" @columnchange="pickerColumnchange">
  21. <view class="u-flex-center border u-border-one-one ht104">
  22. <view class="wt140 u-font28 u-1A1A1A">所在地区:</view>
  23. <input class="u-ml10" placeholder-style="color: #d9d9d9;font-weight: 400;font-size: 26rpx; "
  24. placeholder="请选择地区" disabled
  25. :value="dataform.province_name + '' + dataform.city_name + '' + dataform.area_name" />
  26. <view class="iconfont u-ml25">&#xe6c7;</view>
  27. </view>
  28. </picker> -->
  29. <view class="u-flex-center border u-border-one-one ht104" @click="areaShow=true">
  30. <view class="wt140 u-font28 u-1A1A1A">所在地区:</view>
  31. <input class="u-ml10" placeholder-style="color: #d9d9d9;font-weight: 400;font-size: 26rpx; "
  32. placeholder="请选择地区" disabled
  33. :value="dataform.province_name + '' + dataform.city_name + '' + dataform.area_name" />
  34. <view class="iconfont u-ml25">&#xe6c7;</view>
  35. </view>
  36. <!-- <picker class="pickerList" :disabled="!dataform.area_name" @click="streetClick" :range="streetList" range-key="label" :value="streetListIndex"
  37. @change="picker2Change">
  38. <view class="u-flex-center border u-border-one-one ht104">
  39. <view class="wt140 u-font28 u-1A1A1A">所在街道:</view>
  40. <input class="u-ml10" placeholder-style="color: #d9d9d9;font-weight: 400;font-size: 26rpx; "
  41. placeholder="请选择街道" disabled :value="dataform.street_name" />
  42. <view class="iconfont u-ml25">&#xe6c7;</view>
  43. </view>
  44. </picker> -->
  45. <!-- <picker class="pickerList" :disabled="!dataform.street_name" @click="communityClick" :range="communityList" range-key="label" :value="communityListIndex"
  46. @change="picker3Change">
  47. <view class="u-flex-center border u-border-one-one ht104">
  48. <view class="wt140 u-font28 u-1A1A1A">所在社区:</view>
  49. <input class="u-ml10" placeholder-style="color: #d9d9d9;font-weight: 400;font-size: 26rpx; "
  50. placeholder="请选择社区" disabled :value="dataform.community_name" />
  51. <view class="iconfont u-ml25">&#xe6c7;</view>
  52. </view>
  53. </picker> -->
  54. <view class="u-flex-center ht132" style="align-items: center;">
  55. <view class="wt140 u-font28 u-1A1A1A" style="flex-shrink:0">详细地址:</view>
  56. <textarea class="u-ml10" style="height: 90rpx;width: 70%;margin-top: 36rpx;" maxlength="50"
  57. name="address" placeholder-style="color: #d9d9d9;font-weight: 400;font-size: 26rpx; "
  58. v-model="dataform.address" placeholder="请输入详细地址信息,如道路、门牌号、小区、楼栋号、单元室等"></textarea>
  59. <view class="getBtn" @click="getLocation()">获取地址</view>
  60. </view>
  61. </view>
  62. </view>
  63. <view class="default border u-mt20 u-bg-fff u-flex-center-sb">
  64. <view>设为默认收货地址</view>
  65. <view>
  66. <switch @change="isDefault" color="#FA6138" :checked="dataform.is_default"
  67. style="transform:scale(0.7)"></switch>
  68. </view>
  69. </view>
  70. <view class="u-plr30 mt104">
  71. <!-- <button class="u-btn-two u-bg-00321E u-FFF" @click="getWxAddress">一键获取微信地址</button> -->
  72. <button class="u-btn-two" style="color: #ffffff; background-color: #FA6138;"
  73. form-type="submit">保存</button>
  74. </view>
  75. </form>
  76. <!-- 地区 -->
  77. <u-select v-model="areaShow" mode="mutil-column-auto" :list="newProvinceDataList" @confirm="areaConfirm"></u-select>
  78. </view>
  79. </template>
  80. <script>
  81. import {
  82. myUserInfo
  83. } from "@/api/government.js"
  84. export default {
  85. data() {
  86. return {
  87. config: {
  88. back: true, //false是tolbar页面 是则不写
  89. title: '添加收货地址',
  90. color: '#1A1A1A',
  91. //背景颜色;参数一:透明度(0-1);参数二:背景颜色(array则为线性渐变,string为单色背景)
  92. backgroundColor: [1, '#fff'],
  93. statusBarFontColor: '#1A1A1A'
  94. },
  95. oldpProvinceDataList: [],
  96. newProvinceDataList: [
  97. // [],
  98. // [],
  99. // []
  100. ], // 省市区
  101. multiIndex: [0, 0, 0],
  102. streetList: [], // 街道
  103. streetListIndex: [0],
  104. communityList: [], // 社区
  105. communityListIndex: [0],
  106. dataform: {
  107. name: '', //收货人姓名
  108. phone: '', //联系电话
  109. province_name: '', //省的名称
  110. province_code: '', //省编号
  111. city_code: '', //市编号
  112. city_name: '', //市名称
  113. area_name: '', //区名称
  114. area_code: '', //区编号
  115. street_code: '', //街道编号
  116. street_name: '', //街道名称
  117. community_code: '', //社区编号
  118. community_name: '', //社区名称
  119. address: '', //详细地址
  120. longitude: '', //微信经度
  121. latitude: '', //微信纬度
  122. is_default: false ,//是否是默认
  123. },
  124. areaShow:false,
  125. user:{}
  126. };
  127. },
  128. onLoad(options) {
  129. this.getLocation()
  130. //获取地址详情(id查询)
  131. if (options.id) {
  132. this.$http.get('/useraddress/info/' + options.id)
  133. .then(async res => {
  134. if (res.code == 200) {
  135. this.dataform = res.data;
  136. }
  137. this.getAreaList2()
  138. })
  139. } else {
  140. this.getUserData();
  141. this.getAreaList2();
  142. }
  143. },
  144. methods: {
  145. getUserData() {
  146. myUserInfo().then(res => {
  147. this.user = res.data;
  148. this.dataform.name=this.user.realName;
  149. this.dataform.phone=this.user.mobile;
  150. })
  151. },
  152. getLocation(){
  153. uni.getLocation({
  154. type: 'gcj02',
  155. geocode:true,
  156. success:(res)=>{
  157. console.log(res);
  158. this.dataform.province_name=res.address.province;
  159. this.dataform.city_name=res.address.city;
  160. this.dataform.area_name=res.address.district;
  161. // this.$set(this.dataform,'address',res.address.street+res.address.streetNum+res.address.poiName)
  162. this.$nextTick(()=>{
  163. this.$set(this.dataform,'address',res.address.street+res.address.streetNum+res.address.poiName)
  164. this.dataform.address=res.address.street+res.address.streetNum+res.address.poiName
  165. })
  166. },
  167. fail:(e)=>{
  168. console.log(e)
  169. }
  170. });
  171. },
  172. streetClick(){
  173. if(!this.dataform.area_name){
  174. uni.showToast({
  175. icon:"none",
  176. title: '请先选择地区',
  177. duration: 2000
  178. });
  179. return false
  180. }
  181. },
  182. communityClick(){
  183. if(!this.dataform.street_name){
  184. uni.showToast({
  185. icon:"none",
  186. title: '请先选择街道',
  187. duration: 2000
  188. });
  189. return false
  190. }
  191. },
  192. // 获取省市区
  193. getAreaList2() {
  194. let that = this;
  195. let req = this.$http.get('/cnarea/tree-lbs', {});
  196. req.then(res => {
  197. let provincesList = res.tree.children;
  198. let multiIndex = [0, 0, 0];
  199. // for (let i = 0; i < provincesList.length; i++) {
  200. // this.newProvinceDataList[0].push(provincesList[i]);
  201. // if (provincesList[i].value == this.dataform.province_code) {
  202. // multiIndex[0] = i;
  203. // }
  204. // for (let c = 0; c < provincesList[i].children.length; c++) {
  205. // this.newProvinceDataList[1].push(provincesList[i].children[c]);
  206. // if (provincesList[i].children[c].value == this.dataform.city_code) {
  207. // multiIndex[1] = c;
  208. // }
  209. // if (provincesList[i].children[c].children) {
  210. // for (let a = 0; a < provincesList[i].children[c].children.length; a++) {
  211. // this.newProvinceDataList[2].push(provincesList[i].children[c].children[a]);
  212. // if (provincesList[i].children[c].children[a].value == this.dataform
  213. // .area_code) {
  214. // multiIndex[2] = a;
  215. // }
  216. // }
  217. // } else {
  218. // this.newProvinceDataList[2] = [];
  219. // }
  220. // }
  221. // }
  222. this.newProvinceDataList=provincesList
  223. console.log(this.newProvinceDataList)
  224. that.oldpProvinceDataList = provincesList;
  225. that.multiIndex = multiIndex;
  226. // that.getAreaList(that.newProvinceDataList[2][that.multiIndex[2]].value, 'street');
  227. })
  228. return req;
  229. },
  230. // 获取地区
  231. areaConfirm(e){
  232. if(e.length>0){
  233. this.dataform.province_name=e[0].label;
  234. this.dataform.province_code=e[0].value;
  235. this.dataform.city_name=e[1].label;
  236. this.dataform.city_code=e[1].value;
  237. this.dataform.area_name=e[2].label;
  238. this.dataform.area_code=e[2].value;
  239. this.getAreaList(this.dataform.area_code, 'street');
  240. this.dataform.street_name="";
  241. this.dataform.street_code="";
  242. this.dataform.community_name="";
  243. this.dataform.community_code=""
  244. }
  245. },
  246. formSubmit: function(e) {
  247. let that = this;
  248. let target = e.detail.value;
  249. if (!target.name) {
  250. this.$mUtil.toast('请输入收货人姓名');
  251. return false;
  252. }
  253. if (!target.phone) {
  254. this.$mUtil.toast('请输入联系电话');
  255. return false;
  256. }
  257. if (!target.phone.match(this.$mConfig.telRegex)) {
  258. this.$mUtil.toast('请输入正确手机号');
  259. return false;
  260. }
  261. if (!that.dataform.province_name) {
  262. this.$mUtil.toast('请选择省市区');
  263. return false;
  264. }
  265. // if (!that.dataform.street_name) {
  266. // this.$mUtil.toast('请选择街道');
  267. // return false;
  268. // }
  269. // if (!that.dataform.community_name) {
  270. // this.$mUtil.toast('请选择社区');
  271. // return false;
  272. // }
  273. if (!target.address) {
  274. this.$mUtil.toast('请输入详细地址');
  275. return false;
  276. }
  277. this.dataform.name = target.name;
  278. this.dataform.phone = target.phone;
  279. this.dataform.address = target.address;
  280. if (this.dataform.id) {
  281. this.$http.put('/useraddress/update', this.dataform).then(async res => {
  282. if (res && res.code == 200) {
  283. uni.showToast({
  284. title: '修改成功',
  285. icon: 'none',
  286. duration: 3000,
  287. success() {
  288. // var pages = getCurrentPages();
  289. // var prevPage = pages[pages.length - 2];
  290. // prevPage.$vm.listAddress(1)
  291. setTimeout(() => {
  292. uni.navigateBack({
  293. delta: 1
  294. });
  295. },1000)
  296. }
  297. });
  298. }
  299. });
  300. } else {
  301. this.$http.post('/useraddress/save', this.dataform).then(res => {
  302. if (res && res.code == 200) {
  303. uni.showToast({
  304. title: '添加成功',
  305. icon: 'none',
  306. duration: 3000,
  307. success() {
  308. var pages = getCurrentPages();
  309. var prevPage = pages[pages.length - 2];
  310. prevPage.$vm.listAddress(1);
  311. uni.navigateBack({
  312. delta: 1
  313. });
  314. }
  315. });
  316. }
  317. });
  318. }
  319. },
  320. //是否设置默认地址
  321. isDefault: function(e) {
  322. this.dataform.is_default = e.target.value;
  323. },
  324. //获取微信地址
  325. getWxAddress() {
  326. let that = this;
  327. uni.getSetting({
  328. success(res) {
  329. if (res.authSetting['scope.address']) {
  330. uni.chooseAddress({
  331. success(res) {
  332. that.dataform = {
  333. name: res.userName, //收货人姓名
  334. phone: res.telNumber, //联系电话
  335. province_name: res.provinceName, //省的名称
  336. province_code: '', //省编号
  337. city_code: '', //市编号
  338. city_name: res.cityName, //市名称
  339. area_name: res.countyName, //区名称
  340. area_code: '', //区编号
  341. address: res.detailInfo, //详细地址
  342. longitude: '', //微信经度
  343. latitude: '', //微信纬度
  344. is_default: false //是否是默认
  345. }
  346. }
  347. });
  348. } else {
  349. if (res.authSetting['scope.address'] == false) {
  350. uni.openSetting({
  351. success(res) {}
  352. });
  353. } else {
  354. uni.chooseAddress({
  355. success(res) {}
  356. });
  357. }
  358. }
  359. }
  360. });
  361. },
  362. // 省市区确认事件
  363. pickerChange(e) {
  364. this.multiIndex = e.detail.value;
  365. if (this.newProvinceDataList[2][this.multiIndex[2]] != undefined) {
  366. this.dataform.province_name = this.newProvinceDataList[0][this.multiIndex[0]].label;
  367. this.dataform.province_code = this.newProvinceDataList[0][this.multiIndex[0]].value;
  368. this.dataform.city_name = this.newProvinceDataList[1][this.multiIndex[1]].label;
  369. this.dataform.city_code = this.newProvinceDataList[1][this.multiIndex[1]].value;
  370. this.dataform.area_name = this.newProvinceDataList[2][this.multiIndex[2]].label;
  371. this.dataform.area_code = this.newProvinceDataList[2][this.multiIndex[2]].value;
  372. this.dataform.latitude = this.newProvinceDataList[2][this.multiIndex[2]].latitude;
  373. this.dataform.longitude = this.newProvinceDataList[2][this.multiIndex[2]].longitude;
  374. } else {
  375. this.dataform.province_name = this.newProvinceDataList[0][this.multiIndex[0]].label;
  376. this.dataform.province_code = this.newProvinceDataList[0][this.multiIndex[0]].value;
  377. this.dataform.city_name = this.newProvinceDataList[1][this.multiIndex[1]].label;
  378. this.dataform.city_code = this.newProvinceDataList[1][this.multiIndex[1]].value;
  379. this.dataform.latitude = this.newProvinceDataList[1][this.multiIndex[1]].latitude;
  380. this.dataform.longitude = this.newProvinceDataList[1][this.multiIndex[1]].longitude;
  381. this.dataform.area_name = '';
  382. this.dataform.area_code = '';
  383. }
  384. },
  385. pickerColumnchange(e) {
  386. // 第几列滑动
  387. // console.log(e.detail.column);
  388. // 第几列滑动的下标
  389. // console.log(e.detail.value)
  390. // 第一列滑动
  391. if (e.detail.column === 0) {
  392. this.multiIndex[0] = e.detail.value;
  393. this.newProvinceDataList[1] = this.oldpProvinceDataList[this.multiIndex[0]].children.map((item,
  394. index) => {
  395. return item;
  396. });
  397. //当三级存在
  398. if (this.oldpProvinceDataList[this.multiIndex[0]].children[0].children) {
  399. if (this.oldpProvinceDataList[this.multiIndex[0]].children[this.multiIndex[1]]) {
  400. this.newProvinceDataList[2] = this.oldpProvinceDataList[this.multiIndex[0]].children[this
  401. .multiIndex[1]].children.map((item, index) => {
  402. return item;
  403. });
  404. }
  405. } else {
  406. this.newProvinceDataList[2] = [];
  407. }
  408. // 第一列滑动 第二列 和第三列 都变为第一个
  409. this.multiIndex.splice(1, 1, 0);
  410. this.multiIndex.splice(2, 1, 0);
  411. }
  412. // 第二列滑动
  413. if (e.detail.column === 1) {
  414. this.multiIndex[1] = e.detail.value;
  415. if (this.oldpProvinceDataList[this.multiIndex[0]].children[this.multiIndex[1]].children) {
  416. this.newProvinceDataList[2] = this.oldpProvinceDataList[this.multiIndex[0]].children[this
  417. .multiIndex[1]].children.map((item, index) => {
  418. return item;
  419. });
  420. } else {
  421. this.newProvinceDataList[2] = [];
  422. }
  423. // 第二列 滑动 第三列 变成第一个
  424. this.multiIndex.splice(2, 1, 0);
  425. }
  426. // 第三列滑动
  427. if (e.detail.column === 2) {
  428. // console.log('第三列滑动')
  429. // console.log(this.multiIndex)
  430. if (this.multiIndex[2] != e.detail.value) {
  431. this.dataform.street_code = '';
  432. this.dataform.street_name = '';
  433. this.dataform.community_code = '';
  434. this.dataform.community_name = '';
  435. }
  436. this.multiIndex[2] = e.detail.value;
  437. this.getAreaList(this.newProvinceDataList[2][this.multiIndex[2]].value, 'street');
  438. }
  439. },
  440. /**
  441. * @param {Object} parentCode
  442. * @param {Object} type value: 'street' or 'community'
  443. *@description 根据上一级查询下一级。例如根据“区”查“街道”,根据“街道”查“社区”。10000是查询所有省
  444. */
  445. getAreaList(parentCode, type) {
  446. // this.$http.get(`/cnarea/lbs-children/${parentCode}`, {}).then(res => {
  447. // if (res && res.code == 200) {
  448. // if (type == 'street') {
  449. // this.streetList = res.list;
  450. // this.getAreaList(this.streetList[this.streetListIndex].value, 'community');
  451. // } else {
  452. // this.communityList = res.list;
  453. // }
  454. // }
  455. // });
  456. },
  457. picker2Change(e) {
  458. if (this.streetListIndex != e.detail.value) {
  459. this.dataform.community_code = '';
  460. this.dataform.community_name = '';
  461. }
  462. this.streetListIndex = [e.detail.value];
  463. this.getAreaList(this.streetList[this.streetListIndex].value, 'community');
  464. this.dataform.street_code = this.streetList[this.streetListIndex].value;
  465. this.dataform.street_name = this.streetList[this.streetListIndex].label;
  466. this.dataform.community_name="";
  467. this.dataform.community_code=""
  468. },
  469. picker3Change(e) {
  470. this.communityListIndex = [e.detail.value];
  471. this.dataform.community_code = this.communityList[this.communityListIndex].value;
  472. this.dataform.community_name = this.communityList[this.communityListIndex].label;
  473. }
  474. }
  475. };
  476. </script>
  477. <style lang="scss">
  478. page {
  479. background-color: #f5f5f5;
  480. }
  481. .border {
  482. border-bottom: 3rpx solid #e7e7e7;
  483. }
  484. .wt140 {
  485. width: 140rpx;
  486. }
  487. .ht104 {
  488. height: 104rpx;
  489. line-height: 104rpx;
  490. }
  491. .ht132 {
  492. height: 132rpx;
  493. // line-height: 132rpx;
  494. }
  495. .mt104 {
  496. margin-top: 104rpx;
  497. }
  498. input {
  499. width: 70%;
  500. }
  501. .default {
  502. padding: 38rpx 30rpx;
  503. }
  504. .getBtn{
  505. flex-shrink: 0;
  506. background: rgb(250, 97, 56);
  507. color: #fff;
  508. border-radius: 10rpx;
  509. padding:10rpx 20rpx;
  510. font-size: 24rpx;
  511. }
  512. </style>