Address.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\model\DeliveryAddress;
  4. use think\Db;
  5. /**
  6. * @title 地址管理
  7. * @controller Address
  8. * @group base
  9. */
  10. class Address extends Base
  11. {
  12. public function initialize()
  13. {
  14. parent::initialize();
  15. parent::checkLogin();
  16. }
  17. /**
  18. * @title 设置收货地址(新增或修改)
  19. * @desc 设置收货地址(新增或修改)
  20. * @author qc
  21. * @url /api/Address/setDeliveryAddress
  22. * @method POST
  23. * @header name:Authorization require:1 desc:Token
  24. * @param name:id type:int : default:0 desc:收货地址记录id(不传值是添加)
  25. * @param name:pro_name type:string require:1 default:-- desc:省名
  26. * @param name:city_name type:string require:1 default:-- desc:市名
  27. * @param name:county_name type:string require:1 default:-- desc:县区名
  28. * @param name:detail type:string require:1 default:-- desc:详细地址
  29. * @param name:phone type:string require:1 default:-- desc:联系电话
  30. * @param name:name type:string require:1 default:-- desc:联系人
  31. * @param name:is_mr type:int require:0 default:0 desc:是否是默认(0不默认,1默认)
  32. */
  33. public function setDeliveryAddress()
  34. {
  35. $pro_name = input('post.pro_name');// 省
  36. $city_name = input('post.city_name');// 市
  37. $county_name = input('post.county_name');// 县区
  38. $street_name = input('post.street_name','' );
  39. $pro_id = Db::name('store_area')->where('name',$pro_name)->value('id');
  40. $city_id = Db::name('store_area')->where('name',$city_name)->value('id');
  41. $county_id = Db::name('store_area')->where('name',$county_name)->value('id');
  42. $detail = input('post.detail');// 详细地址
  43. $phone = input('post.phone');
  44. $name = input('post.name');
  45. $id = input('post.id');
  46. $is_mr = input('post.is_mr',0);
  47. $set_data = compact(['pro_id','city_id','county_id','detail','phone','name','is_mr']);
  48. $set_data['user_id'] = $this->user_id;
  49. $set_data['pro_name'] = $pro_name;
  50. $set_data['city_name'] = $city_name;
  51. $set_data['county_name']= $county_name;
  52. $set_data['street_name']= $street_name;
  53. $set_data['mer_name']=$pro_name . $city_name. $county_name;
  54. if($id) {
  55. Db::name('delivery_address')->where(['id'=>$id])->update($set_data);
  56. }else{
  57. Db::name('delivery_address')->insert($set_data);
  58. $id = Db::getLastInsID();
  59. }
  60. if($is_mr) Db::name('delivery_address')->where('user_id','=',$this->user_id)->where('id','<>',$id)->where('is_mr','=',1)->update(['is_mr'=>0]);
  61. $this->success('编辑成功');
  62. }
  63. /**
  64. * @title 修改默认收货地址
  65. * @desc 修改默认收货地址
  66. * @author qc
  67. * @url /api/Address/changeApproveDelivery
  68. * @method POST
  69. * @header name:Authorization require:1 desc:Token
  70. * @param name:id type:int default:0 desc:收货地址记录id
  71. * @param name:is_mr type:int default:1 desc:是否是默认(0不默认,1默认)
  72. */
  73. public function changeApproveDelivery()
  74. {
  75. $id = input('post.id');
  76. $is_mr = input('post.is_mr',1);
  77. if($is_mr) {
  78. Db::name('delivery_address')
  79. ->where('user_id','=',$this->user_id)
  80. ->where('is_mr','=',1)
  81. ->update(['is_mr'=>0]);
  82. Db::name('delivery_address')
  83. ->where('user_id','=',$this->user_id)
  84. ->where('id','=',$id)
  85. ->update(['is_mr'=>1]);
  86. }else{
  87. Db::name('delivery_address')
  88. ->where('user_id','=',$this->user_id)
  89. ->where('id','=',$id)
  90. ->update(['is_mr'=>0]);
  91. }
  92. $this->success('修改成功');
  93. }
  94. /**
  95. * @title 删除收货地址
  96. * @desc 删除收货地址
  97. * @author qc
  98. * @url /api/Address/delDeliveryAddress
  99. * @method POST
  100. * @header name:Authorization require:1 desc:Token
  101. * @param name:id type:int require:1 default:1 desc:收货地址记录id
  102. */
  103. public function delDeliveryAddress()
  104. {
  105. $id = input('post.id');
  106. if(!$id)$this->error('请选择要删除的收货地址');
  107. Db::name('delivery_address')->where(['user_id'=>$this->user_id,'id'=>$id])->update(['is_deleted'=>1,'is_mr'=>0]);
  108. $this->success('删除成功!');
  109. }
  110. /**
  111. * @title 获取收货地址列表
  112. * @desc 获取收货地址列表
  113. * @author qc
  114. * @url /api/Address/getDeliveryAddressList
  115. * @method GET
  116. * @header name:Authorization require:1 desc:Token
  117. * @param name:page type:int default:1 desc:页数
  118. * @param name:page_num type:int default:20 desc:每页数
  119. * @return name:pro_id type:int default:-- desc:省id
  120. * @return name:city_id type:int default:-- desc:市id
  121. * @return name:county_id type:int default:-- desc:县区id
  122. * @return name:pro_name type:string default:-- desc:省名
  123. * @return name:city_name type:string default:-- desc:市名
  124. * @return name:county_name type:string default:-- desc:县区名
  125. * @return name:detail type:string default:-- desc:详细地址
  126. * @return name:phone type:string default:-- desc:联系电话
  127. * @return name:name type:string default:-- desc:联系人
  128. * @return name:is_mr type:int default:0 desc:是否是默认(0不默认,1默认)
  129. */
  130. public function getDeliveryAddressList()
  131. {
  132. $list = DeliveryAddress::where(['user_id'=>$this->user_id,'is_deleted'=>0])
  133. ->order('is_mr desc ,id desc')
  134. ->limit($this->off_set,$this->page_num)
  135. ->select()->toArray();
  136. $total_num = DeliveryAddress::where(['user_id'=>$this->user_id,'is_deleted'=>0])->count();
  137. $this->success('ok',['list'=>$list,'total_count'=>$total_num,'page_num'=>$this->page_num]);
  138. }
  139. /**
  140. * @title 获取收货地址详情
  141. * @desc 获取收货地址详情
  142. * @author qc
  143. * @url /api/Address/getDeliveryAddressDetail
  144. * @method GET
  145. * @header name:Authorization require:1 desc:Token
  146. * @param name:id type:int default:1 desc:收货地址记录id
  147. * @return name:pro_id type:int default:-- desc:省id
  148. * @return name:city_id type:int default:-- desc:市id
  149. * @return name:county_id type:int default:-- desc:县区id
  150. * @return name:pro_name type:string default:-- desc:省名
  151. * @return name:city_name type:string default:-- desc:市名
  152. * @return name:county_name type:string default:-- desc:县区名
  153. * @return name:detail type:string default:-- desc:详细地址
  154. * @return name:phone type:string default:-- desc:联系电话
  155. * @return name:name type:string default:-- desc:联系人
  156. * @return name:is_mr type:int default:0 desc:是否是默认(0不默认,1默认)
  157. */
  158. public function getDeliveryAddressDetail()
  159. {
  160. $detail = Db::name('delivery_address')->find(input('id'));
  161. $this->success('ok',$detail);
  162. }
  163. /**
  164. * @title 获取默认收货地址详情
  165. * @desc 获取默认收货地址详情
  166. * @author qc
  167. * @url /api/Address/getDefaultAddress
  168. * @method GET
  169. * @header name:Authorization require:1 desc:Token
  170. * @return name:pro_id type:int default:-- desc:省id
  171. * @return name:city_id type:int default:-- desc:市id
  172. * @return name:county_id type:int default:-- desc:县区id
  173. * @return name:pro_name type:string default:-- desc:省名
  174. * @return name:city_name type:string default:-- desc:市名
  175. * @return name:county_name type:string default:-- desc:县区名
  176. * @return name:detail type:string default:-- desc:详细地址
  177. * @return name:phone type:string default:-- desc:联系电话
  178. * @return name:name type:string default:-- desc:联系人
  179. * @return name:is_mr type:int default:0 desc:是否是默认(0不默认,1默认)
  180. */
  181. public function getDefaultAddress()
  182. {
  183. $default = DeliveryAddress::where(['user_id'=>$this->user_id,'is_mr'=>1])->find();
  184. $detail = $default ? $default->toArray() : null;
  185. $this->success('ok',$detail);
  186. }
  187. }