微信小程序实现省市区三级地址选择(3)

// pages/chooseCity/chooseCity.js //获取应用实例 const model = require('../cityChoose/cityChoose.js') const config = require('../../utils/config.js') const util = require('../../utils/util.js') const app = getApp(); //记录省市区 var nav = 0; var chooseCity = new Array(3); //记录每一次的parentId var finalParentId = new Array(3); //记录是否到最后一级 var flag = 0; Page({ /** * 页面的初始数据 */ data: { finalCity:"", }, /** * 生命周期函数--监听页面加载 */ onLoad: function(options) { //parentId = 0 取所有省份数据 var that = this; that.getData(0); chooseCity = new Array("","",""); finalParentId = new Array(0,0,0); nav = 0; }, submitChoose:function(e){ if(flag != 1){ util.showLog("请选择完整地址") return; }else{ var address_components = { "province": "", "city": "", "district": ""}; address_components["province"] = chooseCity[0]; address_components["city"] = chooseCity[1]; address_components["district"] = chooseCity[2]; console.log(address_components); app.globalData.address_components = address_components; wx.navigateBack(); } }, cancleChoose:function(e){ console.log(finalParentId); var that = this; if(nav == 0){ wx.navigateBack(); } else { nav = nav - 1; chooseCity[nav] = ""; console.log(chooseCity); that.setData({ finalCity: chooseCity[0] + chooseCity[1] + chooseCity[2] }) that.getData(finalParentId[nav]); } }, bindArea: function(e) { if(flag == 0){ console.log(e); var that = this; var parentId = e.currentTarget.dataset.id; var city = e.currentTarget.dataset.city; //刷新出下一级地址前重复点击 console.log(chooseCity[nav - 1] ); console.log(city); if(chooseCity[nav-1] == city){ return; } that.getData(parentId); chooseCity[nav] = city; finalParentId[nav] = e.currentTarget.dataset.parentid; nav++; console.log(chooseCity) that.setData({ finalCity:chooseCity[0]+chooseCity[1]+chooseCity[2] }) } }, getData(parentId) { var that = this; var url = config.getArea + "?parentId=" + parentId; wx.request({ url: url, success: (res) => { console.log("地区数据请求成功"); console.log(res) if (res.data.length != 0) { flag = 0; //设置数据到全局变量 that.setData({ areaList: res.data, }); }else{ //防止用户再次点击; flag = 1; } }, method: "POST", header: { "content-type": "application/x-www-form-urlencoded;charset=utf-8", }, fail: (res) => { console.log("地区数据请求失败"); } }) }, })

util.js

const formatTime = date => { const year = date.getFullYear() const month = date.getMonth() + 1 const day = date.getDate() const hour = date.getHours() const minute = date.getMinutes() const second = date.getSeconds() return [year, month, day].map(formatNumber).join('https://www.jb51.net/') + ' ' + [hour, minute, second].map(formatNumber).join(':') } const formatNumber = n => { n = n.toString() return n[1] ? n : '0' + n } function showLog(e) { wx.showToast({ title: e, icon: "none" }) } function trim(str) { return str.replace(/(^\s*)|(\s*$)/g, ""); } function showLoading() { wx.showLoading({ title: '加载中', mask: true }) } // 验证码倒计时 function phone_code(t, second) { // t是this,second是重新发送的间隔时间,需要设置按钮可点击 var s = second; // 避免重复点击 t.setData({ phone_code_text: s + "s", phone_code_class: "", phone_code_buff: true }); // 倒计时 var clock = setInterval(function () { if (s > 1) { t.setData({ phone_code_text: --s + "s" }) } else { clearInterval(clock); t.setData({ phone_code_text: "重新发送", phone_code_class: "on", phone_code_buff: false }); // 重置数据 s = second; } }, 1000) } function getNowFormatDate() { var date = new Date(); var seperator1 = "-"; var year = date.getFullYear(); var month = date.getMonth() + 1; var strDate = date.getDate(); if (month >= 1 && month <= 9) { month = "0" + month; } if (strDate >= 0 && strDate <= 9) { strDate = "0" + strDate; } var currentdate = year + seperator1 + month + seperator1 + strDate; return currentdate; } function checkAndCall(sourceId,recordType,tele,app,config){ console.log(app.globalData.haulUserInfo) console.log(tele); if (app.globalData.haulUserInfo == null) { showLog("正在获取用户数据,请稍后。") app.Promise.then(function (value) { console.log(value); if (value) { // success wx.makePhoneCall({ phoneNumber: tele, success: ph => { mycall(config, app,recordType, sourceId, function () { //记录联系次数 }) } }) } else { // failure showLog("注册完成即可联系" + "。。。即将跳转") setTimeout(function () { wx.navigateTo({ url: '../registUser/registUser', }) }, 1000); } }).catch(function (error) { }); } else { // success wx.makePhoneCall({ phoneNumber: tele, success: ph => { mycall(config,app, recordType, sourceId,function () { //记录联系次数 }) } }) } } //记录互相联系 function mycall(config,app, recordType, sourceId, callback) { console.log(typeof (recordType)) var that = this; wx.request({ url: config.insertRecord, method: "POST", data: { sourceId: sourceId, userId: app.globalData.haulUserInfo.id, recordType: recordType }, header: { "content-type": "application/x-www-form-urlencoded", }, success: res => { if (res.data.success) { console.log('联系成功'); callback(); } else { showLog(res.data.error); } } }) } module.exports = { formatNumber: formatNumber, formatTime: formatTime, phone_code_clock: phone_code, showLoading: showLoading, showLog: showLog, getNowFormatDate: getNowFormatDate, trim: trim, mycall: mycall, checkAndCall: checkAndCall }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/3095a6319b38f78130002f3208a9a083.html