Address.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | A3Mall
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2020 http://www.a3-mall.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: xzncit <158373108@qq.com>
  8. // +----------------------------------------------------------------------
  9. namespace app\api\service;
  10. use mall\basic\Users;
  11. use app\common\models\users\UsersAddress as UsersAddressModel;
  12. use app\common\models\Area as AreaModel;
  13. use mall\utils\Check;
  14. class Address extends Service {
  15. /**
  16. * 获取指定会员所有地址
  17. * @return array
  18. * @throws \think\db\exception\DataNotFoundException
  19. * @throws \think\db\exception\DbException
  20. * @throws \think\db\exception\ModelNotFoundException
  21. */
  22. public static function getList(){
  23. $data = UsersAddressModel::where([ "user_id"=>Users::get("id") ])->select()->toArray();
  24. $list = [];
  25. foreach($data as $key=>$item){
  26. $area = [];
  27. foreach([$item["province"],$item["city"],$item["area"]] as $value){
  28. $area[] = AreaModel::where("id",$value)->value("name");
  29. }
  30. $list[$key] = [
  31. "id"=>$item["id"],
  32. "is_default"=>$item['is_default'],
  33. "name"=>$item["accept_name"],
  34. "tel"=>$item["mobile"],
  35. "address"=>implode(" ", $area) . $item["address"]
  36. ];
  37. }
  38. return $list;
  39. }
  40. /**
  41. * 详情
  42. * @param $id
  43. * @return array
  44. * @throws \think\db\exception\DataNotFoundException
  45. * @throws \think\db\exception\DbException
  46. * @throws \think\db\exception\ModelNotFoundException
  47. */
  48. public static function detail($id){
  49. if(!$row = UsersAddressModel::where([ "user_id"=>Users::get("id"), "id"=>$id ])->find()){
  50. throw new \Exception("address empty",0);
  51. }
  52. $extends_info = json_decode($row["extends_info"],true);
  53. return [
  54. "areaCode"=>$extends_info["areaCode"],
  55. "isDefault"=>$row["is_default"] ? true : false,
  56. "name"=> $row["accept_name"],
  57. "tel"=>$row["mobile"],
  58. "addressDetail"=>$row["address"],
  59. "province"=>$row["province"],
  60. "county"=>$row["city"],
  61. "city"=>$row["area"],
  62. "area_name"=>AreaModel::getArea([$row["province"],$row["city"],$row["area"]],',')
  63. ];
  64. }
  65. /**
  66. * 删除地址
  67. * @param $id
  68. * @return bool
  69. */
  70. public static function delete($id){
  71. return UsersAddressModel::where([ "user_id"=>Users::get("id"), "id"=>$id ])->delete();
  72. }
  73. /**
  74. * 编辑收货地址
  75. * @param $post
  76. * @return mixed
  77. * @throws \think\db\exception\DataNotFoundException
  78. * @throws \think\db\exception\DbException
  79. * @throws \think\db\exception\ModelNotFoundException
  80. */
  81. public static function editor($post){
  82. if(empty($post["name"])){
  83. throw new \Exception("请填写姓名!",0);
  84. }else if(!Check::chsAlphaNum($post["name"])){
  85. throw new \Exception("您填写的姓名不合法!",0);
  86. }else if(!Check::mobile($post["tel"])){
  87. throw new \Exception("您填写的手机号码不正确",0);
  88. }else if(empty($post["addressDetail"])){
  89. throw new \Exception("请填写地址",0);
  90. }else if(empty($post["province"])){
  91. throw new \Exception("请选择省份",0);
  92. }else if(empty($post["city"])){
  93. throw new \Exception("请选择市",0);
  94. }else if(empty($post["county"])){
  95. throw new \Exception("请选择区",0);
  96. }else if(empty($post["areaCode"]) && $post["client_type"] == 0){
  97. throw new \Exception("请选择所在地区",0);
  98. }else if(!in_array($post["client_type"],[0,1,2])){ // 0:app 1:mini 2: h5
  99. throw new \Exception("参数错误",0);
  100. }
  101. $province = AreaModel::where("level",1)->where("name","like",'%'.$post["province"].'%')->find();
  102. $city = AreaModel::where("pid",$province["id"])->where("level",2)->where("name","like",'%'.$post["city"].'%')->find();
  103. $county = AreaModel::where("pid",$city["id"])->where("level",3)->where("name","like",'%'.$post["county"].'%')->find();
  104. if(empty($province) || empty($city)){
  105. throw new \Exception("您选择的地址不存在",0);
  106. }
  107. try{
  108. UsersAddressModel::startTrans();
  109. $is_default = isset($post["is_default"]) ? intval($post["is_default"]) : 0;
  110. if($is_default){
  111. UsersAddressModel::where(["user_id"=>Users::get("id")])->update(["is_default"=>0]);
  112. }
  113. if(empty($post["id"])){
  114. $data = [
  115. "user_id"=>Users::get("id"),
  116. "accept_name"=>$post["name"],
  117. "mobile"=>$post["tel"],
  118. "province"=>$province["id"],
  119. "city"=>$city["id"],
  120. "area"=>$county["id"],
  121. "address"=>$post["addressDetail"],
  122. "is_default" => $is_default,
  123. // "extends_info"=>json_encode(["areaCode"=>$post["areaCode"]],JSON_UNESCAPED_UNICODE),
  124. "create_time"=>time()
  125. ];
  126. if(isset($post["client_type"]) && $post["client_type"] == 0){
  127. $data["extends_info"] = json_encode(["areaCode"=>$post["areaCode"]],JSON_UNESCAPED_UNICODE);
  128. }
  129. $lastInsID = UsersAddressModel::create($data)->id;
  130. }else{
  131. $data = [
  132. "accept_name"=>$post["name"],
  133. "mobile"=>$post["tel"],
  134. "province"=>$province["id"],
  135. "city"=>$city["id"],
  136. "area"=>$county["id"],
  137. "address"=>$post["addressDetail"],
  138. "is_default" => $is_default,
  139. // "extends_info"=>json_encode(["areaCode"=>$post["areaCode"]],JSON_UNESCAPED_UNICODE),
  140. "create_time"=>time()
  141. ];
  142. if(isset($post["client_type"]) && $post["client_type"] == 0){
  143. $data["extends_info"] = json_encode(["areaCode"=>$post["areaCode"]],JSON_UNESCAPED_UNICODE);
  144. }
  145. UsersAddressModel::where("id",intval($post["id"]))->where("user_id",Users::get("id"))->save($data);
  146. $lastInsID = $post["id"];
  147. }
  148. UsersAddressModel::commit();
  149. return $lastInsID;
  150. }catch (\Exception $ex){
  151. UsersAddressModel::rollback();
  152. throw new \Exception($ex->getMessage(),$ex->getCode());
  153. }
  154. }
  155. /**
  156. * 设置默认地址
  157. * @param $id
  158. * @return bool
  159. */
  160. public static function setDefaultAddress($id){
  161. UsersAddressModel::where("user_id",Users::get("id"))->update(["is_default"=>0]);
  162. UsersAddressModel::where(["id"=>$id,"user_id"=>Users::get("id")])->update(["is_default"=> 1]);
  163. return true;
  164. }
  165. }