index.vue 13 KB

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