addressForm.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <template>
  2. <view class="container">
  3. <view class="address-form address-form-top">
  4. <u-cell-group :border="false">
  5. <u-field label="收货人" v-model="addressData.username" label-width="160"></u-field>
  6. <u-field label="手机号码" v-model="addressData.phone" label-width="160"></u-field>
  7. <u-cell-item title="所在地" title-width="160" @click="regionShow = true">
  8. <text class="address-text" v-show="addressData.province">{{addressData.province}}-{{addressData.city}}-{{addressData.area}}</text>
  9. </u-cell-item>
  10. <u-field type="textarea" placeholder="详细地址:如道路、门牌号、小区、楼栋号、单元等" v-model="addressData.address" label-width="0"></u-field>
  11. </u-cell-group>
  12. </view>
  13. <view class="address-form address-form-bottom">
  14. <u-cell-group :border="false">
  15. <u-cell-item title="地址标签" title-width="160" @click="tagShow = true">
  16. <text class="address-text">{{addressData.tag}}</text>
  17. </u-cell-item>
  18. <u-cell-item title="设为默认地址" title-width="200" :arrow="false">
  19. <u-switch v-model="addressData.isDefault" active-color="#51A539"></u-switch>
  20. </u-cell-item>
  21. </u-cell-group>
  22. </view>
  23. <view class="address-handle">
  24. <u-button type="success" shape="circle" :ripple="true" @click="submitData" class="handle-custom" :disabled="!addressData.username || !addressData.phone || !addressData.address || !addressData.province">保存</u-button>
  25. </view>
  26. <u-picker mode="region" v-model="regionShow" :area-code="defaultRegion" @confirm="setRegion"></u-picker>
  27. <u-picker mode="selector" v-model="tagShow" :range="tagList" @confirm="setTag"></u-picker>
  28. <u-top-tips ref="uTips"></u-top-tips>
  29. </view>
  30. </template>
  31. <script>
  32. const NET = require('@/utils/request')
  33. const API = require('@/config/api')
  34. export default {
  35. data() {
  36. return {
  37. addressId: '',
  38. addressData: {
  39. username: '',
  40. phone: '',
  41. address: '',
  42. province: '',
  43. city: '',
  44. area: '',
  45. isDefault: false,
  46. tag: '',
  47. },
  48. regionShow: false,
  49. defaultRegion: [],
  50. tagShow: false,
  51. tagList: ['家', '公司', '学校'],
  52. }
  53. },
  54. onLoad(options) {
  55. if (options.addressId) {
  56. this.addressId = options.addressId
  57. uni.setNavigationBarTitle({
  58. title: '编辑地址'
  59. });
  60. NET.request(API.getAddressDetail + this.addressId, {}, 'GET').then(res => {
  61. this.addressData = {
  62. username: res.data.username,
  63. phone: res.data.phone,
  64. address: res.data.address,
  65. province: res.data.province,
  66. city: res.data.city,
  67. area: res.data.area,
  68. isDefault: res.data.isDefault == 'true' || res.data.isDefault,
  69. tag: res.data.tag,
  70. }
  71. this.defaultRegion = [res.data.province, res.data.city, res.data.area]
  72. }).catch(res => {
  73. this.$refs.uTips.show({
  74. title: '查询地址详情失败',
  75. type: 'warning',
  76. })
  77. })
  78. }
  79. },
  80. methods: {
  81. // 设置地址
  82. setRegion(data) {
  83. this.addressData.province = data.province.label
  84. this.addressData.city = data.city.label
  85. this.addressData.area = data.area.label
  86. },
  87. // 设置标签
  88. setTag(data) {
  89. this.addressData.tag = this.tagList[data[0]]
  90. },
  91. // 提交地址数据
  92. submitData() {
  93. if (!this.addressId) {
  94. NET.request(API.addAddress, {
  95. ...this.addressData,
  96. mid: uni.getStorageSync("userData").userId
  97. }, 'POST').then(res => {
  98. this.$refs.uTips.show({
  99. title: '新增地址成功',
  100. type: 'success',
  101. })
  102. setTimeout(() => {
  103. uni.navigateBack()
  104. }, 1000)
  105. }).catch(res => {
  106. this.$refs.uTips.show({
  107. title: '新增地址失败',
  108. type: 'warning',
  109. })
  110. })
  111. } else {
  112. NET.request(API.editAddress, {
  113. ...this.addressData,
  114. mid: uni.getStorageSync("userData").userId,
  115. id: this.addressId
  116. }, 'POST').then(res => {
  117. this.$refs.uTips.show({
  118. title: '编辑地址成功',
  119. type: 'success',
  120. })
  121. setTimeout(() => {
  122. uni.navigateBack()
  123. }, 1000)
  124. }).catch(res => {
  125. this.$refs.uTips.show({
  126. title: '新增地址失败',
  127. type: 'warning',
  128. })
  129. })
  130. }
  131. },
  132. },
  133. }
  134. </script>
  135. <style>
  136. page {
  137. background-color: #f7f7f7;
  138. }
  139. </style>
  140. <style lang="less" scoped>
  141. page {
  142. width: 100%;
  143. height: 100%;
  144. }
  145. .container {
  146. width: 100%;
  147. height: 100%;
  148. float: left;
  149. box-sizing: border-box;
  150. background-color: #f7f7f7;
  151. padding-bottom: 70px;
  152. overflow-y: auto;
  153. .address-form {
  154. width: 100%;
  155. float: left;
  156. box-sizing: border-box;
  157. padding: 0 15px;
  158. background-color: #ffffff;
  159. /deep/.u-field {
  160. padding-left: 0px;
  161. padding-right: 0px;
  162. }
  163. /deep/.u-cell {
  164. padding-left: 0px;
  165. padding-right: 0px;
  166. }
  167. }
  168. .address-form-top {
  169. /deep/.u-cell_title {
  170. color: #999999;
  171. }
  172. /deep/.u-label-text {
  173. color: #999999;
  174. }
  175. }
  176. .address-form-bottom {
  177. margin-top: 10px;
  178. /deep/.u-cell_title {
  179. color: #333333;
  180. }
  181. /deep/.u-label-text {
  182. color: #333333;
  183. }
  184. }
  185. .address-handle {
  186. width: calc(100% - 30px);
  187. height: 40px;
  188. position: fixed;
  189. bottom: 20px;
  190. left: 15px;
  191. .handle-custom {
  192. background-color: #56a83a;
  193. /deep/button {
  194. background-color: #56a83a;
  195. }
  196. /deep/.u-btn--success--disabled {
  197. background-color: #74bd60 !important;
  198. }
  199. }
  200. }
  201. }
  202. </style>