address.vue 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <template>
  2. <view class="bg">
  3. <view v-for="(item,index) in list" :key="index" class="address">
  4. <view @click="selectAddressToPage(item)" class="address_text">
  5. <view class="text1">
  6. <view>{{item.contact}}</view>
  7. <view style="margin-left:8rpx">{{item.contactPhone}}</view>
  8. <view v-if="item.defaultAddress == '1'" class="mr">默认</view>
  9. </view>
  10. <view class="text2">
  11. {{item.address}}
  12. </view>
  13. </view>
  14. <view @click="openPopup(item)">
  15. <u--image width="40rpx" height="40rpx" src="/static/myinfo/iconm_bianj.png" ></u--image>
  16. </view>
  17. </view>
  18. <view class="btnbox">
  19. <view @click="openPopup()" class="btn">
  20. 新增
  21. </view>
  22. </view>
  23. <u-popup :closeable='true' :round="10" :show="popupShow" @close="close" @open="open">
  24. <view class="popupView">
  25. <view class="scrollview">
  26. <u--form :rules="rules" labelPosition="left" :model="form" ref="uForm">
  27. <u-form-item prop="contact" :labelWidth="labelWidth" label="姓名" borderBottom >
  28. <u--input placeholder="请输入姓名" v-model="form.contact" border="none" ></u--input>
  29. </u-form-item>
  30. <u-form-item prop="contactPhone" :labelWidth="labelWidth" label="手机号" borderBottom >
  31. <u--input placeholder="请输入手机号" v-model="form.contactPhone" border="none" ></u--input>
  32. </u-form-item>
  33. <u-form-item prop="address" :labelWidth="labelWidth" label="详细地址" borderBottom >
  34. <u--textarea maxlength="100" v-model="form.address" border="none" placeholder="请输入详细地址" ></u--textarea>
  35. <!-- <u--input placeholder="请输入详细地址" v-model="form.address" border="none" ></u--input> -->
  36. </u-form-item>
  37. <u-form-item prop="switch" :labelWidth="labelWidth" label="设为默认地址" borderBottom >
  38. <u-switch v-model="defaultAddress" ></u-switch>
  39. </u-form-item>
  40. </u--form>
  41. <view style="color: red;" @click="deladdress()" v-if="ismodify">
  42. 删除
  43. </view>
  44. </view>
  45. </view>
  46. <view class="btnbox" style="left: 24rpx;">
  47. <view @click="submit" class="btn">
  48. 保存
  49. </view>
  50. </view>
  51. </u-popup>
  52. </view>
  53. </template>
  54. <script>
  55. import {
  56. deliveryAddressList,
  57. addDeliveryAddress,
  58. delDeliveryAddressList,
  59. modifyDeliveryAddress
  60. } from '@/http/api/common.js'
  61. import * as util from '@/pages/util/util.js'
  62. export default {
  63. data() {
  64. return {
  65. list:[],
  66. defaultAddress:true,
  67. ismodify:false,
  68. form:{
  69. contact:'',
  70. contactPhone:'',
  71. address:'',
  72. defaultAddress:'0'
  73. },
  74. labelWidth:100,
  75. popupShow:false,
  76. rules: {
  77. 'contact': {
  78. type: 'string',
  79. required: true,
  80. message: '请填写姓名',
  81. trigger: ['blur', 'change']
  82. },
  83. 'contactPhone':[
  84. {
  85. type: 'string',
  86. required: true,
  87. message: '请输入手机号',
  88. trigger: ['blur', 'change']
  89. },
  90. {
  91. validator: (rule, value, callback) => {
  92. return uni.$u.test.mobile(value);
  93. },
  94. message: '手机号码格式不正确',
  95. // 触发器可以同时用blur和change
  96. trigger: ['blur'],
  97. }
  98. ],
  99. 'address': {
  100. type: 'string',
  101. required: true,
  102. message: '请输入详细地址',
  103. trigger: ['blur', 'change']
  104. },
  105. selectAddress:false
  106. },
  107. }
  108. },
  109. components: {
  110. // appitem,
  111. },
  112. onLoad(e) {
  113. if (e?.selectAddress == '1') {
  114. this.selectAddress = true
  115. }
  116. //获取地址列表
  117. this.getdeliveryAddressList()
  118. },
  119. onReady() {
  120. //如果需要兼容微信小程序,并且校验规则中含有方法等,只能通过setRules方法设置规则。
  121. this.$refs.uForm?.setRules(this.rules)
  122. },
  123. methods: {
  124. selectAddressToPage(item){
  125. if (this.selectAddress) {
  126. uni.setStorageSync('selectAddress', JSON.stringify(item))
  127. uni.navigateBack({
  128. delta: 1
  129. });
  130. }
  131. },
  132. async getdeliveryAddressList(){
  133. let res = await deliveryAddressList()
  134. if (res.data.code == 200) {
  135. this.list = res.data.data
  136. }
  137. },
  138. async deladdress(){
  139. if (this.form?.id != '') {
  140. let res = await delDeliveryAddressList(this.form?.id)
  141. if (res.data.code == 200) {
  142. util.toastFunc('删除成功',()=>{
  143. this.getdeliveryAddressList()
  144. this.popupShow = false
  145. })
  146. }
  147. }
  148. },
  149. async submit(){
  150. this.$refs.uForm.validate().then(async uFormRes => {
  151. this.form.defaultAddress = this.defaultAddress ? '1' : '0'
  152. let res = {
  153. data:{
  154. code:500
  155. }
  156. }
  157. if (this.ismodify) {
  158. //修改
  159. res = await modifyDeliveryAddress(this.form,this.form.id)
  160. }else{
  161. //新增
  162. res = await addDeliveryAddress(this.form)
  163. }
  164. let msg = this.ismodify ? '修改成功' : '添加成功'
  165. if (res.data.code == 200) {
  166. util.toastFunc(msg,()=>{
  167. this.getdeliveryAddressList()
  168. this.popupShow = false
  169. })
  170. }
  171. }).catch(errors => {
  172. })
  173. },
  174. open() {
  175. // console.log('open');
  176. },
  177. close() {
  178. this.popupShow = false
  179. // console.log('close');
  180. },
  181. openPopup(data=null){
  182. if (data) {
  183. this.ismodify = true //是否修改 true为修改模式
  184. this.form = data
  185. this.defaultAddress = data.defaultAddress=='1'?true:false
  186. this.popupShow = true
  187. }else{
  188. //新增 置空表单数据
  189. this.form = {
  190. contact:'',
  191. contactPhone:'',
  192. address:'',
  193. defaultAddress:'0'
  194. }
  195. this.ismodify = false
  196. this.popupShow = true
  197. }
  198. }
  199. }
  200. }
  201. </script>
  202. <style lang="scss">
  203. .bg{
  204. width: 750rpx;
  205. height: auto;
  206. min-height: 100vh;
  207. background: #F1F1F1;
  208. border-radius: 0rpx 0rpx 0rpx 0rpx;
  209. display: flex;
  210. flex-direction: column;
  211. align-items: center;
  212. }
  213. .address{
  214. width: 702rpx;
  215. min-height: 180rpx;
  216. background: #FFFFFF;
  217. border-radius: 16rpx 16rpx 16rpx 16rpx;
  218. opacity: 1;
  219. display: flex;
  220. align-items: center;
  221. justify-content: space-around;
  222. margin-top: 24rpx;
  223. padding: 20rpx 0;
  224. }
  225. .address_text{
  226. display: flex;
  227. flex-direction: column;
  228. }
  229. .text1{
  230. display: flex;
  231. align-items: center;
  232. min-height: 45rpx;
  233. font-size: 32rpx;
  234. font-weight: bold;
  235. color: #333333;
  236. }
  237. .text2{
  238. width: 547rpx;
  239. min-height: 80rpx;
  240. font-size: 28rpx;
  241. font-weight: 500;
  242. color: #666666;
  243. }
  244. .btnbox{
  245. position: fixed;
  246. bottom: 20rpx;
  247. }
  248. .btn{
  249. width: 702rpx;
  250. height: 80rpx;
  251. background: #46A6FF;
  252. border-radius: 64rpx 64rpx 64rpx 64rpx;
  253. opacity: 1;
  254. font-size: 28rpx;
  255. font-weight: 500;
  256. color: #FFFFFF;
  257. line-height: 80rpx;
  258. text-align: center;
  259. }
  260. .mr{
  261. width: 64rpx;
  262. height: 32rpx;
  263. background: #FF8C33;
  264. border-radius: 8rpx 8rpx 8rpx 8rpx;
  265. opacity: 1;
  266. font-size: 24rpx;
  267. font-weight: 500;
  268. color: #FFFFFF;
  269. text-align: center;
  270. margin-left: 12rpx;
  271. }
  272. </style>