index.vue 15 KB

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