address-edit.vue 19 KB

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