You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

256 lines
7.4 KiB

3 months ago
  1. <template>
  2. <view class="container">
  3. <view class="address-info ">
  4. <u--form labelPosition="left" ref="uForm">
  5. <u-form-item required label="联系人" prop="addressInfo.name" labelWidth="120" borderBottom>
  6. <u--input v-model="addressInfo.name" placeholder="请输入联系人姓名" placeholderStyle="text-align:right;color:#AAA" border="none" inputAlign="right"></u--input>
  7. </u-form-item>
  8. <u-form-item required label="手机号码" prop="addressInfo.phone" labelWidth="120" borderBottom>
  9. <u--input v-model="addressInfo.phone" placeholder="请输入手机号码" placeholderStyle="text-align:right;color:#AAA" border="none" inputAlign="right"></u--input>
  10. </u-form-item>
  11. <u-form-item label="应急联络" prop="addressInfo.emergencyPhone" labelWidth="120" borderBottom>
  12. <u--input v-model="addressInfo.emergencyPhone" placeholder="请输入应急联络号码" placeholderStyle="text-align:right;color:#AAA" border="none" inputAlign="right"></u--input>
  13. </u-form-item>
  14. <u-form-item label="所在城市" prop="addressInfo.city" labelWidth="120" borderBottom>
  15. <picker mode="multiSelector" @columnchange="columnchange" @change="bindMultiPickerColumnChange" :value="multiIndex"
  16. :range="multiArray">
  17. <view style="display: flex;justify-content: space-between;">
  18. <u--input :value="addressInfo.city? (addressInfo.city +' '+ addressInfo.district):''"
  19. disabled disabledColor="#ffffff" placeholder="请选择所在城市"
  20. placeholderStyle="text-align:right;color:#AAA" border="none" inputAlign="right">
  21. </u--input>
  22. <u-icon slot="right" name="arrow-right" color="#AAA"></u-icon>
  23. </view>
  24. </picker>
  25. </u-form-item>
  26. <u-form-item label="详细地址" prop="addressInfo.detailAddress" labelWidth="120" borderBottom>
  27. <u--input v-model="addressInfo.detailAddress" placeholder="如楼栋、门牌号" placeholderStyle="text-align:right;color:#AAA;" border="none" inputAlign="right"></u--input>
  28. </u-form-item>
  29. <u-form-item label="设为默认地址" prop="addressInfo.isDefault" labelWidth="120">
  30. <view>
  31. <u-radio-group :value="addressInfo.isDefault?'默认':''" placement="column" @change="groupChange">
  32. <u-radio name="默认" activeColor="#ffbf60" labelColor="#ffbf60" label="默认"></u-radio>
  33. </u-radio-group>
  34. </view>
  35. </u-form-item>
  36. </u--form>
  37. </view>
  38. <view class="address-save">
  39. <button class="address-save-btn" @click="save" :loading="loading">保存</button>
  40. </view>
  41. </view>
  42. </template>
  43. <script>
  44. import {
  45. getCity
  46. } from '@/api/system/user.js'
  47. import {
  48. getAddressDetails,addAddress,updateAddress
  49. } from '@/api/system/address.js'
  50. export default{
  51. data(){
  52. return{
  53. loading:false,
  54. addressInfo:{
  55. name:"",
  56. phone:"",
  57. emergencyPhone:"",
  58. province:"",
  59. city:"",
  60. district:"",
  61. detailAddress:"",
  62. openId:"",
  63. isDefault:0
  64. },
  65. multiArray: [],
  66. cityList:[],
  67. multiIndex: [0, 0],
  68. addressId:'',
  69. }
  70. },
  71. onLoad(option) {
  72. this.optionType=option.optionType;
  73. if(this.optionType=='edit'){
  74. this.addressId=option.addressId;
  75. this.getAddressDetails(option.addressId);
  76. }
  77. },
  78. mounted() {
  79. this.getCityList()
  80. },
  81. methods:{
  82. getAddressDetails(id){
  83. getAddressDetails(id).then(res=>{
  84. if(res&& res.id){
  85. const {
  86. name,
  87. phone,
  88. emergencyPhone,
  89. province,
  90. city,
  91. district,
  92. detailAddress,
  93. openId,
  94. isDefault
  95. } = res;
  96. this.addressInfo = {
  97. name,
  98. phone,
  99. emergencyPhone,
  100. province,
  101. city,
  102. district,
  103. detailAddress,
  104. openId,
  105. isDefault
  106. };
  107. }else{
  108. this.$modal.showToast('获取地址详情失败')
  109. }
  110. })
  111. },
  112. save(){
  113. let params = this.addressInfo
  114. if(params.phone){
  115. if(!(/^1[3456789]\d{9}$/.test(params.phone))){
  116. this.$modal.showToast('手机号码有误')
  117. return false;
  118. }
  119. } else {
  120. this.$modal.showToast('请输入手机号')
  121. return false;
  122. }
  123. if(params.emergencyPhone){
  124. if(!(/^1[3456789]\d{9}$/.test(params.emergencyPhone))){
  125. this.$modal.showToast('手机号码有误')
  126. return false;
  127. }
  128. }
  129. if(!params.name){
  130. this.$modal.showToast('请输入联系人')
  131. return false;
  132. }
  133. this.loading=true
  134. console.log(params);
  135. if(this.optionType=='edit'){
  136. params.id = this.addressId;
  137. updateAddress(params).then(res=>{
  138. if(res&&res==1){
  139. setTimeout(() => {
  140. let len = getCurrentPages().length;
  141. this.loading=false
  142. if(len >= 2) {
  143. uni.navigateBack();
  144. }else {
  145. uni.redirectTo({url:'/pages/personalCenter/address'});
  146. }
  147. }, 2000);
  148. }else {
  149. this.loading=false
  150. this.$modal.showToast('更新地址失败')
  151. }
  152. })
  153. } else if(this.optionType=='add'){
  154. addAddress(params).then(res=>{
  155. if(res&&res>0){
  156. setTimeout(() => {
  157. let len = getCurrentPages().length;
  158. this.loading=false
  159. if(len >= 2) {
  160. uni.navigateBack();
  161. }else {
  162. uni.redirectTo({url:'/pages/personalCenter/address'});
  163. }
  164. }, 2000);
  165. }else {
  166. this.loading=false
  167. this.$modal.showToast('新增地址失败')
  168. }
  169. })
  170. }
  171. },
  172. getCityList() {
  173. getCity().then(res => {
  174. if (res.code == 200) {
  175. console.log('city', JSON.parse(res.msg));
  176. this.cityList = JSON.parse(res.msg)
  177. const cityLabels = this.cityList.map(item =>item.city)
  178. this.multiArray[0] = cityLabels
  179. this.multiArray[1] = this.cityList[0].region.map(e=> e.region)
  180. } else {
  181. this.$modal.showToast('获取城市失败,请联系系统管理员!');
  182. }
  183. })
  184. },
  185. columnchange(e){
  186. console.log(e)
  187. // 当滚动切换一级分类时,为当前的一级分类添加它的子类
  188. if (e.detail.column == 0) {
  189. const currentCity = this.cityList[e.detail.value]
  190. // #ifdef H5
  191. // 在小程序中直接赋值无效 H5 可直接赋值
  192. this.multiArray[1] = currentCity.region.map(e=>e.region)
  193. // #endif
  194. // #ifdef MP-WEIXIN
  195. // 在 H5 环境下 $set 会导致一级分类无法滚动, 小程序正常运行
  196. this.$set(this.multiArray, 1, currentCity.region.map(e=>e.region))
  197. // #endif
  198. this.multiIndex=[e.detail.value,0]
  199. }
  200. },
  201. bindMultiPickerColumnChange(e) {
  202. console.log('值为:' + e.detail.value)
  203. this.multiIndex = e.detail.value
  204. this.addressInfo.province = this.multiArray[0][this.multiIndex[0]]
  205. this.addressInfo.city = this.multiArray[0][this.multiIndex[0]]
  206. this.addressInfo.district = this.multiArray[1][this.multiIndex[1]]
  207. this.isSelected = true
  208. this.$forceUpdate()
  209. },
  210. groupChange(n) {
  211. console.log('groupChange', n);
  212. this.addressInfo.isDefault=+(!this.addressInfo.isDefault)
  213. },
  214. }
  215. }
  216. </script>
  217. <style lang="scss">
  218. .container {
  219. position: relative;
  220. height: 100%;
  221. padding-bottom: 90px;
  222. .address-save {
  223. background-color: #FFFFFF;
  224. padding: 10px 20px 40px;
  225. width: 100%;
  226. height: 90px;
  227. position: fixed;
  228. bottom: 0;
  229. z-index: 100;
  230. .address-save-btn {
  231. width: 100%;
  232. border-radius: 6px;
  233. background: #FFB13F;
  234. font-size: 16px;
  235. color: #FFFFFF;
  236. }
  237. }
  238. }
  239. .address-info{
  240. background-color: #fff;
  241. padding: 10px 20px;
  242. .u-radio-group{
  243. float: right;
  244. }
  245. }
  246. </style>