UserAddressLogic.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. 'area_id' => $params['area_id']??0,
  103. 'address' => $params['address'],
  104. 'is_default' => $params['is_default'] ?? YesNoEnum::NO,
  105. 'create_time' => time()
  106. ]);
  107. }
  108. /**
  109. * @notes 编辑地址
  110. * @param $params
  111. * @author 段誉
  112. * @date 2021/7/22 14:14
  113. */
  114. public static function editAddress($params)
  115. {
  116. if ($params['is_default'] == YesNoEnum::YES) {
  117. UserAddress::where(['user_id' => $params['user_id']])->update(['is_default' => YesNoEnum::NO]);
  118. }
  119. UserAddress::update([
  120. 'contact' => $params['contact'],
  121. 'mobile' => $params['mobile'],
  122. 'province_id' => $params['province_id'],
  123. 'city_id' => $params['city_id'],
  124. 'district_id' => $params['district_id'],
  125. 'area_id' => $params['area_id']??0,
  126. 'address' => $params['address'],
  127. 'is_default' => $params['is_default'],
  128. ], ['id' => $params['id'], 'user_id' => $params['user_id']]);
  129. }
  130. /**
  131. * @notes 删除
  132. * @param $params
  133. * @author 段誉
  134. * @date 2021/7/22 14:33
  135. */
  136. public static function del($params)
  137. {
  138. UserAddress::destroy(['user_id' => $params['user_id'], 'id' => $params['id']]);
  139. }
  140. /**
  141. * @notes 将省市区名称转为id
  142. * @param $params
  143. * @return string[]
  144. * @author Tab
  145. * @date 2021/8/12 16:20
  146. */
  147. public static function handleRegion($params)
  148. {
  149. $province = self::handleRegionField($params['province'], 1);
  150. $city = self::handleRegionField($params['city'], 2,$province);
  151. $district = self::handleRegionField($params['district'], 3,$city);
  152. $result = [
  153. 'province' => $province,
  154. 'city' => $city,
  155. 'district' => $district,
  156. ];
  157. return $result;
  158. }
  159. /**
  160. * @notes 根据地址名称获取id
  161. * @param $field
  162. * @param $level
  163. * @return mixed|string
  164. * @author Tab
  165. * @date 2021/8/12 16:20
  166. */
  167. public static function handleRegionField($field, $level,$partnerId = 100000)
  168. {
  169. $region = Region::where([
  170. ['level', '=', $level],
  171. ['name', 'like', '%' . $field . '%'],
  172. ['parent_id', '=', $partnerId]
  173. ])->findOrEmpty();
  174. if($region->isEmpty()) {
  175. return '';
  176. }
  177. return $region->id;
  178. }
  179. /**
  180. * @notes 获取地区
  181. * @param int $pid
  182. * @return Region[]|array|\think\Collection
  183. * @throws \think\db\exception\DataNotFoundException
  184. * @throws \think\db\exception\DbException
  185. * @throws \think\db\exception\ModelNotFoundException
  186. * @author cjhao
  187. * @date 2022/11/8 6:42 下午
  188. */
  189. public static function getRegion($pid = 0){
  190. $where = [];
  191. if(0 === $pid){
  192. $where[] = ['level','=',1];
  193. }else{
  194. $where[] = ['parner_id','=',$pid];
  195. }
  196. $regionLists = Region::where($where)->field('id,name')->select();
  197. return $regionLists;
  198. }
  199. public static function getSpecialAreaLists(){
  200. $where[]=['is_show','=',1];
  201. $list = SpecialArea::field('id,name')->where($where)->order('sort desc,id desc')->select()->toArray();
  202. return $list;
  203. }
  204. public static function getSpecialAreaInfo($id){
  205. $where[]=['id','=',$id];
  206. $info = SpecialArea::where($where)->find()->toArray();
  207. return $info;
  208. }
  209. }