耀实惠小程序
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.

370 lines
8.5 KiB

2 years ago
  1. <template>
  2. <view class="wrap flex-1">
  3. <view class="top">
  4. <u-form :model="form" ref="uForm">
  5. <u-form-item prop="name" :border-bottom="false">
  6. <view class="item">
  7. <view class="left">收货人</view>
  8. <input type="text" placeholder-class="line" placeholder="请填写收货人姓名" v-model="form.name" />
  9. </view>
  10. </u-form-item>
  11. <u-form-item prop="phone" :border-bottom="false">
  12. <view class="item">
  13. <view class="left">手机号码</view>
  14. <input type="text" placeholder-class="line" maxlength="11" placeholder="请填写收货人手机号" v-model="form.phone" />
  15. </view>
  16. </u-form-item>
  17. <u-form-item prop="shippingAddress" :border-bottom="false">
  18. <view class="item" @tap="showRegionPicker">
  19. <view class="left">所在地区</view>
  20. <input disabled type="text" placeholder-class="line" placeholder="省市区县、乡镇等" v-model="form.shippingAddress" />
  21. </view>
  22. </u-form-item>
  23. <u-form-item prop="addressDetail" :border-bottom="false">
  24. <view class="item address">
  25. <view class="left">详细地址</view>
  26. <textarea type="text" placeholder-class="line" placeholder="街道、楼牌等" v-model="form.addressDetail" />
  27. </view>
  28. </u-form-item>
  29. </u-form>
  30. </view>
  31. <view class="bottom">
  32. <view class="default">
  33. <view class="left">
  34. <view class="set">设置默认地址</view>
  35. <view class="tips">提醒每次下单会默认推荐该地址</view>
  36. </view>
  37. <view class="right"><u-switch active-color="#01AEEA" v-model="form.defaultFlag" /></view>
  38. </view>
  39. </view>
  40. <view class="p-l-20 p-r-20">
  41. <view class="m-b-20">
  42. <u-button type="primary" shape="circle" @click="save">保存</u-button>
  43. </view>
  44. <u-button type="error" shape="circle" @click="delShow = true" v-if="options.type == 'edit'">删除</u-button>
  45. </view>
  46. <u-picker mode="region" ref="uPicker" v-model="show" @confirm="getSelectAddress"/>
  47. <u-modal v-model="delShow" show-cancel-button confirm-color="#fa3534" @confirm="delAddress" content="您确定要删除该地址吗?" />
  48. </view>
  49. </template>
  50. <script>
  51. export default {
  52. data() {
  53. return {
  54. form: {
  55. id: '',
  56. addressDetail: '',
  57. defaultFlag: false,
  58. name: '',
  59. phone: '',
  60. shippingAddress: '',
  61. },
  62. rules: {
  63. name: [
  64. {
  65. message: '请输入收货人',
  66. },
  67. {
  68. validator: (rule, value, callback) => {
  69. let username = /^[\u4E00-\u9FA5\uf900-\ufa2d·s]{2,99}$/;
  70. return username.test(value);
  71. },
  72. message: '请输入正确格式的收货人',
  73. trigger: ['blur','change']
  74. }
  75. ],
  76. phone: [
  77. {
  78. required: true,
  79. message: '请输入手机号码',
  80. trigger: ['change', 'blur']
  81. },
  82. {
  83. message: '请输入正确格式的手机号码',
  84. validator: (rule, value, callback) => {
  85. return this.$u.test.mobile(value);
  86. }
  87. }
  88. ],
  89. shippingAddress: [
  90. {
  91. required: true,
  92. message: '请选择所在地区',
  93. trigger: ['change', 'blur']
  94. }
  95. ],
  96. addressDetail: [
  97. {
  98. required: true,
  99. message: '请输入详细地址',
  100. trigger: ['change', 'blur']
  101. }
  102. ],
  103. },
  104. show: false,
  105. delShow: false,
  106. options: {},
  107. checked: false,
  108. id:null,
  109. uid: null
  110. };
  111. },
  112. onReady() {
  113. this.$refs.uForm.setRules(this.rules);
  114. },
  115. onLoad(options) {
  116. // 判断是从那来的 默认首页
  117. if(Object.keys(options).length>0){
  118. uni.setNavigationBarTitle({
  119. title: options.title
  120. })
  121. let info = options.info && JSON.parse(decodeURIComponent(options.info))
  122. this.options = { ...options, info}
  123. for (let key in this.form) {
  124. this.form[key] = info[key]
  125. }
  126. }
  127. // if(Object.keys(options).length>0){
  128. // const info = JSON.parse(options.info)
  129. // console.log(info);
  130. // this.name= info.name;
  131. // this.phone= info.phone;
  132. // this.address= info.shippingAddress;
  133. // this.address_detele= info.addressDetail;
  134. // this.checked= info.defaultFlag == 0? false: true;
  135. // this.id = info.id;
  136. // }
  137. },
  138. methods: {
  139. save() {
  140. const that = this
  141. this.$refs.uForm.validate(valid => {
  142. if (valid) {
  143. console.log(that.options)
  144. if(that.options.type === 'add' || that.options.type === 'setting') {
  145. that.add();
  146. }else {
  147. that.edit();
  148. }
  149. } else {
  150. console.log(that.options)
  151. console.log('验证失败');
  152. }
  153. });
  154. },
  155. edit() {
  156. uni.showLoading({
  157. title: '保存中...'
  158. })
  159. let params = { ...this.form, defaultFlag: this.form.defaultFlag ? 1 : 0, id: this.options.info.id}
  160. this.$api('editAddress',params).then(res => {
  161. let { code, result, message } = res
  162. uni.hideLoading()
  163. if(code == 200) {
  164. uni.$emit('editAddress', params)
  165. this.$Toast(message);
  166. setTimeout(() => {
  167. uni.navigateBack({
  168. delta: 1
  169. });
  170. },1500)
  171. }else{
  172. this.$Toast(message);
  173. }
  174. }).catch(err => {
  175. uni.hideLoading()
  176. this.$Toast(err.message);
  177. })
  178. },
  179. add () {
  180. uni.showLoading({
  181. title: '保存中...'
  182. })
  183. let params = { ...this.form, defaultFlag: this.form.defaultFlag ? 1 : 0}
  184. delete params.id;
  185. this.$api('addAddress', params).then(res => {
  186. let { code, result, message } = res
  187. uni.hideLoading()
  188. if(code == 200) {
  189. if (this.options.type === 'add') {
  190. uni.$emit('addAddress')
  191. } else if (this.options.type === 'setting') {
  192. uni.$emit('uploadAddress')
  193. }
  194. this.$Toast(message);
  195. const pageList = getCurrentPages();
  196. if(pageList.length == 1 && pageList.length == 2) {
  197. setTimeout(()=> {
  198. uni.switchTab({
  199. url: '/pages/home/home'
  200. })
  201. },1000)
  202. }else{
  203. setTimeout(() => {
  204. uni.navigateBack({
  205. delta: 1
  206. });
  207. },1000)
  208. }
  209. }else{
  210. this.$Toast(message);
  211. }
  212. }).catch(err => {
  213. uni.hideLoading()
  214. this.$Toast(err.message);
  215. })
  216. },
  217. getSelectAddress(res) {
  218. console.log(res)
  219. this.form.shippingAddress = res.province.label+'-'+res.city.label+'-'+res.area.label;
  220. },
  221. delAddress () {
  222. let params = { ...this.form, isDel: 1, id: this.options.id}
  223. this.$api('deleteAddress', params).then(res => {
  224. let { code, result, message } = res
  225. if(code == 200) {
  226. uni.$emit('delAddress', params)
  227. this.$Toast(message);
  228. setTimeout(() => {
  229. uni.navigateBack({
  230. delta: 1
  231. });
  232. },1500)
  233. }else{
  234. this.$Toast(message);
  235. }
  236. }).catch(err => {
  237. console.log(err);
  238. this.$Toast(err.message);
  239. })
  240. },
  241. showRegionPicker() {
  242. this.show = true;
  243. }
  244. }
  245. };
  246. </script>
  247. <style lang="scss" scoped>
  248. /deep/ .u-form-item{
  249. padding: 0 !important;
  250. margin: 0 !important;
  251. }
  252. /deep/ .line {
  253. color: $u-light-color;
  254. font-size: 28rpx;
  255. }
  256. .wrap {
  257. background-color: #f2f2f2;
  258. .top {
  259. padding: 0 20rpx;
  260. box-sizing: border-box;
  261. background-color: #ffffff;
  262. border-top: solid 2rpx $u-border-color;
  263. padding: 22rpx;
  264. .item {
  265. display: flex;
  266. font-size: 32rpx;
  267. line-height: 100rpx;
  268. align-items: center;
  269. border-bottom: solid 2rpx $u-border-color;
  270. .left {
  271. width: 180rpx;
  272. }
  273. input {
  274. text-align: left;
  275. }
  276. }
  277. .address {
  278. padding: 20rpx 0;
  279. textarea {
  280. // width: 100%;
  281. height: 150rpx;
  282. background-color: #f7f7f7;
  283. line-height: 60rpx;
  284. margin: 40rpx auto;
  285. padding: 20rpx;
  286. }
  287. }
  288. .site-clipboard {
  289. padding-right: 40rpx;
  290. textarea {
  291. // width: 100%;
  292. height: 150rpx;
  293. background-color: #f7f7f7;
  294. line-height: 60rpx;
  295. margin: 40rpx auto;
  296. padding: 20rpx;
  297. }
  298. .clipboard {
  299. display: flex;
  300. justify-content: center;
  301. align-items: center;
  302. font-size: 26rpx;
  303. color: $u-tips-color;
  304. height: 80rpx;
  305. .icon {
  306. margin-top: 6rpx;
  307. margin-left: 10rpx;
  308. }
  309. }
  310. }
  311. }
  312. .bottom {
  313. padding: 0 20rpx;
  314. box-sizing: border-box;
  315. margin-top: 20rpx;
  316. padding: 40rpx;
  317. background-color: #ffffff;
  318. font-size: 28rpx;
  319. .tag {
  320. display: flex;
  321. .left {
  322. width: 160rpx;
  323. }
  324. .right {
  325. display: flex;
  326. flex-wrap: wrap;
  327. .tags {
  328. width: 140rpx;
  329. padding: 16rpx 8rpx;
  330. border: solid 2rpx $u-border-color;
  331. text-align: center;
  332. border-radius: 50rpx;
  333. margin: 0 10rpx 20rpx;
  334. display: flex;
  335. font-size: 28rpx;
  336. align-items: center;
  337. justify-content: center;
  338. color: $u-content-color;
  339. line-height: 1;
  340. }
  341. .plus {
  342. //padding: 10rpx 0;
  343. }
  344. }
  345. }
  346. .default {
  347. margin-top: 50rpx;
  348. display: flex;
  349. justify-content: space-between;
  350. border-bottom: solid 2rpx $u-border-color;
  351. line-height: 64rpx;
  352. .tips {
  353. font-size: 24rpx;
  354. }
  355. .right {
  356. }
  357. }
  358. }
  359. }
  360. </style>