AddressSaveForm.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\client\models\v1;
  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 $town_id;
  27. public $village_id;
  28. public function rules()
  29. {
  30. return [
  31. [['name', 'mobile', 'province_id', 'city_id', 'district_id', 'detail', 'town_id', 'village_id'], 'trim'],
  32. [['name', 'mobile', 'province_id', 'city_id', 'district_id', 'detail',], 'required'],
  33. [['address_id', 'is_default'], 'integer'],
  34. [['latitude','longitude'], 'safe'],
  35. ];
  36. }
  37. public function attributeLabels()
  38. {
  39. return [
  40. 'name' => '收货人',
  41. 'mobile' => '联系电话',
  42. 'province_id' => '所在地区',
  43. 'city_id' => '所在地区',
  44. 'district_id' => '所在地区',
  45. 'town_id' => 'town ID',
  46. 'village_id' => 'Village ID',
  47. 'detail' => '详细地址',
  48. 'latitude' => '地址纬度',
  49. 'longitude' => '地址经度',
  50. ];
  51. }
  52. public function save()
  53. {
  54. if (!$this->validate()) {
  55. return [
  56. 'code' => 1,
  57. 'msg' => $this->getErrorSummary(false)[0],
  58. ];
  59. }
  60. $store_mobile_verify = Option::get(OptionSetting::STORE_MOBILE_VERIFY, get_store_id(), 'store', '')['value'];
  61. $option = Option::get(OptionSetting::STORE_MOBILE_VERIFY, get_store_id(), 'reg', $store_mobile_verify);
  62. if ($option['value']) {
  63. if (strlen($this->mobile) != 11) {
  64. return [
  65. 'code' => 1,
  66. 'msg' => '请输入正确的手机号'
  67. ];
  68. }
  69. }
  70. if ($this->is_default) {
  71. Address::updateAll(['is_default' => 0], ['user_id' => $this->user_id]);
  72. }
  73. $address = Address::findOne([
  74. 'id' => $this->address_id,
  75. 'is_delete' => 0,
  76. 'user_id' => $this->user_id
  77. ]);
  78. if (!$address) {
  79. $address = new Address();
  80. $address->store_id = $this->store_id;
  81. $address->user_id = $this->user_id;
  82. $address->is_delete = Address::DELETE_STATUS_FALSE;
  83. $address->addtime = time();
  84. }
  85. $address->name = trim($this->name);
  86. $address->mobile = $this->mobile;
  87. $address->detail = trim($this->detail);
  88. $province = District::findOne($this->province_id);
  89. if (!$province) {
  90. return [
  91. 'code' => 1,
  92. 'msg' => '省份数据错误,请重新选择',
  93. ];
  94. }
  95. $address->province_id = $province->id;
  96. $address->province = $province->name;
  97. $city = District::findOne($this->city_id);
  98. if (!$city) {
  99. return [
  100. 'code' => 1,
  101. 'msg' => '城市数据错误,请重新选择',
  102. ];
  103. }
  104. $address->city_id = $city->id;
  105. $address->city = $city->name;
  106. $district = District::findOne($this->district_id);
  107. if (!$district) {
  108. return [
  109. 'code' => 1,
  110. 'msg' => '地区数据错误,请重新选择',
  111. ];
  112. }
  113. $old_district_id = $address->district_id;
  114. $address->district_id = $district->id;
  115. $address->district = $district->name;
  116. $address->longitude = $this->longitude ?: $district->lng;
  117. $address->latitude = $this->latitude ?: $district->lat;
  118. /* 2023年7月14日17:11:25 begin */
  119. if($this->town_id && $this->town_id > 0) {
  120. $town = District::findOne($this->town_id);
  121. $address->town_id = $town->id;
  122. $address->town = $town->name;
  123. } elseif($old_district_id != $district->id) {
  124. $address->town_id = 0;
  125. $address->town = '';
  126. }
  127. if($this->village_id && $this->village_id > 0) {
  128. $village = District::findOne($this->village_id);
  129. $address->village_id = $village->id;
  130. $address->village = $village->name;
  131. } elseif($old_district_id != $district->id) {
  132. $address->village_id = 0;
  133. $address->village = '';
  134. }
  135. /* end */
  136. $address->is_default = $this->is_default ? 1 : 0;
  137. if ($address->save()) {
  138. if (empty($address->longitude) || empty($address->latitude)) {
  139. try {
  140. $addressDetail = $address->province . $address->city . $address->district . $address->town . $address->village . $address->detail;
  141. $res = \app\utils\Coordinate::getCoordinateByAddress($addressDetail);
  142. if ($res['code'] == 0) {
  143. $address->longitude = $res['data']['longitude'];
  144. $address->latitude = $res['data']['latitude'];
  145. $address->save();
  146. }
  147. } catch (\Exception $e) {
  148. // Todo
  149. }
  150. }
  151. return [
  152. 'code' => 0,
  153. 'msg' => '保存成功',
  154. 'data' => [
  155. 'address_id' => $address->attributes['id']
  156. ]
  157. ];
  158. } else {
  159. foreach ($address->errors as $error) {
  160. return [
  161. 'code' => 1,
  162. 'msg' => $error[0],
  163. ];
  164. }
  165. }
  166. }
  167. }