| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- // pages/user/address/add/add.js
- var app = getApp();
- import {
- add
- } from 'lodash';
- import {
- address
- } from './../../../../utils/address.js';
- Page({
- /**
- * 页面的初始数据
- */
- data: {
- addressList: address,
- addressRange: [],
- addressValue: [],
- name: "",
- addrText: "",
- renderText: "",
- index0: 0,
- loading: true,
- addrId: null,
- addr: {
- }
- },
- /**
- * 生命周期函数--监听页面加载
- */
- async onLoad(options) {
- this.setData({
- addrId: options.addrId || null,
- loading: false
- })
- },
- /**
- * 生命周期函数--监听页面初次渲染完成
- */
- onReady() {
- },
- /**
- * 生命周期函数--监听页面显示
- */
- async onShow() {
- let index0 = 0;
- let index1 = 0;
- if (this.data.addrId) {
- let addr = await app.ajax.get({
- url: `/admin/bxdClientAddress/${this.data.addrId}`,
- })
- this.setData({
- addr: addr.data.data,
- loading: false
- })
- }
- let addr = this.data.addr;
- if (addr && addr.addrId) {
- try {
- let province = this.data.addressList.findIndex(v => v.name === addr.province);
- let city = this.data.addressList[province].city.findIndex(v => v.name === addr.city);
- let area = this.data.addressList[province].city[city].area.findIndex(v => v === addr.area);
- index0 = province;
- index1 = city;
- this.setData({
- addrText: [addr.province, addr.city, addr.area],
- addressValue: [province, city, area]
- })
- } catch (e) {
- //console.log(e);
- }
- }
- let addressRange = [
- [...this.data.addressList.map(v => v.name)],
- [...this.data.addressList[index0].city.map(v => v.name)],
- [...this.data.addressList[index0].city[index1].area],
- ]
- this.setData({
- addressRange
- })
- },
- async submit(e) {
- let addr = e.detail.value;
- if (addr.receiver == "") {
- wx.showToast({
- title: '收货人不可为空',
- icon: "error"
- });
- return;
- }
- if (addr.mobile == "") {
- wx.showToast({
- title: '手机号不可为空',
- icon: "error"
- });
- return;
- }
- if (!app.isMobile(addr.mobile)) {
- wx.showToast({
- title: '手机号格式有误',
- icon: "error"
- })
- return;
- }
- if (addr.tel != "" && !app.isPhone(addr.tel)) {
- wx.showToast({
- title: '座机格式有误',
- icon: "error"
- });
- return;
- }
- if (this.data.addressValue.length != 3 || this.data.addrText.length == 0) {
- wx.showToast({
- title: '请选择所在区域',
- icon: "error"
- });
- return;
- }
- if (addr.addr == "") {
- wx.showToast({
- title: '请输入详细地址',
- icon: "error"
- });
- return;
- }
- let data = {
- ...addr,
- province: this.data.addrText[0],
- city: this.data.addrText[1],
- area: this.data.addrText[2],
- addrId: this.data.addrId
- }
- await app.ajax.post({
- url: "/admin/bxdClientAddress",
- method: this.data.addrId ? "PUT" : "POST",
- data: data
- })
- setTimeout(() => {
- wx.showToast({
- title: this.data.addrId ? '更新成功' : '新增成功',
- mask: true
- });
- }, 200);
- setTimeout(() => {
- wx.navigateBack({
- delta: 1
- })
- }, 1200);
- },
- bindchange(e) {
- let selectAddr = this.data.addressList[e.detail.value[0]];
- this.setData({
- addressValue: e.detail.value,
- addrText: [selectAddr.name, selectAddr.city[e.detail.value[1]].name, selectAddr.city[e.detail.value[1]].area[e.detail.value[2]]]
- })
- },
- bindcolumnchange(e) {
- let column = e.detail.column;
- let value = e.detail.value;
- let index0 = this.data.index0;
- //console.log(e)
- let addressRange = [];
- if (column == 0) {
- addressRange = [
- [...this.data.addressList.map(v => v.name)],
- [...this.data.addressList[value].city.map(v => v.name)],
- [...this.data.addressList[value].city[0].area],
- ];
- this.setData({
- index0: value
- })
- } else if (column == 1) {
- addressRange = [
- [...this.data.addressList.map(v => v.name)],
- [...this.data.addressList[index0].city.map(v => v.name)],
- [...this.data.addressList[index0].city[value].area],
- ]
- } else {
- return;
- }
- this.setData({
- addressRange
- })
- },
- async renderTextInput(e) {
- if (!e.detail.value.renderText) {
- let text = await wx.getClipboardData();
- this.setData({
- renderText: text.data
- });
- } else {
- this.setData({
- renderText: e.detail.value.renderText
- });
- }
- await this.renderAddr()
- },
- async renderAddr() {
- let {
- data
- } = await app.ajax.gets(`/admin/bxdClientAddress/addressParse?address=${this.data.renderText}`);
- this.setData({
- 'addr.mobile': data.data.mobile || '',
- 'addr.receiver': data.data.name || '',
- 'addr.addr': data.data.detail || '',
- addrText: [data.data.province, data.data.city, data.data.area]
- });
- let province = this.data.addressList.findIndex(v => v.name === data.data.province);
- let city = this.data.addressList[province].city.findIndex(v => v.name === data.data.city);
- let area = this.data.addressList[province].city[city].area.findIndex(v => v === data.data.area);
- this.setData({
- addressValue: [province, city, area],
- })
- let addressRange = [
- [...this.data.addressList.map(v => v.name)],
- [...this.data.addressList[province].city.map(v => v.name)],
- [...this.data.addressList[province].city[city].area],
- ]
- this.setData({
- addressRange
- })
- },
- /**
- * 生命周期函数--监听页面隐藏
- */
- onHide() {
- },
- /**
- * 生命周期函数--监听页面卸载
- */
- onUnload() {
- },
- /**
- * 页面相关事件处理函数--监听用户下拉动作
- */
- onPullDownRefresh() {
- },
- /**
- * 页面上拉触底事件的处理函数
- */
- onReachBottom() {
- },
- /**
- * 用户点击右上角分享
- */
- onShareAppMessage() {
- }
- })
|