addressManage.vue 7.0 KB

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