| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- // pages/address-edit/address-edit.js
- var api = require('../../api.js');
- var area_picker = require('../../area-picker/area-picker.js');
- var app = getApp();
- Page({
- data: {
- name: "",
- mobile: "",
- detail: "",
- district: null,
- is_default: 0,
- // check: false
- },
- changeState: function(e){
- if(e.detail.value){
- this.setData({
- is_default: 1
- })
- }
- else{
- this.setData({
- is_default: 0
- })
- }
- },
- onLoad: function (options) {
- app.pageOnLoad(this);
- var page = this;
- page.getDistrictData(function (data) {
- area_picker.init({
- page: page,
- data: data,
- });
- });
- page.setData({
- address_id: options.id,
- });
- if (options.id) {
- wx.setNavigationBarTitle({
- title: '编辑地址'
- })
- wx.showLoading({
- title: "正在加载",
- mask: true,
- });
- app.request({
- url: api.user.address_detail,
- data: {
- id: options.id,
- },
- success: function (res) {
- wx.hideLoading();
- if (res.code == 0) {
- page.setData(res.data);
- }
- }
- });
- }
- else{
- wx.setNavigationBarTitle({
- title: '新增地址',
- })
- }
- },
- getDistrictData: function (cb) {
- var district = wx.getStorageSync("district");
- if (!district || district.length == 0) {
- wx.showLoading({
- title: "正在加载",
- mask: true,
- });
- app.request({
- url: api.default.district,
- success: function (res) {
- wx.hideLoading();
- if (res.code == 0) {
- district = res.data;
- wx.setStorageSync("district", district);
- cb(district);
- }
- }
- });
- return;
- }
- cb(district);
- },
- onAreaPickerConfirm: function (e) {
- //console.log(e);
- var page = this;
- page.setData({
- district: {
- province: {
- id: e[0].id,
- name: e[0].name,
- },
- city: {
- id: e[1].id,
- name: e[1].name,
- },
- district: {
- id: e[2].id,
- name: e[2].name,
- },
- }
- });
- },
- saveAddress: function () {
- var page = this;
- var myreg = /^([0-9]{6,12})$/;
- var myreg2 = /^(\d{3,4}-\d{6,9})$/;
- console.log(myreg2.test(page.data.mobile));
- if (!myreg.test(page.data.mobile) && !myreg2.test(page.data.mobile)) {
- wx.showToast({
- title: "电话格式不正确",
- image: "/images/icon-warning.png",
- });
- return false;
- }
- wx.showLoading({
- title: "正在保存",
- mask: true,
- });
- var district = page.data.district;
- if (!district) {
- district = {
- province: {
- id: ""
- },
- city: {
- id: ""
- },
- district: {
- id: ""
- }
- };
- }
- app.request({
- url: api.user.address_save,
- method: "post",
- data: {
- address_id: page.data.address_id || "",
- name: page.data.name,
- mobile: page.data.mobile,
- province_id: district.province.id,
- city_id: district.city.id,
- district_id: district.district.id,
- detail: page.data.detail,
- is_default: page.data.is_default
- },
- success: function (res) {
- wx.hideLoading();
- if (res.code == 0) {
- wx.showModal({
- title: "提示",
- content: res.msg,
- showCancel: false,
- success: function (res) {
- if (res.confirm) {
- wx.navigateBack();
- }
- }
- });
- }
- if (res.code == 1) {
- wx.showToast({
- title: res.msg,
- image: "/images/icon-warning.png",
- });
- }
- }
- });
- },
- inputBlur: function (e) {
- //console.log(JSON.stringify(e));
- var name = e.currentTarget.dataset.name;
- var value = e.detail.value;
- //var data = '{"form":{"' + name + '":"' + value + '"}}';
- var data = '{"' + name + '":"' + value + '"}';
- this.setData(JSON.parse(data));
- },
- getWechatAddress: function (e) {
- var page = this;
- wx.chooseAddress({
- success: function (e) {
- if (e.errMsg != 'chooseAddress:ok')
- return;
- wx.showLoading();
- app.request({
- url: api.user.wechat_district,
- data: {
- national_code: e.nationalCode,
- province_name: e.provinceName,
- city_name: e.cityName,
- county_name: e.countyName,
- },
- success: function (res) {
- if (res.code == 1) {
- wx.showModal({
- title: '提示',
- content: res.msg,
- showCancel: false,
- });
- }
- page.setData({
- name: e.userName || "",
- mobile: e.telNumber || "",
- detail: e.detailInfo || "",
- district: res.data.district,
- });
- },
- complete: function () {
- wx.hideLoading();
- }
- });
- }
- });
- },
- onReady: function () {
- },
- onShow: function () {
- },
- });
|