index.vue 14 KB

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