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

618 lines
16 KiB

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