UserAddressLogic.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LikeShop有特色的全开源社交分销电商系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 商业用途务必购买系统授权,以免引起不必要的法律纠纷
  7. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  8. // | 微信公众号:好象科技
  9. // | 访问官网:http://www.likemarket.net
  10. // | 访问社区:http://bbs.likemarket.net
  11. // | 访问手册:http://doc.likemarket.net
  12. // | 好象科技开发团队 版权所有 拥有最终解释权
  13. // +----------------------------------------------------------------------
  14. // | Author: LikeShopTeam-段誉
  15. // +----------------------------------------------------------------------
  16. namespace app\shopapi\logic;
  17. use app\common\enum\YesNoEnum;
  18. use app\common\logic\BaseLogic;
  19. use app\common\model\Region;
  20. use app\common\model\UserAddress;
  21. /**
  22. * 用户地址逻辑
  23. * Class UserAddressLogic
  24. * @package app\shopapi\logic
  25. */
  26. class UserAddressLogic extends BaseLogic
  27. {
  28. /**
  29. * @notes 列表
  30. * @param $userId
  31. * @return array
  32. * @throws \think\db\exception\DataNotFoundException
  33. * @throws \think\db\exception\DbException
  34. * @throws \think\db\exception\ModelNotFoundException
  35. * @author 段誉
  36. * @date 2021/7/22 14:13
  37. */
  38. public static function getLists($userId)
  39. {
  40. return UserAddress::where(['user_id' => $userId])->select()->toArray();
  41. }
  42. /**
  43. * @notes 详情
  44. * @param $addressId
  45. * @param $userId
  46. * @return array
  47. * @author 段誉
  48. * @date 2021/7/22 17:49
  49. */
  50. public static function getDetail($addressId, $userId)
  51. {
  52. return UserAddress::getAddressById($userId, $addressId)->toArray();
  53. }
  54. /**
  55. * @notes 获取默认地址
  56. * @param $userId
  57. * @return array
  58. * @author 段誉
  59. * @date 2021/7/22 14:30
  60. */
  61. public static function getDefault($userId)
  62. {
  63. return UserAddress::getDefaultAddress($userId)->toArray();
  64. }
  65. /**
  66. * @notes 设置默认地址
  67. * @param $params
  68. * @param $userId
  69. * @author 段誉
  70. * @date 2021/7/22 14:30
  71. */
  72. public static function setDefault($params, $userId)
  73. {
  74. UserAddress::where(['user_id' => $userId])->update(['is_default' => YesNoEnum::NO]);
  75. UserAddress::where(['id' => $params['id'], 'user_id' => $userId])->update(['is_default' => YesNoEnum::YES]);
  76. }
  77. /**
  78. * @notes 添加地址
  79. * @param $params
  80. * @param $userId
  81. * @author 段誉
  82. * @date 2021/7/22 14:14
  83. */
  84. public static function addAddress($params, $userId)
  85. {
  86. if ($params['is_default'] == YesNoEnum::YES) {
  87. UserAddress::where(['user_id' => $userId])->update(['is_default' => YesNoEnum::NO]);
  88. } else {
  89. $isFirst = UserAddress::where(['user_id' => $userId])->findOrEmpty();
  90. if ($isFirst->isEmpty()) {
  91. $params['is_default'] = YesNoEnum::YES;
  92. }
  93. }
  94. UserAddress::create([
  95. 'user_id' => $userId,
  96. 'contact' => $params['contact'],
  97. 'mobile' => $params['mobile'],
  98. 'province_id' => $params['province_id'],
  99. 'city_id' => $params['city_id'],
  100. 'district_id' => $params['district_id'],
  101. 'address' => $params['address'],
  102. 'is_default' => $params['is_default'] ?? YesNoEnum::NO,
  103. 'create_time' => time()
  104. ]);
  105. }
  106. /**
  107. * @notes 编辑地址
  108. * @param $params
  109. * @author 段誉
  110. * @date 2021/7/22 14:14
  111. */
  112. public static function editAddress($params)
  113. {
  114. if ($params['is_default'] == YesNoEnum::YES) {
  115. UserAddress::where(['user_id' => $params['user_id']])->update(['is_default' => YesNoEnum::NO]);
  116. }
  117. UserAddress::update([
  118. 'contact' => $params['contact'],
  119. 'mobile' => $params['mobile'],
  120. 'province_id' => $params['province_id'],
  121. 'city_id' => $params['city_id'],
  122. 'district_id' => $params['district_id'],
  123. 'address' => $params['address'],
  124. 'is_default' => $params['is_default'],
  125. ], ['id' => $params['id'], 'user_id' => $params['user_id']]);
  126. }
  127. /**
  128. * @notes 删除
  129. * @param $params
  130. * @author 段誉
  131. * @date 2021/7/22 14:33
  132. */
  133. public static function del($params)
  134. {
  135. UserAddress::destroy(['user_id' => $params['user_id'], 'id' => $params['id']]);
  136. }
  137. /**
  138. * @notes 将省市区名称转为id
  139. * @param $params
  140. * @return string[]
  141. * @author Tab
  142. * @date 2021/8/12 16:20
  143. */
  144. public static function handleRegion($params)
  145. {
  146. $province = self::handleRegionField($params['province'], 1);
  147. $city = self::handleRegionField($params['city'], 2,$province);
  148. $district = self::handleRegionField($params['district'], 3,$city);
  149. $result = [
  150. 'province' => $province,
  151. 'city' => $city,
  152. 'district' => $district,
  153. ];
  154. return $result;
  155. }
  156. /**
  157. * @notes 根据地址名称获取id
  158. * @param $field
  159. * @param $level
  160. * @return mixed|string
  161. * @author Tab
  162. * @date 2021/8/12 16:20
  163. */
  164. public static function handleRegionField($field, $level,$partnerId = 100000)
  165. {
  166. $region = Region::where([
  167. ['level', '=', $level],
  168. ['name', 'like', '%' . $field . '%'],
  169. ['parent_id', '=', $partnerId]
  170. ])->findOrEmpty();
  171. if($region->isEmpty()) {
  172. return '';
  173. }
  174. return $region->id;
  175. }
  176. /**
  177. * @notes 获取地区
  178. * @param int $pid
  179. * @return Region[]|array|\think\Collection
  180. * @throws \think\db\exception\DataNotFoundException
  181. * @throws \think\db\exception\DbException
  182. * @throws \think\db\exception\ModelNotFoundException
  183. * @author cjhao
  184. * @date 2022/11/8 6:42 下午
  185. */
  186. public static function getRegion($pid = 0){
  187. $where = [];
  188. if(0 === $pid){
  189. $where[] = ['level','=',1];
  190. }else{
  191. $where[] = ['parner_id','=',$pid];
  192. }
  193. $regionLists = Region::where($where)->field('id,name')->select();
  194. return $regionLists;
  195. }
  196. }