AddressController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. <?php
  2. namespace App\Http\Controllers\Api\V1;
  3. use App\Models\BaseAreaModel;
  4. use App\Models\WxUserAddressModel;
  5. use App\Services\Base\ErrorCode;
  6. use Illuminate\Http\Request;
  7. use Illuminate\Support\Facades\Auth;
  8. use Illuminate\Support\Facades\Cache;
  9. use Illuminate\Support\Facades\Log;
  10. use Illuminate\Support\Facades\Validator;
  11. class AddressController extends Controller
  12. {
  13. /**
  14. * @api {get} /api/my/address/index 地址列表
  15. * @apiDescription 地址列表
  16. * @apiGroup My
  17. * @apiPermission none
  18. * @apiVersion 0.1.0
  19. * @apiSuccessExample {json} Success-Response:
  20. * HTTP/1.1 200 OK
  21. {
  22. "status": true,
  23. "status_code": 0,
  24. "message": "",
  25. "data": [
  26. {
  27. "id": 1,
  28. "name": "name1",
  29. "tel": "13880642880",
  30. "area": "330104", 区域代码
  31. "address": "蜀西路",
  32. "status": "1", 1默认地址
  33. "addr": {
  34. "merger_name": "中国,浙江省,杭州市,江干区",
  35. }
  36. }
  37. ]
  38. }
  39. * @apiErrorExample {json} Error-Response:
  40. * HTTP/1.1 400 Bad Request
  41. */
  42. public function index()
  43. {
  44. $user = Auth::guard('api')->user();
  45. if (!$user) return $this->error(ErrorCode::ERROR_POWER);
  46. $address = WxUserAddressModel::where('wx_user_id',$user->id)->orderBy('created_at','desc')->get();
  47. foreach ($address as $item) {
  48. $arr = [] ;
  49. $area = BaseAreaModel::find($item['area']);
  50. $city = BaseAreaModel::find($area['pid']);
  51. $province = BaseAreaModel::find($city['pid']);
  52. $arr['province']['name'] = empty($province) ? '' : $province->name;
  53. $arr['province']['id'] = empty($province) ? '' : $province->id;
  54. $arr['city']['name'] = empty($city) ? '' : $city->name;
  55. $arr['city']['id'] = empty($city) ? '' : $city->id;
  56. $arr['district']['name'] = empty($area) ? '' : $area->name;
  57. $arr['district']['id'] = empty($area) ? '' : $area->id;
  58. $item->district = $arr;
  59. }
  60. return $this->api($address);
  61. }
  62. /**
  63. * @api {post} /api/my/address/update 地址编辑
  64. * @apiDescription 地址编辑
  65. * @apiGroup My
  66. * @apiParam {int} [id] 主键id 存在表示修改
  67. * @apiParam {string} name 收货人姓名
  68. * @apiParam {int} tel 收货人电话
  69. * @apiParam {int} area 省市区地址
  70. * @apiParam {string} address 详细地址
  71. * @apiParam {int} status 1或0 1为默认地址
  72. * @apiPermission none
  73. * @apiVersion 0.1.0
  74. * @apiSuccessExample {json} Success-Response:
  75. * HTTP/1.1 200 OK
  76. {
  77. "status": true,
  78. "status_code": 0,
  79. "message": "",
  80. "data": ""
  81. }
  82. * @apiErrorExample {json} Error-Response:
  83. * HTTP/1.1 400 Bad Request
  84. {
  85. "status": false,
  86. "status_code": 2006,
  87. "message": "操作失败",
  88. "data": null
  89. }
  90. */
  91. public function update(Request $request)
  92. {
  93. $user = Auth::guard('api')->user();
  94. if (!$user) return $this->error(ErrorCode::ERROR_POWER);
  95. $validator = Validator::make($request->all(),
  96. [
  97. 'name' => 'required',
  98. 'area' => 'required',
  99. 'tel' => 'required',
  100. 'address' => 'required',
  101. 'status' => 'required',
  102. ],
  103. [
  104. 'name.required' => '联系人不能为空',
  105. 'area.required' => '地址不能为空',
  106. 'tel.required' => '电话号码不能为空',
  107. 'address.required' => '地址不能为空',
  108. 'status.required' => '是否设为默认地址',
  109. ]
  110. );
  111. if ($validator->fails()) {
  112. return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
  113. }
  114. $id = $request->input('id');
  115. $status = $request->input('status');
  116. $tel = $request->input('tel');
  117. $name = $request->input('name');
  118. $address = $request->input('address');
  119. $wx_user_id = $user->id;
  120. $area = $request->input('area');
  121. if ($status==1 ) {
  122. WxUserAddressModel::where('wx_user_id',$user->id)->where('status',1)->update(['status'=>0]);
  123. }
  124. $arr = [
  125. compact('status','tel','name','address','area','wx_user_id'),
  126. ];
  127. if (!empty($id)) {
  128. $data = WxUserAddressModel::find($id);
  129. if (empty($data)) return $this->error(ErrorCode::ADDRESS_NOT_EXIST);
  130. if(!empty($name)) $data->name = $name;
  131. if(!empty($tel)) $data->tel = $tel;
  132. if(!empty($address)) $data->address = $address;
  133. if(!empty($area)) $data->area = $area;
  134. if(!empty($status)) $data->status = $status;
  135. $ok = $data->save();
  136. }else{
  137. $ok = WxUserAddressModel::create($arr[0]);
  138. }
  139. if ($ok) return $this->api('');
  140. return $this->error(ErrorCode::OP_ERROR);
  141. }
  142. /**
  143. * @api {get} /api/my/address/show 地址详情
  144. * @apiDescription 地址详情
  145. * @apiGroup My
  146. * @apiParam {int} id 收货人地址id
  147. * @apiPermission none
  148. * @apiVersion 0.1.0
  149. * @apiSuccessExample {json} Success-Response:
  150. * HTTP/1.1 200 OK
  151. {
  152. "status": true,
  153. "status_code": 0,
  154. "message": "",
  155. "data": {
  156. "id": 1,
  157. "name": "hahahahaha",
  158. "tel": "13880634889",
  159. "area": "330104",
  160. "address": "三泰魔方123",
  161. "status": "0",
  162. "addr": {
  163. "merger_name": "中国,浙江省,杭州市,江干区",
  164. }
  165. }
  166. }
  167. * @apiErrorExample {json} Error-Response:
  168. * HTTP/1.1 400 Bad Request
  169. {
  170. "status": false,
  171. "status_code": 2006,
  172. "message": "操作失败",
  173. "data": null
  174. }
  175. */
  176. public function show(Request $request)
  177. {
  178. $user = Auth::guard('api')->user();
  179. if (!$user) return $this->error(ErrorCode::ERROR_POWER);
  180. $validator = Validator::make($request->all(),
  181. [
  182. 'id' => 'required',
  183. ],
  184. [
  185. 'id.required' => '收货地址主键id不存在',
  186. ]
  187. );
  188. if ($validator->fails()) {
  189. return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
  190. }
  191. $id = $request->input('id');
  192. $data = WxUserAddressModel::find($id);
  193. $area = BaseAreaModel::find($data['area']);
  194. $city = BaseAreaModel::find($area['pid']);
  195. $province = BaseAreaModel::find($city['pid']);
  196. $arr['province']['name'] = empty($province) ? '' : $province->name;
  197. $arr['province']['id'] = empty($province) ? '' : $province->id;
  198. $arr['city']['name'] = empty($city) ? '' : $city->name;
  199. $arr['city']['id'] = empty($city) ? '' : $city->id;
  200. $arr['district']['name'] = empty($area) ? '' : $area->name;
  201. $arr['district']['id'] = empty($area) ? '' : $area->id;
  202. $data->district = $arr;
  203. return $this->api($data);
  204. }
  205. /**
  206. * @api {get} /api/my/address/delete 地址删除
  207. * @apiDescription 地址删除
  208. * @apiGroup My
  209. * @apiParam {int} id 主键id
  210. * @apiPermission none
  211. * @apiVersion 0.1.0
  212. * @apiSuccessExample {json} Success-Response:
  213. * HTTP/1.1 200 OK
  214. {
  215. "status": true,
  216. "status_code": 0,
  217. "message": "",
  218. "data": ""
  219. }
  220. * @apiErrorExample {json} Error-Response:
  221. * HTTP/1.1 400 Bad Request
  222. {
  223. "status": false,
  224. "status_code": 2006,
  225. "message": "操作失败",
  226. "data": null
  227. }
  228. */
  229. public function delete(Request $request)
  230. {
  231. $user = Auth::guard('api')->user();
  232. if (!$user) return $this->error(ErrorCode::ERROR_POWER);
  233. $validator = Validator::make($request->all(),
  234. [
  235. 'id' => 'required',
  236. ],
  237. [
  238. 'id.required' => '主键id不存在',
  239. ]
  240. );
  241. if ($validator->fails()) {
  242. return $this->validatorError($validator->messages()->all(), ErrorCode::CLIENT_WRONG_PARAMS);
  243. }
  244. $id = $request->input('id');
  245. $data = WxUserAddressModel::find($id);
  246. if (empty($data)) return $this->error(ErrorCode::ADDRESS_NOT_EXIST);
  247. $ok = WxUserAddressModel::destroy($id);
  248. if($data->status==1){ //如果默认地址被删除 重新默认一个地址
  249. $info = WxUserAddressModel::where('wx_user_id',$user->id)->first();
  250. $info->status=1;
  251. $info->save();
  252. }
  253. if ($ok) return $this->api('');
  254. return $this->error(ErrorCode::OP_ERROR);
  255. }
  256. /**
  257. * @api {get} /api/my/address/area 返回省市区地址
  258. * @apiDescription 返回省市区地址
  259. * @apiGroup My
  260. * @apiPermission none
  261. * @apiVersion 0.1.0
  262. * @apiSuccessExample {json} Success-Response:
  263. * HTTP/1.1 200 OK
  264. */
  265. public function area()
  266. {
  267. $data = Cache::get('list');
  268. if (empty($data)) {
  269. $list = BaseAreaModel::select('id','name')->where('pid',100000)->get()->toArray();
  270. foreach ($list as $key => $item) {
  271. $list[$key]['list'] = BaseAreaModel::select('id','name')->where('pid',$item['id'])->get()->toArray();
  272. foreach ($list[$key]['list'] as $k => $v){
  273. $list[$key]['list'][$k]['list'] = BaseAreaModel::select('id','name')->where('pid',$v['id'])->get()->toArray();
  274. }
  275. }
  276. Cache::forever('list', $list);
  277. }
  278. return $this->api($data);
  279. }
  280. /**
  281. * @api {get} /api/my/address/edit 修改默认地址
  282. * @apiDescription 修改默认地址
  283. * @apiGroup My
  284. * @apiParam {int} id 主键id
  285. * @apiParam {int} status 1
  286. * @apiPermission none
  287. * @apiVersion 0.1.0
  288. * @apiSuccessExample {json} Success-Response:
  289. * HTTP/1.1 200 OK
  290. * @apiErrorExample {json} Error-Response:
  291. * HTTP/1.1 400 Bad Request
  292. */
  293. public function edit(Request $request)
  294. {
  295. $user = Auth::guard('api')->user();
  296. if (!$user) return $this->error(ErrorCode::ERROR_POWER);
  297. $validator = Validator::make($request->all(),
  298. [
  299. 'id' => 'required',
  300. 'status' => 'required',
  301. ],
  302. [
  303. 'id.required' => '主键id不存在',
  304. 'status.required' => '是否设为默认地址',
  305. ]
  306. );
  307. if ($validator->fails()) {
  308. return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
  309. }
  310. $id = $request->input('id');
  311. $status = $request->input('status');
  312. $data = WxUserAddressModel::find($id);
  313. if (empty($data)) return $this->error(ErrorCode::ADDRESS_NOT_EXIST);
  314. if ($status==1 ) {
  315. WxUserAddressModel::where('wx_user_id',$user->id)->where('status',1)->update(['status'=>0]);
  316. $data->status = $status;
  317. $ok = $data->save();
  318. if ($ok) return $this->api('');
  319. return $this->error(ErrorCode::OP_ERROR);
  320. }
  321. return $this->error(ErrorCode::OP_ERROR);
  322. }
  323. }