123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- import Api from "../../../api/index.js"
- import { requestFnc } from "../../../utils/request.js"
- import { getStore } from "../../../utils/index.js"
- Page({
- data: {
- isLoading: false,
- linkman: "",
- linkPhone: "",
- address: "",
- addressId: "",
- showCity: false,
- provinceCode: "",
- provinceName: "",
- cityCode: "",
- cityName: "",
- areaName: "",
- areaCode: "",
- currentType: "",
- sendType: "1",
- cityList: [],
- searchProvinceCode: "",
- searchCityCode: "",
- checked: false,
- isEdit: false,
- },
- onLoad(options) {
- if (options.address) {
- let adressObj = JSON.parse(options.address)
- this.setData({
- isEdit: true,
- ...adressObj,
- })
- if (adressObj.isDefaultAddress == 1) {
- this.setData({
- checked: true,
- })
- }
- }
- },
- onChange({ detail }) {
- // 需要手动对 checked 状态进行更新
- this.setData({ checked: detail })
- },
- // 选择省、市、区
- onCityClick(e) {
- let type = e.currentTarget.dataset.type
- this.setData({
- currentType: type,
- })
- if (type === "close") {
- this.setData({
- showCity: false,
- })
- return
- }
- if (type === "provice") {
- this.setData({
- searchProvinceCode: "",
- searchCityCode: "",
- })
- }
- if (type === "city") {
- this.setData({
- searchProvinceCode: this.data.provinceCode,
- searchCityCode: "",
- })
- }
- if (type === "county") {
- this.setData({
- searchProvinceCode: this.data.provinceCode,
- searchCityCode: this.data.cityCode,
- })
- }
- this.getProvice(type)
- },
- // 获取输入框的值
- changeInput(e) {
- let key = e.currentTarget.dataset.type
- this.data[key] = e.detail
- this.setData({
- ...this.data,
- })
- },
- // 选择城市
- onSelectCity(event) {
- console.log("event:", event)
- let { value } = event.detail
- if (this.data.currentType === "provice") {
- this.setData({
- provinceCode: value.code,
- provinceName: value.name,
- searchProvinceCode: value.code,
- searchCityCode: "",
- cityCode: "",
- cityName: "",
- areaName: "",
- areaCode: "",
- showCity: false,
- })
- }
- if (this.data.currentType === "city") {
- this.setData({
- cityCode: value.code,
- cityName: value.name,
- searchCityCode: value.code,
- areaName: "",
- areaCode: "",
- showCity: false,
- })
- }
- if (this.data.currentType === "county") {
- this.setData({
- areaName: value.name,
- areaCode: value.code,
- showCity: false,
- })
- }
- },
- // 获取收货地址
- getAddress(cb) {
- let params = {
- filename: Api.getAddress.filename,
- data: {
- accountId: getStore("accountId"),
- },
- }
- requestFnc(Api.getAddress.url, params, (res) => {
- let adressObj = res.dataModel[0]
- this.setData({
- ...adressObj,
- })
- cb && cb()
- })
- },
- // 获取省市区
- getProvice(type) {
- let params = {
- filename: Api.getProvice.filename,
- data: {
- provinceCode: this.data.searchProvinceCode,
- cityCode: this.data.searchCityCode,
- },
- }
- requestFnc(Api.getProvice.url, params, (res) => {
- let list = res.data || []
- for (let obj of list) {
- obj.text = obj.name
- }
- this.setData({
- cityList: list,
- })
- if (type) {
- this.setData({
- showCity: true,
- })
- }
- })
- },
- // 保存地址
- saveAddress() {
- let title = ""
- if (!this.data.linkPhone || !this.data.linkman) {
- title = "联系人和联系方式不能为空"
- }
- if (
- !this.data.provinceCode ||
- !this.data.cityCode ||
- !this.data.areaCode
- ) {
- title = "省市区不能为空"
- }
- if (title) {
- wx.showToast({
- title: title,
- icon: "none",
- })
- return
- }
- this.setData({
- isLoading: true,
- })
- let params = {
- filename: Api.saveAddress.filename,
- data: {
- accountId: getStore("accountId"),
- addressId: this.data.addressId,
- linkman: this.data.linkman,
- address: this.data.address,
- linkPhone: this.data.linkPhone,
- provinceCode: this.data.provinceCode,
- cityCode: this.data.cityCode,
- areaCode: this.data.areaCode,
- postcode: this.data.postcode,
- sendType: this.data.sendType,
- isDefaultAddress: this.data.checked ? "1" : "0",
- },
- }
- requestFnc(
- Api.saveAddress.url,
- params,
- (res) => {
- this.setData({
- isLoading: false,
- })
- wx.navigateBack({
- delta: 1,
- })
- },
- () => {
- this.setData({
- isLoading: false,
- })
- }
- )
- },
- // 删除地址
- delAddress(e) {
- wx.showModal({
- title: "提示",
- content: "确定要删除此地址吗?",
- success: (res) => {
- if (res.confirm) {
- let params = {
- filename: Api.delAddress.filename,
- data: {
- accountId: getStore("accountId"),
- addressId: this.data.addressId,
- },
- }
- this.setData({
- isLoading: true,
- })
- requestFnc(
- Api.delAddress.url,
- params,
- (res) => {
- this.setData({
- isLoading: false,
- })
- wx.navigateBack({
- delta: 1,
- })
- },
- () => {
- this.setData({
- isLoading: false,
- })
- }
- )
- }
- },
- })
- },
- })
|