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

633 lines
16 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" 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 == 0" value="0"></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 == 1" value="1"></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" 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" :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" data-type="house_num" :value="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. var token = this.$cookieStorage.get('user_token');
  340. this.$http.get({
  341. api: 'api/address/' + id,
  342. header: {
  343. Authorization: token
  344. }
  345. }).then(res => {
  346. if (res.statusCode == 200) {
  347. res = res.data;
  348. var data = res.data;
  349. data.is_default = !!data.is_default;
  350. data.address_value = [data.province, data.city, data.area].join(' ');
  351. this.resultInfo.result=data.address_name;
  352. data.address_name = data.address_name.split(' ');
  353. if (res.status) {
  354. this.setData({
  355. detail: data
  356. });
  357. // #ifndef MP-WEIXIN
  358. this.pickerText.label = res.data.address_name.join(' ')
  359. // #endif
  360. } else {
  361. wx.showToast({
  362. title: res.message,
  363. image: '../../../static/error.png'
  364. });
  365. }
  366. } else {
  367. wx.showToast({
  368. title: '获取信息失败',
  369. image: '../../../static/error.png'
  370. });
  371. }
  372. });
  373. },
  374. // 新增收货地址
  375. createAddress(data) {
  376. var address = {
  377. accept_name: data.accept_name,
  378. mobile: data.mobile,
  379. province: data.province,
  380. city: data.city,
  381. area: data.area,
  382. address_name: data.address_name.join(" "),
  383. address: data.address,
  384. sex: data.sex,
  385. is_default: data.is_default ? 1 : 0
  386. };
  387. var token = this.$cookieStorage.get('user_token');
  388. this.$http.post({
  389. api: 'api/address/create',
  390. header: {
  391. Authorization: token
  392. },
  393. data: address
  394. }).then(res => {
  395. if (res.statusCode == 200) {
  396. res = res.data;
  397. if (res.status) {
  398. wx.showModal({
  399. title: '',
  400. content:'新增收货地址成功',
  401. showCancel: false,
  402. success: res => {
  403. if (res.confirm) {
  404. wx.navigateBack();
  405. }
  406. }
  407. });
  408. } else {
  409. wx.showToast({
  410. title: '新增收货地址失败',
  411. image: '../../../static/error.png',
  412. complete: err => {
  413. setTimeout(() => {
  414. wx.navigateBack();
  415. }, 1600);
  416. }
  417. });
  418. }
  419. } else {
  420. wx.showToast({
  421. title: '请求错误',
  422. image: '../../../static/error.png',
  423. complete: err => {
  424. setTimeout(() => {
  425. wx.navigateBack();
  426. }, 1600);
  427. }
  428. });
  429. }
  430. this.setData({
  431. loading: false
  432. });
  433. }).catch(rej => {
  434. this.setData({
  435. loading: false
  436. });
  437. });
  438. },
  439. // 修改收货地址
  440. updateAddress(data) {
  441. var address = {
  442. id: data.id,
  443. accept_name: data.accept_name,
  444. mobile: data.mobile,
  445. province: data.province,
  446. city: data.city,
  447. area: data.area,
  448. address_name: data.address_name.join(" "),
  449. address: data.address,
  450. house_num: data.house_num,
  451. is_default: data.is_default ? 1 : 0
  452. };
  453. var token = this.$cookieStorage.get('user_token');
  454. this.$http.ajax({
  455. api: 'api/address/'+data.id,
  456. method: 'PUT',
  457. header: {
  458. Authorization: token
  459. },
  460. data: address
  461. }).then(res => {
  462. res = res.data;
  463. if (res.status) {
  464. wx.showModal({
  465. title: '',
  466. content: '修改收货地址成功',
  467. showCancel: false,
  468. success: res => {
  469. if (res.confirm) {
  470. wx.navigateBack();
  471. }
  472. }
  473. });
  474. } else {
  475. wx.showToast({
  476. title: '修改收货地址失败',
  477. image: '../../../static/error.png',
  478. complete: err => {
  479. setTimeout(() => {
  480. wx.navigateBack();
  481. }, 1600);
  482. }
  483. });
  484. }
  485. this.setData({
  486. loading: false
  487. });
  488. }).catch(rej => {
  489. this.setData({
  490. loading: false
  491. });
  492. });
  493. },
  494. // 删除收货地址
  495. removeAddress(id) {
  496. var token = this.$cookieStorage.get('user_token');
  497. this.$http.ajax({
  498. api: 'api/address/' + id,
  499. header: {
  500. Authorization: token
  501. },
  502. method: 'DELETE'
  503. }).then(res => {
  504. if (res.statusCode == 200) {
  505. res = res.data;
  506. if (res.status) {
  507. wx.showModal({
  508. title: '',
  509. content: '删除收货地址成功',
  510. showCancel: false,
  511. success: res => {
  512. if (res.confirm) {
  513. wx.navigateBack();
  514. }
  515. }
  516. });
  517. } else {
  518. wx.showToast({
  519. title: '删除收货地址失败',
  520. image: '../../../static/error.png',
  521. complete: err => {
  522. setTimeout(() => {
  523. wx.navigateBack();
  524. }, 1600);
  525. }
  526. });
  527. }
  528. } else {
  529. wx.showToast({
  530. title: '请求错误',
  531. image: '../../../static/error.png',
  532. complete: err => {
  533. setTimeout(() => {
  534. wx.navigateBack();
  535. }, 1600);
  536. }
  537. });
  538. }
  539. this.setData({
  540. deleteLoading: false
  541. });
  542. }).catch(rej => {
  543. this.setData({
  544. deleteLoading: false
  545. });
  546. });
  547. },
  548. setData: function (obj) {
  549. let that = this;
  550. let keys = [];
  551. let val, data;
  552. Object.keys(obj).forEach(function (key) {
  553. keys = key.split('.');
  554. val = obj[key];
  555. data = that.$data;
  556. keys.forEach(function (key2, index) {
  557. if (index + 1 == keys.length) {
  558. that.$set(data, key2, val);
  559. } else {
  560. if (!data[key2]) {
  561. that.$set(data, key2, {});
  562. }
  563. }
  564. data = data[key2];
  565. });
  566. });
  567. }
  568. },
  569. computed: {},
  570. watch: {}
  571. };
  572. </script>
  573. <style rel="stylesheet/less" lang="less" scoped>
  574. @import "add";
  575. .identify-address>textarea{
  576. border-color: #e5e5e5 !important;
  577. }
  578. </style>