addressManage.vue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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.address ?addressData.address :'详细地址:如道路、门牌号、小区、楼房号、单元室等'}}</view>
  18. <textarea v-else
  19. class="textarea"
  20. type="text"
  21. v-model="addressData.address"
  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. address: '', //地址
  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.address = optionData.address?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. address:data.address,//地址
  134. mobile:data.mobile, //手机
  135. defaultFlag:data.defaultFlag//是否默认收货地址(0 不是默认,1 默认)
  136. }
  137. this.postAddressData(this.params)
  138. }else{
  139. this.params = this.addressData;
  140. this.postAddressData(this.params)
  141. }
  142. },
  143. postAddressData(params){
  144. this.btnText(true)
  145. addNewAddress(params).then( response=>{
  146. this.$util.msg('保存成功',1500,true,'success')
  147. this.btnText(false)
  148. setTimeout(()=>{
  149. uni.navigateBack();
  150. },2000)
  151. }).catch(response =>{
  152. this.$util.msg(response.msg,2000);
  153. this.btnText(false)
  154. })
  155. },
  156. btnText(flg){
  157. if(flg){
  158. this.isConfirm = true;
  159. this.buttonText = '保存中...'
  160. }else{
  161. this.isConfirm = false;
  162. this.buttonText = '保存'
  163. }
  164. },
  165. onShouHuoRen(e){
  166. this.addressData.shouHuoRen = e.detail.value;
  167. this.initInput();
  168. },
  169. onMobile(e){
  170. this.addressData.mobile = e.detail.value;
  171. this.initInput();
  172. },
  173. onTextareaInput(e){
  174. this.addressData.address = e.detail.value;
  175. this.initInput();
  176. },
  177. initInput(){
  178. if(this.addressData.shouHuoRen !== "" && this.addressData.mobile !=="" && this.addressData.address !==""){
  179. this.isConfirm =false;
  180. }else{
  181. this.isConfirm =true;
  182. }
  183. }
  184. },
  185. onShow() {
  186. this.$api.getStorage().then((resolve) =>{
  187. this.addressData.userID = resolve.userID
  188. })
  189. }
  190. }
  191. </script>
  192. <style lang="scss">
  193. page{
  194. height: auto;
  195. background: $bg-color;
  196. border-top: 1px solid #EBEBEB;
  197. }
  198. .add-content{
  199. width: 702rpx;
  200. padding:0 24rpx;
  201. }
  202. .icon-xiayibu{
  203. font-size: 36rpx;
  204. color: $text-color;
  205. }
  206. .text-textarea{
  207. width: 654rpx;
  208. height: 216rpx;
  209. padding: 24rpx;
  210. background: #F7F7F7;
  211. .textarea{
  212. width: 100%;
  213. height: 100%;
  214. font-size: $font-size-28;
  215. color: $text-color;
  216. z-index: 1;
  217. }
  218. .textarea.hide{
  219. opacity: 0;
  220. }
  221. .textarea.show{
  222. color: #999999;
  223. }
  224. }
  225. .default-row{
  226. background: #FFFFFF;
  227. margin-top: 16upx;
  228. .tit{
  229. font-size: $font-size-28;
  230. line-height: 40rpx;
  231. color: #333333;
  232. flex: 1;
  233. }
  234. switch{
  235. transform: translateX(16upx) scale(.9);
  236. }
  237. }
  238. .add-btn{
  239. width: 702rpx;
  240. height: 88rpx;
  241. font-size: $font-size-28;
  242. line-height: 88rpx;
  243. color: #FFFFFF;
  244. margin: 0 auto;
  245. text-align: center;
  246. background: $btn-confirm;
  247. border-radius: 14rpx;
  248. margin-top: 80rpx;
  249. }
  250. .add-btn.disabled{
  251. background: #F8F8F8;
  252. border-radius: 14rpx;
  253. }
  254. </style>