UserAddressLogic.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. use app\common\model\SpecialArea;
  22. /**
  23. * 用户地址逻辑
  24. * Class UserAddressLogic
  25. * @package app\shopapi\logic
  26. */
  27. class UserAddressLogic extends BaseLogic
  28. {
  29. /**
  30. * @notes 列表
  31. * @param $userId
  32. * @return array
  33. * @throws \think\db\exception\DataNotFoundException
  34. * @throws \think\db\exception\DbException
  35. * @throws \think\db\exception\ModelNotFoundException
  36. * @author 段誉
  37. * @date 2021/7/22 14:13
  38. */
  39. public static function getLists($userId)
  40. {
  41. return UserAddress::where(['user_id' => $userId])->select()->toArray();
  42. }
  43. /**
  44. * @notes 详情
  45. * @param $addressId
  46. * @param $userId
  47. * @return array
  48. * @author 段誉
  49. * @date 2021/7/22 17:49
  50. */
  51. public static function getDetail($addressId, $userId)
  52. {
  53. return UserAddress::getAddressById($userId, $addressId)->toArray();
  54. }
  55. /**
  56. * @notes 获取默认地址
  57. * @param $userId
  58. * @return array
  59. * @author 段誉
  60. * @date 2021/7/22 14:30
  61. */
  62. public static function getDefault($userId)
  63. {
  64. return UserAddress::getDefaultAddress($userId)->toArray();
  65. }
  66. /**
  67. * @notes 设置默认地址
  68. * @param $params
  69. * @param $userId
  70. * @author 段誉
  71. * @date 2021/7/22 14:30
  72. */
  73. public static function setDefault($params, $userId)
  74. {
  75. UserAddress::where(['user_id' => $userId])->update(['is_default' => YesNoEnum::NO]);
  76. UserAddress::where(['id' => $params['id'], 'user_id' => $userId])->update(['is_default' => YesNoEnum::YES]);
  77. }
  78. /**
  79. * @notes 添加地址
  80. * @param $params
  81. * @param $userId
  82. * @author 段誉
  83. * @date 2021/7/22 14:14
  84. */
  85. public static function addAddress($params, $userId)
  86. {
  87. if ($params['is_default'] == YesNoEnum::YES) {
  88. UserAddress::where(['user_id' => $userId])->update(['is_default' => YesNoEnum::NO]);
  89. } else {
  90. $isFirst = UserAddress::where(['user_id' => $userId])->findOrEmpty();
  91. if ($isFirst->isEmpty()) {
  92. $params['is_default'] = YesNoEnum::YES;
  93. }
  94. }
  95. UserAddress::create([
  96. 'user_id' => $userId,
  97. 'contact' => $params['contact'],
  98. 'mobile' => $params['mobile'],
  99. 'province_id' => $params['province_id'],
  100. 'city_id' => $params['city_id'],
  101. 'district_id' => $params['district_id'],
  102. 'address' => $params['address'],
  103. 'is_default' => $params['is_default'] ?? YesNoEnum::NO,
  104. 'create_time' => time()
  105. ]);
  106. }
  107. /**
  108. * @notes 编辑地址
  109. * @param $params
  110. * @author 段誉
  111. * @date 2021/7/22 14:14
  112. */
  113. public static function editAddress($params)
  114. {
  115. if ($params['is_default'] == YesNoEnum::YES) {
  116. UserAddress::where(['user_id' => $params['user_id']])->update(['is_default' => YesNoEnum::NO]);
  117. }
  118. UserAddress::update([
  119. 'contact' => $params['contact'],
  120. 'mobile' => $params['mobile'],
  121. 'province_id' => $params['province_id'],
  122. 'city_id' => $params['city_id'],
  123. 'district_id' => $params['district_id'],
  124. 'address' => $params['address'],
  125. 'is_default' => $params['is_default'],
  126. ], ['id' => $params['id'], 'user_id' => $params['user_id']]);
  127. }
  128. /**
  129. * @notes 删除
  130. * @param $params
  131. * @author 段誉
  132. * @date 2021/7/22 14:33
  133. */
  134. public static function del($params)
  135. {
  136. UserAddress::destroy(['user_id' => $params['user_id'], 'id' => $params['id']]);
  137. }
  138. /**
  139. * @notes 将省市区名称转为id
  140. * @param $params
  141. * @return string[]
  142. * @author Tab
  143. * @date 2021/8/12 16:20
  144. */
  145. public static function handleRegion($params)
  146. {
  147. $province = self::handleRegionField($params['province'], 1);
  148. $city = self::handleRegionField($params['city'], 2,$province);
  149. $district = self::handleRegionField($params['district'], 3,$city);
  150. $result = [
  151. 'province' => $province,
  152. 'city' => $city,
  153. 'district' => $district,
  154. ];
  155. return $result;
  156. }
  157. /**
  158. * @notes 根据地址名称获取id
  159. * @param $field
  160. * @param $level
  161. * @return mixed|string
  162. * @author Tab
  163. * @date 2021/8/12 16:20
  164. */
  165. public static function handleRegionField($field, $level,$partnerId = 100000)
  166. {
  167. $region = Region::where([
  168. ['level', '=', $level],
  169. ['name', 'like', '%' . $field . '%'],
  170. ['parent_id', '=', $partnerId]
  171. ])->findOrEmpty();
  172. if($region->isEmpty()) {
  173. return '';
  174. }
  175. return $region->id;
  176. }
  177. /**
  178. * @notes 获取地区
  179. * @param int $pid
  180. * @return Region[]|array|\think\Collection
  181. * @throws \think\db\exception\DataNotFoundException
  182. * @throws \think\db\exception\DbException
  183. * @throws \think\db\exception\ModelNotFoundException
  184. * @author cjhao
  185. * @date 2022/11/8 6:42 下午
  186. */
  187. public static function getRegion($pid = 0){
  188. $where = [];
  189. if(0 === $pid){
  190. $where[] = ['level','=',1];
  191. }else{
  192. $where[] = ['parner_id','=',$pid];
  193. }
  194. $regionLists = Region::where($where)->field('id,name')->select();
  195. return $regionLists;
  196. }
  197. public static function getSpecialAreaLists(){
  198. $where[]=['is_show','=',1];
  199. $list = SpecialArea::field('id,name')->where($where)->order('sort desc,id desc')->select()->toArray();
  200. return $list;
  201. }
  202. public static function getSpecialAreaInfo($id){
  203. $where[]=['id','=',$id];
  204. $info = SpecialArea::where($where)->findOrEmpty();
  205. return $info;
  206. }
  207. }