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