create.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <template>
  2. <view class="content">
  3. <view class="row b-b">
  4. <text class="tit">联系人</text>
  5. <input class="input" type="text" v-model="addressData.consignee" placeholder="收货人姓名" placeholder-class="placeholder" />
  6. </view>
  7. <view class="row b-b">
  8. <text class="tit">手机号</text>
  9. <input class="input" type="number" v-model="addressData.phone" placeholder="收货人手机号码" placeholder-class="placeholder" />
  10. </view>
  11. <view class="row b-b">
  12. <text class="tit">城市</text>
  13. <input placeholder="请选择城市" disabled="true" :value="addressData.province + ' ' + addressData.city + ' ' + addressData.county" @click="lotusAddressData.visible = true" class="input">
  14. <lotus-address v-on:choseVal="choseValue" :lotusAddressData="lotusAddressData"></lotus-address>
  15. </input>
  16. </view>
  17. <view class="row b-b">
  18. <text class="tit">地图:</text>
  19. <input class="input" @click="openMap()" type="text" value="" v-model="mapAddressName" placeholder="在地图上搜索并选择地址" disabled="true" />
  20. <text class="yticon icon-shouhuodizhi"></text>
  21. </view>
  22. <view class="row b-b">
  23. <text class="tit">详细</text>
  24. <input class="input" type="text" v-model="addressData.address" placeholder="街道、楼号、门牌" placeholder-class="placeholder" />
  25. </view>
  26. <view class="row default-row">
  27. <text class="tit">设为默认</text>
  28. <switch :checked="addressData.defaultAddress===0?false:true" color="#2AAC34" @change="switchChange" />
  29. </view>
  30. <button class="add-btn" @click="confirm">提交</button>
  31. </view>
  32. </template>
  33. <script>
  34. import lotusAddress from "@/components/Winglau14-lotusAddress/Winglau14-lotusAddress.vue"
  35. export default {
  36. data() {
  37. return {
  38. mapAddressName: '',
  39. addressData: {
  40. consignee: '',
  41. phone: '',
  42. province: '',
  43. city: '',
  44. county: '',
  45. address: '',
  46. defaultAddress: 0,
  47. def: false
  48. },
  49. lotusAddressData:{
  50. visible:false,
  51. provinceName:'',
  52. cityName:'',
  53. townName:'',
  54. },
  55. }
  56. },
  57. onLoad(option){
  58. let title = '新增收货地址';
  59. if(option.type==='edit'){
  60. title = '编辑收货地址'
  61. this.addressData = JSON.parse(option.data)
  62. }else {
  63. if(option.data){
  64. title = '新增收货地址'
  65. this.addressData = JSON.parse(option.data)
  66. }
  67. }
  68. this.manageType = option.type;
  69. uni.setNavigationBarTitle({
  70. title
  71. })
  72. },
  73. methods: {
  74. switchChange(e){
  75. this.addressData.defaultAddress = e.detail.value?1:0
  76. this.addressData.def = e.detail.value
  77. },
  78. choseValue(res){
  79. //res数据源包括已选省市区与省市区code
  80. this.lotusAddressData.visible = false;//visible为显示与关闭组件标识true显示false隐藏
  81. if(res.isChose){
  82. console.log(res)
  83. this.lotusAddressData.provinceName = res.provice;//省
  84. this.lotusAddressData.cityName = res.city;//市
  85. this.lotusAddressData.townName = res.town;//区
  86. //赋值到addressData
  87. this.addressData.province = res.provice
  88. this.addressData.city = res.city
  89. this.addressData.county = res.town
  90. }
  91. },
  92. openMap() {
  93. var _this = this;
  94. uni.chooseLocation({
  95. success: function(res) {
  96. console.log('res', res);
  97. _this.addressData.latitude = res.latitude;
  98. _this.addressData.longitude = res.longitude;
  99. _this.mapAddressName= res.name;
  100. _this.addressData.address = res.name;
  101. }
  102. });
  103. },
  104. //提交
  105. confirm(){
  106. const that = this
  107. let data = this.addressData;
  108. if(!data.consignee){
  109. this.$api.msg('请填写收货人姓名');
  110. return;
  111. }
  112. if (!/^(1[3|4|5|6|7|8|9][0-9])\d{8}$/.test(data.phone)) {
  113. that.$api.msg('请输入正确的手机号码');
  114. return
  115. }
  116. if (!data.province) {
  117. that.$api.msg('请选择省市区');
  118. return
  119. }
  120. if (!data.city) {
  121. that.$api.msg('请选择二级城市')
  122. return
  123. }
  124. if (!data.county) {
  125. that.$api.msg('请选择三级区或县')
  126. return
  127. }
  128. if(!data.address){
  129. that.$api.msg('请输入详细地址');
  130. return
  131. }
  132. //this.$api.prePage()获取上一页实例,可直接调用上页所有数据和方法,在App.vue定义
  133. //this.$api.msg(`地址${this.manageType=='edit' ? '修改': '添加'}成功`);
  134. if (that.manageType === 'edit') {
  135. that.$api.request('post', 'address/app/updateAddress', {
  136. ...that.addressData,
  137. addressId : that.addressData.id
  138. }).then(res => {
  139. // 修改为更通用的方式
  140. const pages = getCurrentPages();
  141. const prevPage = pages[pages.length - 2];
  142. if (prevPage && prevPage.$vm) {
  143. prevPage.$vm.loadData && prevPage.$vm.loadData(); // 调用上一页的刷新方法
  144. }
  145. uni.navigateBack();
  146. })
  147. } else {
  148. that.$api.request('post', 'address/app/addAddress', that.addressData).then(res => {
  149. const pages = getCurrentPages();
  150. const prevPage = pages[pages.length - 2];
  151. if (prevPage && prevPage.$vm) {
  152. prevPage.$vm.loadData && prevPage.$vm.loadData(); // 调用上一页的刷新方法
  153. }
  154. uni.navigateBack();
  155. })
  156. }
  157. },
  158. },
  159. components:{
  160. "lotus-address":lotusAddress
  161. },
  162. }
  163. </script>
  164. <style lang="scss">
  165. page{
  166. background: $page-color-base;
  167. padding-top: 16upx;
  168. }
  169. .row{
  170. display: flex;
  171. align-items: center;
  172. position: relative;
  173. padding:0 30upx;
  174. height: 110upx;
  175. background: #fff;
  176. .tit{
  177. flex-shrink: 0;
  178. width: 120upx;
  179. font-size: 30upx;
  180. color: $font-color-dark;
  181. }
  182. .input{
  183. flex: 1;
  184. font-size: 30upx;
  185. color: $font-color-dark;
  186. }
  187. .icon-shouhuodizhi{
  188. font-size: 36upx;
  189. color: $font-color-light;
  190. }
  191. }
  192. .default-row{
  193. margin-top: 16upx;
  194. .tit{
  195. flex: 1;
  196. }
  197. switch{
  198. transform: translateX(16upx) scale(.9);
  199. }
  200. }
  201. .add-btn{
  202. display: flex;
  203. align-items: center;
  204. justify-content: center;
  205. width: 690upx;
  206. height: 80upx;
  207. margin: 60upx auto;
  208. font-size: $font-lg;
  209. color: #fff;
  210. background-color: $base-color;
  211. border-radius: 10upx;
  212. // box-shadow: 1px 2px 5px rgba(219, 63, 96, 0.4);
  213. }
  214. </style>