index.vue 11 KB

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