add-address.vue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <template>
  2. <view>
  3. <!--标题栏-->
  4. <bar-title bgColor="bg-white" isBack :title="id?'编辑地址': '新增地址'">
  5. <block slot="content">{{id?'编辑地址': '新增地址'}}</block>
  6. </bar-title>
  7. <!--表单-->
  8. <view class="cu-form-group margin-top">
  9. <view class="title">收货人名</view>
  10. <input v-model="name" placeholder="您的姓名"/>
  11. </view>
  12. <view class="cu-form-group">
  13. <view class="title">手机号码</view>
  14. <input v-model="phone" placeholder="卖家和快递员联系您的方式"/>
  15. </view>
  16. <view class="cu-form-group">
  17. <view class="title">所在地区</view>
  18. <picker mode="multiSelector" @columnchange="bindMultiPickerColumnChange" :value="areasIndex" range-key="name" :range="areas" @change="multChange">
  19. <view class="picker">
  20. <block v-for="(item,index) in areas" :key="index" v-if="index < 3">
  21. {{areas[index] && areas[index][areasIndex[index]] && areas[index][areasIndex[index]].name}}
  22. <block v-if="index < areas.length - 1">,</block>
  23. </block>
  24. </view>
  25. </picker>
  26. </view>
  27. <view class="cu-form-group">
  28. <view class="title">详细地址</view>
  29. <input v-model="address" placeholder="请输入详细的地址信息"/>
  30. </view>
  31. <view class="bg-white wecanui-footer-fixed foot-pb">
  32. <view class="flex flex-direction">
  33. <button class="cu-btn bg-red" @click="submit">提交保存</button>
  34. </view>
  35. </view>
  36. </view>
  37. </template>
  38. <script>
  39. import barTitle from '@/components/zaiui-common/basics/bar-title';
  40. import _tool from '@/static/zaiui/util/tools.js'; //工具函数
  41. export default {
  42. components: {
  43. barTitle,
  44. },
  45. data() {
  46. return {
  47. name: '',
  48. phone: '',
  49. id: '',
  50. address: '',
  51. addressShow: false,
  52. areas: [[{name: '省份选择', value: '-1'}],[{name: '城市选择', value: '-1'}],[{name: '县区选择', value: '-1'}]],
  53. areasIndex: [0,0,0],
  54. roleDeep: 4,
  55. type: 2
  56. }
  57. },
  58. onLoad(options) {
  59. this.id = options.id || '';
  60. if(!this.id) {
  61. this.loadAreas(0, 1);
  62. return;
  63. }
  64. this.reqByuser({
  65. url: '?l=address.getInfo',
  66. data: {
  67. id: this.id
  68. },
  69. success: res => {
  70. if(res.data.status == 1) {
  71. let data = res.data.data;
  72. this.phone = data.phone;
  73. this.address = data.address;
  74. this.city = data.city;
  75. this.type = data.type;
  76. this.name = data.name;
  77. this.initAreasIndex([data.province_id, data.city_id, data.county_id])
  78. }
  79. }
  80. })
  81. },
  82. onReady() {
  83. _tool.setBarColor(true);
  84. uni.pageScrollTo({
  85. scrollTop: 0,
  86. duration: 0
  87. });
  88. },
  89. methods: {
  90. initAreasIndex(areas) {
  91. let _areas = [].concat(areas);
  92. let loop = (id, level) => {
  93. this.loadAreas(id, level, 0, res => {
  94. let index = res.findIndex(item => areas[level-1] == item.id);
  95. if(level == 1) {
  96. index++;
  97. }
  98. this.areasIndex[level-1] = Math.max(0, index);
  99. let nextId = _areas.shift();
  100. if(nextId && level<3) {
  101. loop(nextId, level+1)
  102. } else {}
  103. }, 1);
  104. }
  105. loop(0,1)
  106. },
  107. loadAreas(parent, level, next, callback) {
  108. if(level > 4) return;
  109. this.req({
  110. url: 'http://center.yuandaibao.com/package/area/?l=data.get',
  111. data: {
  112. level,
  113. parent,
  114. },
  115. success: res => {
  116. if(res.data.status == 1) {
  117. let data = res.data.data;
  118. if(level-1 < 3) {
  119. let list = data.list;
  120. if(parent == 0) {
  121. list = [{name: '省份选择', value: '-1'}, ...list]
  122. }
  123. this.areas.splice(level-1,1,list)
  124. }
  125. typeof callback == 'function' && callback(data.list)
  126. if(next && level < this.roleDeep - 1) {
  127. // let firstNode = data.list[0].value == -1 ? data.list[1] : data.list[0];
  128. let firstNode = data.list[0];
  129. if(firstNode.id !== -1) {
  130. this.loadAreas(firstNode.id, level + 1);
  131. }
  132. }
  133. }
  134. }
  135. });
  136. },
  137. multChange(e) {
  138. let area = this.areas.map((item, index) => {
  139. return item[e.detail.value[index]] && item[e.detail.value[index]].value
  140. }).join(',');
  141. area += ',' + this.streetAreas[this.streetIndex].value;
  142. if (this.areas.length == this.roleDeep && area.indexOf('-1') == -1) {
  143. const role = this.roles[this.roleIndex];
  144. }
  145. },
  146. bindMultiPickerColumnChange(e) {
  147. // 矫正下游select index
  148. let areasIndex = this.areasIndex.map((item, index) => {
  149. if(index == e.detail.column) {
  150. return e.detail.value
  151. }
  152. if(index < e.detail.column) {
  153. return item;
  154. }
  155. return 0;
  156. });
  157. this.$set(this, 'areasIndex', areasIndex);
  158. if(e.detail.column < this.roleDeep -1) {
  159. let value = this.areas[e.detail.column][e.detail.value].id;
  160. if(e.detail.column == 0) {
  161. this.areas.splice(2,1, [{name: '县区选择', value: '-1'}])
  162. }
  163. this.streetAreas = [{name: '街道选择', value: '-1'}];
  164. this.streetIndex = 0;
  165. if(value == -1) {
  166. let arr = [].concat(this.areas);
  167. arr.splice(e.detail.column+1, 5)
  168. this.areas = arr;
  169. return;
  170. }
  171. this.loadAreas(this.areas[e.detail.column][e.detail.value].id, e.detail.column+2, 1);
  172. }
  173. },
  174. // 打开窗口
  175. selectAddress(){
  176. this.$refs.addressEl.show();
  177. },
  178. // 确认选中
  179. confirmChange(address){
  180. console.log(address)
  181. this.address = address.province + '/' + address.city + '/' + address.area;
  182. },
  183. submit() {
  184. let { name, phone, type, id, address } = this;
  185. let area = this.areas.map((item, index) => item[this.areasIndex[index]].id).join(',');
  186. // let _address = this.areas.map((item, index) => item[this.areasIndex[index]].name).join('')
  187. // _address += address;
  188. if(!name) {
  189. uni.showToast({
  190. icon:'error',
  191. title:'请填写昵称'
  192. })
  193. return;
  194. }
  195. if(!phone) {
  196. uni.showToast({
  197. icon:'error',
  198. title:'请填写手机号'
  199. })
  200. return;
  201. }
  202. if(!/1\d{10}/.test(phone)) {
  203. uni.showToast({
  204. icon:'error',
  205. title:'手机号格式错误'
  206. })
  207. return;
  208. }
  209. if(!area || area.indexOf('-1') > -1) {
  210. uni.showToast({
  211. icon:'error',
  212. title:'请填写地址'
  213. })
  214. return;
  215. }
  216. if(!address) {
  217. uni.showToast({
  218. icon:'error',
  219. title:'请填写详细地址'
  220. })
  221. return;
  222. }
  223. let data = {
  224. name,
  225. province: this.areas[0][this.areasIndex[0]].id,
  226. city: this.areas[1][this.areasIndex[1]].id,
  227. county: this.areas[2][this.areasIndex[2]].id,
  228. address,
  229. phone, type, id
  230. };
  231. this.reqByuser({
  232. url: '?l=address.up',
  233. data,
  234. success: res => {
  235. if(res.data.status == 1) {
  236. uni.showToast({
  237. icon: 'success',
  238. title: '提交成功'
  239. })
  240. uni.navigateBack({})
  241. }
  242. }
  243. })
  244. },
  245. }
  246. }
  247. </script>
  248. <style lang="scss">
  249. /* #ifdef APP-PLUS */
  250. @import "../../static/colorui/main.css";
  251. @import "../../static/colorui/icon.css";
  252. @import "../../static/zaiui/style/app.scss";
  253. /* #endif */
  254. .wecanui-footer-fixed .flex-direction {
  255. padding: 18.18upx;
  256. }
  257. </style>