123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518 |
- var util = require('../../../../../utils/util.js');
- var api = require('../../../../../utils/api');
- var Parse = getApp().Parse;
- import placeArrays from "../../../../../utils/citysearch"
- const placeArray=placeArrays.placeArray
- Page({
- data: {
- type:1,
- address: {
- id: 0,
- province_id: 0,
- city_id: 0,
- district_id: 0,
- address: '',
- full_region: '',
- name: '',
- mobile: '',
- is_default: 0
- },
- addressId: 0,
- openSelectRegion: false,
- selectRegionList: [{
- id: 0,
- name: '省份',
- parent_id: 1,
- type: 1
- },
- {
- id: 0,
- name: '城市',
- parent_id: 1,
- type: 2
- },
- {
- id: 0,
- name: '区县',
- parent_id: 1,
- type: 3
- }
- ],
- regionType: 1,
- regionList: [],
- selectRegionDone: false,
- placeArray: placeArray,
- province: placeArray[0].name, //- 省
- pIndex: 0,
- city: placeArray[0].city[0].name, //- 市
- cIndex: 0,
- area: placeArray[0].city[0].area[0], //- 区
- aIndex: 0,
- isAddress:false,
- fullName:""
- },
- onClose(){
- this.setData({
- isAddress:false
- })
- },
- confirm(){
- let full_region = this.data.province+this.data.city+this.data.area
- this.setData({
- ['address.full_region']:full_region,
- isAddress:false
- })
- },
- showAddress(){
- this.setData({
- isAddress:true
- })
- },
- changeProvince: function(e){
- const val = e.detail.value //这些val都是数组形式的了
- this.setData({
- pIndex: val,
- cIndex: 0,
- aIndex: 0,
- province: placeArray[val].name,
- city: placeArray[val].city[0].name,
- area: placeArray[val].city[0].area[0]
- })
- },
- changeCity: function(e){
- const val = e.detail.value
- this.setData({
- cIndex: val,
- aIndex: 0,
- city: placeArray[this.data.pIndex].city[val].name,
- area: placeArray[this.data.pIndex].city[val].area[0]
- })
- },
- changeArea: function(e){
- const val = e.detail.value
- this.setData({
- aIndex: val,
- area: placeArray[this.data.pIndex].city[this.data.cIndex].area[val]
- })
- },
- mobilechange(e) {
- let mobile = e.detail.value;
- let address = this.data.address;
- if (util.testMobile(mobile)) {
- address.mobile = mobile;
- this.setData({
- address: address
- });
- }
- },
- bindinputName(event) {
- let address = this.data.address;
- address.name = event.detail.value;
- this.setData({
- address: address
- });
- },
- bindinputAddress(event) {
- let address = this.data.address;
- address.address = event.detail.value;
- this.setData({
- address: address
- });
- },
- switchChange(e) {
- let status = e.detail.value;
- let is_default = 0;
- if (status == true) {
- is_default = 1;
- }
- let address = 'address.is_default';
- this.setData({
- [address]: is_default
- });
- },
- async getAddressDetail() {
- let that = this;
- let checkedAddress = {}
- let query = new Parse.Query("ShopAddress");
- query.equalTo("objectId", that.data.addressId);
- let address = await query.first()
- if(address)
- {
- checkedAddress.name = address.get("name")
- checkedAddress.country_id = address.get("country_id")
- checkedAddress.province_id = address.get("province_id")
- checkedAddress.city_id = address.get("city_id")
- checkedAddress.district_id = address.get("district_id")
- checkedAddress.address = address.get("address")
- checkedAddress.mobile = address.get("mobile")
- checkedAddress.is_default = address.get("is_default")
- checkedAddress.province_name = address.get("province_name")
- checkedAddress.city_name = address.get("city_name")
- checkedAddress.district_name = address.get("district_name")
- checkedAddress.full_region = address.get("full_region")
- that.setData({
- address:checkedAddress
- })
- }
- // util.request(api.AddressDetail, {
- // id: that.data.addressId
- // }).then(function(res) {
- // if (res.errno === 0) {
- // that.setData({
- // address: res.data
- // });
- // }
- // });
- },
- deleteAddress: function() {
- let id = this.data.addressId;
- wx.showModal({
- title: '提示',
- content: '您确定要删除么?',
- success: function(res) {
- if (res.confirm) {
- util.request(api.DeleteAddress, {
- id: id
- }, 'POST').then(function(res) {
- if (res.errno === 0) {
- wx.removeStorageSync('addressId');
- util.showErrorToast('删除成功');
- wx.navigateBack();
- } else {
- util.showErrorToast(res.errmsg);
- }
- });
- }
- }
- })
- },
- setRegionDoneStatus() {
- let that = this;
- let doneStatus = that.data.selectRegionList.every(item => {
- return item.id != 0;
- });
- that.setData({
- selectRegionDone: doneStatus
- })
- },
- chooseRegion() {
- let that = this;
- this.setData({
- openSelectRegion: !this.data.openSelectRegion
- });
- //设置区域选择数据
- let address = this.data.address;
- if (address.province_id > 0 && address.city_id > 0 && address.district_id > 0) {
- let selectRegionList = this.data.selectRegionList;
- selectRegionList[0].id = address.province_id;
- selectRegionList[0].name = address.province_name;
- selectRegionList[0].parent_id = 1;
- selectRegionList[1].id = address.city_id;
- selectRegionList[1].name = address.city_name;
- selectRegionList[1].parent_id = address.province_id;
- selectRegionList[2].id = address.district_id;
- selectRegionList[2].name = address.district_name;
- selectRegionList[2].parent_id = address.city_id;
- this.setData({
- selectRegionList: selectRegionList,
- regionType: 3
- });
- this.getRegionList(address.city_id);
- } else {
- this.setData({
- selectRegionList: [{
- id: 0,
- name: '省份',
- parent_id: 1,
- type: 1
- },
- {
- id: 0,
- name: '城市',
- parent_id: 1,
- type: 2
- },
- {
- id: 0,
- name: '区县',
- parent_id: 1,
- type: 3
- }
- ],
- regionType: 1
- })
- this.getRegionList(1);
- }
- this.setRegionDoneStatus();
- },
- onLoad: function(options) {
- let {activeColor,titleColor}=options
- this.setData({activeColor,titleColor})
- // 页面初始化 options为页面跳转所带来的参数
- if (options.id) {
- this.setData({
- addressId: options.id,
- type:2
- });
- this.getAddressDetail();
- }
- // this.getRegionList(1);
- },
- onReady: function() {
- },
- async saveEditAddress(){
- let that = this
- let query = new Parse.Query("ShopAddress");
- query.equalTo("objectId", that.data.addressId);
- let shopAddress = await query.first()
- shopAddress.set("name",that.data.address.name)
- shopAddress.set("mobile",that.data.address.mobile)
- shopAddress.set("province_id",that.data.address.province_id)
- shopAddress.set("city_id",that.data.address.city_id)
- shopAddress.set("district_id",that.data.address.district_id)
- shopAddress.set("address",that.data.address.address)
- shopAddress.set("province_name",that.data.province)
- shopAddress.set("city_name",that.data.city)
- shopAddress.set("district_name",that.data.area)
- shopAddress.set("full_region",that.data.address.full_region)
- shopAddress.set("is_default",that.data.address.is_default)
- if(that.data.address.is_default == 1){
- that.setDefault()
- }
- await shopAddress.save().then(data=>{
- wx.navigateBack()
- })
- },
- selectRegionType(event) {
- let that = this;
- let regionTypeIndex = event.target.dataset.regionTypeIndex;
- let selectRegionList = that.data.selectRegionList;
- //判断是否可点击
- if (regionTypeIndex + 1 == this.data.regionType || (regionTypeIndex - 1 >= 0 && selectRegionList[regionTypeIndex - 1].id <= 0)) {
- return false;
- }
- this.setData({
- regionType: regionTypeIndex + 1
- })
- let selectRegionItem = selectRegionList[regionTypeIndex];
- this.getRegionList(selectRegionItem.parent_id);
- this.setRegionDoneStatus();
- },
- selectRegion(event) {
- let that = this;
- let regionIndex = event.target.dataset.regionIndex;
- let regionItem = this.data.regionList[regionIndex];
- let regionType = regionItem.type;
- let selectRegionList = this.data.selectRegionList;
- selectRegionList[regionType - 1] = regionItem;
- if (regionType != 3) {
- this.setData({
- selectRegionList: selectRegionList,
- regionType: regionType + 1
- })
- this.getRegionList(regionItem.id);
- } else {
- this.setData({
- selectRegionList: selectRegionList
- })
- }
- //重置下级区域为空
- selectRegionList.map((item, index) => {
- if (index > regionType - 1) {
- item.id = 0;
- item.name = index == 1 ? '城市' : '区县';
- item.parent_id = 0;
- }
- return item;
- });
- this.setData({
- selectRegionList: selectRegionList
- })
- that.setData({
- regionList: that.data.regionList.map(item => {
- //标记已选择的
- if (that.data.regionType == item.type && that.data.selectRegionList[that.data.regionType - 1].id == item.id) {
- item.selected = true;
- } else {
- item.selected = false;
- }
- return item;
- })
- });
- this.setRegionDoneStatus();
- },
- doneSelectRegion() {
- if (this.data.selectRegionDone === false) {
- return false;
- }
- let address = this.data.address;
- let selectRegionList = this.data.selectRegionList;
- address.province_id = selectRegionList[0].id;
- address.city_id = selectRegionList[1].id;
- address.district_id = selectRegionList[2].id;
- address.province_name = selectRegionList[0].name;
- address.city_name = selectRegionList[1].name;
- address.district_name = selectRegionList[2].name;
- address.full_region = selectRegionList.map(item => {
- return item.name;
- }).join('');
- this.setData({
- address: address,
- openSelectRegion: false
- });
- },
- cancelSelectRegion() {
- this.setData({
- openSelectRegion: false,
- regionType: this.data.regionDoneStatus ? 3 : 1
- });
- },
- getRegionList(regionId) {
- let that = this;
- let regionType = that.data.regionType;
- util.request(api.RegionList, {
- parentId: regionId
- }).then(function(res) {
- if (res.errno === 0) {
- that.setData({
- regionList: res.data.map(item => {
- //标记已选择的
- if (regionType == item.type && that.data.selectRegionList[regionType - 1].id == item.id) {
- item.selected = true;
- } else {
- item.selected = false;
- }
- return item;
- })
- });
- }
- });
- },
- async saveAddress() {
- let address = this.data.address;
- let company = getApp().globalData.company
- let user = Parse.User.current();
- if (address.name == '' || address.name == undefined) {
- util.showErrorToast('请输入姓名');
- return false;
- }
- if (address.mobile == '' || address.mobile == undefined) {
- util.showErrorToast('请输入手机号码');
- return false;
- }
- if (address.province == "" || address.city == "") {
- util.showErrorToast('请输入省市区');
- return false;
- }
- if (address.address == '' || address.address == undefined) {
- util.showErrorToast('请输入详细地址');
- return false;
- }
- let that = this;
- let ShopAddress = Parse.Object.extend("ShopAddress");
- let shopAddress = new ShopAddress();
- shopAddress.set("user",{
- "__type": "Pointer",
- "className": "_User",
- "objectId": user.id
- })
- shopAddress.set("company",{
- "__type": "Pointer",
- "className": "Company",
- "objectId": company
- })
- shopAddress.set("name",that.data.address.name)
- shopAddress.set("mobile",that.data.address.mobile)
- shopAddress.set("province_id",that.data.address.province_id)
- shopAddress.set("city_id",that.data.address.city_id)
- shopAddress.set("district_id",that.data.address.district_id)
- shopAddress.set("address",that.data.address.address)
- shopAddress.set("province_name",that.data.province)
- shopAddress.set("city_name",that.data.city)
- shopAddress.set("district_name",that.data.area)
- shopAddress.set("full_region",that.data.address.full_region)
- shopAddress.set("is_default",that.data.address.is_default)
- if(that.data.address.is_default == 1){
- that.setDefault()
- }
- await shopAddress.save().then(data=>{
- wx.navigateBack()
- })
- // util.request(api.SaveAddress, {
- // id: address.id,
- // name: address.name,
- // mobile: address.mobile,
- // province_id: address.province_id,
- // city_id: address.city_id,
- // district_id: address.district_id,
- // address: address.address,
- // is_default: address.is_default,
- // }, 'POST').then(function(res) {
- // if (res.errno === 0) {
- //
- // }
- // });
- },
- setDefault(){
- let ShopAddress = new Parse.Query("ShopAddress")
- ShopAddress.equalTo("user",Parse.User.current().id)
- ShopAddress.equalTo("is_default",1)
- ShopAddress.first().then(res=>{
- if(res){
- res.set("is_default",0)
- res.save()
- }
- })
- },
- onShow: function() {
- let id = this.data.addressId;
- if (id > 0) {
- wx.setNavigationBarTitle({
- title: '编辑地址',
- })
- } else {
- wx.setNavigationBarTitle({
- title: '新增地址',
- })
- }
- },
- onHide: function() {
- // 页面隐藏
- },
- onUnload: function() {
- // 页面关闭
- }
- })
|