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

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