addressAdd.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. <template>
  2. <view class="add-address">
  3. <view class="add-form">
  4. <view class="form-item">
  5. <input class="input" placeholder="姓名" v-model="address.userName" auto-focus />
  6. </view>
  7. <view class="form-item">
  8. <input class="input" v-model="address.telNumber" maxlength="15" type="number" placeholder="手机号码" />
  9. </view>
  10. <view class="form-item">
  11. <input class="input" v-model="address.full_region" disabled="true" @tap="chooseRegion" placeholder="省份、城市、区县" />
  12. </view>
  13. <view class="form-item">
  14. <input class="input" v-model="address.detailInfo" placeholder="详细地址, 如街道、楼盘号等" />
  15. </view>
  16. <view class="form-default">
  17. <text @tap="bindIsDefault" :class="'default-input '+(address.is_default == 1 ? 'selected' : '')">设为默认地址</text>
  18. </view>
  19. </view>
  20. <view class="btns">
  21. <button class="cannel" @tap="cancelAddress">取消</button>
  22. <button class="save" @tap="saveAddress">保存</button>
  23. </view>
  24. <view class="region-select" v-if="openSelectRegion">
  25. <view class="hd">
  26. <view class="region-selected">
  27. <view :class="'item '+(item.id == 0 ? 'disabled ' : ' ') + ((regionType -1) === index ? 'selected' : '')" @tap="selectRegionType"
  28. :data-region-type-index="index" v-for="(item, index) in selectRegionList" :key="item.id">{{item.name}}</view>
  29. </view>
  30. <view :class="'done '+(selectRegionDone ? '' : 'disabled')" @tap="doneSelectRegion">确定</view>
  31. </view>
  32. <view class="bd">
  33. <scroll-view scroll-y="true" class="region-list">
  34. <view :class="'item ' + (item.selected ? 'selected' : '')" @tap="selectRegion" :data-region-index="index" v-for="(item, index) in regionList"
  35. :key="item.id">{{item.name}}</view>
  36. </scroll-view>
  37. </view>
  38. </view>
  39. <view class="bg-mask" @tap="cancelSelectRegion" v-if="openSelectRegion"></view>
  40. </view>
  41. </template>
  42. <script>
  43. const util = require("@/utils/util.js");
  44. const api = require('@/utils/api.js');
  45. export default {
  46. data() {
  47. return {
  48. address: {
  49. id: 0,
  50. province_id: 0,
  51. city_id: 0,
  52. district_id: 0,
  53. address: '',
  54. full_region: '',
  55. userName: '',
  56. telNumber: '',
  57. is_default: 0
  58. },
  59. addressId: 0,
  60. openSelectRegion: false,
  61. selectRegionList: [{
  62. id: 0,
  63. name: '省份',
  64. parent_id: 1,
  65. type: 1
  66. },
  67. {
  68. id: 0,
  69. name: '城市',
  70. parent_id: 1,
  71. type: 2
  72. },
  73. {
  74. id: 0,
  75. name: '区县',
  76. parent_id: 1,
  77. type: 3
  78. }
  79. ],
  80. regionType: 1,
  81. regionList: [],
  82. selectRegionDone: false
  83. }
  84. },
  85. methods: {
  86. bindIsDefault() {
  87. let address = this.address;
  88. address.is_default = !address.is_default;
  89. this.address = address;
  90. },
  91. getAddressDetail() {
  92. let that = this;
  93. util.request(api.AddressDetail, {
  94. id: that.addressId
  95. }).then(function(res) {
  96. if (res.errno === 0) {
  97. if (res.data) {
  98. that.address = res.data
  99. }
  100. }
  101. });
  102. },
  103. setRegionDoneStatus() {
  104. let that = this;
  105. let doneStatus = that.selectRegionList.every(item => {
  106. return item.id != 0;
  107. });
  108. that.selectRegionDone = doneStatus
  109. },
  110. chooseRegion() {
  111. let that = this;
  112. that.openSelectRegion = !that.openSelectRegion;
  113. //设置区域选择数据
  114. let address = that.address;
  115. if (address.province_id > 0 && address.city_id > 0 && address.district_id > 0) {
  116. let selectRegionList = that.selectRegionList;
  117. selectRegionList[0].id = address.province_id;
  118. selectRegionList[0].name = address.province_name;
  119. selectRegionList[0].parent_id = 1;
  120. selectRegionList[1].id = address.city_id;
  121. selectRegionList[1].name = address.city_name;
  122. selectRegionList[1].parent_id = address.province_id;
  123. selectRegionList[2].id = address.district_id;
  124. selectRegionList[2].name = address.district_name;
  125. selectRegionList[2].parent_id = address.city_id;
  126. that.selectRegionList = selectRegionList
  127. that.regionType = 3
  128. that.getRegionList(address.city_id);
  129. } else {
  130. that.selectRegionList = [{
  131. id: 0,
  132. name: '省份',
  133. parent_id: 1,
  134. type: 1
  135. }, {
  136. id: 0,
  137. name: '城市',
  138. parent_id: 1,
  139. type: 2
  140. }, {
  141. id: 0,
  142. name: '区县',
  143. parent_id: 1,
  144. type: 3
  145. }]
  146. that.regionType = 1
  147. that.getRegionList(1);
  148. }
  149. that.setRegionDoneStatus();
  150. },
  151. selectRegionType(event) {
  152. let that = this;
  153. let regionTypeIndex = event.target.dataset.regionTypeIndex;
  154. let selectRegionList = that.selectRegionList;
  155. //判断是否可点击
  156. if (regionTypeIndex + 1 == that.regionType || (regionTypeIndex - 1 >= 0 && selectRegionList[regionTypeIndex - 1].id <=
  157. 0)) {
  158. return false;
  159. }
  160. that.regionType = regionTypeIndex + 1
  161. let selectRegionItem = selectRegionList[regionTypeIndex];
  162. this.getRegionList(selectRegionItem.parent_id);
  163. this.setRegionDoneStatus();
  164. },
  165. selectRegion(event) {
  166. let that = this;
  167. let regionIndex = event.target.dataset.regionIndex;
  168. let regionItem = that.regionList[regionIndex];
  169. let regionType = regionItem.type;
  170. let selectRegionList = that.selectRegionList;
  171. selectRegionList[regionType - 1] = regionItem;
  172. that.selectRegionList = selectRegionList
  173. if (regionType != 3) {
  174. that.regionType = regionType + 1
  175. that.getRegionList(regionItem.id);
  176. }
  177. //重置下级区域为空
  178. selectRegionList.map((item, index) => {
  179. if (index > regionType - 1) {
  180. item.id = 0;
  181. item.name = index == 1 ? '城市' : '区县';
  182. item.parent_id = 0;
  183. }
  184. return item;
  185. });
  186. that.selectRegionList = selectRegionList
  187. that.regionList = that.regionList.map(item => {
  188. //标记已选择的
  189. if (that.regionType == item.type && that.selectRegionList[that.regionType - 1].id == item.id) {
  190. item.selected = true;
  191. } else {
  192. item.selected = false;
  193. }
  194. return item;
  195. })
  196. this.setRegionDoneStatus();
  197. },
  198. doneSelectRegion() {
  199. if (this.selectRegionDone === false) {
  200. return false;
  201. }
  202. let address = this.address;
  203. let selectRegionList = this.selectRegionList;
  204. address.province_id = selectRegionList[0].id;
  205. address.city_id = selectRegionList[1].id;
  206. address.district_id = selectRegionList[2].id;
  207. address.province_name = selectRegionList[0].name;
  208. address.city_name = selectRegionList[1].name;
  209. address.district_name = selectRegionList[2].name;
  210. address.full_region = selectRegionList.map(item => {
  211. return item.name;
  212. }).join('');
  213. this.address = address
  214. this.openSelectRegion = false
  215. },
  216. cancelSelectRegion() {
  217. this.regionType = this.regionDoneStatus ? 3 : 1
  218. this.openSelectRegion = false
  219. },
  220. getRegionList(regionId) {
  221. let that = this;
  222. let regionType = that.regionType;
  223. util.request(api.RegionList, {
  224. parentId: regionId
  225. }).then(function(res) {
  226. if (res.errno === 0) {
  227. that.regionList = res.data.map(item => {
  228. //标记已选择的
  229. if (regionType == item.type && that.selectRegionList[regionType - 1].id == item.id) {
  230. item.selected = true;
  231. } else {
  232. item.selected = false;
  233. }
  234. return item;
  235. })
  236. }
  237. });
  238. },
  239. cancelAddress() {
  240. uni.navigateBack({
  241. url: '/pages/shopping/address/address',
  242. })
  243. },
  244. saveAddress() {
  245. let address = this.address;
  246. if (address.userName == '') {
  247. util.toast('请输入姓名');
  248. return false;
  249. }
  250. if (address.telNumber == '') {
  251. util.toast('请输入手机号码');
  252. return false;
  253. }
  254. if (address.district_id == 0) {
  255. util.toast('请输入省市区');
  256. return false;
  257. }
  258. if (address.detailInfo == '') {
  259. util.toast('请输入详细地址');
  260. return false;
  261. }
  262. let that = this;
  263. util.request(api.AddressSave, {
  264. id: address.id,
  265. userName: address.userName,
  266. telNumber: address.telNumber,
  267. province_id: address.province_id,
  268. city_id: address.city_id,
  269. district_id: address.district_id,
  270. is_default: address.is_default,
  271. provinceName: address.province_name,
  272. cityName: address.city_name,
  273. countyName: address.district_name,
  274. detailInfo: address.detailInfo,
  275. }, 'POST', 'application/json').then(function(res) {
  276. if (res.errno === 0) {
  277. uni.navigateBack({
  278. url: '/pages/shopping/address/address',
  279. })
  280. }
  281. });
  282. }
  283. },
  284. onLoad: function(options) {
  285. // 页面初始化 options为页面跳转所带来的参数
  286. if (options.id != '' && options.id != 0) {
  287. this.addressId = options.id;
  288. this.getAddressDetail();
  289. }
  290. this.getRegionList(1);
  291. }
  292. }
  293. </script>
  294. <style lang="scss">
  295. page {
  296. height: 100%;
  297. background: #f4f4f4;
  298. }
  299. .add-address .add-form {
  300. background: #fff;
  301. width: 100%;
  302. height: auto;
  303. overflow: hidden;
  304. }
  305. .add-address .form-item {
  306. height: 116rpx;
  307. padding-left: 31.25rpx;
  308. border-bottom: 1px solid #d9d9d9;
  309. display: flex;
  310. align-items: center;
  311. padding-right: 31.25rpx;
  312. }
  313. .add-address .input {
  314. flex: 1;
  315. height: 44rpx;
  316. line-height: 44rpx;
  317. overflow: hidden;
  318. }
  319. .add-address .form-default {
  320. border-bottom: 1px solid #d9d9d9;
  321. height: 96rpx;
  322. background: #fafafa;
  323. padding-top: 28rpx;
  324. font-size: 28rpx;
  325. }
  326. .default-input {
  327. margin: 0 auto;
  328. display: block;
  329. width: 240rpx;
  330. height: 40rpx;
  331. padding-left: 50rpx;
  332. line-height: 40rpx;
  333. background: url(http://yanxuan.nosdn.127.net/hxm/yanxuan-wap/p/20161201/style/img/sprites/checkbox-sed825af9d3-a6b8540d42.png) 1rpx -448rpx no-repeat;
  334. background-size: 38rpx 486rpx;
  335. font-size: 28rpx;
  336. }
  337. .default-input.selected {
  338. background: url(http://yanxuan.nosdn.127.net/hxm/yanxuan-wap/p/20161201/style/img/sprites/checkbox-sed825af9d3-a6b8540d42.png) 0 -192rpx no-repeat;
  339. background-size: 38rpx 486rpx;
  340. }
  341. .add-address .btns {
  342. position: fixed;
  343. bottom: 0;
  344. left: 0;
  345. overflow: hidden;
  346. display: flex;
  347. height: 100rpx;
  348. width: 100%;
  349. }
  350. .add-address .cannel,
  351. .add-address .save {
  352. flex: 1;
  353. height: 100rpx;
  354. text-align: center;
  355. line-height: 100rpx;
  356. font-size: 28rpx;
  357. color: #fff;
  358. border: none;
  359. border-radius: 0;
  360. }
  361. .add-address .cannel {
  362. background: #333;
  363. }
  364. .add-address .save {
  365. background: #b4282d;
  366. }
  367. .region-select {
  368. width: 100%;
  369. height: 600rpx;
  370. background: #fff;
  371. position: fixed;
  372. z-index: 10;
  373. left: 0;
  374. bottom: 0;
  375. }
  376. .region-select .hd {
  377. height: 108rpx;
  378. width: 100%;
  379. border-bottom: 1px solid #f4f4f4;
  380. padding: 46rpx 30rpx 0 30rpx;
  381. }
  382. .region-select .region-selected {
  383. float: left;
  384. height: 60rpx;
  385. display: flex;
  386. }
  387. .region-select .region-selected .item {
  388. max-width: 140rpx;
  389. margin-right: 30rpx;
  390. text-align: left;
  391. line-height: 60rpx;
  392. height: 100%;
  393. color: #333;
  394. font-size: 28rpx;
  395. overflow: hidden;
  396. text-overflow: ellipsis;
  397. white-space: nowrap;
  398. }
  399. .region-select .region-selected .item.disabled {
  400. color: #999;
  401. }
  402. .region-select .region-selected .item.selected {
  403. color: #b4282d;
  404. }
  405. .region-select .done {
  406. float: right;
  407. height: 60rpx;
  408. width: 60rpx;
  409. border: none;
  410. background: #fff;
  411. line-height: 60rpx;
  412. text-align: center;
  413. color: #333;
  414. font-size: 28rpx;
  415. }
  416. .region-select .done.disabled {
  417. color: #999;
  418. }
  419. .region-select .bd {
  420. height: 492rpx;
  421. width: 100%;
  422. padding: 0 30rpx;
  423. }
  424. .region-select {
  425. height: auto;
  426. overflow: scroll;
  427. }
  428. .region-list {
  429. width: 100%;
  430. height: 100%;
  431. line-height: 104rpx;
  432. text-align: left;
  433. color: #333;
  434. font-size: 28rpx;
  435. }
  436. .region-select .item {
  437. width: 100%;
  438. height: 104rpx;
  439. line-height: 104rpx;
  440. text-align: left;
  441. color: #333;
  442. font-size: 28rpx;
  443. }
  444. .region-select .region-list .item.selected {
  445. color: #b4282d;
  446. }
  447. .bg-mask {
  448. height: 100%;
  449. width: 100%;
  450. background: rgba(0, 0, 0, 0.4);
  451. position: fixed;
  452. top: 0;
  453. left: 0;
  454. z-index: 8;
  455. }
  456. </style>