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

642 lines
17 KiB

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