金诚优选前端代码
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.

641 lines
17 KiB

4 years ago
4 years ago
4 years ago
  1. <template>
  2. <view id="address-add">
  3. <lf-nav :title="page_title" :showIcon="true" bgColor="#fff"></lf-nav>
  4. <view class="indetify-img" v-show="parseResult" @tap="closeImg">
  5. <image src="https://cdn.guojiang.club/%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20200403161800.png" mode="widthFix"></image>
  6. </view>
  7. <view class="address-info">
  8. <view class="info-item">
  9. <label for="name">姓名:</label>
  10. <view class="form-control">
  11. <input type="text" id="name" placeholder="请输入收货人姓名" data-type="accept_name" :value="detail.accept_name" @input="input" />
  12. </view>
  13. </view>
  14. <view class="info-item">
  15. <radio-group class="lf-flex">
  16. <label>
  17. <radio-group @change="radioChange">
  18. <radio :checked="detail.sex == '男'" value="男"></radio>
  19. </radio-group>
  20. <text class="lf-m-l-15">先生</text>
  21. </label>
  22. <label>
  23. <radio-group @change="radioChange">
  24. <radio :checked="detail.sex == '女'" value="女"></radio>
  25. </radio-group>
  26. <text class="lf-m-l-15">女士</text>
  27. </label>
  28. </radio-group>
  29. </view>
  30. <view class="info-item">
  31. <label for="phone">联系电话:</label>
  32. <view class="form-control">
  33. <input type="number" maxlength="11" placeholder="请输入联系电话" id="phone" data-type="mobile" :value="detail.mobile" @input="input" />
  34. </view>
  35. </view>
  36. <!-- #ifdef MP-WEIXIN -->
  37. <view class="info-item">
  38. <label>所在地:</label>
  39. <view class="form-control select">
  40. <picker mode="region" @change="bindRegionChange" :value="detail.address_name">
  41. <view class="picker">
  42. {{detail.address_name[0] || ''}} {{detail.address_name[1] || ''}} {{detail.address_name[2] || ''}}
  43. </view>
  44. </picker>
  45. </view>
  46. </view>
  47. <!-- #endif -->
  48. <!-- #ifdef APP-PLUS || H5 -->
  49. <view class="mpvue-picker">
  50. <view class="info-item uni-btn-v" @click="showMulLinkageThreePicker">
  51. <label>所在地:</label>
  52. <view class="form-control select">
  53. <view class="picker uni-common-mt">
  54. {{pickerText.label || ''}}
  55. </view>
  56. </view>
  57. </view>
  58. <mpvue-city-picker :themeColor="themeColor" ref="mpvueCityPicker" :pickerValueDefault="cityPickerValueDefault"
  59. @onConfirm="onConfirm"></mpvue-city-picker>
  60. </view>
  61. <!-- #endif -->
  62. <view class="info-item">
  63. <label for="address">详细地址:</label>
  64. <view class="form-control">
  65. <input type="text" id="address" data-type="address" placeholder="请输入详细地址" :value="detail.address" @input="input" />
  66. </view>
  67. </view>
  68. <view class="info-item">
  69. <label for="name">门牌号:</label>
  70. <view class="form-control">
  71. <input type="text" id="name" placeholder="请输入门牌号(选填)" data-type="house_num" :value="detail.house_num" @input="input" />
  72. </view>
  73. </view>
  74. <view class="info-item lf-m-t-10">
  75. <label class="checkbox" @tap="check">
  76. <!-- #ifdef H5 -->
  77. <checkbox color="red" :checked="detail.is_default"></checkbox>
  78. <!-- #endif -->
  79. <!-- #ifdef APP-PLUS -->
  80. <checkbox color="#FFFFFF" :checked="detail.is_default"></checkbox>
  81. <!-- #endif -->
  82. <!-- #ifdef MP-WEIXIN -->
  83. <checkbox color="#FFFFFF" :checked="detail.is_default"></checkbox>
  84. <!-- #endif -->
  85. <text class="lf-m-l-10">设为默认地址</text>
  86. </label>
  87. </view>
  88. </view>
  89. <!-- <view class="identify-address">
  90. <textarea @input="changeIdentify" :value="address_text" placeholder-class="textarea-placeholder" placeholder="粘贴或输入整段地址,点击“识别”自动拆分姓名、电话和地址。" />
  91. <view class="btn-box" v-if="address_text">
  92. <view class="clear" @tap="clearInfo">清空</view>
  93. <view class="sure" @tap="sureAddress">识别</view>
  94. </view>
  95. </view> -->
  96. <view class="button-box">
  97. <button type="primary" :style="'background: ' + config.mainColor" class="submit" @tap="submit" :loading="loading">保存</button>
  98. <!-- <button type="warn" class="delete" :style="'background: ' + config.secColor" v-if="id" @tap="deleteAddress" :loading="deleteLoading">删除</button> -->
  99. </view>
  100. </view>
  101. </template>
  102. <script>
  103. import {pageLogin, getUrl,config,is} from '@/common/js/utils.js';
  104. // #ifdef H5
  105. import wPicker from "@/components/w-picker/w-picker.vue";
  106. // #endif
  107. // #ifdef APP-PLUS || H5
  108. import mpvueCityPicker from '@/components/mpvue-citypicker/mpvueCityPicker.vue';
  109. import provinceData from '@/components/mpvue-citypicker/city-data/province.js';
  110. import cityData from '@/components/mpvue-citypicker/city-data/city.js';
  111. // #endif
  112. import AddressParse from '@/common/js/zh-address-parse.min.js'
  113. export default {
  114. data() {
  115. return {
  116. detail: {
  117. is_default: false,
  118. address_name: [
  119. '','',''
  120. ],
  121. area: 430105,
  122. city: 430100,
  123. province: 430000,
  124. sex: '男',
  125. house_num: ''
  126. },
  127. order_no: '',
  128. id: '',
  129. loading: false,
  130. deleteLoading: false,
  131. config: '',
  132. title: 'Hello',
  133. /* mode:"range", */
  134. defaultVal:[0,0,0,0,0,0,0],
  135. tabList:[
  136. {
  137. mode:"region",
  138. name:"省市区"
  139. }
  140. ],
  141. tabIndex:0,
  142. resultInfo:{
  143. result:""
  144. },
  145. // #ifdef H5
  146. mulLinkageTwoPicker:cityData,
  147. // #endif
  148. cityPickerValueDefault: [0, 0, 1],
  149. themeColor: '#007AFF',
  150. pickerText:{
  151. label:'',
  152. value:'',
  153. cityCode:''
  154. },
  155. mode: '',
  156. deepLength: 1,
  157. pickerValueDefault: [0],
  158. pickerValueArray: [],
  159. address_text:'',
  160. parseResult:'',
  161. page_title: ''
  162. };
  163. },
  164. components:{
  165. // #ifdef H5
  166. /* wPicker */
  167. // #endif
  168. // #ifdef APP-PLUS || H5
  169. mpvueCityPicker
  170. // #endif
  171. },
  172. onShow() {// let app =getApp();
  173. // app.isBirthday().then(()=>{
  174. // if(this.$cookieStorage.get("birthday_gift")){
  175. // var giftData=this.$cookieStorage.get("birthday_gift").data;
  176. // new app.ToastPannel().__page.showText(giftData);
  177. // }
  178. // });
  179. },
  180. onLoad(e) {
  181. // 第三方平台配置颜色
  182. var config = this.$cookieStorage.get('globalConfig') || '';
  183. this.setData({
  184. config: config
  185. });
  186. // pageLogin(getUrl());
  187. this.setData({
  188. id: e.id
  189. });
  190. if (e.id) {
  191. // wx.setNavigationBarTitle({
  192. // title: '修改收货地址'
  193. // });
  194. this.page_title = '修改收货地址';
  195. this.queryAddress(e.id);
  196. } else {
  197. this.page_title = '新增收货地址';
  198. // wx.setNavigationBarTitle({
  199. // title: '新增收货地址'
  200. // });
  201. }
  202. },
  203. methods: {
  204. radioChange(e){
  205. this.detail['sex'] = e.target.value;
  206. },
  207. //清楚图片
  208. closeImg(){
  209. this.parseResult = '';
  210. },
  211. //清空所有地址
  212. clearInfo(){
  213. this.address_text = '';
  214. },
  215. //识别地址
  216. sureAddress(){
  217. if(this.address_text){
  218. const options = {
  219. type: 0, // 哪种方式解析,0:正则,1:树查找
  220. textFilter: [], // 预清洗的字段
  221. nameMaxLength: 4, // 查找最大的中文名字长度
  222. }
  223. let parseResult = AddressParse(this.address_text,options);
  224. this.parseResult = parseResult;
  225. this.detail.accept_name = parseResult.name;
  226. this.detail.mobile = parseResult.phone;
  227. this.detail.address = parseResult.detail;
  228. // #ifdef MP-WEIXIN
  229. this.detail.address_name = [parseResult.province,parseResult.city,parseResult.area];
  230. // #endif
  231. // #ifdef APP-PLUS || H5
  232. this.detail.address_name = [parseResult.province,parseResult.city,parseResult.area];
  233. let address = [parseResult.province,parseResult.city,parseResult.area];
  234. this.pickerText.label = address.join(' ');
  235. // #endif
  236. } else{
  237. return
  238. }
  239. },
  240. changeIdentify(e){
  241. this.address_text = e.detail.value;
  242. },
  243. showMulLinkageThreePicker() {
  244. this.$refs.mpvueCityPicker.show()
  245. },
  246. onConfirm(e) {
  247. this.pickerText = e;
  248. var index = e.value;
  249. var province_code = provinceData[index[0]].value+"0000";
  250. var city_code = cityData[index[0]][index[1]].value+"00";
  251. var label = e.label;
  252. var address_name = label.split('-');
  253. // 设置传给后台的参数
  254. this.detail.address_name = address_name;
  255. this.detail.city = city_code;
  256. this.detail.province = province_code;
  257. this.detail.area = e.cityCode;
  258. },
  259. bindRegionChange(e) {
  260. this.setData({
  261. 'detail.address_name': e.detail.value
  262. });
  263. },
  264. check(e) {
  265. this.setData({
  266. "detail.is_default": !this.detail.is_default
  267. });
  268. },
  269. input(e) {
  270. var type = e.currentTarget.dataset.type;
  271. var value = e.detail.value;
  272. // this.setData({
  273. // [`detail.${type}`]: value
  274. // });
  275. //
  276. this.detail[type]=value;
  277. },
  278. deleteAddress() {
  279. this.setData({
  280. deleteLoading: true
  281. });
  282. this.removeAddress(this.id);
  283. },
  284. /* onConfirm(val){
  285. console.log(val);
  286. //如果页面需要调用多个mode类型,可以根据mode处理结果渲染到哪里;
  287. // switch(this.mode){
  288. // case "date":
  289. // break;
  290. // }
  291. this.detail.address_name[0]=val.checkArr[0];
  292. this.detail.address_name[1]=val.checkArr[1];
  293. this.detail.address_name[2]=val.checkArr[2];
  294. this.resultInfo.result=val.result;
  295. },
  296. */
  297. toggleTab(item,index){
  298. this.tabIndex=index;
  299. this.mode=item.mode;
  300. this.defaultVal=item.value;
  301. this.$refs[item.mode].show();
  302. },
  303. submit() {
  304. this.setData({
  305. loading: true
  306. });
  307. var message = null;
  308. if (!is.has(this.detail.accept_name)) {
  309. message = '请输入姓名';
  310. } else if (!is.has(this.detail.mobile)) {
  311. message = '请输入手机号码';
  312. } else if (!is.mobile(this.detail.mobile)) {
  313. message = '请输入正确的手机号码';
  314. } else if (!is.has(this.detail.address_name)||!is.has(this.detail.address_name[0]) ) {
  315. message = '请选择地址';
  316. } else if (!is.has(this.detail.address)) {
  317. message = '请输入详细地址';
  318. }
  319. if (message) {
  320. this.setData({
  321. loading: false
  322. });
  323. wx.showModal({
  324. title: '',
  325. content: message,
  326. showCancel: false
  327. });
  328. } else {
  329. if (this.id) {
  330. this.updateAddress(this.detail);
  331. } else {
  332. this.createAddress(this.detail);
  333. }
  334. }
  335. },
  336. // 获取收货地址详情
  337. queryAddress(id) {
  338. uni.showLoading({
  339. title: '正在获取中'
  340. })
  341. var token = this.$cookieStorage.get('user_token');
  342. this.$http.get({
  343. api: 'api/address/' + id,
  344. header: {
  345. Authorization: token
  346. }
  347. }).then(res => {
  348. if (res.statusCode == 200) {
  349. res = res.data;
  350. var data = res.data;
  351. data.is_default = !!data.is_default;
  352. data.address_value = [data.province, data.city, data.area].join(' ');
  353. this.resultInfo.result=data.address_name;
  354. data.address_name = data.address_name.split(' ');
  355. if (res.status) {
  356. this.setData({
  357. detail: data
  358. });
  359. // #ifndef MP-WEIXIN
  360. this.pickerText.label = res.data.address_name.join(' ')
  361. // #endif
  362. } else {
  363. wx.showToast({
  364. title: res.message,
  365. image: '../../../static/error.png'
  366. });
  367. }
  368. } else {
  369. wx.showToast({
  370. title: '获取信息失败',
  371. image: '../../../static/error.png'
  372. });
  373. }
  374. uni.hideLoading();
  375. }).catch(err => uni.hideLoading())
  376. },
  377. // 新增收货地址
  378. createAddress(data) {
  379. var address = {
  380. accept_name: data.accept_name,
  381. mobile: data.mobile,
  382. province: data.province,
  383. city: data.city,
  384. area: data.area,
  385. address_name: data.address_name.join(" "),
  386. address: data.address,
  387. house_num: data.house_num,
  388. sex: data.sex,
  389. is_default: data.is_default ? 1 : 0
  390. };
  391. var token = this.$cookieStorage.get('user_token');
  392. this.$http.post({
  393. api: 'api/address/create',
  394. header: {
  395. Authorization: token
  396. },
  397. data: address
  398. }).then(res => {
  399. if (res.statusCode == 200) {
  400. res = res.data;
  401. if (res.status) {
  402. wx.showModal({
  403. title: '',
  404. content:'新增收货地址成功',
  405. showCancel: false,
  406. success: res => {
  407. if (res.confirm) {
  408. wx.navigateBack();
  409. }
  410. }
  411. });
  412. } else {
  413. wx.showToast({
  414. title: '新增收货地址失败',
  415. image: '../../../static/error.png',
  416. complete: err => {
  417. setTimeout(() => {
  418. wx.navigateBack();
  419. }, 1600);
  420. }
  421. });
  422. }
  423. } else {
  424. wx.showToast({
  425. title: '请求错误',
  426. image: '../../../static/error.png',
  427. complete: err => {
  428. setTimeout(() => {
  429. wx.navigateBack();
  430. }, 1600);
  431. }
  432. });
  433. }
  434. this.setData({
  435. loading: false
  436. });
  437. }).catch(rej => {
  438. this.setData({
  439. loading: false
  440. });
  441. });
  442. },
  443. // 修改收货地址
  444. updateAddress(data) {
  445. var address = {
  446. id: data.id,
  447. accept_name: data.accept_name,
  448. mobile: data.mobile,
  449. province: data.province,
  450. city: data.city,
  451. area: data.area,
  452. address_name: data.address_name.join(" "),
  453. address: data.address,
  454. house_num: data.house_num,
  455. sex: data.sex,
  456. is_default: data.is_default ? 1 : 0
  457. };
  458. var token = this.$cookieStorage.get('user_token');
  459. this.$http.ajax({
  460. api: 'api/address/'+data.id,
  461. method: 'PUT',
  462. header: {
  463. Authorization: token
  464. },
  465. data: address
  466. }).then(res => {
  467. res = res.data;
  468. if (res.status) {
  469. wx.showModal({
  470. title: '',
  471. content: '修改收货地址成功',
  472. showCancel: false,
  473. success: res => {
  474. if (res.confirm) {
  475. wx.navigateBack();
  476. }
  477. }
  478. });
  479. } else {
  480. wx.showToast({
  481. title: '修改收货地址失败',
  482. image: '../../../static/error.png',
  483. complete: err => {
  484. setTimeout(() => {
  485. wx.navigateBack();
  486. }, 1600);
  487. }
  488. });
  489. }
  490. this.setData({
  491. loading: false
  492. });
  493. }).catch(rej => {
  494. this.setData({
  495. loading: false
  496. });
  497. });
  498. },
  499. // 删除收货地址
  500. removeAddress(id) {
  501. var token = this.$cookieStorage.get('user_token');
  502. this.$http.ajax({
  503. api: 'api/address/' + id,
  504. header: {
  505. Authorization: token
  506. },
  507. method: 'DELETE'
  508. }).then(res => {
  509. if (res.statusCode == 200) {
  510. res = res.data;
  511. if (res.status) {
  512. wx.showModal({
  513. title: '',
  514. content: '删除收货地址成功',
  515. showCancel: false,
  516. success: res => {
  517. if (res.confirm) {
  518. wx.navigateBack();
  519. }
  520. }
  521. });
  522. } else {
  523. wx.showToast({
  524. title: '删除收货地址失败',
  525. image: '../../../static/error.png',
  526. complete: err => {
  527. setTimeout(() => {
  528. wx.navigateBack();
  529. }, 1600);
  530. }
  531. });
  532. }
  533. } else {
  534. wx.showToast({
  535. title: '请求错误',
  536. image: '../../../static/error.png',
  537. complete: err => {
  538. setTimeout(() => {
  539. wx.navigateBack();
  540. }, 1600);
  541. }
  542. });
  543. }
  544. this.setData({
  545. deleteLoading: false
  546. });
  547. }).catch(rej => {
  548. this.setData({
  549. deleteLoading: false
  550. });
  551. });
  552. },
  553. setData: function (obj) {
  554. let that = this;
  555. let keys = [];
  556. let val, data;
  557. Object.keys(obj).forEach(function (key) {
  558. keys = key.split('.');
  559. val = obj[key];
  560. data = that.$data;
  561. keys.forEach(function (key2, index) {
  562. if (index + 1 == keys.length) {
  563. that.$set(data, key2, val);
  564. } else {
  565. if (!data[key2]) {
  566. that.$set(data, key2, {});
  567. }
  568. }
  569. data = data[key2];
  570. });
  571. });
  572. }
  573. },
  574. computed: {},
  575. watch: {}
  576. };
  577. </script>
  578. <style lang="less" scoped>
  579. @import "add";
  580. .identify-address>textarea{
  581. border-color: #e5e5e5 !important;
  582. }
  583. /deep/.input-placeholder{
  584. font-weight: initial;
  585. }
  586. </style>