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

620 lines
16 KiB

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