index.vue 15 KB

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