index.vue 14 KB

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