|
- // pages/address/address.js
- const app = getApp()
- Page({
-
- /**
- * 页面的初始数据
- */
- data: {
- imgUrl: app.globalData.urlStatic,//图片路径
- submitData:{
- realName:"",
- mobile: "",//电话
- province: "",//省份
- city: "",//城市
- district: "",//地区
- addressDetail:""//详细
- },
- isAddress: false,//是否有地址
- phoneInputShow: false,//是否显示电话输入框
- marginT:0,
- },
-
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad: function (options) {
- app.globalData.nowPage = 4;
- if (app.globalData.userPhoneType=="ios"){
- this.setData({
- marginT:-20
- })
- }
- if (app.globalData.openid) {
- this.loadFun();
- } else {
- app.globalData.openidSuccessFuc = this.loadFun;
- }
- // if (app.globalData.userMobile) {
- // this.data.submitData.mobile = app.globalData.userMobile;
- // this.setData({
- // phoneInputShow: true,
- // submitData: this.data.submitData
- // })
- // }
- },
- loadFun: function () {
- this.getAddress();
- },
-
- /**
- * 生命周期函数--监听页面初次渲染完成
- */
- onReady: function () {
-
- },
-
- /**
- * 生命周期函数--监听页面显示
- */
- onShow: function () {
-
- },
-
- /**
- * 生命周期函数--监听页面隐藏
- */
- onHide: function () {
-
- },
-
- /**
- * 生命周期函数--监听页面卸载
- */
- onUnload: function () {
-
- },
-
- /**
- * 页面相关事件处理函数--监听用户下拉动作
- */
- onPullDownRefresh: function () {
-
- },
-
- /**
- * 页面上拉触底事件的处理函数
- */
- onReachBottom: function () {
-
- },
-
- /**
- * 用户点击右上角分享
- */
- onShareAppMessage: function () {
- return app.sharePack();
- },
- addressChange:function(e){//所在地区发生改变
- this.data.submitData.province = e.detail.value[0];
- this.data.submitData.city = e.detail.value[1];
- this.data.submitData.district = e.detail.value[2];
- this.setData({
- submitData:this.data.submitData
- })
- },
- getRealName: function (e) {//获取收货人
- this.data.submitData.realName = e.detail.value;
- this.setData({
- submitData: this.data.submitData
- })
- },
- getMobile: function (e) {//获取手机号码
- this.data.submitData.mobile = e.detail.value;
- this.setData({
- submitData: this.data.submitData
- })
- },
- getAddressDetail: function (e) {//获取详细地址
- this.data.submitData.addressDetail = e.detail.value;
- this.setData({
- submitData: this.data.submitData
- })
- },
- getAddress: function () {//获取地址
- app.wxRequest(app.globalData.urlRoot +"address/getAddress",{},res=>{
- if(res.code == 200){
- if(res.data){
- this.data.isAddress = true;
- this.data.submitData.realName = res.data.realName;
- this.data.submitData.mobile = res.data.mobile;
- this.data.submitData.province = res.data.province;
- this.data.submitData.city = res.data.city;
- this.data.submitData.district = res.data.district;
- this.data.submitData.addressDetail = res.data.addressDetail;
- this.setData({
- submitData: this.data.submitData
- })
- }
- }else{
- wx.showToast({
- title: res.msg,
- icon:"none"
- })
- }
- },this);
- },
- addAddress:function(){//添加地址
- app.wxRequest(app.globalData.urlRoot + "address/addAddress", this.data.submitData,res => {
- wx.showToast({
- title: res.msg,
- })
- if (res.code == 200) {
- wx.navigateBack({
- delta: 1
- })
- }
- },this,"POST")
- },
- updateAddress: function () {//更新地址
- app.wxRequest(app.globalData.urlRoot + "address/updateAddress", this.data.submitData, res => {
- wx.showToast({
- title: res.msg,
- })
- if (res.code == 200) {
- wx.navigateBack({
- delta:1
- })
- }
- }, this,"POST");
- },
- getUserPhone: function (e) {//获取用户手机号
- this.setData({
- phoneInputShow: true
- })
- if (e.detail.errMsg == 'getPhoneNumber:ok') {
- app.getMobile(e.detail.encryptedData, e.detail.iv, res => {
- if (res.code == 200) {
- if (res.data && res.data.decodeData) {
- this.data.submitData.mobile = res.data.decodeData.phoneNumber;
- this.setData({
- submitData: this.data.submitData
- })
- }
- } else {
- wx.showToast({
- title: res.msg,
- icon: "none"
- })
- }
- }, this);
- }
- },
- submitAddress:function(){//保存地址
- if (!this.data.submitData.realName){
- wx.showToast({
- title: '请输入收货人',
- icon:"none"
- })
- return;
- }
- if (!app.mobileVerify(this.data.submitData.mobile)) {
- if (this.data.submitData.mobile) {
- wx.showToast({
- title: '请输入正确的电话',
- icon: 'none'
- })
- } else {
- wx.showToast({
- title: '请输入电话',
- icon: 'none'
- })
- }
- return;
- }
- if (!this.data.submitData.province) {
- wx.showToast({
- title: '请选择所在地区',
- icon: 'none'
- })
- return;
- }
- if (!this.data.submitData.addressDetail) {
- wx.showToast({
- title: '请输入详细地址',
- icon: "none"
- })
- return;
- }
- if(this.data.isAddress){
- this.updateAddress();
- } else {
- this.addAddress();
- }
- }
- })
|