index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. <template>
  2. <view :data-theme="theme">
  3. <form @submit="formSubmit" report-submit='true'>
  4. <view class='addAddress borderPad'>
  5. <view class='list borRadius14'>
  6. <view class='item acea-row' style="border: none;" v-if="addType">
  7. <view class='name'>驿站名称</view>
  8. <input type='text' placeholder="请输入驿站名称" placeholder-style="color:#ccc;" name='expressName' :value="userAddress.expressName"
  9. placeholder-class='placeholder' maxlength="20"></input>
  10. </view>
  11. <view class='item acea-row' style="border: none;" v-else>
  12. <view class='name'>姓名</view>
  13. <input type='text' placeholder="请输入姓名" placeholder-style="color:#ccc;" name='realName' :value="userAddress.realName"
  14. placeholder-class='placeholder' maxlength="20"></input>
  15. </view>
  16. <view class='item acea-row' v-if="!addType">
  17. <view class='name'>手机号码</view>
  18. <input type='number' placeholder='请输入手机号码' placeholder-style="color:#ccc;" name="phone" :value='userAddress.phone'
  19. placeholder-class='placeholder' maxlength="11"></input>
  20. </view>
  21. <view class='item acea-row row-between-wrapper'>
  22. <view class='name'>所在地区</view>
  23. <view class="address">
  24. <view class="region_count" @click="changeRegion">
  25. <text v-if="!addressInfo.length" style="color:#cdcdcd;">请选择地址</text>
  26. <text v-else>{{addressText}}</text>
  27. </view>
  28. </view>
  29. <text class="iconfont icon-xiangyou"></text>
  30. </view>
  31. <view class='item acea-row row-between-wrapper'>
  32. <view class='name'>详细地址</view>
  33. <input class="address" type='text' placeholder='请填写具体地址' placeholder-style="color:#ccc;" name='detail'
  34. placeholder-class='placeholder' v-model='userAddress.detail' maxlength="100"></input>
  35. <view class='iconfont icon-dizhi font_color' @tap="chooseLocation"></view>
  36. </view>
  37. </view>
  38. <view class='default acea-row row-middle borRadius14'>
  39. <checkbox-group @change='ChangeIsDefault'>
  40. <checkbox :checked="userAddress.isDefault" />设置为默认地址
  41. </checkbox-group>
  42. </view>
  43. <button class='keepBnt bg_color' form-type="submit">{{orderNo!=0?'保存并使用':'立即保存'}}</button>
  44. <view v-if="!addType">
  45. <!-- #ifdef MP -->
  46. <view class="wechatAddress" v-if="!addressId" @click="getWxAddress">导入微信地址</view>
  47. <!-- #endif -->
  48. <!-- #ifdef H5 -->
  49. <view class="wechatAddress" v-if="this.$wechat.isWeixin() && !addressId" @click="getAddress">导入微信地址</view>
  50. <!-- #endif -->
  51. </view>
  52. </view>
  53. </form>
  54. <view v-show="showLoading" class="bg-fixed"></view>
  55. <areaWindow ref="areaWindow" :display="display" :address="addressInfo" @submit="OnChangeAddress" @changeClose="changeClose"></areaWindow>
  56. </view>
  57. </template>
  58. <script>
  59. import {
  60. editAddress,
  61. editExpressAddress,
  62. addAddress,
  63. addExpressAddress,
  64. getAddressDetail,
  65. getAddressExpressDetail,
  66. getCity,
  67. getWxAddressInfo
  68. } from '@/api/user.js';
  69. import {
  70. getCityList
  71. } from "@/utils";
  72. import {
  73. toLogin
  74. } from '@/libs/login.js';
  75. import {
  76. mapGetters
  77. } from "vuex";
  78. import areaWindow from '../components/areaWindow';
  79. import {
  80. Debounce
  81. } from '@/utils/validate.js'
  82. import {
  83. addressWxImportApi
  84. } from '@/api/public.js';
  85. let app = getApp();
  86. export default {
  87. components: {
  88. areaWindow
  89. },
  90. data() {
  91. return {
  92. cartId: '', //购物车id
  93. pinkId: 0, //拼团id
  94. couponId: 0, //优惠券id
  95. addressId: 0, //地址id
  96. userAddress: {
  97. isDefault: false
  98. }, //地址详情
  99. region: ['省', '市', '区'],
  100. valueRegion: [0, 0, 0, 0],
  101. district: [],
  102. multiArray: [],
  103. multiIndex: [0, 0, 0],
  104. cityId: 0,
  105. bargain: false, //是否是砍价
  106. combination: false, //是否是拼团
  107. secKill: false, //是否是秒杀
  108. theme: app.globalData.theme,
  109. showLoading: false,
  110. display: false,
  111. addressInfo: [],
  112. addressData: [],
  113. addType: null,
  114. orderNo: '' //订单id
  115. };
  116. },
  117. computed: {
  118. ...mapGetters(['isLogin']),
  119. addressText() {
  120. return this.addressInfo.map(v => v.regionName).join('/');
  121. }
  122. },
  123. watch: {
  124. isLogin: {
  125. handler: function(newV, oldV) {
  126. if (newV) {
  127. this.getUserAddress();
  128. }
  129. },
  130. deep: true
  131. }
  132. },
  133. onLoad(options) {
  134. if (this.isLogin) {
  135. this.orderNo = options.orderNo || 0;
  136. this.addressId = options.id || 0;
  137. this.addType = options.addType || null;
  138. uni.setNavigationBarTitle({
  139. title: this.addressId ? '编辑地址' : '添加地址'
  140. })
  141. this.getUserAddress();
  142. } else {
  143. toLogin();
  144. }
  145. },
  146. methods: {
  147. // 关闭地址弹窗;
  148. changeClose: function() {
  149. this.display = false;
  150. },
  151. OnChangeAddress(address) {
  152. this.addressInfo = address;
  153. },
  154. changeRegion() {
  155. this.display = true;
  156. },
  157. getUserAddress: function() {
  158. if (!this.addressId) return false;
  159. let that = this;
  160. const getAddApi = this.addType ? getAddressExpressDetail(this.addressId) : getAddressDetail(this.addressId);
  161. getAddApi.then(res => {
  162. if (res.data) {
  163. that.$set(that, 'userAddress', res.data);
  164. that.addressInfo = [{
  165. regionName: res.data.province,
  166. parentId: 1,
  167. isChild: true,
  168. regionId: res.data.provinceId,
  169. regionType: 1
  170. }, {
  171. regionName: res.data.city,
  172. parentId: res.data.provinceId,
  173. regionId: res.data.cityId,
  174. isChild: true,
  175. regionType: 2
  176. }, {
  177. regionName: res.data.district,
  178. parentId: res.data.cityId,
  179. regionId: res.data.districtId,
  180. isChild: true,
  181. regionType: 3
  182. }, {
  183. isChild: false,
  184. parentId: res.data.districtId,
  185. regionName: res.data.street,
  186. regionType: 4
  187. }]
  188. }
  189. });
  190. },
  191. toggleTab(str) {
  192. this.$refs[str].show();
  193. },
  194. onConfirm(val) {
  195. this.region = val.checkArr[0] + '-' + val.checkArr[1] + '-' + val.checkArr[2];
  196. },
  197. //选择定位
  198. chooseLocation: function() {
  199. this.$util.$L.getLocation().then(res => {
  200. uni.chooseLocation({
  201. latitude: uni.getStorageSync('user_latitude'),
  202. longitude: uni.getStorageSync('user_longitude'),
  203. success: (res) => {
  204. this.$set(this.userAddress, 'detail', res.name);
  205. this.$set(this.userAddress, 'latitude', res.latitude);
  206. this.$set(this.userAddress, 'longitude', res.longitude);
  207. }
  208. })
  209. })
  210. },
  211. //导入微信地址回显到地址栏
  212. addressWxImportAdd(res) {
  213. this.userAddress.realName = res.userName;
  214. this.userAddress.phone = res.telNumber;
  215. this.userAddress.detail = res.detailInfo;
  216. getWxAddressInfo({
  217. "cityName": res.cityName,
  218. "countryName": res.countryName || res.countyName,
  219. "detail": res.detailInfo,
  220. "nationalCode": res.nationalCode,
  221. "provinceName": res.provinceName,
  222. "streetName": res.streetName || res.addressStreetFourthStageName
  223. }).then(r => {
  224. this.addressInfo = [{
  225. regionName: r.data.province,
  226. parentId: 1,
  227. isChild: true,
  228. regionId: r.data.provinceId,
  229. regionType: 1
  230. }, {
  231. regionName: r.data.city,
  232. parentId: r.data.provinceId,
  233. regionId: r.data.cityId,
  234. isChild: true,
  235. regionType: 2
  236. }, {
  237. regionName: r.data.district,
  238. parentId: r.data.cityId,
  239. regionId: r.data.districtId,
  240. isChild: true,
  241. regionType: 3
  242. }, {
  243. isChild: false,
  244. parentId: r.data.districtId,
  245. regionName: r.data.street,
  246. regionType: 4
  247. }]
  248. });
  249. },
  250. // 导入共享地址(小程序)
  251. getWxAddress: function() {
  252. this.$util.addressWxImport().then(userInfo => {
  253. this.addressWxImportAdd(userInfo);
  254. });
  255. },
  256. // 导入共享地址(微信);
  257. getAddress() {
  258. let that = this;
  259. that.$wechat.openAddress().then(userInfo => {
  260. that.addressWxImportAdd(userInfo);
  261. });
  262. },
  263. /**
  264. * 提交用户添加地址
  265. *
  266. */
  267. formSubmit: Debounce(function(e) {
  268. let that = this,
  269. value = e.detail.value;
  270. if (!this.addType) {
  271. if (!value.realName) return that.$util.Tips({
  272. title: '请填写收货人姓名'
  273. });
  274. if (!value.phone) return that.$util.Tips({
  275. title: '请填写手机号码'
  276. });
  277. if (!/^1(3|4|5|7|8|9|6)\d{9}$/i.test(value.phone)) return that.$util.Tips({
  278. title: '请输入正确的手机号码'
  279. });
  280. } else {
  281. if (!value.expressName) return that.$util.Tips({
  282. title: '请填写驿站名称'
  283. });
  284. }
  285. if (!that.addressInfo.length == '省-市-区-街道') return that.$util.Tips({
  286. title: '请选择所在地区'
  287. });
  288. if (!value.detail) return that.$util.Tips({
  289. title: '请填写详细地址'
  290. });
  291. value.id = that.addressId;
  292. value.province = that.addressInfo[0].regionName;
  293. value.provinceId = that.addressInfo[0].regionId;
  294. value.city = that.addressInfo[1].regionName;
  295. value.cityId = that.addressInfo[1].regionId;
  296. value.district = that.addressInfo[2].regionName;
  297. value.districtId = that.addressInfo[2].regionId;
  298. value.street = that.addressInfo.length > 3 ? that.addressInfo[3].regionName : '';
  299. value.isDefault = that.userAddress.isDefault;
  300. if (this.addType) {
  301. value.latitude = that.userAddress.latitude;
  302. value.longitude = that.userAddress.longitude;
  303. }
  304. uni.showLoading({
  305. title: '保存中',
  306. mask: true
  307. })
  308. if (that.addressId) {
  309. const editApi = this.addType ? editExpressAddress(value) : editAddress(value);
  310. editApi.then(res => {
  311. that.$util.Tips({
  312. title: '修改成功',
  313. icon: 'success'
  314. });
  315. uni.hideLoading();
  316. that.onSuccess();
  317. }).catch(err => {
  318. uni.hideLoading();
  319. return that.$util.Tips({
  320. title: err
  321. });
  322. })
  323. } else {
  324. const addApi = this.addType ? addExpressAddress(value) : addAddress(value);
  325. addApi.then(res => {
  326. that.$util.Tips({
  327. title: '添加成功',
  328. icon: 'success'
  329. });
  330. that.addressId = res.data;
  331. uni.hideLoading();
  332. that.onSuccess();
  333. }).catch(err => {
  334. return that.$util.Tips({
  335. title: err
  336. });
  337. uni.hideLoading();
  338. })
  339. }
  340. }),
  341. onSuccess() {
  342. let orderNo = this.orderNo;
  343. let id = this.addressId;
  344. setTimeout(() => {
  345. if (orderNo != 0 && id != 0) {
  346. uni.redirectTo({
  347. url: '/pages/goods/order_confirm/index?is_address=1&orderNo=' + orderNo +
  348. '&addressId=' + id
  349. })
  350. } else {
  351. // #ifdef H5
  352. return history.back();
  353. // #endif
  354. // #ifndef H5
  355. return uni.navigateBack({
  356. delta: 1,
  357. })
  358. // #endif
  359. }
  360. }, 1000);
  361. },
  362. ChangeIsDefault: function(e) {
  363. this.$set(this.userAddress, 'isDefault', !this.userAddress.isDefault);
  364. }
  365. }
  366. }
  367. </script>
  368. <style scoped lang="scss">
  369. .bg-fixed {
  370. width: 100%;
  371. height: 750rpx;
  372. position: absolute;
  373. top: 0;
  374. }
  375. .addAddress {
  376. padding-top: 20rpx;
  377. }
  378. .bg_color {
  379. @include main_bg_color(theme);
  380. }
  381. .addAddress .list {
  382. background-color: #fff;
  383. padding: 0 24rpx;
  384. }
  385. .addAddress .list .item {
  386. border-top: 1rpx solid #eee;
  387. padding: 24rpx 0;
  388. justify-content: flex-start;
  389. align-items: center;
  390. }
  391. .addAddress .list .item .name {
  392. font-size: 30rpx;
  393. color: #333;
  394. width: 120rpx;
  395. }
  396. .addAddress .list .item input,
  397. .address {
  398. flex: 1;
  399. width: 490rpx;
  400. margin-left: 20rpx;
  401. font-size: 30rpx;
  402. font-weight: 400;
  403. }
  404. .addAddress .list .item .placeholder {
  405. color: #ccc;
  406. }
  407. .addAddress .list .item picker .picker {
  408. width: 410rpx;
  409. font-size: 30rpx;
  410. }
  411. .addAddress .default {
  412. padding: 0 30rpx;
  413. height: 90rpx;
  414. background-color: #fff;
  415. margin-top: 23rpx;
  416. }
  417. .addAddress .default checkbox {
  418. margin-right: 15rpx;
  419. }
  420. .addAddress .keepBnt {
  421. width: 690rpx;
  422. height: 86rpx;
  423. border-radius: 50rpx;
  424. text-align: center;
  425. line-height: 86rpx;
  426. margin: 80rpx auto 24rpx auto;
  427. font-size: 32rpx;
  428. color: #fff;
  429. }
  430. .addAddress .wechatAddress {
  431. width: 690rpx;
  432. height: 86rpx;
  433. border-radius: 50rpx;
  434. text-align: center;
  435. line-height: 86rpx;
  436. margin: 0 auto;
  437. font-size: 32rpx;
  438. @include main_color(theme);
  439. @include coupons_border_color(theme);
  440. }
  441. .font_color {
  442. @include main_color(theme);
  443. }
  444. .relative {
  445. position: relative;
  446. }
  447. .icon-dizhi {
  448. font-size: 44rpx;
  449. z-index: 100;
  450. }
  451. .abs_right {
  452. position: absolute;
  453. right: 0;
  454. }
  455. /deep/ checkbox .uni-checkbox-input.uni-checkbox-input-checked {
  456. @include main_bg_color(theme);
  457. @include coupons_border_color(theme);
  458. color: #fff !important
  459. }
  460. /deep/ checkbox .wx-checkbox-input.wx-checkbox-input-checked {
  461. @include main_bg_color(theme);
  462. @include coupons_border_color(theme);
  463. color: #fff !important;
  464. margin-right: 0 !important;
  465. }
  466. </style>