addressManage.vue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. console.log(option.data)
  73. let title = '添加新地址'
  74. if(option.type==='edit'){
  75. title = '编辑收货地址'
  76. this.isConfirm = false;
  77. this.addType = 2;
  78. this.addressData = JSON.parse(option.data)
  79. this.addressData.allAddress = `${this.addressData.province}-${this.addressData.city}-${this.addressData.town}`
  80. this.addressData.addressDetail = this.addressData.address
  81. this.addressData.defaultFlag = this.addressData.defaultFlag;
  82. if(this.addressData.defaultFlag == 1){
  83. this.switchDefault = true
  84. }else{
  85. this.switchDefault = false
  86. }
  87. }
  88. uni.setNavigationBarTitle({
  89. title
  90. })
  91. },
  92. methods: {
  93. switchChange(e){
  94. // console.log('==========',e.detail.value);
  95. if(e.detail.value== true){
  96. this.addressData.defaultFlag = 1;
  97. }else{
  98. this.addressData.defaultFlag = 0;
  99. }
  100. },
  101. // 三级联动选择
  102. showMulLinkageThreePicker() {
  103. this.isShowInput = true
  104. this.$refs.mpvueCityPicker.show()
  105. },
  106. onConfirm(e) {
  107. // console.log('地址',e);
  108. this.addressData.allAddress = e.name;
  109. this.addressData.townID = e.cityCode;
  110. },
  111. //提交
  112. confirm(){
  113. let data = this.addressData;
  114. if(!data.shouHuoRen){
  115. this.$util.msg('请填写收货人');
  116. }
  117. if(data.address =='所在地区'){
  118. this.$util.msg('请选择所在地区');
  119. }
  120. if(!/(^1[0-9][0-9]{9}$)/.test(data.mobile)){
  121. this.$util.msg('请输入正确的手机号码');
  122. return;
  123. }
  124. if(this.addType ==1){
  125. this.params ={
  126. userID:data.userID , //用户id ,只在新增收货地址时传
  127. shouHuoRen:data.shouHuoRen ,//收货人
  128. townID:data.townID, //区ID
  129. addressDetail:data.addressDetail,//地址
  130. mobile:data.mobile, //手机
  131. defaultFlag:data.defaultFlag//是否默认收货地址(0 不是默认,1 默认)
  132. }
  133. this.postAddressData(this.params)
  134. }else{
  135. this.params = this.addressData;
  136. this.postAddressData(this.params)
  137. }
  138. },
  139. postAddressData(res){
  140. this.btnText(true)
  141. addNewAddress(res).then( response=>{
  142. this.$util.msg('添加成功',2000);
  143. this.btnText(false)
  144. setTimeout(()=>{
  145. uni.navigateBack();
  146. },2000)
  147. }).catch(response =>{
  148. this.$util.msg(response.msg,2000);
  149. setTimeout(function(){
  150. uni.switchTab({
  151. url:'/pages/tabBar/home/home'
  152. })
  153. },1000)
  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. // console.log(this.addressData.shouHuoRen)
  168. this.initInput();
  169. },
  170. onMobile(e){
  171. this.addressData.mobile = e.detail.value;
  172. // console.log(this.addressData.mobile)
  173. this.initInput();
  174. },
  175. onTextareaInput(e){
  176. this.addressData.addressDetail = e.detail.value;
  177. // console.log(this.addressData.addressDetail)
  178. this.initInput();
  179. },
  180. initInput(){
  181. if(this.addressData.shouHuoRen !== "" && this.addressData.mobile !=="" && this.addressData.addressDetail !==""){
  182. this.isConfirm =false;
  183. }else{
  184. this.isConfirm =true;
  185. }
  186. }
  187. },
  188. onShow() {
  189. this.$api.getStorage().then((resolve) =>{
  190. this.addressData.userID = resolve.userID
  191. })
  192. }
  193. }
  194. </script>
  195. <style lang="scss">
  196. page{
  197. height: auto;
  198. background: $bg-color;
  199. border-top: 1px solid #EBEBEB;
  200. }
  201. .add-content{
  202. width: 702rpx;
  203. padding:0 24rpx;
  204. }
  205. .icon-xiayibu{
  206. font-size: 36rpx;
  207. color: $text-color;
  208. }
  209. .text-textarea{
  210. width: 654rpx;
  211. height: 216rpx;
  212. padding: 24rpx;
  213. background: #F7F7F7;
  214. .textarea{
  215. width: 100%;
  216. height: 100%;
  217. font-size: $font-size-28;
  218. color: $text-color;
  219. z-index: 1;
  220. }
  221. .textarea.hide{
  222. opacity: 0;
  223. }
  224. .textarea.show{
  225. color: #999999;
  226. }
  227. }
  228. .default-row{
  229. background: #FFFFFF;
  230. margin-top: 16upx;
  231. .tit{
  232. font-size: $font-size-28;
  233. line-height: 40rpx;
  234. color: #333333;
  235. flex: 1;
  236. }
  237. switch{
  238. transform: translateX(16upx) scale(.9);
  239. }
  240. }
  241. .add-btn{
  242. width: 702rpx;
  243. height: 88rpx;
  244. font-size: $font-size-28;
  245. line-height: 88rpx;
  246. color: #FFFFFF;
  247. margin: 0 auto;
  248. text-align: center;
  249. background: $btn-confirm;
  250. border-radius: 14rpx;
  251. margin-top: 80rpx;
  252. }
  253. .add-btn.disabled{
  254. background: #F8F8F8;
  255. border-radius: 14rpx;
  256. }
  257. </style>