Addr.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. namespace app\Home\controller;
  3. use think\Db;
  4. class Addr extends Base
  5. {
  6. public function __construct()
  7. {
  8. parent::__construct();
  9. }
  10. /**
  11. * @param id 地址id
  12. */
  13. public function Info(){
  14. $addrid = isset($this->post['id'])&&$this->post['id']!==""?$this->post['id']:"";
  15. if($addrid==""){
  16. return error_show(1005,"参数id 不能为空");
  17. }
  18. $list = Db::name("rela_addr")->alias('a')->join("addr b","a.addrid=b.id","left")->where(["
  19. a.accountid"=>$this->userinfo['id'],"b.is_del"=>0,"b.id"=>$addrid])->field("b.id,b.provice,b.provice_name,b.city,
  20. b.city_name,b.area,b.area_name,b.addr,b.contector,b.mobile,b.addtime")->find();
  21. if(empty($list)){
  22. return error_show(1004,"未找到数据");
  23. }
  24. return app_show(0,"获取数据成功",$list);
  25. }
  26. /**
  27. * @param
  28. */
  29. public function list(){
  30. $page = isset($this->post['page'])&&$this->post['page']!="" ? intval($this->post['page']):1;
  31. $size = isset($this->post['size'])&&$this->post['size']!="" ? intval($this->post['size']):10;
  32. $count= Db::name("rela_addr")->alias('a')->join("addr b","a.addrid=b.id","left")->where(["
  33. a.accountid"=>$this->userinfo['id'],"b.is_del"=>0])->count();
  34. $total = ceil($count/$size);
  35. $page=$page>$total ? $total:$page;
  36. $list = Db::name("rela_addr")->alias('a')->join("addr b","a.addrid=b.id","left")->where(["
  37. a.accountid"=>$this->userinfo['id'],"b.is_del"=>0])->page($page,$size)->field("b.id,b.provice,b.provice_name,b.city,
  38. b.city_name,b.area,b.area_name,b.addr,b.contector,b.mobile,b.addtime")->select();
  39. return app_show(0,"获取成功", ["list"=>$list,"count"=>$count]);
  40. }
  41. /**
  42. * @param contector
  43. * @param mobile
  44. * @param provice
  45. * @param city
  46. * @param area
  47. */
  48. public function Add(){
  49. $contector = isset($this->post['contector'])&&$this->post['contector']!=""? trim($this->post['contector']):"";
  50. if($contector==""){
  51. return error_show(1004,"参数contector 不能为空");
  52. }
  53. $mobile = isset($this->post['mobile'])&&$this->post['mobile']!="" ? trim($this->post['mobile']):"";
  54. if($mobile==""){
  55. return error_show(1005,"参数mobile 不能为空");
  56. }
  57. $province = isset($this->post['provice'])&&$this->post['provice']!=""? trim($this->post['provice']):"";
  58. $city = isset($this->post['city'])&&$this->post['city']!=""? trim($this->post['city']):"";
  59. $area = isset($this->post['area'])&&$this->post['area']!=""? trim($this->post['area']):"";
  60. if($province==""){
  61. return error_show(1005,"参数provice 不能为空");
  62. }
  63. if($city==""){
  64. return error_show(1005,"参数city 不能为空");
  65. }
  66. if($area==""){
  67. return error_show(1005,"参数area 不能为空");
  68. }
  69. $province_info = Db::name("province")->where(['province_code'=>$province])->find();
  70. if(empty($province_info)){
  71. return error_show(1005,"省级数据未找到");
  72. }
  73. $cityinfo = Db::name("city")->where(['province_code'=>$province,"city_code"=>$city])->find();
  74. if(empty($cityinfo)){
  75. return error_show(1005,"市级数据未找到");
  76. }
  77. $areainfo = Db::name("area")->where(['area_code'=>$area,"city_code"=>$city])->find();
  78. if(empty($areainfo)){
  79. return error_show(1005,"区县级数据未找到");
  80. }
  81. $addr = isset($this->post['addr'])&&$this->post['addr']!=""?$this->post['addr']:"";
  82. if($addr==""){
  83. return error_show(1005,"参数addr 不能为空");
  84. }
  85. Db::startTrans();
  86. try{
  87. $data=[
  88. "addr"=>$addr,
  89. "provice"=>$province,
  90. "provice_name"=>$province_info['name'],
  91. "city"=>$city,
  92. "city_name"=>$cityinfo['name'],
  93. "area"=>$area,
  94. "area_name"=>$areainfo['name'],
  95. "contector"=>$contector,
  96. "mobile"=>$mobile,
  97. 'status'=>1,
  98. "is_del"=>0,
  99. "addtime"=>date("Y-m-d H:i:s"),
  100. "updatetime"=>date("Y-m-d H:i:s")
  101. ];
  102. $addrid =Db::name("addr")->insert($data,false,true);
  103. if($addrid>0){
  104. $rela = Db::name("rela_addr")->insert(['accountid'=>$this->userinfo['id'],"addrid"=>$addrid]);
  105. if($rela){
  106. Db::commit();
  107. return app_show(0,"地址新建成功");
  108. }
  109. }
  110. Db::rollback();
  111. return error_show(1008,"地址新建失败");
  112. }catch (\Exception $e){
  113. Db::rollback();
  114. return error_show(1008,$e->getMessage());
  115. }
  116. }
  117. /**
  118. * @param contector
  119. * @param mobile
  120. * @param provice
  121. * @param city
  122. * @param area
  123. * @param id
  124. */
  125. public function Edit(){
  126. $addrid = isset($this->post['id'])&&$this->post['id']!==""?$this->post['id']:"";
  127. if($addrid==""){
  128. return error_show(1005,"参数id 不能为空");
  129. }
  130. $list = Db::name("rela_addr")->alias('a')->join("addr b","a.addrid=b.id","left")->where(["
  131. a.accountid"=>$this->userinfo['id'],"b.is_del"=>0,"b.id"=>$addrid])->field("b.id,b.provice,b.provice_name,b.city,
  132. b.city_name,b.area,b.area_name,b.addr,b.contector,b.mobile,b.addtime")->find();
  133. if(empty($list)){
  134. return error_show(1004,"未找到数据");
  135. }
  136. $contector = isset($this->post['contector'])&&$this->post['contector']!=""? trim($this->post['contector']):"";
  137. if($contector==""){
  138. return error_show(1004,"参数contector 不能为空");
  139. }
  140. $mobile = isset($this->post['mobile'])&&$this->post['mobile']!="" ? trim($this->post['mobile']):"";
  141. if($mobile==""){
  142. return error_show(1005,"参数mobile 不能为空");
  143. }
  144. $province = isset($this->post['provice'])&&$this->post['provice']!=""? trim($this->post['provice']):"";
  145. $city = isset($this->post['city'])&&$this->post['city']!=""? trim($this->post['city']):"";
  146. $area = isset($this->post['area'])&&$this->post['area']!=""? trim($this->post['area']):"";
  147. if($province==""){
  148. return error_show(1005,"参数provice 不能为空");
  149. }
  150. if($city==""){
  151. return error_show(1005,"参数city 不能为空");
  152. }
  153. if($area==""){
  154. return error_show(1005,"参数area 不能为空");
  155. }
  156. $province_info = Db::name("province")->where(['province_code'=>$province])->find();
  157. if(empty($province_info)){
  158. return error_show(1005,"省级数据未找到");
  159. }
  160. $cityinfo = Db::name("city")->where(['province_code'=>$province,"city_code"=>$city])->find();
  161. if(empty($cityinfo)){
  162. return error_show(1005,"市级数据未找到");
  163. }
  164. $areainfo = Db::name("area")->where(['area_code'=>$area,"city_code"=>$city])->find();
  165. if(empty($areainfo)){
  166. return error_show(1005,"区县级数据未找到");
  167. }
  168. $addr = isset($this->post['addr'])&&$this->post['addr']!=""?$this->post['addr']:"";
  169. if($addr==""){
  170. return error_show(1005,"参数addr 不能为空");
  171. }
  172. Db::startTrans();
  173. try{
  174. $data=[
  175. "id"=>$addrid,
  176. "addr"=>$addr,
  177. "provice"=>$province,
  178. "provice_name"=>$province_info['name'],
  179. "city"=>$city,
  180. "city_name"=>$cityinfo['name'],
  181. "area"=>$area,
  182. "area_name"=>$areainfo['name'],
  183. "contector"=>$contector,
  184. "mobile"=>$mobile,
  185. "updatetime"=>date("Y-m-d H:i:s")
  186. ];
  187. $addrup =Db::name("addr")->update($data);
  188. if($addrup){
  189. Db::commit();
  190. return app_show(0,"地址修改成功");
  191. }
  192. Db::rollback();
  193. return error_show(1008,"地址修改失败");
  194. }catch (\Exception $e){
  195. Db::rollback();
  196. return error_show(1008,$e->getMessage());
  197. }
  198. }
  199. /**
  200. * @param id 地址id
  201. */
  202. public function Delete(){
  203. $addrid = isset($this->post['id'])&&$this->post['id']!==""?$this->post['id']:"";
  204. if($addrid==""){
  205. return error_show(1005,"参数id 不能为空");
  206. }
  207. $list = Db::name("rela_addr")->alias('a')->join("addr b","a.addrid=b.id","left")->where(["
  208. a.accountid"=>$this->userinfo['id'],"b.is_del"=>0,"b.id"=>$addrid])->field("b.id,b.provice,b.provice_name,b.city,
  209. b.city_name,b.area,b.area_name,b.addr,b.contector,b.mobile,b.addtime")->find();
  210. if(empty($list)){
  211. return error_show(1004,"未找到数据");
  212. }
  213. $del= Db::name("addr")->where(['id'=>$addrid])->update(["is_del"=>1,"updatetime"=>date("Y-m-d H:i:s")]);
  214. if($del){
  215. return app_show(0,"地址删除成功");
  216. }
  217. return error_show(1005,"地址删除失败");
  218. }
  219. }