AddressSaveForm.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\alliance\models;
  8. use app\constants\OptionSetting;
  9. use app\models\Address;
  10. use app\models\District;
  11. use app\models\Option;
  12. class AddressSaveForm extends \yii\base\Model
  13. {
  14. public $store_id;
  15. public $user_id;
  16. public $address_id;
  17. public $name;
  18. public $mobile;
  19. public $province_id;
  20. public $city_id;
  21. public $district_id;
  22. public $detail;
  23. public $longitude;
  24. public $latitude;
  25. public $is_default;
  26. public function rules()
  27. {
  28. return [
  29. [['name', 'mobile', 'province_id', 'city_id', 'district_id', 'detail',], 'trim'],
  30. [['name', 'mobile', 'province_id', 'city_id', 'district_id', 'detail',], 'required'],
  31. [['address_id', 'is_default'], 'integer'],
  32. [['latitude','longitude'], 'safe'],
  33. ];
  34. }
  35. public function attributeLabels()
  36. {
  37. return [
  38. 'name' => '收货人',
  39. 'mobile' => '联系电话',
  40. 'province_id' => '所在地区',
  41. 'city_id' => '所在地区',
  42. 'district_id' => '所在地区',
  43. 'detail' => '详细地址',
  44. 'latitude' => '地址纬度',
  45. 'longitude' => '地址经度',
  46. ];
  47. }
  48. public function save()
  49. {
  50. if (!$this->validate()) {
  51. return [
  52. 'code' => 1,
  53. 'msg' => $this->getErrorSummary(false)[0],
  54. ];
  55. }
  56. $store_mobile_verify = Option::get(OptionSetting::STORE_MOBILE_VERIFY, get_store_id(), 'store', '')['value'];
  57. $option = Option::get(OptionSetting::STORE_MOBILE_VERIFY, get_store_id(), 'reg', $store_mobile_verify);
  58. if ($option['value']) {
  59. if (strlen($this->mobile) != 11) {
  60. return [
  61. 'code' => 1,
  62. 'msg' => '请输入正确的手机号'
  63. ];
  64. }
  65. }
  66. if ($this->is_default) {
  67. Address::updateAll(['is_default' => 0], ['user_id' => $this->user_id]);
  68. }
  69. $address = Address::findOne([
  70. 'id' => $this->address_id,
  71. 'is_delete' => 0,
  72. 'user_id' => $this->user_id
  73. ]);
  74. if (!$address) {
  75. $address = new Address();
  76. $address->store_id = $this->store_id;
  77. $address->user_id = $this->user_id;
  78. $address->is_delete = Address::DELETE_STATUS_FALSE;
  79. $address->addtime = time();
  80. }
  81. $address->name = trim($this->name);
  82. $address->mobile = $this->mobile;
  83. $address->detail = trim($this->detail);
  84. $province = District::findOne($this->province_id);
  85. if (!$province) {
  86. return [
  87. 'code' => 1,
  88. 'msg' => '省份数据错误,请重新选择',
  89. ];
  90. }
  91. $address->province_id = $province->id;
  92. $address->province = $province->name;
  93. $city = District::findOne($this->city_id);
  94. if (!$city) {
  95. return [
  96. 'code' => 1,
  97. 'msg' => '城市数据错误,请重新选择',
  98. ];
  99. }
  100. $address->city_id = $city->id;
  101. $address->city = $city->name;
  102. $district = District::findOne($this->district_id);
  103. if (!$district) {
  104. return [
  105. 'code' => 1,
  106. 'msg' => '地区数据错误,请重新选择',
  107. ];
  108. }
  109. $address->district_id = $district->id;
  110. $address->district = $district->name;
  111. $address->longitude = $this->longitude ?: $district->lng;
  112. $address->latitude = $this->latitude ?: $district->lat;
  113. $address->is_default = $this->is_default ? 1 : 0;
  114. if ($address->save()) {
  115. return [
  116. 'code' => 0,
  117. 'msg' => '保存成功',
  118. 'data' => [
  119. 'address_id' => $address->attributes['id']
  120. ]
  121. ];
  122. } else {
  123. foreach ($address->errors as $error) {
  124. return [
  125. 'code' => 1,
  126. 'msg' => $error[0],
  127. ];
  128. }
  129. }
  130. }
  131. }