addAddress.vue 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. <template>
  2. <view class="content">
  3. <view class="inner">
  4. <view class="address-box">
  5. <textarea class="textarea" v-model="content" placeholder="请粘贴或输入文本,点击'识别'自动识别姓名、电话、地址,格式:深圳市龙岗区坂田街道长坑路西2巷2号202 黄大大 18888888888" />
  6. <view class="btns" >
  7. <view class="btn parse" @click="parseAddress()">识别</view>
  8. </view>
  9. </view>
  10. <view class="form-box">
  11. <view class="form-item">
  12. <text class="label">联系人</text>
  13. <input type="text" v-model="form.realName" maxlength="10" placeholder="姓名" class="form-input" />
  14. </view>
  15. <view class="form-item">
  16. <text class="label">手机号</text>
  17. <input type="number" v-model="form.phone" maxlength="11" placeholder="手机号" class="form-input" />
  18. </view>
  19. <view class="form-item">
  20. <text class="label">所在地区</text>
  21. <picker :value="multiIndex" class="birth-picker" mode="multiSelector" range-key="n" :range="addressList" @change="pickerChange" @columnchange="pickerColumnchange">
  22. <view class="right-box">
  23. <view class="input-box">
  24. <input type="text" v-model="form.address" placeholder="请选择" class="form-input" disabled="disabled" />
  25. </view>
  26. <image class="arrow" src="../../static/images/arrow_gray.png" mode=""></image>
  27. </view>
  28. </picker>
  29. </view>
  30. <view class="form-item">
  31. <text class="label">详细地址</text>
  32. <textarea class="form-textarea" v-model="form.detail" placeholder="请输入详细地址" />
  33. </view>
  34. </view>
  35. <!-- 设为默认地址 -->
  36. <view class="setting-box">
  37. <text class="label">设为默认地址</text>
  38. <evan-switch v-model="isDefault" activeColor="#018C39" inactiveColor="rgba(0, 0, 0, 0.1)"></evan-switch>
  39. </view>
  40. </view>
  41. <view class="btn-box">
  42. <view class="sub-btn" @click="submit()">保存地址</view>
  43. </view>
  44. </view>
  45. </template>
  46. <script>
  47. import {parseAddress,getCity,getCitys,getAddressById,addAddress,editAddress} from '@/api/address'
  48. import EvanSwitch from '@/components/evan-switch/evan-switch.vue'
  49. export default {
  50. components: {
  51. EvanSwitch
  52. },
  53. data() {
  54. return {
  55. content:null,
  56. type:null,
  57. id:null,
  58. isDefault: false,
  59. addressList:[[],[],[]],
  60. multiIndex:[0,0,0],
  61. address:[],
  62. form: {
  63. realName:null,
  64. phone:null,
  65. detail:null,
  66. address: null,
  67. isDefault: 0,
  68. }
  69. }
  70. },
  71. onLoad(option) {
  72. this.type=option.type;
  73. if(this.type=='edit'){
  74. uni.setNavigationBarTitle({
  75.   title:"修改收货地址"
  76. })
  77. this.id=option.id;
  78. this.getAddressById();
  79. }
  80. else{
  81. uni.setNavigationBarTitle({
  82.   title:"新增收货地址"
  83. })
  84. }
  85. this.getCitys()
  86. },
  87. methods: {
  88. parseAddress(){
  89. if(this.content==null||this.content==""){
  90. uni.showToast({ icon:'none',title: '请输入地址信息'});
  91. return;
  92. }
  93. var data={content:this.content};
  94. parseAddress(data).then(
  95. res => {
  96. if(res.code==200){
  97. this.form.realName=res.data.name
  98. this.form.phone=res.data.mobile
  99. this.form.address=res.data.provinceName+res.data.cityName+res.data.expAreaName
  100. this.form.province=res.data.provinceName
  101. this.form.city=res.data.cityName
  102. this.form.district=res.data.expAreaName
  103. this.form.detail=res.data.streetName+res.data.address
  104. }else{
  105. uni.showToast({
  106. icon:'none',
  107. title: res.msg,
  108. });
  109. }
  110. },
  111. rej => {}
  112. );
  113. },
  114. getAddressById(){
  115. var data={id:this.id};
  116. getAddressById(data).then(
  117. res => {
  118. if(res.code==200){
  119. this.form=res.data;
  120. this.isDefault=this.form.isDefault==1?true:false;
  121. this.form.address=this.form.province+this.form.city+this.form.district
  122. }else{
  123. uni.showToast({
  124. icon:'none',
  125. title: res.msg,
  126. });
  127. }
  128. },
  129. rej => {}
  130. );
  131. },
  132. submit(){
  133. if(this.type=="add"){
  134. this.addAddress()
  135. }
  136. else if(this.type=="edit"){
  137. this.editAddress()
  138. }
  139. },
  140. editAddress(){
  141. this.form.isDefault=this.isDefault?1:0
  142. editAddress(this.form).then(
  143. res => {
  144. if(res.code==200){
  145. uni.showToast({
  146. icon:'success',
  147. title: "操作成功",
  148. });
  149. setTimeout(function() {
  150. uni.$emit('refreshAddress');
  151. uni.navigateBack({
  152. delta: 1
  153. })
  154. }, 500);
  155. }else{
  156. uni.showToast({
  157. icon:'none',
  158. title: res.msg,
  159. });
  160. }
  161. },
  162. rej => {}
  163. );
  164. },
  165. addAddress(){
  166. this.form.isDefault=this.isDefault?1:0
  167. addAddress(this.form).then(
  168. res => {
  169. if(res.code==200){
  170. uni.showToast({
  171. icon:'success',
  172. title: "操作成功",
  173. });
  174. setTimeout(function() {
  175. uni.$emit('refreshAddress');
  176. uni.navigateBack({
  177. delta: 1
  178. })
  179. }, 500);
  180. }else{
  181. uni.showToast({
  182. icon:'none',
  183. title: res.msg,
  184. });
  185. }
  186. },
  187. rej => {}
  188. );
  189. },
  190. // 地区选择
  191. pickerChange(e) {
  192. this.multiIndex = e.detail.value;
  193. // 数组内的下标
  194. // 获取一级类目
  195. // 获取二级类目
  196. // 获取三级类目
  197. this.form.address=this.addressList[0][this.multiIndex[0]].n+this.addressList[1][this.multiIndex[1]].n+this.addressList[2][this.multiIndex[2]].n
  198. this.form.province=this.addressList[0][this.multiIndex[0]].n
  199. this.form.city=this.addressList[1][this.multiIndex[1]].n
  200. this.form.district=this.addressList[2][this.multiIndex[2]].n
  201. this.form.cityId=this.addressList[1][this.multiIndex[1]].v;
  202. },
  203. pickerColumnchange(e){
  204. // 第一列滑动
  205. if(e.detail.column === 0){
  206. this.multiIndex[0] = e.detail.value
  207. // console.log('第一列滑动');
  208. this.addressList[1] = this.address[this.multiIndex[0]].c;
  209. this.addressList[2] = this.address[this.multiIndex[0]].c[0].c
  210. // 第一列滑动 第二列 和第三列 都变为第一个
  211. this.multiIndex.splice(1, 1, 0)
  212. this.multiIndex.splice(2, 1, 0)
  213. }
  214. // 第二列滑动
  215. if(e.detail.column === 1){
  216. this.multiIndex[1] = e.detail.value
  217. // console.log('第二列滑动');
  218. // console.log(this.multiIndex)
  219. this.addressList[2] = this.address[this.multiIndex[0]].c[this.multiIndex[1]].c
  220. // 第二列 滑动 第三列 变成第一个
  221. this.multiIndex.splice(2, 1, 0)
  222. }
  223. // 第三列滑动
  224. if(e.detail.column === 2){
  225. this.multiIndex[2] = e.detail.value
  226. }
  227. },
  228. getCitys(){
  229. getCitys().then(
  230. res => {
  231. if(res.code==200){
  232. this.address=res.data
  233. for(var i=0; i<this.address.length; i++){
  234. this.addressList[0].push(this.address[i])
  235. }
  236. for(var i=0; i<this.address[0].c.length; i++){
  237. this.addressList[1].push(this.address[0].c[i])
  238. }
  239. for(var i=0; i<this.address[0].c[0].c.length; i++){
  240. this.addressList[2].push(this.address[0].c[0].c[i])
  241. }
  242. }else{
  243. uni.showToast({
  244. icon:'none',
  245. title: "请求失败",
  246. });
  247. }
  248. },
  249. rej => {}
  250. );
  251. }
  252. }
  253. }
  254. </script>
  255. <style lang="scss">
  256. page{
  257. height: 100%;
  258. }
  259. .content{
  260. height: 100%;
  261. display: flex;
  262. flex-direction: column;
  263. justify-content: space-between;
  264. .inner{
  265. height: calc(100% - 120upx);
  266. padding: 20upx;
  267. .address-box{
  268. padding: 30upx;
  269. background: #FFFFFF;
  270. border-radius: 16upx;
  271. margin-bottom: 20upx;
  272. position: relative;
  273. .textarea{
  274. width: 100%;
  275. height: 200upx;
  276. font-size: 30upx;
  277. color: #999999;
  278. padding-bottom:100rpx;
  279. }
  280. .btns{
  281. right:10rpx;
  282. bottom:10rpx;
  283. position: absolute;
  284. .btn{
  285. width: 155upx;
  286. height: 64upx;
  287. line-height: 64upx;
  288. font-size: 26upx;
  289. font-family: PingFang SC;
  290. font-weight: 500;
  291. text-align: center;
  292. border-radius: 32upx;
  293. &.parse{
  294. background: #018C39;
  295. color: #FFFFFF;
  296. }
  297. }
  298. }
  299. }
  300. .form-box{
  301. padding: 0 30upx;
  302. background: #FFFFFF;
  303. border-radius: 16upx;
  304. .form-item{
  305. padding: 30upx 0;
  306. display: flex;
  307. align-items: flex-start;
  308. border-bottom: 1px solid #F1F1F1;
  309. &:last-child{
  310. border-bottom: none;
  311. }
  312. .label{
  313. width: 180upx;
  314. text-align: left;
  315. font-size: 30upx;
  316. line-height: 44upx;
  317. font-family: PingFang SC;
  318. font-weight: 500;
  319. color: #222222;
  320. flex-shrink: 0;
  321. }
  322. input{
  323. text-align: left;
  324. }
  325. .form-input{
  326. font-size: 30upx;
  327. font-family: PingFang SC;
  328. font-weight: 500;
  329. color: #999999;
  330. text-align: left;
  331. }
  332. .form-textarea{
  333. font-size: 30upx;
  334. color: #999999;
  335. height: 100upx;
  336. padding: 4upx 0;
  337. }
  338. .birth-picker {
  339. flex: 1;
  340. display: flex;
  341. align-items: center;
  342. .right-box{
  343. width: 100%;
  344. display: flex;
  345. align-items: center;
  346. .input-box{
  347. width: 470upx;
  348. }
  349. .arrow{
  350. width: 13upx;
  351. height: 23upx;
  352. margin-left: 20upx;
  353. }
  354. }
  355. }
  356. }
  357. }
  358. .setting-box{
  359. height: 88upx;
  360. background: #FFFFFF;
  361. border-radius: 16upx;
  362. margin-top: 20upx;
  363. padding: 0 30upx;
  364. display: flex;
  365. align-items: center;
  366. justify-content: space-between;
  367. .label{
  368. font-size: 28upx;
  369. font-family: PingFang SC;
  370. font-weight: 500;
  371. color: #111111;
  372. }
  373. }
  374. }
  375. .btn-box{
  376. height: 120upx;
  377. padding: 0 30upx;
  378. display: flex;
  379. align-items: center;
  380. justify-content: center;
  381. background: #FFFFFF;
  382. .sub-btn{
  383. width: 100%;
  384. height: 88upx;
  385. line-height: 88upx;
  386. text-align: center;
  387. font-size: 30upx;
  388. font-family: PingFang SC;
  389. font-weight: bold;
  390. color: #FFFFFF;
  391. background: #018C39;
  392. border-radius: 44upx;
  393. }
  394. }
  395. }
  396. </style>