add.vue 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. <!-- 添加修改地址 -->
  2. <template>
  3. <view class="address-wrap">
  4. <u-form :model="model" :rules="rules" ref="uForm" :errorType="errorType">
  5. <!-- 地址定位 -->
  6. <view class="location-item u-flex u-row-between u-p-20 u-m-b-20" @tap="chooseLocation">
  7. <view class="u-flex">
  8. <u-icon name="map-fill" color="#ff9447"></u-icon>
  9. <text style="color: #ff9447;">{{ chooseAddress }}</text>
  10. </view>
  11. <text class="u-iconfont uicon-arrow-right" style="color: #666"></text>
  12. </view>
  13. <view class="address-box">
  14. <!-- 地址表单 -->
  15. <u-form-item :labelStyle="labelStyle"   label-width="150" label-position="left" label="收货人:"
  16. prop="consignee">
  17. <u-input placeholder="请填写收货人姓名" v-model="model.consignee" type="text"></u-input>
  18. </u-form-item>
  19. <u-form-item :labelStyle="labelStyle"   label-width="150" label-position="left" label="手机号:"
  20. prop="phone">
  21. <u-input placeholder="请输入收货人手机号" v-model="model.phone" type="number"></u-input>
  22. </u-form-item>
  23. <u-form-item :labelStyle="labelStyle" label-width="150" label-position="left" label="所在地区:"
  24. prop="area_text">
  25. <u-input type="select" v-model="model.area_text" :select-open="showSelect" placeholder="请选择地区"
  26. @click="showSelect = true"></u-input>
  27. </u-form-item>
  28. <u-form-item :labelStyle="labelStyle" label-position="top" label-width="150" label="详细地址:"
  29. prop="address">
  30. <u-input border borderColor="#eee" height="150" type="textarea" v-model="model.address"
  31. placeholder="如道路、门牌号、小区、楼栋号、单元室等" />
  32. </u-form-item>
  33. </view>
  34. <!-- 设置默认 -->
  35. <view class="default-box u-flex u-row-between">
  36. <text class="title">设为默认地址</text>
  37. <u-switch v-model="model.isDefault" activeColor="#ff9447" size="40"></u-switch>
  38. </view>
  39. </u-form>
  40. <block v-if="!showSelect">
  41. <view class="" style="height: 90rpx;"></view>
  42. <view :style="{paddingBottom:safeAreaBottom}" class="footer-fixed center" style="bottom: 10%;">
  43. <view @click="submit" class="cu-btn df btn-bg-color round " style="width: 90%;height: 80rpx;">
  44. <text>保存收货地址</text>
  45. </view>
  46. </view>
  47. </block>
  48. <!-- 省市区选择器 -->
  49. <city-select v-model="showSelect" @city-change="getRegion"></city-select>
  50. </view>
  51. </template>
  52. <script>
  53. import citySelect from '../../comps/u-city-select.vue';
  54. export default {
  55. components: {
  56. citySelect
  57. },
  58. data() {
  59. return {
  60. showSelect: false, //省市区
  61. addressAreaList: [],
  62. addressId: 0, //收货地址ID
  63. labelStyle: {
  64. 'font-size': '28rpx',
  65. 'font-weight': '600',
  66. color: '#595959'
  67. },
  68. model: {
  69. id: '',
  70. userId:'',
  71. consignee: '',
  72. phone: '',
  73. area_text: '',
  74. address: '',
  75. isDefault: false,
  76. latitude: '',
  77. longitude: ''
  78. },
  79. rules: {
  80. phone: [{
  81. required: true,
  82. message: '请输入收货人手机号',
  83. trigger: ['change', 'blur']
  84. },
  85. {
  86. validator: (rule, value, callback) => {
  87. return this.$u.test.mobile(value);
  88. },
  89. message: '手机号码不正确',
  90. trigger: ['change', 'blur']
  91. }
  92. ],
  93. consignee: [{
  94. required: true,
  95. message: '请填写收货人姓名',
  96. trigger: ['change', 'blur']
  97. }],
  98. area_text: [{
  99. required: true,
  100. message: '请选择地区',
  101. trigger: ['change', 'blur']
  102. }],
  103. address: [{
  104. required: true,
  105. message: '请输入详细地址',
  106. trigger: ['change', 'blur']
  107. }]
  108. },
  109. errorType: ['message'],
  110. chooseAddress: '点击选择地理位置'
  111. };
  112. },
  113. computed: {},
  114. onReady() {
  115. this.$refs.uForm.setRules(this.rules);
  116. },
  117. onLoad(options) {
  118. this.addressId = options.addressId
  119. this.model.phone=this.vuex_phone
  120. this.model.userId=this.vuex_userId
  121. uni.setNavigationBarTitle({
  122. title: this.addressId ? '编辑收货地址' : '添加收货地址'
  123. });
  124. if (this.$isNotEmpty(this.addressId)) {
  125. //编辑
  126. this.fetchAddress()
  127. return
  128. }
  129. console.log("1111");
  130. //新增
  131. if (options.wxAddress) {
  132. // 微信导入
  133. this.wxAddressInit(options)
  134. }else{
  135. //定位获取
  136. this.getLocation()
  137. }
  138. },
  139. methods: {
  140. fetchAddress(){
  141. this.$api.address.detail({id:this.addressId}).then(res=>{
  142. if (this.$isNotEmpty(res.data)) {
  143. this.chooseAddress=res.data.address
  144. this.model=res.data
  145. this.model.area_text = res.data.provinceName + '-' + res.data.cityName + '-' + res.data.areaName
  146. }
  147. })
  148. },
  149. async analyseLocation(data, type = 0) {
  150. let params = {
  151. longitude: data.longitude,
  152. latitude: data.latitude
  153. }
  154. let res = await this.$api.activity.getLocation(params)
  155. let result = res.data.result
  156. this.model.area_text = result.address_component.province + '-' +
  157. result.address_component.city + '-' +
  158. result.address_component.district
  159. if (type == 1) {
  160. this.chooseAddress = result.formatted_addresses.recommend
  161. this.model.address= result.formatted_addresses.recommend
  162. }else{
  163. this.chooseAddress = data.name
  164. this.model.address= data.name
  165. }
  166. },
  167. async chooseLocation() {
  168. let res = await this.$mpi.chooseLocation()
  169. if (res.errMsg != 'chooseLocation:ok') {
  170. this.$u.toast('选择地址失败')
  171. return
  172. }
  173. this.analyseLocation(res)
  174. },
  175. async getLocation() {
  176. let res = await this.$mpi.getLocation()
  177. if (res.errMsg != 'getLocation:ok') {
  178. return
  179. }
  180. this.analyseLocation(res, 1)
  181. },
  182. getRegion(data) {
  183. console.log(data)
  184. this.model.area_text = data.province.label + '-' + data.city.label + '-' + data.area.label
  185. },
  186. // 微信导入数据格式
  187. wxAddressInit(options) {
  188. let wxAddress = JSON.parse(options.wxAddress); //微信导入
  189. console.log(wxAddress);
  190. this.model.consignee = wxAddress.userName;
  191. this.model.phone = wxAddress.telNumber;
  192. this.model.area_text = `${wxAddress.provinceName}-${wxAddress.cityName}-${wxAddress.countyName}`;
  193. this.model.address = wxAddress.detailInfo.replace(/%20/g, '');
  194. this.model.isDefault = false;
  195. this.chooseAddress=this.model.address
  196. },
  197. // 提交
  198. submit() {
  199. this.$refs.uForm.validate(valid => {
  200. valid ? this.editAddress() : console.log('验证失败');
  201. });
  202. },
  203. // 编辑添加地址
  204. editAddress() {
  205. let that = this;
  206. let area= this.model.area_text.split("-")
  207. let data=this.$u.deepClone(this.model)
  208. data.provinceName=area[0]
  209. data.cityName=area[1]
  210. data.areaName=area[2]
  211. this.$api.address.submit(data).then(res=>{
  212. if (res.success) {
  213. this.$dialog.showModalAndBack('操作成功')
  214. }
  215. })
  216. },
  217. // 删除收货地址
  218. deleteAddress() {
  219. const that = this;
  220. that.$http(
  221. 'address.del', {
  222. id: that.addressId
  223. },
  224. '删除中...'
  225. ).then(res => {
  226. if (res.code === 1) {
  227. that.$Router.back();
  228. }
  229. });
  230. }
  231. }
  232. };
  233. </script>
  234. <style lang="scss">
  235. // 点击定位
  236. .location-item {
  237. font-size: 28rpx;
  238. font-weight: 500;
  239. background-color: #fff;
  240. color: rgba(167, 111, 13, 1);
  241. }
  242. // 表单
  243. .address-box {
  244. background-color: #fff;
  245. padding: 0 30rpx;
  246. }
  247. .address-item {
  248. height: 96rpx;
  249. background: #fff;
  250. border-bottom: 1rpx solid rgba(#dfdfdf, 0.5);
  251. padding: 0 25rpx;
  252. .item-title {
  253. color: #333;
  254. font-size: 28rpx;
  255. white-space: nowrap;
  256. }
  257. .inp {
  258. color: #333;
  259. font-size: 28rpx;
  260. }
  261. }
  262. .area-box {
  263. min-height: 120rpx;
  264. padding-bottom: 60rpx;
  265. background: #fff;
  266. padding: 30rpx 25rpx;
  267. .item-title {
  268. font-size: 28rpx;
  269. line-height: 28rpx;
  270. vertical-align: middle;
  271. white-space: nowrap;
  272. }
  273. .area-inp {
  274. color: #333;
  275. font-size: 28rpx;
  276. vertical-align: middle;
  277. margin-top: 8rpx;
  278. width: 550rpx;
  279. }
  280. }
  281. .default-box {
  282. height: 100rpx;
  283. padding: 0 25rpx;
  284. background: #fff;
  285. margin-top: 20rpx;
  286. .title {
  287. font-size: 28rpx;
  288. }
  289. .switch {
  290. transform: scale(0.8);
  291. }
  292. }
  293. // 底部按钮
  294. .foot_box {
  295. .delete-btn {
  296. line-height: 70rpx;
  297. background: linear-gradient(93deg, rgba(208, 19, 37, 1), rgba(237, 60, 48, 1));
  298. box-shadow: 0px 7rpx 6rpx 0px rgba(#ed3c30, 0.22);
  299. font-size: 28rpx;
  300. font-weight: 500;
  301. color: rgba(255, 255, 255, 1);
  302. border-radius: 35rpx;
  303. padding: 0;
  304. margin-right: 20rpx;
  305. }
  306. .save-btn {
  307. line-height: 70rpx;
  308. background: linear-gradient(90deg, rgba(233, 180, 97, 1), rgba(238, 204, 137, 1));
  309. border: 1rpx solid rgba(238, 238, 238, 1);
  310. border-radius: 40rpx;
  311. color: rgba(#fff, 0.9);
  312. }
  313. }
  314. </style>