add.vue 8.5 KB

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