addressManage.vue 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. <template>
  2. <view class="container clearfix">
  3. <view class="add-content">
  4. <view class="input-row b-b">
  5. <view class="label">收货人:</view>
  6. <input class="input" type="text" v-model="addressData.receiver" @input="onShouHuoRen" placeholder="请输入收货人姓名" placeholder-class="placeholder" />
  7. </view>
  8. <view class="input-row b-b">
  9. <view class="label">手机号:</view>
  10. <input class="input" type="number" maxlength="11" v-model="addressData.mobile" @input="onMobile" placeholder="请输入手机号码" placeholder-class="placeholder" />
  11. </view>
  12. <view class="input-row b-b" @click="showMulLinkageThreePicker">
  13. <view class="label address">所在地区:</view>
  14. <text class="input" :style="addressData.allAddress=='请选择收货地区'?'color:#b2b2b2':''">
  15. {{addressData.allAddress}}
  16. </text>
  17. <text class="iconfont icon-xiangyou"></text>
  18. </view>
  19. <view class="text-textarea b-b">
  20. <view class="textarea show" v-if="isShowInput">
  21. {{addressData.address ? addressData.address :'详细地址:如道路、门牌号、小区、楼房号、单元室等'}}
  22. </view>
  23. <textarea v-else
  24. class="textarea"
  25. type="text"
  26. v-model="addressData.address"
  27. placeholder="详细地址:如道路、门牌号、小区、楼房号、单元室等"
  28. placeholder-class="placeholder"
  29. maxlength="50"
  30. @input="onTextareaInput"
  31. :class="isShowInput ? '':''"
  32. />
  33. </view>
  34. <view class="text-input default-row">
  35. <text class="tit">设为默认地址</text>
  36. <switch :checked="switchDefault" color="#F3B574" @change="switchChange" style="transform:scale(0.8)" />
  37. </view>
  38. <button class="add-btn" :disabled="isConfirm" :class="[isConfirm == true ? 'disabled':'']" @click="confirm">{{buttonText}}</button>
  39. </view>
  40. <mpvue-city-picker :themeColor="themeColor"
  41. ref="mpvueCityPicker"
  42. :pickerValueDefault="cityPickerValueDefault"
  43. @onCancel="onCancel"
  44. @onConfirm="onConfirm">
  45. </mpvue-city-picker>
  46. </view>
  47. </template>
  48. <script>
  49. import mpvueCityPicker from '@/components/mpvue-citypicker/mpvueCityPicker.vue'
  50. import authorize from '@/common/config/authorize.js'
  51. import { mapState, mapMutations } from 'vuex'
  52. export default {
  53. components:{
  54. mpvueCityPicker
  55. },
  56. data() {
  57. return {
  58. buttonText:'保存',
  59. addType:1, //记录添加地址的类型
  60. isConfirm:true,
  61. isShowInput:false,
  62. switchDefault:false,
  63. addressData:{
  64. addressId:'',
  65. allAddress:'请选择收货地区',
  66. userId: '', //用户id ,只在新增收货地址时传
  67. receiver: '', //收货人
  68. mobile:'', //收货人手机
  69. townId:'', //区ID
  70. address: '', //地址
  71. defaultFlag:0, //是否默认收货地址(0 不是默认,1 默认)
  72. },
  73. params:{}, //参数
  74. isSelect: false,
  75. }
  76. },
  77. onLoad(option){
  78. let title = '添加新地址'
  79. if(option.type==='edit'){
  80. title = '编辑收货地址'
  81. this.isConfirm = false
  82. this.addType = 2
  83. let optionData = JSON.parse(option.data)
  84. this.addressData.allAddress = `${optionData.province}-${optionData.city}-${optionData.town}`
  85. this.addressData.address = optionData.address?optionData.address:''
  86. this.addressData.defaultFlag = optionData.defaultFlag
  87. this.addressData.userId = optionData.userId
  88. this.addressData.mobile = optionData.mobile
  89. this.addressData.townId = optionData.townId
  90. this.addressData.addressId = optionData.addressId
  91. this.addressData.receiver = optionData.receiver
  92. if(this.addressData.defaultFlag == 1){
  93. this.switchDefault = true
  94. }else{
  95. this.switchDefault = false
  96. }
  97. }
  98. if(option.type=='select'){this.isSelect = true}
  99. uni.setNavigationBarTitle({
  100. title
  101. })
  102. },
  103. computed: {
  104. ...mapState(['userInfo'])
  105. },
  106. methods: {
  107. switchChange(e){
  108. if(e.detail.value== true){
  109. this.addressData.defaultFlag = '1'
  110. }else{
  111. this.addressData.defaultFlag = '0'
  112. }
  113. },
  114. // 三级联动选择
  115. showMulLinkageThreePicker() {
  116. this.isShowInput = true
  117. this.$refs.mpvueCityPicker.show()
  118. },
  119. onConfirm(e) {
  120. console.log(e)
  121. this.addressData.allAddress = e.name
  122. this.addressData.townId = e.townCode
  123. this.initInput()
  124. },
  125. //提交
  126. confirm(){
  127. let data = this.addressData
  128. if(!data.receiver){
  129. this.$util.msg('请填写收货人')
  130. }
  131. if(data.address =='所在地区'){
  132. this.$util.msg('请选择所在地区')
  133. }
  134. if(!/(^1[0-9][0-9]{9}$)/.test(data.mobile)){
  135. this.$util.msg('请输入正确的手机号码')
  136. return
  137. }
  138. if(this.addType ==1){
  139. this.params ={
  140. userId:data.userId , //用户id ,只在新增收货地址时传
  141. receiver:data.receiver ,//收货人
  142. townId:data.townId, //区ID
  143. address:data.address,//地址
  144. mobile:data.mobile, //手机
  145. defaultFlag:data.defaultFlag//是否默认收货地址(0 不是默认,1 默认)
  146. }
  147. this.postAddressData(this.params)
  148. }else{
  149. this.params = this.addressData
  150. this.postAddressData(this.params)
  151. }
  152. },
  153. postAddressData(params){
  154. this.btnText(true)
  155. this.UserService.AddressSave(params).then( response=>{
  156. this.$util.msg('保存成功',1500,true,'success')
  157. this.btnText(false)
  158. console.log('params', params)
  159. if(this.isSelect) {
  160. uni.setStorageSync('selectAddress', response.data)
  161. var pages = getCurrentPages()
  162. var prevPage = pages[pages.length - 2] //上一个页面
  163. prevPage.setData({select:'select'})
  164. }
  165. setTimeout(()=>{
  166. uni.navigateBack()
  167. },2000)
  168. }).catch(error =>{
  169. this.$util.msg(error.msg,2000)
  170. this.btnText(false)
  171. })
  172. },
  173. btnText(flg){
  174. if(flg){
  175. this.isConfirm = true
  176. this.buttonText = '保存中...'
  177. }else{
  178. this.isConfirm = false
  179. this.buttonText = '保存'
  180. }
  181. },
  182. onShouHuoRen(e){
  183. this.addressData.receiver = e.detail.value
  184. this.initInput()
  185. },
  186. onMobile(e){
  187. this.addressData.mobile = e.detail.value
  188. this.initInput()
  189. },
  190. onTextareaInput(e){
  191. this.addressData.address = e.detail.value
  192. this.initInput()
  193. },
  194. initInput(){
  195. if(this.addressData.receiver !== '' && this.addressData.mobile !=='' && this.addressData.address !=='' && this.addressData.townId!==''){
  196. this.isConfirm =false
  197. }else{
  198. this.isConfirm =true
  199. }
  200. }
  201. },
  202. onShow() {
  203. this.$api.getStorage().then((resolve) =>{
  204. this.addressData.userId = resolve.userId ? resolve.userId : 0
  205. })
  206. }
  207. }
  208. </script>
  209. <style lang="scss">
  210. page{
  211. height: auto;
  212. background: $bg-color;
  213. // border-top: 1px solid #EBEBEB;
  214. }
  215. .placeholder{color: #b2b2b2;}
  216. .add-content{
  217. width: 702rpx;
  218. padding:0 24rpx;
  219. .input-row{
  220. align-items: center;
  221. position: relative;
  222. width: 702rpx;
  223. height: 88rpx;
  224. margin: 0 auto;
  225. border-bottom: 2rpx solid #e1e1e1;
  226. .label{
  227. width:100rpx ;
  228. line-height: 88rpx;
  229. color: #666666;
  230. font-size: $font-size-28;
  231. float: left;
  232. &.address{
  233. width: 120rpx;
  234. }
  235. }
  236. .input{
  237. width: 500rpx;
  238. height: 100%;
  239. font-size: $font-size-28;
  240. line-height: 88rpx;
  241. display: inline-block;
  242. color: #333333;
  243. float: left;
  244. padding-left: 20rpx;
  245. }
  246. &.b-b{
  247. margin: 20rpx 0;
  248. }
  249. }
  250. }
  251. .icon-xiangyou{
  252. font-size: 36rpx;
  253. color: #b2b2b2;
  254. position: absolute;
  255. right: 0;
  256. top: 10rpx;
  257. }
  258. .text-textarea{
  259. width: 654rpx;
  260. height: 147rpx;
  261. padding: 24rpx;
  262. // background: #F7F7F7;
  263. border-bottom: 2rpx solid #e1e1e1;
  264. .textarea{
  265. width: 100%;
  266. height: 100%;
  267. font-size: $font-size-28;
  268. color: $text-color;
  269. z-index: 1;
  270. }
  271. .textarea.hide{
  272. opacity: 0;
  273. }
  274. .textarea.show{
  275. color: #999999;
  276. }
  277. }
  278. .default-row{
  279. background: #FFFFFF;
  280. margin-top: 16upx;
  281. .tit{
  282. font-size: $font-size-28;
  283. line-height: 40rpx;
  284. color: #333333;
  285. flex: 1;
  286. }
  287. switch{
  288. transform: translateX(16upx) scale(.9);
  289. }
  290. }
  291. .add-btn{
  292. width: 702rpx;
  293. height:90rpx;
  294. font-size: $font-size-30;
  295. line-height: 90rpx;
  296. color: #FFFFFF;
  297. margin: 0 auto;
  298. text-align: center;
  299. background: $btn-confirm;
  300. border-radius: 45rpx;
  301. border-radius: 44rpx;
  302. margin-top: 80rpx;
  303. }
  304. .add-btn.disabled{
  305. background: #F8F8F8;
  306. border-radius: 44rpx;
  307. }
  308. </style>