addressManage.vue 7.9 KB

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