add-address.vue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <!-- 添加收货地址 -->
  2. <template>
  3. <view class="wrap">
  4. <view class="list">
  5. <view class="item">
  6. <view class="label">
  7. 收货人
  8. </view>
  9. <input type="text" v-model="params.name" class="input" placeholder="请填写收货人姓名" placeholder-class="placeholder" />
  10. </view>
  11. <view class="item">
  12. <view class="label">
  13. 手机号码
  14. </view>
  15. <input type="text" v-model="params.phone" class="input" placeholder="请填写手机号码"
  16. placeholder-class="placeholder" />
  17. </view>
  18. <view class="item">
  19. <view class="label">
  20. 所在地区
  21. </view>
  22. <picker
  23. class="picker"
  24. mode="multiSelector"
  25. range-key="short_name"
  26. @change="bindPickerChange"
  27. @columnchange="bindColumnchange"
  28. :value="addressPickerValue"
  29. :range="addressPickerData"
  30. >
  31. <input
  32. class="input"
  33. disabled
  34. :value="addressPickerValueStr"
  35. placeholder="省、市、区、街道"
  36. placeholder-class="placeholder" />
  37. </picker>
  38. <image src="../../../static/back-icon2.png" class="back-icon" mode="widthFix"></image>
  39. </view>
  40. <view class="item-other">
  41. <view class="label">
  42. 详细地址
  43. </view>
  44. <textarea class="textarea" v-model="params.detail" placeholder="小区楼栋/乡村名称"
  45. placeholder-class="placeholder" />
  46. </view>
  47. <view class="item">
  48. <view class="row-between">
  49. 设为默认地址
  50. <switch :checked="select" style="transform: scale(0.5);" color="#F2501A" @change="switchChange" />
  51. </view>
  52. </view>
  53. </view>
  54. <view class="btn-box">
  55. <button type="default" class="active" @tap="setDeliveryAddress()">保存</button>
  56. </view>
  57. </view>
  58. </template>
  59. <script>
  60. import {
  61. areaGetAreaChildrenByID,
  62. userManageGetDeliveryAddressDetail,
  63. userManageSetDeliveryAddress
  64. } from '../../../common/service.js';
  65. import { validatorFun } from '../../../common/utils/util';
  66. export default {
  67. data() {
  68. return {
  69. select: false,
  70. params: {
  71. id: 0, // 收货地址记录id(不传值是添加)
  72. pro_name: '', // 省名
  73. city_name: '', // 市名
  74. county_name: '', // 县区名
  75. street_name: '', // 街道
  76. detail: '', // 详细地址
  77. phone: '', // 联系电话
  78. name: '', // 联系人
  79. is_mr: 0, // 是否是默认(0不默认,1默认)
  80. },
  81. // 地址
  82. addressPickerData: [[], [], [], []],
  83. addressPickerDataNames: [[], [], [], []],
  84. addressPickerValue: [0, 0, 0, 0],
  85. addressPickerValueStr: '',
  86. }
  87. },
  88. onLoad(option) {
  89. const id = parseInt(option.id || 0);
  90. if (id > 0) {
  91. this.params.id = id;
  92. this.getDetail();
  93. } else {
  94. this.getAreaChildrenByID();
  95. }
  96. },
  97. methods: {
  98. setDeliveryAddress() {
  99. const params = this.params;
  100. const errList = validatorFun(params, [
  101. ['name', ['notNull', '请输入收货人']],
  102. ['phone', ['notNull', '请输入手机号码'], ['isMobile', '请输入正确手机号']],
  103. ['pro_name', ['notNull', '请输入选择所在地区']],
  104. ['city_name', ['notNull', '请输入选择所在地区']],
  105. ['county_name', ['notNull', '请输入选择所在地区']],
  106. ['street_name', ['notNull', '请输入选择所在地区']],
  107. ['detail', ['notNull', '请输入详细地址']],
  108. ]);
  109. if (errList.length > 0) {
  110. return uni.showToast({
  111. icon: 'none',
  112. title: errList[0].errMsg,
  113. });
  114. }
  115. userManageSetDeliveryAddress({
  116. data: params,
  117. success: ({code, msg, data}) => {
  118. if (code == 1) {
  119. uni.navigateBack({
  120. delta: 1,
  121. });
  122. }
  123. uni.showToast({
  124. icon: 'none',
  125. title: msg,
  126. });
  127. }
  128. });
  129. },
  130. getDetail() {
  131. userManageGetDeliveryAddressDetail({
  132. data: { id: this.params.id },
  133. success: ({code, msg, data})=> {
  134. if (code == 1) {
  135. const {
  136. id,
  137. pro_id,
  138. city_id,
  139. county_id,
  140. pro_name,
  141. city_name,
  142. county_name,
  143. street_name,
  144. detail,
  145. phone,
  146. name,
  147. is_mr,
  148. } = data;
  149. this.params = {
  150. id,
  151. pro_name,
  152. city_name,
  153. county_name,
  154. street_name,
  155. detail,
  156. phone,
  157. name,
  158. is_mr,
  159. };
  160. const arr = [
  161. pro_name,
  162. city_name,
  163. county_name,
  164. street_name,
  165. ];
  166. this.select = is_mr == 1;
  167. this.addressPickerValueStr = arr.join(' ');
  168. let count = 0;
  169. const fn = (id2 = 0, idx2, level = 1) => {
  170. const name = arr.splice(0, 1)[0];
  171. this.getAreaChildrenByID(id2, idx2, level, level, () => {
  172. const idx = this.addressPickerDataNames[level - 1].indexOf(name);
  173. if ( arr.length > 0 ) {
  174. let id3 = this.addressPickerData[level - 1][idx].id;
  175. fn(id3, idx, level + 1);
  176. } else {
  177. this.getAreaChildrenByID(0, idx, level + 1, level);
  178. }
  179. });
  180. }
  181. fn();
  182. } else {
  183. uni.showToast({
  184. icon: 'none',
  185. title: msg,
  186. });
  187. }
  188. }
  189. });
  190. },
  191. switchChange(e) {
  192. this.select = e.detail.value;
  193. this.params.is_mr = e.detail.value ? 1 : 0;
  194. },
  195. getAreaChildrenByID(id = 0, idx = 0, level = 1, maxLevel = 4, callback) {
  196. const list = [...this.addressPickerData];
  197. const listIds = [...this.addressPickerDataNames];
  198. const vals = [...this.addressPickerValue];
  199. vals[level - 2] = idx;
  200. const fn = (id = 0, level2 = 1) => {
  201. if (level2 > maxLevel) {
  202. this.$set(this, 'addressPickerData', list);
  203. this.$set(this, 'addressPickerDataNames', listIds);
  204. this.$set(this, 'addressPickerValue', vals);
  205. callback && callback();
  206. return;
  207. }
  208. areaGetAreaChildrenByID({
  209. data: {id},
  210. success: ({code, msg, data}) => {
  211. if (code == 1) {
  212. list[level2 - 1] = data.list;
  213. listIds[level2 - 1] = data.list.map(item => item.name);
  214. vals[level2 - 1] = 0;
  215. fn(data.list[0].id, level2 + 1);
  216. } else {
  217. uni.showToast({
  218. icon: 'none',
  219. title: msg,
  220. });
  221. }
  222. }
  223. });
  224. };
  225. fn(id, level);
  226. },
  227. selectedDefault: function() {
  228. this.select = !this.select;
  229. },
  230. bindPickerChange(e) {
  231. const [pIdx, cIdx, aIdx, sIdx] = e.detail.value;
  232. const [province, city, area, street] = this.addressPickerData;
  233. this.addressPickerValue = e.detail.value;
  234. this.addressPickerValueStr = `${province[pIdx].name} ${city[cIdx].name} ${area[aIdx].name} ${street[sIdx].name}`;
  235. this.params.pro_name = province[pIdx].name;
  236. this.params.city_name = city[cIdx].name;
  237. this.params.county_name = area[aIdx].name;
  238. this.params.street_name= street[sIdx].name;
  239. },
  240. bindColumnchange(e) {
  241. const {column, value} = e.detail;
  242. this.getAreaChildrenByID(this.addressPickerData[column][value].id, value, column + 2);
  243. },
  244. }
  245. }
  246. </script>
  247. <style scoped lang="scss">
  248. @import "./add-address.css";
  249. </style>