| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\alliance\models;
- use app\constants\OptionSetting;
- use app\models\Address;
- use app\models\District;
- use app\models\Option;
- class AddressSaveForm extends \yii\base\Model
- {
- public $store_id;
- public $user_id;
- public $address_id;
- public $name;
- public $mobile;
- public $province_id;
- public $city_id;
- public $district_id;
- public $detail;
- public $longitude;
- public $latitude;
- public $is_default;
- public function rules()
- {
- return [
- [['name', 'mobile', 'province_id', 'city_id', 'district_id', 'detail',], 'trim'],
- [['name', 'mobile', 'province_id', 'city_id', 'district_id', 'detail',], 'required'],
- [['address_id', 'is_default'], 'integer'],
- [['latitude','longitude'], 'safe'],
- ];
- }
- public function attributeLabels()
- {
- return [
- 'name' => '收货人',
- 'mobile' => '联系电话',
- 'province_id' => '所在地区',
- 'city_id' => '所在地区',
- 'district_id' => '所在地区',
- 'detail' => '详细地址',
- 'latitude' => '地址纬度',
- 'longitude' => '地址经度',
- ];
- }
- public function save()
- {
- if (!$this->validate()) {
- return [
- 'code' => 1,
- 'msg' => $this->getErrorSummary(false)[0],
- ];
- }
- $store_mobile_verify = Option::get(OptionSetting::STORE_MOBILE_VERIFY, get_store_id(), 'store', '')['value'];
- $option = Option::get(OptionSetting::STORE_MOBILE_VERIFY, get_store_id(), 'reg', $store_mobile_verify);
- if ($option['value']) {
- if (strlen($this->mobile) != 11) {
- return [
- 'code' => 1,
- 'msg' => '请输入正确的手机号'
- ];
- }
- }
- if ($this->is_default) {
- Address::updateAll(['is_default' => 0], ['user_id' => $this->user_id]);
- }
- $address = Address::findOne([
- 'id' => $this->address_id,
- 'is_delete' => 0,
- 'user_id' => $this->user_id
- ]);
- if (!$address) {
- $address = new Address();
- $address->store_id = $this->store_id;
- $address->user_id = $this->user_id;
- $address->is_delete = Address::DELETE_STATUS_FALSE;
- $address->addtime = time();
- }
- $address->name = trim($this->name);
- $address->mobile = $this->mobile;
- $address->detail = trim($this->detail);
- $province = District::findOne($this->province_id);
- if (!$province) {
- return [
- 'code' => 1,
- 'msg' => '省份数据错误,请重新选择',
- ];
- }
- $address->province_id = $province->id;
- $address->province = $province->name;
- $city = District::findOne($this->city_id);
- if (!$city) {
- return [
- 'code' => 1,
- 'msg' => '城市数据错误,请重新选择',
- ];
- }
- $address->city_id = $city->id;
- $address->city = $city->name;
- $district = District::findOne($this->district_id);
- if (!$district) {
- return [
- 'code' => 1,
- 'msg' => '地区数据错误,请重新选择',
- ];
- }
- $address->district_id = $district->id;
- $address->district = $district->name;
- $address->longitude = $this->longitude ?: $district->lng;
- $address->latitude = $this->latitude ?: $district->lat;
- $address->is_default = $this->is_default ? 1 : 0;
- if ($address->save()) {
- return [
- 'code' => 0,
- 'msg' => '保存成功',
- 'data' => [
- 'address_id' => $address->attributes['id']
- ]
- ];
- } else {
- foreach ($address->errors as $error) {
- return [
- 'code' => 1,
- 'msg' => $error[0],
- ];
- }
- }
- }
- }
|