Addr.php 8.6 KB

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