addAddress.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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="https://jnlzjk-1323137866.cos.ap-chongqing.myqcloud.com/shop/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="#FF233C" 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,getCityId} 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. uni.hideShareMenu({
  80. menus: ["shareAppMessage", "shareTimeline"]
  81. })
  82. }
  83. else{
  84. uni.setNavigationBarTitle({
  85.   title:"新增收货地址"
  86. })
  87. uni.showShareMenu({
  88. withShareTicket: true,
  89. //小程序的原生菜单中显示分享按钮,才能够让发送给朋友与分享到朋友圈两个按钮可以点击
  90. menus: ["shareAppMessage", "shareTimeline"] //不设置默认发送给朋友
  91. })
  92. }
  93. this.getCitys()
  94. },
  95. //发送给朋友
  96. onShareAppMessage(res) {
  97. if (this.utils.isLogin()) {
  98. var user = JSON.parse(uni.getStorageSync('userInfo'))
  99. return {
  100. title: '邀请您填写地址,下单更方便',
  101. path: '/pages_user/user/addAddress?type='+this.type,
  102. imageUrl: 'https://bjzmky-1323137866.cos.ap-chongqing.myqcloud.com/app/address-share.jpg' //分享图标,路径可以是本地文件路径、代码包文件路径或者网络图片路径.支持PNG及JPG。显示图片长宽比是 5:4
  103. }
  104. }
  105. },
  106. //分享到朋友圈
  107. onShareTimeline(res) {
  108. if (this.utils.isLogin()) {
  109. var user = JSON.parse(uni.getStorageSync('userInfo'))
  110. return {
  111. title: '邀请您填写地址,下单更方便',
  112. query: 'type='+this.type, //页面参数
  113. imageUrl: 'https://bjzmky-1323137866.cos.ap-chongqing.myqcloud.com/app/address-share.jpg' //分享图标,路径可以是本地文件路径、代码包文件路径或者网络图片路径.支持PNG及JPG。显示图片长宽比是 5:4
  114. }
  115. }
  116. },
  117. methods: {
  118. parseAddress(){
  119. if(this.content==null||this.content==""){
  120. uni.showToast({ icon:'none',title: '请输入地址信息'});
  121. return;
  122. }
  123. var data={content:this.content};
  124. parseAddress(data).then(
  125. res => {
  126. if(res.code==200){
  127. this.form.realName=res.data.name
  128. this.form.phone=res.data.mobile
  129. this.form.address=res.data.provinceName+res.data.cityName+res.data.expAreaName
  130. this.form.province=res.data.provinceName
  131. this.form.city=res.data.cityName
  132. this.form.district=res.data.expAreaName
  133. this.form.detail=res.data.streetName+res.data.address
  134. this.getCityId(this.form.province,this.form.city,this.form.district)
  135. }else{
  136. uni.showToast({
  137. icon:'none',
  138. title: res.msg,
  139. });
  140. }
  141. },
  142. rej => {}
  143. );
  144. },
  145. getAddressById(){
  146. var data={id:this.id};
  147. getAddressById(data).then(
  148. res => {
  149. if(res.code==200){
  150. this.form=res.data;
  151. this.isDefault=this.form.isDefault==1?true:false;
  152. this.form.address=this.form.province+this.form.city+this.form.district
  153. }else{
  154. uni.showToast({
  155. icon:'none',
  156. title: res.msg,
  157. });
  158. }
  159. },
  160. rej => {}
  161. );
  162. },
  163. // 获得cityId
  164. getCityId(province, city, district) {
  165. getCityId({
  166. province: province,
  167. city: city,
  168. district: district
  169. }).then(res => {
  170. if (res.code == 200) {
  171. this.form.cityId = res.cityId
  172. } else {
  173. uni.showToast({
  174. title: res.msg,
  175. icon: 'none'
  176. });
  177. }
  178. },
  179. rej => {}
  180. );
  181. },
  182. submit(){
  183. if(this.type=="add"){
  184. this.addAddress()
  185. }
  186. else if(this.type=="edit"){
  187. this.editAddress()
  188. }
  189. },
  190. editAddress(){
  191. this.form.isDefault=this.isDefault?1:0
  192. editAddress(this.form).then(
  193. res => {
  194. if(res.code==200){
  195. uni.showToast({
  196. icon:'success',
  197. title: "操作成功",
  198. });
  199. setTimeout(function() {
  200. uni.$emit('refreshAddress');
  201. uni.navigateBack({
  202. delta: 1,
  203. fail: () => {
  204. uni.redirectTo({
  205. url: "/pages_user/user/address"
  206. });
  207. }
  208. });
  209. }, 500);
  210. }else{
  211. uni.showToast({
  212. icon:'none',
  213. title: res.msg,
  214. });
  215. }
  216. },
  217. rej => {}
  218. );
  219. },
  220. addAddress(){
  221. this.form.isDefault=this.isDefault?1:0
  222. addAddress(this.form).then(
  223. res => {
  224. if(res.code==200){
  225. uni.showToast({
  226. icon:'success',
  227. title: "操作成功",
  228. });
  229. setTimeout(function() {
  230. uni.$emit('refreshAddress');
  231. uni.navigateBack({
  232. delta: 1,
  233. fail: () => {
  234. uni.redirectTo({
  235. url: "/pages_user/user/address"
  236. });
  237. }
  238. });
  239. }, 500);
  240. }else{
  241. uni.showToast({
  242. icon:'none',
  243. title: res.msg,
  244. });
  245. }
  246. },
  247. rej => {}
  248. );
  249. },
  250. // 地区选择
  251. pickerChange(e) {
  252. this.multiIndex = e.detail.value;
  253. // 数组内的下标
  254. // 获取一级类目
  255. // 获取二级类目
  256. // 获取三级类目
  257. this.form.address=this.addressList[0][this.multiIndex[0]].n+this.addressList[1][this.multiIndex[1]].n+this.addressList[2][this.multiIndex[2]].n
  258. this.form.province=this.addressList[0][this.multiIndex[0]].n
  259. this.form.city=this.addressList[1][this.multiIndex[1]].n
  260. this.form.district=this.addressList[2][this.multiIndex[2]].n
  261. this.form.cityId=this.addressList[1][this.multiIndex[1]].v;
  262. },
  263. pickerColumnchange(e){
  264. // 第一列滑动
  265. if(e.detail.column === 0){
  266. this.multiIndex[0] = e.detail.value
  267. // console.log('第一列滑动');
  268. this.addressList[1] = this.address[this.multiIndex[0]].c;
  269. this.addressList[2] = this.address[this.multiIndex[0]].c[0].c
  270. // 第一列滑动 第二列 和第三列 都变为第一个
  271. this.multiIndex.splice(1, 1, 0)
  272. this.multiIndex.splice(2, 1, 0)
  273. }
  274. // 第二列滑动
  275. if(e.detail.column === 1){
  276. this.multiIndex[1] = e.detail.value
  277. // console.log('第二列滑动');
  278. // console.log(this.multiIndex)
  279. this.addressList[2] = this.address[this.multiIndex[0]].c[this.multiIndex[1]].c
  280. // 第二列 滑动 第三列 变成第一个
  281. this.multiIndex.splice(2, 1, 0)
  282. }
  283. // 第三列滑动
  284. if(e.detail.column === 2){
  285. this.multiIndex[2] = e.detail.value
  286. }
  287. },
  288. getCitys(){
  289. getCitys().then(
  290. res => {
  291. if(res.code==200){
  292. this.address=res.data
  293. for(var i=0; i<this.address.length; i++){
  294. this.addressList[0].push(this.address[i])
  295. }
  296. for(var i=0; i<this.address[0].c.length; i++){
  297. this.addressList[1].push(this.address[0].c[i])
  298. }
  299. for(var i=0; i<this.address[0].c[0].c.length; i++){
  300. this.addressList[2].push(this.address[0].c[0].c[i])
  301. }
  302. }else{
  303. uni.showToast({
  304. icon:'none',
  305. title: "请求失败",
  306. });
  307. }
  308. },
  309. rej => {}
  310. );
  311. }
  312. }
  313. }
  314. </script>
  315. <style lang="scss">
  316. page{
  317. height: 100%;
  318. }
  319. .content{
  320. height: 100%;
  321. display: flex;
  322. flex-direction: column;
  323. justify-content: space-between;
  324. .inner{
  325. height: calc(100% - 120upx);
  326. padding: 20upx;
  327. .address-box{
  328. padding: 30upx;
  329. background: #FFFFFF;
  330. border-radius: 16upx;
  331. margin-bottom: 20upx;
  332. position: relative;
  333. .textarea{
  334. width: 100%;
  335. height: 200upx;
  336. font-size: 30upx;
  337. color: #999999;
  338. padding-bottom:100rpx;
  339. }
  340. .btns{
  341. right:10rpx;
  342. bottom:10rpx;
  343. position: absolute;
  344. .btn{
  345. width: 155upx;
  346. height: 64upx;
  347. line-height: 64upx;
  348. font-size: 26upx;
  349. font-family: PingFang SC;
  350. font-weight: 500;
  351. text-align: center;
  352. border-radius: 32upx;
  353. &.parse{
  354. background: #FF233C;
  355. color: #FFFFFF;
  356. }
  357. }
  358. }
  359. }
  360. .form-box{
  361. padding: 0 30upx;
  362. background: #FFFFFF;
  363. border-radius: 16upx;
  364. .form-item{
  365. padding: 30upx 0;
  366. display: flex;
  367. align-items: flex-start;
  368. border-bottom: 1px solid #F1F1F1;
  369. &:last-child{
  370. border-bottom: none;
  371. }
  372. .label{
  373. width: 180upx;
  374. text-align: left;
  375. font-size: 30upx;
  376. line-height: 44upx;
  377. font-family: PingFang SC;
  378. font-weight: 500;
  379. color: #222222;
  380. flex-shrink: 0;
  381. }
  382. input{
  383. text-align: left;
  384. }
  385. .form-input{
  386. font-size: 30upx;
  387. font-family: PingFang SC;
  388. font-weight: 500;
  389. color: #999999;
  390. text-align: left;
  391. }
  392. .form-textarea{
  393. font-size: 30upx;
  394. color: #999999;
  395. height: 100upx;
  396. padding: 4upx 0;
  397. }
  398. .birth-picker {
  399. flex: 1;
  400. display: flex;
  401. align-items: center;
  402. .right-box{
  403. width: 100%;
  404. display: flex;
  405. align-items: center;
  406. .input-box{
  407. width: 470upx;
  408. }
  409. .arrow{
  410. width: 13upx;
  411. height: 23upx;
  412. margin-left: 20upx;
  413. }
  414. }
  415. }
  416. }
  417. }
  418. .setting-box{
  419. height: 88upx;
  420. background: #FFFFFF;
  421. border-radius: 16upx;
  422. margin-top: 20upx;
  423. padding: 0 30upx;
  424. display: flex;
  425. align-items: center;
  426. justify-content: space-between;
  427. .label{
  428. font-size: 28upx;
  429. font-family: PingFang SC;
  430. font-weight: 500;
  431. color: #111111;
  432. }
  433. }
  434. }
  435. .btn-box{
  436. height: 120upx;
  437. padding: 0 30upx;
  438. display: flex;
  439. align-items: center;
  440. justify-content: center;
  441. background: #FFFFFF;
  442. .sub-btn{
  443. width: 100%;
  444. height: 88upx;
  445. line-height: 88upx;
  446. text-align: center;
  447. font-size: 30upx;
  448. font-family: PingFang SC;
  449. font-weight: bold;
  450. color: #FFFFFF;
  451. background: #FF233C;
  452. border-radius: 44upx;
  453. }
  454. }
  455. }
  456. </style>