addressForm.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. if (res.isSuccess) {
  99. this.$refs.uTips.show({
  100. title: '新增地址成功',
  101. type: 'success',
  102. })
  103. setTimeout(() => {
  104. uni.navigateBack()
  105. }, 1000)
  106. } else {
  107. this.$refs.uTips.show({
  108. title: res.msg,
  109. type: 'warning',
  110. })
  111. }
  112. }).catch(res => {
  113. this.$refs.uTips.show({
  114. title: '新增地址失败',
  115. type: 'warning',
  116. })
  117. })
  118. } else {
  119. NET.request(API.editAddress, {
  120. ...this.addressData,
  121. mid: uni.getStorageSync("userData").userId,
  122. id: this.addressId
  123. }, 'POST').then(res => {
  124. if (res.isSuccess) {
  125. this.$refs.uTips.show({
  126. title: '编辑地址成功',
  127. type: 'success',
  128. })
  129. setTimeout(() => {
  130. uni.navigateBack()
  131. }, 1000)
  132. } else {
  133. this.$refs.uTips.show({
  134. title: res.msg,
  135. type: 'warning',
  136. })
  137. }
  138. }).catch(res => {
  139. this.$refs.uTips.show({
  140. title: '编辑地址失败',
  141. type: 'warning',
  142. })
  143. })
  144. }
  145. },
  146. },
  147. }
  148. </script>
  149. <style>
  150. page {
  151. background-color: #f7f7f7;
  152. }
  153. </style>
  154. <style lang="less" scoped>
  155. page {
  156. width: 100%;
  157. height: 100%;
  158. }
  159. .container {
  160. width: 100%;
  161. height: 100%;
  162. float: left;
  163. box-sizing: border-box;
  164. background-color: #f7f7f7;
  165. padding-bottom: 70px;
  166. overflow-y: auto;
  167. .address-form {
  168. width: 100%;
  169. float: left;
  170. box-sizing: border-box;
  171. padding: 0 15px;
  172. background-color: #ffffff;
  173. /deep/.u-field {
  174. padding-left: 0px;
  175. padding-right: 0px;
  176. }
  177. /deep/.u-cell {
  178. padding-left: 0px;
  179. padding-right: 0px;
  180. }
  181. }
  182. .address-form-top {
  183. /deep/.u-cell_title {
  184. color: #999999;
  185. }
  186. /deep/.u-label-text {
  187. color: #999999;
  188. }
  189. }
  190. .address-form-bottom {
  191. margin-top: 10px;
  192. /deep/.u-cell_title {
  193. color: #333333;
  194. }
  195. /deep/.u-label-text {
  196. color: #333333;
  197. }
  198. }
  199. .address-handle {
  200. width: calc(100% - 30px);
  201. height: 40px;
  202. position: fixed;
  203. bottom: 20px;
  204. left: 15px;
  205. .handle-custom {
  206. background-color: #56a83a;
  207. /deep/button {
  208. background-color: #56a83a;
  209. }
  210. /deep/.u-btn--success--disabled {
  211. background-color: #74bd60 !important;
  212. }
  213. }
  214. }
  215. }
  216. </style>