index.vue 13 KB

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