Address.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. <?php
  2. /*
  3. * FecShop file.
  4. *
  5. * @link http://www.fecshop.com/
  6. * @copyright Copyright (c) 2016 FecShop Software LLC
  7. * @license http://www.fecshop.com/license/
  8. */
  9. namespace fecshop\services\customer;
  10. //use fecshop\models\mysqldb\customer\Address as MyAddress;
  11. use fecshop\services\Service;
  12. use Yii;
  13. /**
  14. * Address child services.
  15. * @author Terry Zhao <2358269014@qq.com>
  16. * @since 1.0
  17. */
  18. class Address extends Service
  19. {
  20. protected $currentCountry;
  21. protected $currentState;
  22. protected $_addressModelName = '\fecshop\models\mysqldb\customer\Address';
  23. protected $_addressModel;
  24. public $is_default = 1;
  25. public $is_not_default = 2;
  26. public function init()
  27. {
  28. parent::init();
  29. list($this->_addressModelName, $this->_addressModel) = \Yii::mapGet($this->_addressModelName);
  30. }
  31. protected function actionGetPrimaryKey()
  32. {
  33. return 'address_id';
  34. }
  35. /**
  36. * @param $primaryKey | Int
  37. * @return Object(MyCoupon)
  38. * 通过id找到customer address的对象
  39. */
  40. protected function actionGetByPrimaryKey($primaryKey)
  41. {
  42. $one = $this->_addressModel->findOne($primaryKey);
  43. $primaryKey = $this->getPrimaryKey();
  44. if ($one[$primaryKey]) {
  45. return $one;
  46. } else {
  47. return new $this->_addressModelName();
  48. }
  49. }
  50. /**
  51. * @param $address_id | Int , address表的id
  52. * @param $customer_id | Int , 用户id
  53. * 在这里在主键查询的同时,加入customer_id,这样查询的肯定是这个用户的,
  54. * 这样就防止有的用户去查询其他用户的address信息。
  55. */
  56. protected function actionGetAddressByIdAndCustomerId($address_id, $customer_id)
  57. {
  58. $primaryKey = $this->getPrimaryKey();
  59. $one = $this->_addressModel->findOne([
  60. $primaryKey => $address_id,
  61. 'customer_id' => $customer_id,
  62. ]);
  63. if ($one[$primaryKey]) {
  64. return $one;
  65. } else {
  66. return false;
  67. }
  68. }
  69. /**
  70. * @param $filter|array
  71. * @return Array;
  72. * 通过过滤条件,得到coupon的集合。
  73. * example filter:
  74. * [
  75. * 'numPerPage' => 20,
  76. * 'pageNum' => 1,
  77. * 'orderBy' => ['_id' => SORT_DESC, 'sku' => SORT_ASC ],
  78. * 'where' => [
  79. * ['>','price',1],
  80. * ['<=','price',10]
  81. * ['sku' => 'uk10001'],
  82. * ],
  83. * 'asArray' => true,
  84. * ]
  85. */
  86. protected function actionColl($filter = '')
  87. {
  88. $query = $this->_addressModel->find();
  89. $query = Yii::$service->helper->ar->getCollByFilter($query, $filter);
  90. $coll = $query->all();
  91. return [
  92. 'coll' => $coll,
  93. 'count'=> $query->limit(null)->offset(null)->count(),
  94. ];
  95. }
  96. /**
  97. * @return Array
  98. * 得到当前用户的所有货运地址数组
  99. */
  100. protected function actionCurrentAddressList()
  101. {
  102. $arr = [];
  103. if (!Yii::$app->user->isGuest) {
  104. $identity = Yii::$app->user->identity;
  105. $customer_id = $identity['id'];
  106. if ($customer_id) {
  107. $filter = [
  108. 'numPerPage' => 30,
  109. 'pageNum' => 1,
  110. 'orderBy' => ['is_default' => SORT_ASC ,'updated_at' => SORT_DESC],
  111. 'where' => [
  112. ['customer_id' => $customer_id],
  113. ],
  114. 'asArray' => true,
  115. ];
  116. $coll = $this->coll($filter);
  117. $ii = 0;
  118. if (is_array($coll['coll']) && !empty($coll['coll'])) {
  119. foreach ($coll['coll'] as $one) {
  120. $address_id = $one['address_id'];
  121. $first_name = $one['first_name'];
  122. $last_name = $one['last_name'];
  123. $email = $one['email'];
  124. $telephone = $one['telephone'];
  125. $street1 = $one['street1'];
  126. $street2 = $one['street2'];
  127. $is_default = $one['is_default'];
  128. $city = $one['city'];
  129. $state = $one['state'];
  130. $zip = $one['zip'];
  131. $area = $one['area'];
  132. $country = Yii::$service->helper->country->getCountryNameByKey($one['country']);
  133. $state = Yii::$service->helper->country->getStateByContryCode($one['country'],$one['state']);
  134. $address_info = [
  135. 'address_id' => $address_id,
  136. 'first_name' => $first_name,
  137. 'last_name' => $last_name,
  138. 'email' => $email,
  139. 'telephone' => $telephone,
  140. 'street1' => $street1,
  141. 'is_default' => $is_default,
  142. 'city' => $city,
  143. 'state' => $state,
  144. 'zip' => $zip,
  145. 'area' => $area,
  146. ];
  147. //$state = Yii::$service->helper->country->getStateByContryCode($one['country'],$one['state']);
  148. $str = $first_name.' '.$last_name.' '.$email.' '.
  149. $street1.' '.$street2.' '.$city.' '.$state.' '.$country.' '.
  150. $zip.' '.$telephone;
  151. if ($is_default == 1) {
  152. $ii = 1;
  153. }
  154. $arr[$address_id] = [
  155. 'address' => $str,
  156. 'is_default'=>$is_default,
  157. 'address_info' => $address_info,
  158. ];
  159. }
  160. if (!$ii) {
  161. // 如果没有默认的地址,则取第一个当默认
  162. foreach ($arr as $k=>$v) {
  163. $arr[$k]['is_default'] = 1;
  164. break;
  165. }
  166. }
  167. }
  168. }
  169. }
  170. return $arr;
  171. }
  172. /**
  173. * @param $one|array , 保存的address数组
  174. * @return int 返回保存的 address_id 的值。
  175. */
  176. protected function actionSave($one)
  177. {
  178. if (!is_array($one) || empty($one)) {
  179. Yii::$service->helper->errors->add('address data is empty');
  180. return false;
  181. }
  182. $primaryKey = $this->getPrimaryKey();
  183. $primaryVal = isset($one[$primaryKey]) ? $one[$primaryKey] : '';
  184. if ($primaryVal) {
  185. $model = $this->_addressModel->findOne($primaryVal);
  186. if (!$model) {
  187. Yii::$service->helper->errors->add('address {primaryKey} is not exist', ['primaryKey' => $this->getPrimaryKey()]);
  188. return false;
  189. }
  190. } else {
  191. $model = new $this->_addressModelName();
  192. $model->created_at = time();
  193. }
  194. $model->attributes = $one;
  195. // 规则验证
  196. if ($model->validate()) {
  197. $model->updated_at = time();
  198. // 保存地址。
  199. $model = Yii::$service->helper->ar->save($model, $one);
  200. if (!$model) {
  201. return false;
  202. }
  203. } else {
  204. $errors = $model->errors;
  205. Yii::$service->helper->errors->addByModelErrors($errors);
  206. return false;
  207. }
  208. $primaryVal = $model[$primaryKey];
  209. if ($one['is_default'] == 1) {
  210. $customer_id = $one['customer_id'];
  211. if ($customer_id && $primaryVal) {
  212. $this->_addressModel->updateAll(
  213. ['is_default'=>2], // $attributes
  214. 'customer_id = :customer_id and '.$primaryKey.' != :primaryVal ', // $condition
  215. [
  216. 'customer_id' => $customer_id,
  217. 'primaryVal' => $primaryVal,
  218. ]
  219. );
  220. }
  221. }
  222. return $primaryVal;
  223. }
  224. /**
  225. * @param $ids | Int or Array
  226. * @return bool
  227. * 如果传入的是id数组,则删除多个address,如果传入的是Int,则删除一个address
  228. * 删除address的同时,删除掉购物车中的address_id
  229. * 删除address的同时,如果删除的是default address,那么重新找出来一个address作为default address并保存到表中。
  230. */
  231. protected function actionRemove($ids, $customer_id)
  232. {
  233. if (!$ids) {
  234. Yii::$service->helper->errors->add('remove id is empty');
  235. return false;
  236. }
  237. if (is_array($ids) && !empty($ids)) {
  238. foreach ($ids as $id) {
  239. $model = $this->_addressModel->findOne($id);
  240. if (isset($model[$this->getPrimaryKey()]) && !empty($model[$this->getPrimaryKey()])) {
  241. if ($customer_id) {
  242. if ($model['customer_id'] == $customer_id) {
  243. $this->removeCartAddress($model['customer_id'], $id);
  244. $model->delete();
  245. } else {
  246. Yii::$service->helper->errors->add('remove address is not current customer address');
  247. }
  248. }
  249. //} else {
  250. // $this->removeCartAddress($model['customer_id'], $id);
  251. // $model->delete();
  252. //}
  253. } else {
  254. Yii::$service->helper->errors->add('Address Remove Errors:ID {id} is not exist', ['id' => $id]);
  255. return false;
  256. }
  257. }
  258. } else {
  259. $id = $ids;
  260. $model = $this->_addressModel->findOne($id);
  261. if (isset($model[$this->getPrimaryKey()]) && !empty($model[$this->getPrimaryKey()])) {
  262. if ($customer_id) {
  263. if ($model['customer_id'] == $customer_id) {
  264. $this->removeCartAddress($model['customer_id'], $id);
  265. $model->delete();
  266. } else {
  267. Yii::$service->helper->errors->add('remove address is not current customer address');
  268. }
  269. }
  270. //} else {
  271. // $this->removeCartAddress($model['customer_id'], $id);
  272. // $model->delete();
  273. //}
  274. } else {
  275. Yii::$service->helper->errors->add('Address Remove Errors:ID:{id} is not exist', ['id'=> $id]);
  276. return false;
  277. }
  278. }
  279. // 查看是否有默认地址?如果该用户存在记录,但是没有默认地址,
  280. // 则查找用户是否存在非默认地址,如果存在,则取一个设置为默认地址
  281. $addressOne = $this->_addressModel->find()->asArray()
  282. ->where(['customer_id' => $customer_id, 'is_default' => 1])
  283. ->one();
  284. if (!$addressOne['address_id']) {
  285. $assOne = $this->_addressModel->find()
  286. ->where(['customer_id' => $customer_id])
  287. ->one();
  288. if ($assOne['address_id']) {
  289. $assOne->is_default = 1;
  290. $assOne->updated_at = time();
  291. $assOne->save();
  292. }
  293. }
  294. return true;
  295. }
  296. /**
  297. * @param $customer_id | Int ,
  298. * @param $address_id | Int,address id
  299. * 删除购物车中的address部分。
  300. */
  301. protected function removeCartAddress($customer_id, $address_id)
  302. {
  303. $cart = Yii::$service->cart->quote->getCartByCustomerId($customer_id);
  304. if (isset($cart['customer_address_id']) && !empty($cart['customer_address_id'])) {
  305. if ($cart['customer_address_id'] == $address_id) {
  306. $cart->customer_address_id = '';
  307. $cart->save();
  308. }
  309. }
  310. }
  311. /**
  312. * @param $customer_id | int 用户id
  313. * @param $address_id | int 用户地址id
  314. * @return boolean
  315. * 将某个address_id对应的地址数据,设置成默认地址
  316. */
  317. public function setDefault($customer_id, $address_id)
  318. {
  319. $primaryKey = $this->getPrimaryKey();
  320. // 当前的设置成1
  321. $address_one = $this->_addressModel->findOne([
  322. 'customer_id' => $customer_id,
  323. $primaryKey => $address_id,
  324. ]);
  325. if (!$address_one[$primaryKey]) {
  326. Yii::$service->helper->errors->add('customer address is empty');
  327. return false;
  328. }
  329. $address_one->is_default = 1;
  330. $address_one->updated_at = time();
  331. $address_one->save();
  332. // 将其他的设置成2
  333. $this->_addressModel->updateAll(
  334. ['is_default'=>2], // $attributes
  335. 'customer_id = :customer_id and '.$primaryKey.' != :primaryVal ', // $condition
  336. [
  337. 'customer_id' => $customer_id,
  338. 'primaryVal' => $address_id,
  339. ]
  340. );
  341. return true;
  342. }
  343. public function getDefualtAddressId($customer_id = '')
  344. {
  345. if(!$customer_id){
  346. $identity = Yii::$app->user->identity;
  347. $customer_id = $identity['id'];
  348. }
  349. if (!$customer_id) {
  350. return null;
  351. }
  352. $addressOne = $this->_addressModel->find()->asArray()
  353. ->where(['customer_id' => $customer_id,'is_default' => 1])
  354. ->one();
  355. if($addressOne['address_id']){
  356. return $addressOne['address_id'];
  357. }
  358. return null;
  359. }
  360. /*
  361. * @param $customer_id | int 用户的id
  362. * @return Array Or ''
  363. * 得到customer的默认地址。
  364. */
  365. /*
  366. protected function actionGetDefaultAddress($customer_id = ''){
  367. if(!$customer_id){
  368. $identity = Yii::$app->user->identity;
  369. $customer_id = $identity['id'];
  370. }
  371. if($customer_id ){
  372. $addressOne = $this->_addressModel->find()->asArray()
  373. ->where(['customer_id' => $customer_id,'is_default' => 1])
  374. ->one();
  375. if($addressOne['address_id']){
  376. return $addressOne;
  377. }else{
  378. $assOne = $this->_addressModel->find()->asArray()
  379. ->where(['customer_id' => $customer_id])
  380. ->one();
  381. if($assOne['address_id']){
  382. return $assOne;
  383. }
  384. }
  385. }
  386. }
  387. */
  388. /**
  389. * 得到登陆用户的默认货运地址。
  390. */
  391. public function getDefaultAddress(){
  392. if (Yii::$app->user->isGuest) {
  393. Yii::$service->helper->errors->add('customer is guest');
  394. return null;
  395. }
  396. $identity = Yii::$app->user->identity;
  397. $customer_id = $identity->id;
  398. $one = $this->_addressModel->findOne([
  399. 'customer_id' => $customer_id,
  400. 'is_default' => $this->is_default,
  401. ]);
  402. if ($one['customer_id']) {
  403. return $one;
  404. }
  405. return null;
  406. }
  407. }