address-edit.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. <template>
  2. <app-layout>
  3. <view class="app-view">
  4. <!-- 自动识别功能 -->
  5. <view v-if="form.type === 0 || is_refund_address > 0"
  6. class="automatic dir-left-nowrap cross-center main-between">
  7. <input type="text" v-model="automatic" placeholder-class="place-automatic"
  8. placeholder="复制收货信息(姓名前后请输入空格)" @confirm="getAddress">
  9. <button @click="getAddress" :class="getTheme + '-m-text ' + getTheme">自动识别</button>
  10. </view>
  11. <view class="app-line app-border main-between cross-center">
  12. <text class="app-text box-grow-0">收货人</text>
  13. <input class="address-input" type="text" v-model="form.name"/>
  14. </view>
  15. <view class="app-line app-border main-between cross-center">
  16. <text class="app-text box-grow-0">联系电话</text>
  17. <input class="address-input" type="number" v-model="form.mobile"/>
  18. </view>
  19. <view v-if="form.type === 0 || is_refund_address > 0"
  20. class="app-line app-border main-between cross-center">
  21. <text class="app-text box-grow-0">所在地区</text>
  22. <view class="box-grow-1 area-left">
  23. <app-area-picker v-if="tt_area_show"
  24. @customevent="areaEvent"
  25. :ids="[form.province_id,form.city_id,form.district_id]"></app-area-picker>
  26. </view>
  27. </view>
  28. <!-- #ifndef MP-TOUTIAO -->
  29. <view v-if="form.type === 1 && is_refund_address == 0"
  30. class="app-line app-border main-between cross-center">
  31. <text class="app-text box-grow-0">定位地址</text>
  32. <view class="h-100 main-right dir-left-nowrap cross-center box-grow-1" @click="getChooseLocation">
  33. <view style="line-height: 1.5;text-align: right" class="t-omit-two c-address">{{form.location}}</view>
  34. <icon class="box-grow-0 icon-arrow-image"></icon>
  35. </view>
  36. </view>
  37. <!-- #endif -->
  38. <view v-if="form.type === 0 || is_refund_address > 0" class="app-line main-between cross-center">
  39. <text class="app-text box-grow-0">详细地址</text>
  40. <input class="address-input" type="text" :focus="focus" @blur="focus = false" v-model="form.detail"/>
  41. </view>
  42. <view v-if="form.type === 1" class="app-line main-between cross-center">
  43. <text class="app-text box-grow-0">门牌号</text>
  44. <input class="address-input" placeholder="请输入具体门牌号以便配送" type="text" :focus="focus" @blur="focus = false"
  45. v-model="form.detail"/>
  46. </view>
  47. <view v-if="false" class="tip" @click="detailFocus">请输入具体门牌号以便配送</view>
  48. </view>
  49. <view class="app-button">
  50. <app-button @click="submit" :theme="getTheme" arrangement="row" type="important" round width="90%">
  51. <text class="app-text">保存地址</text>
  52. </app-button>
  53. </view>
  54. </app-layout>
  55. </template>
  56. <script>
  57. import {mapGetters} from 'vuex';
  58. import appAreaPicker from '../../components/page-component/app-area-picker/app-area-picker.vue';
  59. import addressParse from './address-parse.js';
  60. import appTextArea from '../../components/basic-component/app-textarea/app-textarea.vue';
  61. export default {
  62. name: "address-edit",
  63. data() {
  64. return {
  65. form: {
  66. location: '',
  67. latitude: '',
  68. longitude: '',
  69. id: '',
  70. name: '',
  71. mobile: '',
  72. address: '',
  73. province_id: 0,
  74. city_id: 0,
  75. district_id: 0,
  76. detail: '',
  77. type: '',
  78. },
  79. is_refund_address: 0,
  80. detail_url: '',
  81. list: [],
  82. submit_status: false,
  83. tt_area_show: false,
  84. automatic: '',
  85. provinceString: '',
  86. cityString: '',
  87. areaString: '',
  88. focus: false,
  89. }
  90. },
  91. computed: {
  92. ...mapGetters('mallConfig', {
  93. getTheme: 'getTheme'
  94. }),
  95. },
  96. components: {
  97. appAreaPicker,
  98. appTextArea
  99. },
  100. onLoad: function (options) {
  101. this.form.type = options.type == 1 ? 1 : 0;
  102. this.getAutoInfo();
  103. if (options.is_refund_address > 0) {
  104. this.is_refund_address = options.is_refund_address;
  105. this.detail_url = this.$api.app_admin.refund_address_edit;
  106. uni.setNavigationBarTitle({
  107. title: '编辑退货地址'
  108. });
  109. } else {
  110. this.is_refund_address = 0;
  111. this.detail_url = this.$api.user.address_detail;
  112. }
  113. if (options.form) {
  114. const optionsForm = JSON.parse(options.form);
  115. if (typeof optionsForm.type === 'undefined') {
  116. optionsForm.type = this.form.type;
  117. }
  118. this.form = optionsForm;
  119. }
  120. if (options.id > 0) {
  121. this.$showLoading({title: `加载中`});
  122. this.$request({
  123. url: this.detail_url,
  124. data: options
  125. }).then(info => {
  126. this.$hideLoading();
  127. if (info.code === 0) {
  128. if (options.is_refund_address > 0) {
  129. const detail = info.data.detail;
  130. this.getInfo(Object.assign({detail: detail.address_detail}, detail));
  131. } else {
  132. this.form = info.data.list;
  133. }
  134. } else {
  135. uni.showToast({title: info.msg, icon: 'none'});
  136. }
  137. this.tt_area_show = true;
  138. }).catch(() => {
  139. this.$hideLoading();
  140. this.tt_area_show = true;
  141. });
  142. } else {
  143. this.tt_area_show = true;
  144. }
  145. },
  146. methods: {
  147. async getAutoInfo() {
  148. const res = await this.$request({
  149. url: this.$api.user.auto_address_info,
  150. method: 'get'
  151. });
  152. if (res.code === 0) {
  153. let autoInfo = JSON.parse(res.data);
  154. const provinces = autoInfo.reduce((per, cur) => {
  155. const {children, ...others} = cur;
  156. return per.concat(others);
  157. }, []);
  158. const cities = autoInfo.reduce((per, cur) => {
  159. return per.concat(cur.children.map(({children, ...others}) => ({...others, provinceCode: cur.code})));
  160. }, []);
  161. const areas = autoInfo.reduce((per, cur) => {
  162. const provinceCode = cur.code;
  163. return per.concat(cur.children.reduce((p, c) => {
  164. const cityCode = c.code;
  165. return p.concat(c.children.map(({children, ...others}) => ({...others, cityCode, provinceCode,})));
  166. }, []));
  167. }, []);
  168. this.provinceString = JSON.stringify(provinces);
  169. this.cityString = JSON.stringify(cities);
  170. this.areaString = JSON.stringify(areas);
  171. }
  172. },
  173. // #ifndef MP-TOUTIAO
  174. async getChooseLocation() {
  175. const self = this;
  176. const [err, res] = await uni.chooseLocation();
  177. if (res) {
  178. self.form.location = res.address + res.name;
  179. self.form.latitude = res.latitude;
  180. self.form.longitude = res.longitude;
  181. }
  182. if (err) {
  183. const setting = () => {
  184. uni.showModal({
  185. title: '授权权限',
  186. content: '请先授权地理位置权限',
  187. success(res) {
  188. if (res.confirm) {
  189. uni.openSetting({
  190. success(settingdata) {
  191. if (settingdata.authSetting['scope.userLocation']) {
  192. uni.chooseLocation({
  193. success: function (res) {
  194. self.form.location = res.address + res.name;
  195. self.form.latitude = res.latitude;
  196. self.form.longitude = res.longitude;
  197. },
  198. });
  199. } else {
  200. uni.showToast({
  201. title: '授权失败',
  202. icon: 'none'
  203. });
  204. }
  205. }
  206. })
  207. }
  208. }
  209. })
  210. };
  211. //#ifdef MP-BAIDU
  212. if (err.errCode === 1003) {
  213. setting();
  214. }
  215. //#endif
  216. //#ifdef MP-WEIXIN
  217. if (err.errMsg === `chooseLocation:fail auth deny`) {
  218. setting();
  219. }
  220. //#endif
  221. }
  222. },
  223. // #endif
  224. getInfo(detail) {
  225. this.$request({
  226. url: this.$api.user.wechat_district,
  227. data: {
  228. province_name: detail.address[0],
  229. city_name: detail.address[1],
  230. county_name: detail.address[2],
  231. },
  232. }).then(info => {
  233. if (info.code === 0) {
  234. let { province, city, district } = info.data.district;
  235. detail.province_id = province.id;
  236. detail.city_id = city.id;
  237. detail.district_id = district.id;
  238. this.form = detail;
  239. }
  240. }).catch(() => {
  241. uni.showToast({
  242. title: '网络异常',
  243. icon: 'none',
  244. });
  245. })
  246. },
  247. areaEvent(data) {
  248. if (data) {
  249. this.form.province_id = data.province.id;
  250. this.form.city_id = data.city.id;
  251. this.form.district_id = data.district.id;
  252. this.list = [data.province.name, data.city.name, data.district.name];
  253. }
  254. },
  255. async submit() {
  256. try {
  257. if (this.submit_status) return;
  258. let content;
  259. if (!this.form.detail) {
  260. content = this.form.type == 1 ? '门牌号不能为空' : '详细地址不能为空';
  261. }
  262. if (!this.form.province_id && this.form.type == 0) {
  263. content = '地区不能为空';
  264. }
  265. if (!this.form.mobile) {
  266. content = '联系方式不能为空';
  267. }
  268. if (!(this.$validation.landline(this.form.mobile) || this.$validation.mobile(this.form.mobile))) {
  269. content = '联系方式格式错误';
  270. }
  271. if (!this.form.name) {
  272. content = '姓名不能为空';
  273. }
  274. if (content) {
  275. uni.showToast({
  276. title: content,
  277. icon: 'none'
  278. });
  279. return;
  280. }
  281. this.submit_status = true;
  282. let para, url;
  283. if (this.is_refund_address > 0) {
  284. let {id, name, mobile, detail} = this.form;
  285. url = this.$api.app_admin.refund_address_edit;
  286. para = {
  287. form: JSON.stringify({
  288. id,
  289. name,
  290. mobile,
  291. address_detail: detail,
  292. address: this.list,
  293. is_default: 0,
  294. remark: '',
  295. })
  296. }
  297. } else {
  298. url = this.$api.user.address_save;
  299. para = Object.assign({}, this.form);
  300. }
  301. const info = await this.$request({
  302. url: url,
  303. data: para,
  304. method: 'POST',
  305. });
  306. this.submit_status = false;
  307. if (info.code === 0) {
  308. uni.showToast({title: info.msg, icon: 'none'});
  309. uni.navigateBack({delta: 1});
  310. } else {
  311. uni.showToast({title: info.msg, icon: 'none'});
  312. }
  313. } catch (e) {
  314. this.submit_status = false;
  315. throw new Error(e);
  316. }
  317. },
  318. getAddress() {
  319. let data = addressParse(this.automatic, {} ,this.provinceString, this.cityString, this.areaString);
  320. if (this.$validation.empty(data)) return;
  321. const district = uni.getStorageSync("_DISTRICT");
  322. let { name, province, city, area, phone, detail } = data;
  323. for (let i = 0; i < district.length; i++) {
  324. if (district[i].name === province) {
  325. for (let j = 0; j < district[i].list.length; j++) {
  326. if (district[i].list[j].name === city) {
  327. for (let k = 0; k < district[i].list[j].list.length; k++) {
  328. if (district[i].list[j].list[k].name === area) {
  329. this.form.province_id = district[i].id;
  330. this.form.city_id = district[i].list[j].id;
  331. this.form.district_id = district[i].list[j].list[k].id;
  332. break;
  333. }
  334. }
  335. break;
  336. } else if (city === '市辖区') {
  337. for (let k = 0; k < district[i].list[j].list.length; k++) {
  338. if (district[i].list[j].list[k].name === area) {
  339. this.form.province_id = district[i].id;
  340. this.form.city_id = district[i].list[j].id;
  341. this.form.district_id = district[i].list[j].list[k].id;
  342. break;
  343. }
  344. }
  345. break;
  346. }
  347. }
  348. break;
  349. }
  350. }
  351. this.form.name = name;
  352. this.form.detail = `${detail}`;
  353. this.form.mobile = phone;
  354. },
  355. detailFocus() {
  356. this.focus = true;
  357. }
  358. }
  359. }
  360. </script>
  361. <style scoped lang="scss">
  362. // 自动识别功能
  363. .automatic {
  364. height: #{100upx};
  365. margin-bottom: #{20upx};
  366. padding: #{0 32upx 0 24upx};
  367. background-color: #ffffff;
  368. button {
  369. width: #{150upx};
  370. height: #{60upx};
  371. padding: 0;
  372. line-height: #{58upx};
  373. border-radius: #{5upx};
  374. border: #{1upx} solid;
  375. font-size: #{26upx};
  376. background-color: transparent;
  377. margin: 0;
  378. text-align: center;
  379. }
  380. input {
  381. width: calc(100% - #{182upx});
  382. font-size: #{28upx};
  383. color:#353535;
  384. }
  385. .place-automatic {
  386. font-size: #{26upx};
  387. color: #999999;
  388. }
  389. }
  390. .popup {
  391. position: fixed;
  392. background: rgba(0, 0, 0, 0.5);
  393. padding: #{50rpx};
  394. top: 0;
  395. left: 0;
  396. right: 0;
  397. bottom: 0;
  398. z-index: 2000;
  399. .textarea {
  400. width: 100%;
  401. background: #fff;
  402. border: #{1rpx} solid #ccc;
  403. z-index: 1;
  404. padding: #{24rpx};
  405. border-radius: #{5rpx};
  406. }
  407. .c {
  408. position: fixed;
  409. left: 0;
  410. top: 0;
  411. right: 0;
  412. bottom: 0;
  413. z-index: 0;
  414. }
  415. }
  416. .h-100 {
  417. height: 100%;
  418. }
  419. .app-button {
  420. margin: #{40rpx} #{24rpx};
  421. }
  422. .address-input {
  423. width: #{580rpx};
  424. font-size: #{28rpx};
  425. color: #353535;
  426. }
  427. .area-left {
  428. padding-left: #{32rpx};
  429. text-align: right;
  430. margin-left: auto;
  431. }
  432. .app-view {
  433. background: #f7f7f7;
  434. .app-line {
  435. background-color: #ffffff;
  436. height: #{99rpx};
  437. line-height: #{99rpx};
  438. padding: #{0 24upx};
  439. text.app-text {
  440. font-size: #{28rpx};
  441. color: $uni-important-color-black;
  442. width: #{124rpx};
  443. }
  444. }
  445. .app-border {
  446. border-bottom: #{1rpx} solid $uni-weak-color-one;
  447. }
  448. .c-address {
  449. padding-right: #{24rpx};
  450. font-size: #{28rpx};
  451. color: #353535;
  452. }
  453. .icon-arrow-image {
  454. width: #{12rpx};
  455. height: #{24rpx};
  456. background-image: url("../../static/image/icon/arrow-right.png");
  457. background-size: 100% auto;
  458. background-repeat: no-repeat;
  459. }
  460. .tip {
  461. padding: #{0 24rpx 24rpx 24rpx};
  462. background: #ffffff;
  463. color: #b7b7b7;
  464. }
  465. }
  466. </style>