123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364 |
- //@ts-ignore
- import Toast from 'tdesign-miniprogram/toast/index'
- import AddressApi from '../../../../services/address'
- import { areaData } from '../../../../utils/area'
- import checkauth from '../../../../utils/checkauth'
- const innerPhoneReg = '^1(?:3\\d|4[4-9]|5[0-35-9]|6[67]|7[0-8]|8\\d|9\\d)\\d{8}$'
- const innerNameReg = '^[a-zA-Z\\d\\u4e00-\\u9fa5]+$'
- const labelsOptions = [
- { id: 0, name: '家' },
- { id: 1, name: '公司' },
- ]
- let privateData = {
- verifyTips: '',
- }
- Page({
- options: {
- multipleSlots: true,
- },
- externalClasses: ['theme-wrapper-class'],
- behaviors: [checkauth],
- /**
- * 页面的初始数据
- */
- data: {
- wait: false,
- locationState: {
- id: null,
- addressTag: '',
- cityCode: '',
- cityName: '',
- detailAddress: '',
- districtCode: '',
- districtName: '',
- isDefault: false,
- name: '',
- phone: '',
- provinceCode: '',
- provinceName: '',
- isEdit: false,
- isOrderDetail: false,
- isOrderSure: false,
- labelIndex: null,
- },
- areaData: areaData,
- labels: labelsOptions,
- areaPickerVisible: false,
- submitActive: false,
- visible: false,
- labelValue: '',
- columns: 3,
- },
- init() {
- const id = this.data.locationState.id
- if (id && Number(id) > 0) {
- this.setData({
- wait: true,
- })
- AddressApi.GetForEdit({
- id,
- })
- .then((rsp) => {
- const { result } = rsp
- if (result && result.wxAddress) {
- const addr = result.wxAddress
- const tag = addr.addressTag
- let labelIndex = null
- if (tag && tag.length > 0) {
- const { labels } = this.data
- const find = labels.filter((o) => o.name === tag)
- console.log('find', find)
- if (!find || find.length === 0) {
- console.log('create')
- labelIndex = labels[labels.length - 1].id + 1
- this.setData({
- labels: [...labels, { id: labelIndex, name: tag }],
- })
- } else {
- labelIndex = find[0].id
- }
- }
- this.setData(
- {
- wait: false,
- locationState: {
- ...addr,
- labelIndex,
- },
- },
- () => {
- const { isLegal, tips } = this.onVerifyInputLegal()
- this.setData({
- submitActive: isLegal,
- })
- privateData.verifyTips = tips
- },
- )
- }
- })
- .catch(() => {
- this.setData({
- wait: false,
- })
- })
- }
- },
- /**
- * 生命周期函数--监听页面加载
- */
- //@ts-ignore
- onLoad(query: { id: number }) {
- const { id = -1 } = query
- this.setData({
- 'locationState.id': id,
- })
- },
- /**
- * 生命周期函数--监听页面初次渲染完成
- */
- onReady() {},
- /**
- * 生命周期函数--监听页面显示
- */
- onShow() {
- const self = this
- //@ts-ignore
- this.afterAuth(() => {
- self.init()
- })
- },
- /**
- * 生命周期函数--监听页面隐藏
- */
- onHide() {},
- /**
- * 生命周期函数--监听页面卸载
- */
- onUnload() {},
- /**
- * 页面相关事件处理函数--监听用户下拉动作
- */
- onPullDownRefresh() {},
- /**
- * 页面上拉触底事件的处理函数
- */
- onReachBottom() {},
- /**
- * 用户点击右上角分享
- */
- onShareAppMessage() {},
- onPickArea() {
- this.setData({ areaPickerVisible: true })
- },
- onPickLabels(e: WechatMiniprogram.CustomEvent<{}, {}, { item: any }>) {
- const { item } = e.currentTarget.dataset
- const {
- locationState: { labelIndex = undefined },
- labels = [],
- } = this.data
- let payload = {
- labelIndex: item,
- addressTag: labels[item].name,
- }
- if (item === labelIndex) {
- payload = { labelIndex: null, addressTag: '' }
- }
- this.setData({
- 'locationState.labelIndex': payload.labelIndex,
- 'locationState.addressTag': payload.addressTag,
- })
- this.triggerEvent('triggerUpdateValue', payload)
- },
- addLabels() {
- this.setData({
- visible: true,
- })
- },
- confirmHandle() {
- const { labels, labelValue } = this.data
- this.setData({
- visible: false,
- labels: [...labels, { id: labels[labels.length - 1].id + 1, name: labelValue }],
- labelValue: '',
- })
- },
- cancelHandle() {
- this.setData({
- visible: false,
- labelValue: '',
- })
- },
- onCheckDefaultAddress({ detail }: { detail: { value: boolean } }) {
- const { value } = detail
- this.setData({
- 'locationState.isDefault': value,
- })
- },
- onInputValue(
- e: WechatMiniprogram.CustomEvent<
- { value: string; areas: { code: string; name: string }[] },
- {},
- { item: string }
- >,
- ) {
- const { item } = e.currentTarget.dataset
- const { value = '', areas = [] } = e.detail
- if (item === 'address') {
- this.setData(
- {
- 'locationState.provinceCode': areas[0].code,
- 'locationState.provinceName': areas[0].name,
- 'locationState.cityName': areas[1].name,
- 'locationState.cityCode': areas[1].code,
- 'locationState.districtCode': areas[2].code,
- 'locationState.districtName': areas[2].name,
- areaPickerVisible: false,
- },
- () => {
- const { isLegal, tips } = this.onVerifyInputLegal()
- this.setData({
- submitActive: isLegal,
- })
- privateData.verifyTips = tips
- },
- )
- } else {
- this.setData(
- {
- [`locationState.${item}`]: value,
- },
- () => {
- const { isLegal, tips } = this.onVerifyInputLegal()
- this.setData({
- submitActive: isLegal,
- })
- privateData.verifyTips = tips
- },
- )
- }
- },
- onVerifyInputLegal() {
- const { name, phone, detailAddress, districtName } = this.data.locationState
- const nameRegExp = new RegExp(innerNameReg)
- const phoneRegExp = new RegExp(innerPhoneReg)
- if (!name || !name.trim()) {
- return {
- isLegal: false,
- tips: '请填写收货人',
- }
- }
- if (!nameRegExp.test(name)) {
- return {
- isLegal: false,
- tips: '收货人仅支持输入中文、英文(区分大小写)、数字',
- }
- }
- if (!phone || !phone.trim()) {
- return {
- isLegal: false,
- tips: '请填写手机号',
- }
- }
- if (!phoneRegExp.test(phone)) {
- return {
- isLegal: false,
- tips: '请填写正确的手机号',
- }
- }
- if (!districtName || !districtName.trim()) {
- return {
- isLegal: false,
- tips: '请选择省市区信息',
- }
- }
- if (!detailAddress || !detailAddress.trim()) {
- return {
- isLegal: false,
- tips: '请完善详细地址',
- }
- }
- if (detailAddress && detailAddress.trim().length > 50) {
- return {
- isLegal: false,
- tips: '详细地址不能超过50个字符',
- }
- }
- return {
- isLegal: true,
- tips: '',
- }
- },
- formSubmit() {
- const { submitActive } = this.data
- if (!submitActive) {
- Toast({
- context: this,
- selector: '#t-toast',
- message: privateData.verifyTips,
- icon: '',
- duration: 1000,
- })
- return
- }
- const { locationState } = this.data
- const bag = {
- id: null,
- phone: locationState.phone,
- name: locationState.name,
- provinceName: locationState.provinceName,
- provinceCode: locationState.provinceCode,
- cityName: locationState.cityName,
- cityCode: locationState.cityCode,
- districtName: locationState.districtName,
- districtCode: locationState.districtCode,
- detailAddress: locationState.detailAddress,
- isDefault: locationState.isDefault,
- addressTag: locationState.addressTag,
- // latitude: locationState.latitude,
- // longitude: locationState.longitude,
- }
- if (Number(locationState.id) > 0) {
- bag.id = locationState.id
- }
- this.setData({
- wait: true,
- })
- AddressApi.CreateOrUpdate({
- wxAddress: bag,
- })
- .then((rsp) => {
- this.setData({
- wait: false,
- })
- if (rsp.result > 0) {
- wx.navigateBack({ delta: 1 })
- }
- })
- .catch(() => {
- this.setData({
- wait: false,
- })
- })
- },
- })
|