| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- <?php
- // +----------------------------------------------------------------------
- // | LikeShop有特色的全开源社交分销电商系统
- // +----------------------------------------------------------------------
- // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
- // | 商业用途务必购买系统授权,以免引起不必要的法律纠纷
- // | 禁止对系统程序代码以任何目的,任何形式的再发布
- // | 微信公众号:好象科技
- // | 访问官网:http://www.likemarket.net
- // | 访问社区:http://bbs.likemarket.net
- // | 访问手册:http://doc.likemarket.net
- // | 好象科技开发团队 版权所有 拥有最终解释权
- // +----------------------------------------------------------------------
- // | Author: LikeShopTeam-段誉
- // +----------------------------------------------------------------------
- namespace app\shopapi\logic;
- use app\common\enum\YesNoEnum;
- use app\common\logic\BaseLogic;
- use app\common\model\Region;
- use app\common\model\UserAddress;
- use app\common\model\SpecialArea;
- /**
- * 用户地址逻辑
- * Class UserAddressLogic
- * @package app\shopapi\logic
- */
- class UserAddressLogic extends BaseLogic
- {
- /**
- * @notes 列表
- * @param $userId
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- * @author 段誉
- * @date 2021/7/22 14:13
- */
- public static function getLists($userId)
- {
- return UserAddress::where(['user_id' => $userId])->select()->toArray();
- }
- /**
- * @notes 详情
- * @param $addressId
- * @param $userId
- * @return array
- * @author 段誉
- * @date 2021/7/22 17:49
- */
- public static function getDetail($addressId, $userId)
- {
- return UserAddress::getAddressById($userId, $addressId)->toArray();
- }
- /**
- * @notes 获取默认地址
- * @param $userId
- * @return array
- * @author 段誉
- * @date 2021/7/22 14:30
- */
- public static function getDefault($userId)
- {
- return UserAddress::getDefaultAddress($userId)->toArray();
- }
- /**
- * @notes 设置默认地址
- * @param $params
- * @param $userId
- * @author 段誉
- * @date 2021/7/22 14:30
- */
- public static function setDefault($params, $userId)
- {
- UserAddress::where(['user_id' => $userId])->update(['is_default' => YesNoEnum::NO]);
- UserAddress::where(['id' => $params['id'], 'user_id' => $userId])->update(['is_default' => YesNoEnum::YES]);
- }
- /**
- * @notes 添加地址
- * @param $params
- * @param $userId
- * @author 段誉
- * @date 2021/7/22 14:14
- */
- public static function addAddress($params, $userId)
- {
- if ($params['is_default'] == YesNoEnum::YES) {
- UserAddress::where(['user_id' => $userId])->update(['is_default' => YesNoEnum::NO]);
- } else {
- $isFirst = UserAddress::where(['user_id' => $userId])->findOrEmpty();
- if ($isFirst->isEmpty()) {
- $params['is_default'] = YesNoEnum::YES;
- }
- }
- UserAddress::create([
- 'user_id' => $userId,
- 'contact' => $params['contact'],
- 'mobile' => $params['mobile'],
- 'province_id' => $params['province_id'],
- 'city_id' => $params['city_id'],
- 'district_id' => $params['district_id'],
- 'address' => $params['address'],
- 'is_default' => $params['is_default'] ?? YesNoEnum::NO,
- 'create_time' => time()
- ]);
- }
- /**
- * @notes 编辑地址
- * @param $params
- * @author 段誉
- * @date 2021/7/22 14:14
- */
- public static function editAddress($params)
- {
- if ($params['is_default'] == YesNoEnum::YES) {
- UserAddress::where(['user_id' => $params['user_id']])->update(['is_default' => YesNoEnum::NO]);
- }
- UserAddress::update([
- 'contact' => $params['contact'],
- 'mobile' => $params['mobile'],
- 'province_id' => $params['province_id'],
- 'city_id' => $params['city_id'],
- 'district_id' => $params['district_id'],
- 'address' => $params['address'],
- 'is_default' => $params['is_default'],
- ], ['id' => $params['id'], 'user_id' => $params['user_id']]);
- }
- /**
- * @notes 删除
- * @param $params
- * @author 段誉
- * @date 2021/7/22 14:33
- */
- public static function del($params)
- {
- UserAddress::destroy(['user_id' => $params['user_id'], 'id' => $params['id']]);
- }
- /**
- * @notes 将省市区名称转为id
- * @param $params
- * @return string[]
- * @author Tab
- * @date 2021/8/12 16:20
- */
- public static function handleRegion($params)
- {
- $province = self::handleRegionField($params['province'], 1);
- $city = self::handleRegionField($params['city'], 2,$province);
- $district = self::handleRegionField($params['district'], 3,$city);
- $result = [
- 'province' => $province,
- 'city' => $city,
- 'district' => $district,
- ];
- return $result;
- }
- /**
- * @notes 根据地址名称获取id
- * @param $field
- * @param $level
- * @return mixed|string
- * @author Tab
- * @date 2021/8/12 16:20
- */
- public static function handleRegionField($field, $level,$partnerId = 100000)
- {
- $region = Region::where([
- ['level', '=', $level],
- ['name', 'like', '%' . $field . '%'],
- ['parent_id', '=', $partnerId]
- ])->findOrEmpty();
- if($region->isEmpty()) {
- return '';
- }
- return $region->id;
- }
- /**
- * @notes 获取地区
- * @param int $pid
- * @return Region[]|array|\think\Collection
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- * @author cjhao
- * @date 2022/11/8 6:42 下午
- */
- public static function getRegion($pid = 0){
- $where = [];
- if(0 === $pid){
- $where[] = ['level','=',1];
- }else{
- $where[] = ['parner_id','=',$pid];
- }
- $regionLists = Region::where($where)->field('id,name')->select();
- return $regionLists;
- }
- public static function getSpecialAreaLists(){
- $where[]=['is_show','=',1];
- $list = SpecialArea::field('id,name')->where($where)->order('sort desc,id desc')->select()->toArray();
- return $list;
- }
- public static function getSpecialAreaInfo($id){
- $where[]=['id','=',$id];
- $info = SpecialArea::where($where)->findOrEmpty();
- return $info;
- }
- }
|