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

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