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.

262 lines
7.6 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. if(this.optionType=='edit'){
  135. params.id = this.addressId;
  136. updateAddress(params).then(res=>{
  137. if(res&&res==1){
  138. this.$globalData.newOrderData.currentAddress={
  139. id:params.id,
  140. name:params.name,
  141. phone:params.phone,
  142. province:params.province,
  143. city:params.city,
  144. district:params.district,
  145. detailAddress:params.detailAddress,
  146. }
  147. this.loading=false
  148. uni.redirectTo({
  149. url: `/pages/newOrder/serviceNew`
  150. });
  151. }else {
  152. this.loading=false
  153. this.$modal.showToast('更新地址失败')
  154. }
  155. })
  156. } else if(this.optionType=='add'){
  157. addAddress(params).then(res=>{
  158. if(res&&res>0){
  159. this.$globalData.newOrderData.currentAddress={
  160. id:res,
  161. name:params.name,
  162. phone:params.phone,
  163. province:params.province,
  164. city:params.city,
  165. district:params.district,
  166. detailAddress:params.detailAddress,
  167. }
  168. this.loading=false
  169. uni.redirectTo({
  170. url: `/pages/newOrder/serviceNew`
  171. });
  172. }else {
  173. this.loading=false
  174. this.$modal.showToast('新增地址失败')
  175. }
  176. })
  177. }
  178. },
  179. getCityList() {
  180. getCity().then(res => {
  181. if (res.code == 200) {
  182. console.log('city', JSON.parse(res.msg));
  183. this.cityList = JSON.parse(res.msg)
  184. const cityLabels = this.cityList.map(item =>item.city)
  185. this.multiArray[0] = cityLabels
  186. this.multiArray[1] = this.cityList[0].region.map(e=> e.region)
  187. } else {
  188. this.$modal.showToast('获取城市失败,请联系系统管理员!');
  189. }
  190. })
  191. },
  192. columnchange(e){
  193. console.log(e)
  194. // 当滚动切换一级分类时,为当前的一级分类添加它的子类
  195. if (e.detail.column == 0) {
  196. const currentCity = this.cityList[e.detail.value]
  197. // #ifdef H5
  198. // 在小程序中直接赋值无效 H5 可直接赋值
  199. this.multiArray[1] = currentCity.region.map(e=>e.region)
  200. // #endif
  201. // #ifdef MP-WEIXIN
  202. // 在 H5 环境下 $set 会导致一级分类无法滚动, 小程序正常运行
  203. this.$set(this.multiArray, 1, currentCity.region.map(e=>e.region))
  204. // #endif
  205. this.multiIndex=[e.detail.value,0]
  206. }
  207. },
  208. bindMultiPickerColumnChange(e) {
  209. console.log('值为:' + e.detail.value)
  210. this.multiIndex = e.detail.value
  211. this.addressInfo.province = this.multiArray[0][this.multiIndex[0]]
  212. this.addressInfo.city = this.multiArray[0][this.multiIndex[0]]
  213. this.addressInfo.district = this.multiArray[1][this.multiIndex[1]]
  214. this.isSelected = true
  215. this.$forceUpdate()
  216. },
  217. groupChange(n) {
  218. console.log('groupChange', n);
  219. this.addressInfo.isDefault=+(!this.addressInfo.isDefault)
  220. },
  221. }
  222. }
  223. </script>
  224. <style lang="scss">
  225. .container {
  226. position: relative;
  227. height: 100%;
  228. padding-bottom: 90px;
  229. .address-save {
  230. background-color: #FFFFFF;
  231. padding: 10px 20px 40px;
  232. width: 100%;
  233. height: 90px;
  234. position: fixed;
  235. bottom: 0;
  236. z-index: 100;
  237. .address-save-btn {
  238. width: 100%;
  239. border-radius: 6px;
  240. background: #FFB13F;
  241. font-size: 16px;
  242. color: #FFFFFF;
  243. }
  244. }
  245. }
  246. .address-info{
  247. background-color: #fff;
  248. padding: 10px 20px;
  249. .u-radio-group{
  250. float: right;
  251. }
  252. }
  253. </style>