index.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. <template>
  2. <view :style="colorStyle">
  3. <form @submit="formSubmit">
  4. <view class='addAddress'>
  5. <view class='list'>
  6. <view class='item acea-row row-between-wrapper'>
  7. <view class='name'>姓名</view>
  8. <input type='text' placeholder='请输入姓名' name='real_name' :value="userAddress.real_name"
  9. placeholder-class='placeholder'></input>
  10. </view>
  11. <view class='item acea-row row-between-wrapper'>
  12. <view class='name'>联系电话</view>
  13. <input type='number' placeholder='请输入联系电话' name="phone" :value='userAddress.phone'
  14. placeholder-class='placeholder' pattern="\d*"></input>
  15. </view>
  16. <view class='item acea-row row-between-wrapper'>
  17. <view class='name'>所在地区</view>
  18. <view class="address">
  19. <picker mode="multiSelector" @change="bindRegionChange"
  20. @columnchange="bindMultiPickerColumnChange" :value="valueRegion" :range="multiArray">
  21. <view class='acea-row'>
  22. <view class="picker">{{region[0]}},{{region[1]}},{{region[2]}}</view>
  23. <view class='iconfont icon-dizhi fontcolor'></view>
  24. </view>
  25. </picker>
  26. </view>
  27. </view>
  28. <view class='item acea-row row-between-wrapper'>
  29. <view class='name'>详细地址</view>
  30. <input type='text' placeholder='请填写具体地址' name='detail' placeholder-class='placeholder'
  31. :value='userAddress.detail'></input>
  32. </view>
  33. </view>
  34. <view class='default acea-row row-middle' @click='ChangeIsDefault'>
  35. <checkbox-group >
  36. <checkbox :checked="userAddress.is_default ? true : false" />设置为默认地址
  37. </checkbox-group>
  38. </view>
  39. <button class='keepBnt bg-color' form-type="submit">立即保存</button>
  40. <!-- #ifdef MP -->
  41. <view class="wechatAddress" v-if="!id" @click="getWxAddress">导入微信地址</view>
  42. <!-- #endif -->
  43. <!-- #ifdef H5 -->
  44. <view class="wechatAddress" v-if="this.$wechat.isWeixin() && !id" @click="getAddress">导入微信地址</view>
  45. <!-- #endif -->
  46. </view>
  47. </form>
  48. <!-- #ifdef MP -->
  49. <!-- <authorize @onLoadFun="onLoadFun" :isAuto="isAuto" :isShowAuth="isShowAuth" @authColse="authColse"></authorize> -->
  50. <!-- #endif -->
  51. <!-- #ifndef MP -->
  52. <home></home>
  53. <!-- #endif -->
  54. </view>
  55. </template>
  56. <script>
  57. import {
  58. editAddress,
  59. getAddressDetail
  60. } from '@/api/user.js';
  61. import {
  62. getCity
  63. } from '@/api/api.js';
  64. import {
  65. toLogin
  66. } from '@/libs/login.js';
  67. import {
  68. mapGetters
  69. } from "vuex";
  70. // #ifdef MP
  71. import authorize from '@/components/Authorize';
  72. // #endif
  73. import home from '@/components/home';
  74. import colors from '@/mixins/color.js';
  75. export default {
  76. components: {
  77. // #ifdef MP
  78. authorize,
  79. // #endif
  80. home,
  81. },
  82. mixins:[colors],
  83. data() {
  84. return {
  85. regionDval: ['浙江省', '杭州市', '滨江区'],
  86. cartId: '', //购物车id
  87. pinkId: 0, //拼团id
  88. couponId: 0, //优惠券id
  89. id: 0, //地址id
  90. userAddress: {
  91. is_default: false
  92. }, //地址详情
  93. region: ['省', '市', '区'],
  94. valueRegion: [0, 0, 0],
  95. isAuto: false, //没有授权的不会自动授权
  96. isShowAuth: false, //是否隐藏授权
  97. district: [],
  98. multiArray: [],
  99. multiIndex: [0, 0, 0],
  100. cityId: 0,
  101. defaultRegion: ['广东省', '广州市', '番禺区'],
  102. defaultRegionCode: '110101',
  103. news: '',
  104. noCoupon: 0
  105. };
  106. },
  107. computed: mapGetters(['isLogin']),
  108. watch: {
  109. isLogin: {
  110. handler: function(newV, oldV) {
  111. if (newV) {
  112. this.getUserAddress();
  113. }
  114. },
  115. deep: true
  116. }
  117. },
  118. onLoad(options) {
  119. if (this.isLogin) {
  120. this.cartId = options.cartId || '';
  121. this.pinkId = options.pinkId || 0;
  122. this.couponId = options.couponId || 0;
  123. this.id = options.id || 0;
  124. this.noCoupon = options.noCoupon || 0;
  125. this.news = options.new || '';
  126. uni.setNavigationBarTitle({
  127. title: options.id ? '修改地址' : '添加地址'
  128. })
  129. this.getUserAddress();
  130. this.getCityList();
  131. } else {
  132. toLogin();
  133. }
  134. },
  135. methods: {
  136. // #ifdef APP-PLUS
  137. // 获取选择的地区
  138. handleGetRegion(region) {
  139. this.region = region
  140. },
  141. // #endif
  142. // 回去地址数据
  143. getCityList: function() {
  144. let that = this;
  145. getCity().then(res => {
  146. this.district = res.data
  147. that.initialize();
  148. })
  149. },
  150. initialize: function() {
  151. let that = this,
  152. province = [],
  153. city = [],
  154. area = [];
  155. if (that.district.length) {
  156. let cityChildren = that.district[0].c || [];
  157. let areaChildren = cityChildren.length ? (cityChildren[0].c || []) : [];
  158. that.district.forEach(function(item) {
  159. province.push(item.n);
  160. });
  161. cityChildren.forEach(function(item) {
  162. city.push(item.n);
  163. });
  164. areaChildren.forEach(function(item) {
  165. area.push(item.n);
  166. });
  167. this.multiArray = [province, city, area]
  168. }
  169. },
  170. bindRegionChange: function(e) {
  171. let multiIndex = this.multiIndex,
  172. province = this.district[multiIndex[0]] || {
  173. c: []
  174. },
  175. city = province.c[multiIndex[1]] || {
  176. v: 0
  177. },
  178. multiArray = this.multiArray,
  179. value = e.detail.value;
  180. this.region = [multiArray[0][value[0]], multiArray[1][value[1]], multiArray[2][value[2]]]
  181. // this.$set(this.region,0,multiArray[0][value[0]]);
  182. // this.$set(this.region,1,multiArray[1][value[1]]);
  183. // this.$set(this.region,2,multiArray[2][value[2]]);
  184. this.cityId = city.v
  185. this.valueRegion = [0, 0, 0]
  186. this.initialize();
  187. },
  188. bindMultiPickerColumnChange: function(e) {
  189. let that = this,
  190. column = e.detail.column,
  191. value = e.detail.value,
  192. currentCity = this.district[value] || {
  193. c: []
  194. },
  195. multiArray = that.multiArray,
  196. multiIndex = that.multiIndex;
  197. multiIndex[column] = value;
  198. switch (column) {
  199. case 0:
  200. let areaList = currentCity.c[0] || {
  201. c: []
  202. };
  203. multiArray[1] = currentCity.c.map((item) => {
  204. return item.n;
  205. });
  206. multiArray[2] = areaList.c.map((item) => {
  207. return item.n;
  208. });
  209. break;
  210. case 1:
  211. let cityList = that.district[multiIndex[0]].c[multiIndex[1]].c || [];
  212. multiArray[2] = cityList.map((item) => {
  213. return item.n;
  214. });
  215. break;
  216. case 2:
  217. break;
  218. }
  219. // #ifdef MP || APP-PLUS
  220. this.$set(this.multiArray, 0, multiArray[0]);
  221. this.$set(this.multiArray, 1, multiArray[1]);
  222. this.$set(this.multiArray, 2, multiArray[2]);
  223. // #endif
  224. // #ifdef H5
  225. this.multiArray = multiArray;
  226. // #endif
  227. this.multiIndex = multiIndex
  228. // this.setData({ multiArray: multiArray, multiIndex: multiIndex});
  229. },
  230. // 授权回调
  231. onLoadFun: function() {
  232. this.getUserAddress();
  233. },
  234. // 授权关闭
  235. authColse: function(e) {
  236. this.isShowAuth = e
  237. },
  238. toggleTab(str) {
  239. this.$refs[str].show();
  240. },
  241. // bindRegionChange: function(e) {
  242. // this.$set(this, 'region', e.detail.value);
  243. // },
  244. onConfirm(val) {
  245. this.region = val.checkArr[0] + '-' + val.checkArr[1] + '-' + val.checkArr[2];
  246. },
  247. getUserAddress: function() {
  248. if (!this.id) return false;
  249. let that = this;
  250. getAddressDetail(this.id).then(res => {
  251. // let region = [res.data.province, res.data.city, res.data.district];
  252. let region = [res.data.province, res.data.city, res.data.district];
  253. that.$set(that, 'userAddress', res.data);
  254. that.$set(that, 'region', region);
  255. that.cityId = res.data.city_id
  256. });
  257. },
  258. // 导入共享地址(小程序)
  259. getWxAddress: function() {
  260. let that = this;
  261. uni.authorize({
  262. scope: 'scope.address',
  263. success: function(res) {
  264. uni.chooseAddress({
  265. success: function(res) {
  266. let addressP = {};
  267. addressP.province = res.provinceName;
  268. addressP.city = res.cityName;
  269. addressP.district = res.countyName;
  270. editAddress({
  271. address: addressP,
  272. is_default: 1,
  273. real_name: res.userName,
  274. post_code: res.postalCode,
  275. phone: res.telNumber,
  276. detail: res.detailInfo,
  277. id: 0,
  278. type: 1,
  279. }).then(res => {
  280. setTimeout(function() {
  281. if (that.cartId) {
  282. let cartId = that.cartId;
  283. let pinkId = that.pinkId;
  284. let couponId = that.couponId;
  285. that.cartId = '';
  286. that.pinkId = '';
  287. that.couponId = '';
  288. uni.navigateTo({
  289. url: '/pages/users/order_confirm/index?cartId=' +
  290. cartId +
  291. '&addressId=' + (
  292. that.id ? that
  293. .id :
  294. res.data
  295. .id) +
  296. '&pinkId=' +
  297. pinkId +
  298. '&couponId=' +
  299. couponId +
  300. '&new=' + that
  301. .news +
  302. '&noCoupon=' + that
  303. .noCoupon
  304. });
  305. } else {
  306. uni.navigateBack({
  307. delta: 1
  308. });
  309. }
  310. }, 1000);
  311. return that.$util.Tips({
  312. title: "添加成功",
  313. icon: 'success'
  314. });
  315. }).catch(err => {
  316. return that.$util.Tips({
  317. title: err
  318. });
  319. });
  320. },
  321. fail: function(res) {
  322. if (res.errMsg == 'chooseAddress:cancel') return that.$util
  323. .Tips({
  324. title: '取消选择'
  325. });
  326. },
  327. })
  328. },
  329. fail: function(res) {
  330. uni.showModal({
  331. title: '您已拒绝导入微信地址权限',
  332. content: '是否进入权限管理,调整授权?',
  333. success(res) {
  334. if (res.confirm) {
  335. uni.openSetting({
  336. success: function(res) {}
  337. });
  338. } else if (res.cancel) {
  339. return that.$util.Tips({
  340. title: '已取消!'
  341. });
  342. }
  343. }
  344. })
  345. },
  346. })
  347. },
  348. // 导入共享地址(微信);
  349. getAddress() {
  350. let that = this;
  351. that.$wechat.openAddress().then(userInfo => {
  352. editAddress({
  353. id: this.id,
  354. real_name: userInfo.userName,
  355. phone: userInfo.telNumber,
  356. address: {
  357. province: userInfo.provinceName,
  358. city: userInfo.cityName,
  359. district: userInfo.countryName
  360. },
  361. detail: userInfo.detailInfo,
  362. is_default: 1,
  363. post_code: userInfo.postalCode,
  364. type: 1,
  365. })
  366. .then(() => {
  367. setTimeout(function() {
  368. if (that.cartId) {
  369. let cartId = that.cartId;
  370. let pinkId = that.pinkId;
  371. let couponId = that.couponId;
  372. that.cartId = '';
  373. that.pinkId = '';
  374. that.couponId = '';
  375. uni.navigateTo({
  376. url: '/pages/users/order_confirm/index?cartId=' +
  377. cartId + '&addressId=' + (that.id ? that.id :
  378. res.data
  379. .id) + '&pinkId=' + pinkId + '&couponId=' +
  380. couponId + '&new=' + that.news +
  381. '&noCoupon=' + that
  382. .noCoupon
  383. });
  384. } else {
  385. uni.navigateTo({
  386. url: '/pages/users/user_address_list/index'
  387. })
  388. // history.back();
  389. }
  390. }, 1000);
  391. // close();
  392. that.$util.Tips({
  393. title: "添加成功",
  394. icon: 'success'
  395. });
  396. })
  397. .catch(err => {
  398. // close();
  399. return that.$util.Tips({
  400. title: err || "添加失败"
  401. });
  402. });
  403. }).catch(err => {});
  404. },
  405. /**
  406. * 提交用户添加地址
  407. *
  408. */
  409. formSubmit: function(e) {
  410. let that = this,
  411. value = e.detail.value;
  412. if (!value.real_name) return that.$util.Tips({
  413. title: '请填写收货人姓名'
  414. });
  415. if (!value.phone) return that.$util.Tips({
  416. title: '请填写联系电话'
  417. });
  418. if (!/^1(3|4|5|7|8|9|6)\d{9}$/i.test(value.phone)) return that.$util.Tips({
  419. title: '请输入正确的手机号码'
  420. });
  421. if (that.region[0] == '省') return that.$util.Tips({
  422. title: '请选择所在地区'
  423. });
  424. if (!value.detail) return that.$util.Tips({
  425. title: '请填写详细地址'
  426. });
  427. value.id = that.id;
  428. let regionArray = that.region;
  429. value.address = {
  430. province: regionArray[0],
  431. city: regionArray[1],
  432. district: regionArray[2],
  433. city_id: that.cityId,
  434. };
  435. value.is_default = that.userAddress.is_default ? 1 : 0;
  436. uni.showLoading({
  437. title: '保存中',
  438. mask: true
  439. })
  440. editAddress(value).then(res => {
  441. if (that.id)
  442. that.$util.Tips({
  443. title: '修改成功',
  444. icon: 'success'
  445. });
  446. else
  447. that.$util.Tips({
  448. title: '添加成功',
  449. icon: 'success'
  450. });
  451. setTimeout(function() {
  452. if (that.cartId) {
  453. let cartId = that.cartId;
  454. let pinkId = that.pinkId;
  455. let couponId = that.couponId;
  456. that.cartId = '';
  457. that.pinkId = '';
  458. that.couponId = '';
  459. uni.navigateTo({
  460. url: '/pages/users/order_confirm/index?new=' + that.news +
  461. '&cartId=' + cartId + '&addressId=' + (that.id ? that.id :
  462. res.data.id) + '&pinkId=' + pinkId + '&couponId=' +
  463. couponId +
  464. '&noCoupon=' + that
  465. .noCoupon
  466. });
  467. } else {
  468. // #ifdef H5
  469. return history.back();
  470. // #endif
  471. // #ifndef H5
  472. return uni.navigateBack({
  473. delta: 1,
  474. })
  475. // #endif
  476. }
  477. }, 1000);
  478. }).catch(err => {
  479. return that.$util.Tips({
  480. title: err
  481. });
  482. })
  483. },
  484. ChangeIsDefault: function(e) {
  485. this.$set(this.userAddress, 'is_default', !this.userAddress.is_default);
  486. }
  487. }
  488. }
  489. </script>
  490. <style scoped lang="scss">
  491. .fontcolor{
  492. color: var(--view-theme);
  493. }
  494. .addAddress .list {
  495. background-color: #fff;
  496. }
  497. .addAddress .list .item {
  498. padding: 30rpx;
  499. border-top: 1rpx solid #eee;
  500. }
  501. .addAddress .list .item .name {
  502. width: 195rpx;
  503. font-size: 30rpx;
  504. color: #333;
  505. }
  506. .addAddress .list .item .address {
  507. // width: 412rpx;
  508. flex: 1;
  509. margin-left: 20rpx;
  510. }
  511. .addAddress .list .item input {
  512. width: 475rpx;
  513. font-size: 30rpx;
  514. }
  515. .addAddress .list .item .placeholder {
  516. color: #ccc;
  517. }
  518. .addAddress .list .item picker {
  519. width: 475rpx;
  520. }
  521. .addAddress .list .item picker .picker {
  522. width: 410rpx;
  523. font-size: 30rpx;
  524. }
  525. .addAddress .list .item picker .iconfont {
  526. font-size: 43rpx;
  527. }
  528. .addAddress .default {
  529. padding: 0 30rpx;
  530. height: 90rpx;
  531. background-color: #fff;
  532. margin-top: 23rpx;
  533. }
  534. .addAddress .default checkbox {
  535. margin-right: 15rpx;
  536. }
  537. .addAddress .keepBnt {
  538. width: 690rpx;
  539. height: 86rpx;
  540. border-radius: 50rpx;
  541. text-align: center;
  542. line-height: 86rpx;
  543. margin: 50rpx auto;
  544. font-size: 32rpx;
  545. color: #fff;
  546. }
  547. .addAddress .wechatAddress {
  548. width: 690rpx;
  549. height: 86rpx;
  550. border-radius: 50rpx;
  551. text-align: center;
  552. line-height: 86rpx;
  553. margin: 0 auto;
  554. font-size: 32rpx;
  555. color: var(--view-theme);
  556. border: 1px solid var(--view-theme);
  557. }
  558. </style>