addAddress.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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="#2BC7B9" 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. });
  92. return;
  93. }
  94. var data={content:this.content};
  95. parseAddress(data).then(
  96. res => {
  97. if(res.code==200){
  98. this.form.realName=res.data.name
  99. this.form.phone=res.data.mobile
  100. this.form.address=res.data.provinceName+res.data.cityName+res.data.expAreaName
  101. this.form.province=res.data.provinceName
  102. this.form.city=res.data.cityName
  103. this.form.district=res.data.expAreaName
  104. this.form.detail=res.data.streetName+res.data.address
  105. this.form.detail=this.form.detail.replace(/\s+/g,"");
  106. }else{
  107. uni.showToast({
  108. icon:'none',
  109. title: res.msg,
  110. });
  111. }
  112. },
  113. rej => {}
  114. );
  115. },
  116. getAddressById(){
  117. var data={id:this.id};
  118. getAddressById(data).then(
  119. res => {
  120. if(res.code==200){
  121. this.form=res.data;
  122. this.isDefault=this.form.isDefault==1?true:false;
  123. this.form.address=this.form.province+this.form.city+this.form.district
  124. }else{
  125. uni.showToast({
  126. icon:'none',
  127. title: res.msg,
  128. });
  129. }
  130. },
  131. rej => {}
  132. );
  133. },
  134. submit(){
  135. if(this.type=="add"){
  136. this.addAddress()
  137. }
  138. else if(this.type=="edit"){
  139. this.editAddress()
  140. }
  141. },
  142. editAddress(){
  143. this.form.isDefault=this.isDefault?1:0
  144. editAddress(this.form).then(
  145. res => {
  146. if(res.code==200){
  147. uni.showToast({
  148. icon:'success',
  149. title: "操作成功",
  150. });
  151. setTimeout(function() {
  152. uni.$emit('refreshAddress');
  153. uni.navigateBack({
  154. delta: 1
  155. })
  156. }, 500);
  157. }else{
  158. uni.showToast({
  159. icon:'none',
  160. title: res.msg,
  161. });
  162. }
  163. },
  164. rej => {}
  165. );
  166. },
  167. addAddress(){
  168. this.form.isDefault=this.isDefault?1:0
  169. addAddress(this.form).then(
  170. res => {
  171. if(res.code==200){
  172. uni.showToast({
  173. icon:'success',
  174. title: "操作成功",
  175. });
  176. setTimeout(function() {
  177. uni.$emit('refreshAddress');
  178. uni.navigateBack({
  179. delta: 1
  180. })
  181. }, 500);
  182. }else{
  183. uni.showToast({
  184. icon:'none',
  185. title: res.msg,
  186. });
  187. }
  188. },
  189. rej => {}
  190. );
  191. },
  192. // 地区选择
  193. pickerChange(e) {
  194. this.multiIndex = e.detail.value;
  195. // 数组内的下标
  196. // 获取一级类目
  197. // 获取二级类目
  198. // 获取三级类目
  199. this.form.address=this.addressList[0][this.multiIndex[0]].n+this.addressList[1][this.multiIndex[1]].n+this.addressList[2][this.multiIndex[2]].n
  200. this.form.province=this.addressList[0][this.multiIndex[0]].n
  201. this.form.city=this.addressList[1][this.multiIndex[1]].n
  202. this.form.district=this.addressList[2][this.multiIndex[2]].n
  203. this.form.cityId=this.addressList[1][this.multiIndex[1]].v;
  204. },
  205. pickerColumnchange(e){
  206. // 第一列滑动
  207. if(e.detail.column === 0){
  208. this.multiIndex[0] = e.detail.value
  209. // console.log('第一列滑动');
  210. this.addressList[1] = this.address[this.multiIndex[0]].c;
  211. this.addressList[2] = this.address[this.multiIndex[0]].c[0].c
  212. // 第一列滑动 第二列 和第三列 都变为第一个
  213. this.multiIndex.splice(1, 1, 0)
  214. this.multiIndex.splice(2, 1, 0)
  215. }
  216. // 第二列滑动
  217. if(e.detail.column === 1){
  218. this.multiIndex[1] = e.detail.value
  219. // console.log('第二列滑动');
  220. // console.log(this.multiIndex)
  221. this.addressList[2] = this.address[this.multiIndex[0]].c[this.multiIndex[1]].c
  222. // 第二列 滑动 第三列 变成第一个
  223. this.multiIndex.splice(2, 1, 0)
  224. }
  225. // 第三列滑动
  226. if(e.detail.column === 2){
  227. this.multiIndex[2] = e.detail.value
  228. }
  229. },
  230. getCitys(){
  231. getCitys().then(
  232. res => {
  233. if(res.code==200){
  234. this.address=res.data
  235. for(var i=0; i<this.address.length; i++){
  236. this.addressList[0].push(this.address[i])
  237. }
  238. for(var i=0; i<this.address[0].c.length; i++){
  239. this.addressList[1].push(this.address[0].c[i])
  240. }
  241. for(var i=0; i<this.address[0].c[0].c.length; i++){
  242. this.addressList[2].push(this.address[0].c[0].c[i])
  243. }
  244. }else{
  245. uni.showToast({
  246. icon:'none',
  247. title: "请求失败",
  248. });
  249. }
  250. },
  251. rej => {}
  252. );
  253. }
  254. }
  255. }
  256. </script>
  257. <style lang="scss">
  258. page{
  259. height: 100%;
  260. }
  261. .content{
  262. height: 100%;
  263. display: flex;
  264. flex-direction: column;
  265. justify-content: space-between;
  266. .inner{
  267. height: calc(100% - 120upx);
  268. padding: 20upx;
  269. .address-box{
  270. padding: 30upx;
  271. background: #FFFFFF;
  272. border-radius: 16upx;
  273. margin-bottom: 20upx;
  274. position: relative;
  275. .textarea{
  276. width: 100%;
  277. height: 200upx;
  278. font-size: 30upx;
  279. color: #999999;
  280. padding-bottom:100rpx;
  281. }
  282. .btns{
  283. right:10rpx;
  284. bottom:10rpx;
  285. position: absolute;
  286. .btn{
  287. width: 155upx;
  288. height: 64upx;
  289. line-height: 64upx;
  290. font-size: 26upx;
  291. font-family: PingFang SC;
  292. font-weight: 500;
  293. text-align: center;
  294. border-radius: 32upx;
  295. &.parse{
  296. background: #2BC7B9;
  297. color: #FFFFFF;
  298. }
  299. }
  300. }
  301. }
  302. .form-box{
  303. padding: 0 30upx;
  304. background: #FFFFFF;
  305. border-radius: 16upx;
  306. .form-item{
  307. padding: 30upx 0;
  308. display: flex;
  309. align-items: flex-start;
  310. border-bottom: 1px solid #F1F1F1;
  311. &:last-child{
  312. border-bottom: none;
  313. }
  314. .label{
  315. width: 180upx;
  316. text-align: left;
  317. font-size: 30upx;
  318. line-height: 44upx;
  319. font-family: PingFang SC;
  320. font-weight: 500;
  321. color: #222222;
  322. flex-shrink: 0;
  323. }
  324. input{
  325. text-align: left;
  326. }
  327. .form-input{
  328. font-size: 30upx;
  329. font-family: PingFang SC;
  330. font-weight: 500;
  331. color: #999999;
  332. text-align: left;
  333. }
  334. .form-textarea{
  335. font-size: 30upx;
  336. color: #999999;
  337. height: 100upx;
  338. padding: 4upx 0;
  339. }
  340. .birth-picker {
  341. flex: 1;
  342. display: flex;
  343. align-items: center;
  344. .right-box{
  345. width: 100%;
  346. display: flex;
  347. align-items: center;
  348. .input-box{
  349. width: 470upx;
  350. }
  351. .arrow{
  352. width: 13upx;
  353. height: 23upx;
  354. margin-left: 20upx;
  355. }
  356. }
  357. }
  358. }
  359. }
  360. .setting-box{
  361. height: 88upx;
  362. background: #FFFFFF;
  363. border-radius: 16upx;
  364. margin-top: 20upx;
  365. padding: 0 30upx;
  366. display: flex;
  367. align-items: center;
  368. justify-content: space-between;
  369. .label{
  370. font-size: 28upx;
  371. font-family: PingFang SC;
  372. font-weight: 500;
  373. color: #111111;
  374. }
  375. }
  376. }
  377. .btn-box{
  378. height: 120upx;
  379. padding: 0 30upx;
  380. display: flex;
  381. align-items: center;
  382. justify-content: center;
  383. background: #FFFFFF;
  384. .sub-btn{
  385. width: 100%;
  386. height: 88upx;
  387. line-height: 88upx;
  388. text-align: center;
  389. font-size: 30upx;
  390. font-family: PingFang SC;
  391. font-weight: bold;
  392. color: #FFFFFF;
  393. background: #2BC7B9;
  394. border-radius: 44upx;
  395. }
  396. }
  397. }
  398. </style>