AddressForm.vue 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <template><page-meta :root-font-size="fontSize+'px'"></page-meta>
  2. <member-base :show="false"><view class="div container">
  3. <view class="div common-header-wrap">
  4. <view :style="'height:'+navHeight+'px'"></view>
  5. <view class="common-header-holder"></view>
  6. <view class="common-header-fixed">
  7. <title-header />
  8. <uni-nav-bar :title="getTitle" class="common-header" left-icon="back" @clickLeft="goBack">
  9. </uni-nav-bar>
  10. </view>
  11. </view>
  12. <view v-if="config && config.chain_isuse==1" class="div notice">
  13. <text class="span">使用代收点</text>
  14. <view class="div common-btn ds-button-small btn" @click="goChain"><text class="span">使用</text></view>
  15. </view>
  16. <view class="div main-content">
  17. <flex-line class="field-line" :show-border="true"><text class="span field-name">姓名</text><view class="div field-line-right" slot="right"><input class="field-input" v-model="address_info.address_realname" /></view></flex-line>
  18. <flex-line class="field-line" :show-border="true"><text class="span field-name">手机</text><view class="div field-line-right" slot="right"><input class="field-input" v-model="address_info.address_mob_phone" /></view></flex-line>
  19. <flex-line :is-link="true" :show-border="true">
  20. <text class="span">地区</text>
  21. <view class="div" slot="right" @click="onRegion">
  22. <text class="span">{{address_info.area_info}}</text>
  23. </view>
  24. </flex-line>
  25. <flex-line class="field-line" :show-border="true"><text class="span field-name">地址</text><view class="div field-line-right" slot="right"><input class="field-input" v-model="address_info.address_detail" /></view></flex-line>
  26. <flex-line :show-border="true"><text class="span line-name">默认地址</text><view class="div" slot="right"><switch @change="switchChange" :checked="!!address_info.address_is_default"></switch></view></flex-line>
  27. <view class="div pt-10 pb-10"><view class="div common-btn ds-button-large" @click="submit">{{getSumitTitle}}</view></view>
  28. </view>
  29. <region-picker ref="picker" v-on:onConfirm="onPickerConfirm"></region-picker>
  30. </view></member-base>
  31. </template>
  32. <script>
  33. import {getFontSize} from '@/util/common'
  34. import TitleHeader from '../../TitleHeader'
  35. import { urlencode } from '@/util/common'
  36. import MemberBase from '../MemberBase'
  37. import { getPointByAddress } from '../../../util/bmap'
  38. import { mapState, mapMutations, mapActions } from 'vuex'
  39. import RegionPicker from '../../RegionPicker'
  40. import MapSelect from '../../MapSelect'
  41. import { getAddressInfo, addAddress, editAddress } from '../../../api/memberAddress'
  42. import flexLine from '../../flexLine'
  43. export default {
  44. components:{
  45. TitleHeader,
  46. MemberBase,
  47. flexLine,
  48. RegionPicker,
  49. // MapSelect
  50. },
  51. computed:{
  52. fontSize(){
  53. return getFontSize()
  54. },
  55. ...mapState({
  56. config: state => state.config.config
  57. }),
  58. isAddMode () {
  59. let mode = this.action
  60. // add: 添加地址,edit: 编辑地址
  61. if (mode === 'add') {
  62. return true
  63. } else {
  64. return false
  65. }
  66. },
  67. getTitle () {
  68. if (this.isAddMode) {
  69. return '新增地址'
  70. } else {
  71. return '修改收货地址'
  72. }
  73. },
  74. getSumitTitle () {
  75. let isFromCheckout = this.isFromCheckout
  76. if (isFromCheckout) {
  77. return '保存并使用'
  78. } else {
  79. return '保存'
  80. }
  81. }
  82. },
  83. mounted(){
  84. // #ifdef MP-WEIXIN
  85. this.navHeight = uni.getMenuButtonBoundingClientRect().top
  86. // #endif
  87. },
  88. data(){
  89. return {
  90. navHeight: 0,
  91. isFromCheckout:0,
  92. action:'',
  93. popMap: false,
  94. address_id: 0,
  95. goBackLevel : 1,
  96. address_info: {
  97. address_realname: '',
  98. address_mob_phone: '',
  99. address_detail: '',
  100. address_is_default: true,
  101. area_info: '请选择地区',
  102. city_id: 0,
  103. area_id: 0,
  104. address_longitude: 0,
  105. address_latitude: 0
  106. }
  107. }
  108. },
  109. onLoad: function (option) {
  110. this.action=option.action
  111. this.isFromCheckout=option.isFromCheckout
  112. this.goBackLevel =option.goBackLevel ? parseInt(option.goBackLevel) : 1
  113. this.fetchConfig()
  114. if (!this.isAddMode) {
  115. this.address_id = option.address_id
  116. getAddressInfo(this.address_id).then(res => {
  117. this.address_info.address_realname = res.result.address_info.address_realname
  118. this.address_info.address_mob_phone = res.result.address_info.address_mob_phone
  119. this.address_info.address_detail = res.result.address_info.address_detail
  120. this.address_info.area_info = res.result.address_info.area_info
  121. this.address_info.city_id = res.result.address_info.city_id
  122. this.address_info.area_id = res.result.address_info.area_id
  123. this.address_info.address_longitude = res.result.address_info.address_longitude
  124. this.address_info.address_latitude = res.result.address_info.address_latitude
  125. if (res.result.address_info.address_is_default === '1') {
  126. this.address_info.address_is_default = true
  127. } else {
  128. this.address_info.address_is_default = false
  129. }
  130. }).catch(function (error) {
  131. uni.showToast({icon:'none',title: error.message})
  132. })
  133. }
  134. },
  135. methods: {
  136. ...mapActions({
  137. fetchConfig: 'fetchConfig'
  138. }),
  139. switchChange(e){
  140. this.address_info.address_is_default=e.detail.value
  141. },
  142. goChain () {
  143. uni.navigateTo({ url: '/pages/member/address/ChainForm'+'?'+urlencode( { action: 'add' } )})
  144. },
  145. goBack () {
  146. uni.navigateBack({delta:this.goBackLevel})
  147. },
  148. getPosition () {
  149. getPointByAddress(this.address_info.area_info + this.address_info.address_detail).then(res => {
  150. if (res.result.location) {
  151. this.address_info.address_longitude = res.result.location.lng
  152. this.address_info.address_latitude = res.result.location.lat
  153. }
  154. }).catch(function (error) {
  155. uni.showToast({icon:'none',title: error.message})
  156. })
  157. },
  158. setPosition (lat, lng, name) {
  159. this.popMap = false
  160. this.address_info.address_detail = name
  161. this.address_info.address_longitude = lng
  162. this.address_info.address_latitude = lat
  163. },
  164. submit () {
  165. if (this.address_info.address_realname === '') {
  166. uni.showToast({icon:'none',title: '请填写收件人姓名'})
  167. return
  168. }
  169. if (this.address_info.address_realname.length === 0) {
  170. uni.showToast({icon:'none',title: '请填写收件人姓名'})
  171. return
  172. }
  173. if (this.address_info.address_realname.length < 2 || this.address_info.address_realname.length > 15) {
  174. uni.showToast({icon:'none',title: '2-15个字符限制'})
  175. return
  176. }
  177. if (this.address_info.address_mob_phone === '') {
  178. uni.showToast({icon:'none',title: '请填写手机号码'})
  179. return
  180. }
  181. if (this.address_info.address_mob_phone.length === 0) {
  182. uni.showToast({icon:'none',title: '请填写手机号码'})
  183. return
  184. }
  185. if (this.address_info.area_id === 0 || this.address_info.area_id === undefined) {
  186. uni.showToast({icon:'none',title: '请选择所在地区'})
  187. return
  188. }
  189. if (this.address_info.address_detail === '') {
  190. uni.showToast({icon:'none',title: '请填写详细地址'})
  191. return
  192. }
  193. if (this.address_info.address_detail.length === 0) {
  194. uni.showToast({icon:'none',title: '请填写详细地址'})
  195. }
  196. if (this.address_info.address_longitude === 0 || this.address_info.address_latitude === 0) {
  197. // uni.showToast({icon:'none',title: '请在地图选址'})
  198. }
  199. if (this.isAddMode) {
  200. uni.showLoading({ title: '加载中' })
  201. addAddress(this.address_info).then(
  202. (response) => {
  203. uni.hideLoading()
  204. this.updateSelectedAddress()
  205. }, (error) => {
  206. uni.hideLoading()
  207. uni.showToast({icon:'none',title: error.message})
  208. })
  209. } else {
  210. uni.showLoading({ title: '加载中' })
  211. editAddress(this.address_info, this.address_id).then(
  212. (response) => {
  213. uni.hideLoading()
  214. this.updateSelectedAddress()
  215. }, (error) => {
  216. uni.hideLoading()
  217. uni.showToast({icon:'none',title: error.message})
  218. })
  219. }
  220. },
  221. onRegion (picker, values) {
  222. this.$refs.picker.show()
  223. },
  224. onPickerConfirm (values) {
  225. this.address_info.area_info = this.getRegionStr(values)
  226. this.address_info.area_id = values[2].area_id
  227. this.address_info.city_id = values[1].area_id
  228. },
  229. getRegionStr (values) {
  230. let title = ''
  231. for (let i = 0; i < values.length; i++) {
  232. const element = values[i]
  233. if (i !== 0) {
  234. title = title + ' ' + element.area_name
  235. } else {
  236. title = title + element.area_name
  237. }
  238. }
  239. return title
  240. },
  241. updateSelectedAddress () {
  242. uni.navigateBack({delta:1})
  243. }
  244. }
  245. }
  246. </script>
  247. <style lang="scss" scoped>
  248. .main-content{background: #fff;padding:0 $pageSpace}
  249. .right-arrow{transform: rotate(-90deg);color:#ddd;font-size:$fontSize;display: inline-block;}
  250. .input-wrap{position: relative;
  251. .i{position: absolute;right:0;top:0;line-height: 2.4rem;display: block;width:2rem;text-align: center;font-size:$h1}
  252. }
  253. .notice{font-size:$subFontSize;padding:.5rem;background:#FCF8E3;color:#C09853;line-height: 1.65rem;
  254. .red{color:#F00}
  255. .btn{float:right}
  256. }
  257. </style>