addressManage.vue 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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.getComStorage('orderUserInfo').then(resolve => {
  231. this.addressData.userId = resolve.userId ? resolve.userId : 0
  232. console.log('userId', this.addressData.userId)
  233. })
  234. }
  235. }
  236. </script>
  237. <style lang="scss">
  238. page {
  239. height: auto;
  240. background: $bg-color;
  241. // border-top: 1px solid #EBEBEB;
  242. }
  243. .placeholder {
  244. color: #b2b2b2;
  245. }
  246. .add-content {
  247. width: 702rpx;
  248. padding: 0 24rpx;
  249. .input-row {
  250. align-items: center;
  251. position: relative;
  252. width: 702rpx;
  253. height: 88rpx;
  254. margin: 0 auto;
  255. border-bottom: 2rpx solid #e1e1e1;
  256. .label {
  257. width: 100rpx;
  258. line-height: 88rpx;
  259. color: #666666;
  260. font-size: $font-size-28;
  261. float: left;
  262. &.address {
  263. width: 120rpx;
  264. }
  265. }
  266. .input {
  267. width: 500rpx;
  268. height: 100%;
  269. font-size: $font-size-28;
  270. line-height: 88rpx;
  271. display: inline-block;
  272. color: #333333;
  273. float: left;
  274. padding-left: 20rpx;
  275. }
  276. &.b-b {
  277. margin: 20rpx 0;
  278. }
  279. }
  280. }
  281. .icon-xiayibu {
  282. font-size: 36rpx;
  283. color: #b2b2b2;
  284. position: absolute;
  285. right: 0;
  286. top: 10rpx;
  287. }
  288. .text-textarea {
  289. width: 654rpx;
  290. height: 147rpx;
  291. padding: 24rpx;
  292. // background: #F7F7F7;
  293. border-bottom: 2rpx solid #e1e1e1;
  294. .textarea {
  295. width: 100%;
  296. height: 100%;
  297. font-size: $font-size-28;
  298. color: $text-color;
  299. z-index: 1;
  300. }
  301. .textarea.hide {
  302. opacity: 0;
  303. }
  304. .textarea.show {
  305. color: #999999;
  306. }
  307. }
  308. .default-row {
  309. background: #ffffff;
  310. margin-top: 16upx;
  311. .tit {
  312. font-size: $font-size-28;
  313. line-height: 40rpx;
  314. color: #333333;
  315. flex: 1;
  316. }
  317. switch {
  318. transform: translateX(16upx) scale(0.9);
  319. }
  320. }
  321. .add-btn {
  322. width: 702rpx;
  323. height: 90rpx;
  324. font-size: $font-size-30;
  325. line-height: 90rpx;
  326. color: #ffffff;
  327. margin: 0 auto;
  328. text-align: center;
  329. background: $btn-confirm;
  330. border-radius: 45rpx;
  331. border-radius: 44rpx;
  332. margin-top: 80rpx;
  333. }
  334. .add-btn.disabled {
  335. background: #f8f8f8;
  336. border-radius: 44rpx;
  337. }
  338. </style>