add.vue 8.3 KB

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