index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. <template>
  2. <view>
  3. <view class="to_bottom" v-if="!addrList.length"> -----还没有地址啦-----</view>
  4. <view class="addr_list">
  5. <view class="addr_item" v-for="(item,index) in addrList" :key="index" >
  6. <view class="contact_user">
  7. <text class="contact_name">{{item.contact_name}}</text>
  8. <text class="contact_phone">{{item.contact_phone}}</text>
  9. </view>
  10. <view class="contact_addr">
  11. {{item.contact_province}} {{item.contact_city}} {{item.contact_area}} {{item.contact_addr}}
  12. </view>
  13. <view class="contact_handler">
  14. <view class="radio_label" @click="setDefault(index)">
  15. <image class="radio_icon" :src="item.is_default?'../../static/icon/radioed.png' :'../../static/icon/radio.png' " ></image>
  16. <text :class="item.is_default?'radioed_text':''">{{item.is_default?'已默认':'设为默认'}}</text>
  17. </view>
  18. <view class="addr_right_btn" @click="removeAddr(index)">
  19. 删除
  20. </view>
  21. <view class="addr_right_btn" @click="openForm(item)">
  22. 编辑
  23. </view>
  24. </view>
  25. </view>
  26. </view>
  27. <view class="create_box">
  28. <button class="create_addr" @click="openForm()">新建收货地址</button>
  29. </view>
  30. <view class="to_bottom" v-if="isLast"> -----到底啦-----</view>
  31. <uni-popup ref="addrForm" type="bottom" class="popup" background-color="#FFFFFF" @change="popupChange">
  32. <view class="contact_addr_form">
  33. <!-- 地区代表表单 -->
  34. <form class="info_form">
  35. <view class="form_group">
  36. <view class="group_title">收货人:</view>
  37. <view class="group_box">
  38. <input type="text" class="form_ctrl" placeholder="请输入收件人" maxlength="20" required="" v-model="addrRequest.contact_name" />
  39. </view>
  40. </view>
  41. <view class="form_group">
  42. <view class="group_title">手机号:</view>
  43. <view class="group_box">
  44. <input type="text" class="form_ctrl" placeholder="请输入手机号" maxlength="11" required="" v-model="addrRequest.contact_phone" />
  45. </view>
  46. </view>
  47. <view class="form_group">
  48. <view class="group_title">所在地区:</view>
  49. <view class="group_box">
  50. <picker mode="multiSelector" class="form_ctrl" :range="cityArray" @columnchange="cityChange" @change="cityFinish" >
  51. <view class="area_text">
  52. <text v-if="addrRequest.contact_province">{{addrRequest.contact_province}}</text>
  53. <text v-if="addrRequest.contact_city">/{{addrRequest.contact_city}}</text>
  54. <text v-if="addrRequest.contact_area">/{{addrRequest.contact_area}}</text>
  55. </view>
  56. </picker>
  57. </view>
  58. </view>
  59. <view class="form_group">
  60. <view class="group_title">详细地址:</view>
  61. <view class="group_box">
  62. <input type="text" class="form_ctrl" placeholder="请输入详细地址:街道/小区/门牌号" maxlength="11" required="" v-model="addrRequest.contact_addr" />
  63. </view>
  64. </view>
  65. <view class="form_group">
  66. <view class="group_title">设为默认</view>
  67. <view class="group_box">
  68. <switch color="#E03519" style="transform:scale(0.6);float: right;" @change="defaultSwitch" :checked="addrRequest.is_default?true:false" />
  69. </view>
  70. </view>
  71. <button class="submit_btn" @click="saveAddr">保存地址</button>
  72. </form>
  73. </view>
  74. </uni-popup>
  75. </view>
  76. </template>
  77. <script>
  78. import {getProvinces,getMyCity,getAreas} from "../../utils/city";
  79. export default {
  80. data() {
  81. return {
  82. // 产品列表
  83. addrList:[],
  84. // 请求参数
  85. requestParam:{
  86. page:1,
  87. status:0,
  88. },
  89. // 保存地址
  90. addrRequest:{
  91. id:0,
  92. contact_name:"",
  93. contact_phone:"",
  94. contact_province:"",
  95. contact_city:"",
  96. contact_area:"",
  97. contact_addr:"",
  98. is_default:0,
  99. },
  100. // 是否最后一页
  101. isLast:false,
  102. // 是否请求中
  103. isReqing:false,
  104. // 所在地区
  105. cityArray: [[],[],[]],
  106. cityValue: [0,0,0],
  107. // 自动弹出
  108. autoShowForm: false,
  109. // 异步通知
  110. AddrEmit: false,
  111. };
  112. },
  113. onLoad(param) {
  114. this.AddrEmit = param.notify == 'addr' ? true : false;
  115. this.autoShowForm = param.type == 'create' ? true : false;
  116. // 获取列表
  117. this.getList();
  118. },
  119. onReady() {
  120. if( this.autoShowForm ){
  121. var that = this;
  122. // 500毫秒后自动弹出
  123. setTimeout(function(){
  124. that.openForm();
  125. },500)
  126. }
  127. },
  128. onShow() {
  129. // 替换对应的数据
  130. this.cityArray.splice(0,1,getProvinces());
  131. this.cityArray.splice(1,1,getMyCity(this.cityValue[0]));
  132. this.cityArray.splice(2,1,getAreas(this.cityValue[0],this.cityValue[1]));
  133. // 默认地址
  134. this.addrRequest.contact_province = this.cityArray[0][this.cityValue[0]]
  135. this.addrRequest.contact_city = this.cityArray[1][this.cityValue[1]];
  136. this.addrRequest.contact_area = this.cityArray[2][this.cityValue[2]];
  137. },
  138. methods:{
  139. // 获取列表
  140. getList(){
  141. // 登录提示
  142. // if( !this.$checkAccess.alterLogin() ) return ;
  143. // 判断数据
  144. this.$http.request('api/custom_addr/get_list').then((callback)=>{
  145. // 获取成功
  146. if( callback.code == 'success' ){
  147. this.addrList = callback.data;
  148. // 通知地址变更
  149. if( this.AddrEmit ) uni.$emit('addr_list_change',{list:this.addrList});
  150. }
  151. });
  152. },
  153. // 打开弹窗
  154. openForm(item){
  155. if( !item ) {
  156. this.addrRequest.id = 0;
  157. this.addrRequest.contact_name = '';
  158. this.addrRequest.contact_phone = '';
  159. this.addrRequest.contact_addr = '';
  160. this.addrRequest.is_default = 0;
  161. }else{
  162. this.addrRequest = item;
  163. }
  164. // 显示下单弹出层
  165. this.$refs.addrForm.open('bottom');
  166. },
  167. // 默认开关
  168. defaultSwitch(e){
  169. this.addrRequest.is_default = e.detail.value ? 1 : 0;
  170. },
  171. // 弹出层
  172. popupChange(e){
  173. // 禁止滚动穿透
  174. this.show = e.show
  175. },
  176. // 城市修改
  177. cityChange(e){
  178. // 替换三个选项
  179. this.cityValue.splice(e.detail.column,1,e.detail.value);
  180. // 下一级设置为0
  181. if( e.detail.column == 0 ){
  182. this.cityValue.splice(1,1,0);
  183. this.cityValue.splice(2,1,0);
  184. }
  185. if( e.detail.column == 1 ){
  186. this.cityValue.splice(2,1,0);
  187. }
  188. // 替换对应的数据
  189. this.cityArray.splice(0,1,getProvinces());
  190. this.cityArray.splice(1,1,getMyCity(this.cityValue[0]));
  191. this.cityArray.splice(2,1,getAreas(this.cityValue[0],this.cityValue[1]));
  192. },
  193. // 城市选择
  194. cityFinish(e){
  195. this.addrRequest.contact_province = this.cityArray[0][this.cityValue[0]];
  196. this.addrRequest.contact_city = this.cityArray[1][this.cityValue[1]];
  197. this.addrRequest.contact_area = this.cityArray[2][this.cityValue[2]];
  198. },
  199. // 城市选中
  200. cityFinish(e){
  201. this.addrRequest.contact_province = this.cityArray[0][this.cityValue[0]];
  202. this.addrRequest.contact_city = this.cityArray[1][this.cityValue[1]];
  203. this.addrRequest.contact_area = this.cityArray[2][this.cityValue[2]];
  204. },
  205. // 保存地址
  206. saveAddr(){
  207. // 判断姓名
  208. if( !this.addrRequest.contact_name ){
  209. uni.showToast({icon:"none",title:"请填写收件人"});
  210. return;
  211. }
  212. if( this.addrRequest.contact_name.length < 2 ){
  213. uni.showToast({icon:"none",title:"请填写收件人完整姓名"});
  214. return;
  215. }
  216. if( !this.addrRequest.contact_phone ){
  217. uni.showToast({icon:"none",title:"请填写收件人手机号"});
  218. return;
  219. }
  220. if( !this.addrRequest.contact_addr ){
  221. uni.showToast({icon:"none",title:"请填写联系地址"});
  222. return;
  223. }
  224. if( this.addrRequest.contact_addr.length < 3 ){
  225. uni.showToast({icon:"none",title:"请填写可用联系地址"});
  226. return;
  227. }
  228. // 请求状态
  229. uni.showLoading({mask:true});
  230. // 请求路径
  231. var url = this.addrRequest.id ? 'api/custom_addr/edit' : 'api/custom_addr/add';
  232. // 授权成功以后,调用绑定
  233. this.$http.request(url,this.addrRequest,'post').then((re)=>{
  234. // 关闭
  235. uni.hideLoading();
  236. // 成功的话
  237. if( re.code != 'success' ){
  238. // 跳转
  239. uni.showToast({title: re.msg,icon:"none"});
  240. return;
  241. }
  242. // 地址变动
  243. this.getList();
  244. // 显示下单弹出层
  245. this.$refs.addrForm.close();
  246. });
  247. },
  248. // 删除地址
  249. removeAddr(index){
  250. // 授权成功以后,调用绑定
  251. this.$http.request('api/custom_addr/del',{id:this.addrList[index].id},'post').then((re)=>{
  252. // 关闭
  253. uni.hideLoading();
  254. // 成功的话
  255. if( re.code != 'success' ){
  256. // 跳转
  257. uni.showToast({title: re.msg,icon:"none"});
  258. return;
  259. }
  260. // 成功删除该项
  261. this.addrList.splice(index,1);
  262. // 通知地址变更
  263. if( this.AddrEmit ) uni.$emit('addr_list_change',{list:this.addrList});
  264. });
  265. },
  266. // 设置默认
  267. setDefault(index){
  268. // 如果已经是默认状态
  269. if( this.addrList[index].is_default ) return;
  270. // 授权成功以后,调用绑定
  271. this.$http.request('api/custom_addr/set_default',{id:this.addrList[index].id},'post').then((re)=>{
  272. // 关闭
  273. uni.hideLoading();
  274. // 成功的话
  275. if( re.code != 'success' ){
  276. // 跳转
  277. uni.showToast({title: re.msg,icon:"none"});
  278. return;
  279. }
  280. // 更新其他的默认项
  281. for (let i in this.addrList) {
  282. this.addrList[i].is_default = 0;
  283. }
  284. // 成功修改值
  285. this.addrList[index].is_default = 1;
  286. // 通知地址变更
  287. if( this.AddrEmit ) uni.$emit('addr_list_change',{list:this.addrList});
  288. });
  289. },
  290. }
  291. }
  292. </script>
  293. <style lang="less">
  294. .addr_list{
  295. width: 730rpx;
  296. display: block;
  297. overflow: hidden;
  298. margin: 0rpx auto;
  299. .addr_item{
  300. display: block;
  301. font-size: 24rpx;
  302. overflow: hidden;
  303. line-height: 40rpx;
  304. padding: 15rpx 10rpx;
  305. border-radius: 15rpx;
  306. padding-bottom: 0rpx;
  307. margin-bottom: 10rpx;
  308. background-color: #FFFFFF;
  309. .contact_user{
  310. display: block;
  311. font-size: 24rpx;
  312. line-height: 50rpx;
  313. .contact_name{
  314. font-size: 26rpx;
  315. font-weight: bold;
  316. margin-right: 16rpx;
  317. }
  318. }
  319. .contact_addr{
  320. display: block;
  321. font-size: 24rpx;
  322. line-height: 30rpx;
  323. padding: 10rpx 5rpx;
  324. border-bottom: 2rpx solid #dddddd;
  325. }
  326. .contact_handler{
  327. height: 60rpx;
  328. display: block;
  329. line-height: 60rpx;
  330. font-size: 20rpx;
  331. .radio_label{
  332. float: left;
  333. height: 60rpx;
  334. line-height: 60rpx;
  335. .radio_icon{
  336. width: 40rpx;
  337. height: 40rpx;
  338. vertical-align: middle;
  339. }
  340. .radioed_text{
  341. color: #E03519;
  342. }
  343. }
  344. .addr_right_btn{
  345. float: right;
  346. height: 40rpx;
  347. display: block;
  348. line-height: 40rpx;
  349. padding: 10rpx 35rpx;
  350. }
  351. }
  352. }
  353. }
  354. .create_box{
  355. left: 0rpx;
  356. width: 750rpx;
  357. height: 140rpx;
  358. display: block;
  359. position: fixed;
  360. bottom: var(--window-bottom);
  361. .create_addr{
  362. width: 700rpx;
  363. height: 80rpx;
  364. display: block;
  365. color: #FFFFFF;
  366. font-size: 30rpx;
  367. overflow: hidden;
  368. line-height: 80rpx;
  369. padding: 0rpx 0rpx;
  370. text-align: center;
  371. margin: 0rpx auto;
  372. margin-top: 20rpx;
  373. border-radius: 40rpx;
  374. background-color: #E03519;
  375. }
  376. }
  377. .popup{
  378. .info_form{
  379. display: block;
  380. overflow: hidden;
  381. padding: 20rpx 0rpx;
  382. padding-top: 80rpx;
  383. background: #FFFFFF;
  384. .form_group{
  385. display: block;
  386. overflow: hidden;
  387. line-height: 60rpx;
  388. padding: 20rpx 35rpx;
  389. .group_title{
  390. float: left;
  391. width: 160rpx;
  392. display: block;
  393. overflow: hidden;
  394. font-size: 30rpx;
  395. margin-right: 20rpx;
  396. }
  397. .group_box{
  398. float: left;
  399. width: 480rpx;
  400. display: block;
  401. .form_ctrl{
  402. height: 56rpx;
  403. font-size: 24rpx;
  404. padding: 0rpx 20rpx;
  405. line-height: 56rpx;;
  406. border: 2rpx solid #DDDDDD;
  407. }
  408. .area_text{
  409. width: 446rpx;
  410. font-size: 20rpx;
  411. overflow: hidden;
  412. white-space: nowrap;
  413. text-overflow: ellipsis;
  414. }
  415. .group_image{
  416. width: 200rpx;
  417. height: 200rpx;
  418. }
  419. .choose_image{
  420. display: block;
  421. width: 200rpx;
  422. height: 200rpx;
  423. font-size: 38rpx;
  424. text-align: center;
  425. line-height: 200rpx;
  426. border: 2rpx solid #DDDDDD;
  427. }
  428. }
  429. }
  430. .submit_btn{
  431. width: 700rpx;
  432. height: 80rpx;
  433. display: block;
  434. color: #FFFFFF;
  435. font-size: 30rpx;
  436. overflow: hidden;
  437. line-height: 80rpx;
  438. padding: 0rpx 0rpx;
  439. text-align: center;
  440. margin: 20rpx auto;
  441. margin-top: 40rpx;
  442. border-radius: 40rpx;
  443. background-color: #E03519;
  444. }
  445. }
  446. }
  447. </style>