Memberaddress.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. <?php
  2. namespace app\api\controller;
  3. use think\facade\Lang;
  4. /**
  5. *
  6. *
  7. * ----------------------------------------------------------------------------
  8. *
  9. * 用户收货地址控制器
  10. */
  11. class Memberaddress extends MobileMember
  12. {
  13. public function initialize()
  14. {
  15. parent::initialize();
  16. Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/memberaddress.lang.php');
  17. }
  18. /**
  19. * @api {POST} api/memberaddress/address_list 获取用户地址列表
  20. * @apiVersion 1.0.0
  21. * @apiGroup MemberAddress
  22. *
  23. * @apiHeader {String} X-DS-KEY 用户授权token
  24. *
  25. * @apiSuccess {String} code 返回码,10000为成功
  26. * @apiSuccess {String} message 返回消息
  27. * @apiSuccess {Object} result 返回数据
  28. * @apiSuccess {String} result.address_list 地址列表
  29. * @apiSuccess {String} result.address_list.address_id 地址ID
  30. * @apiSuccess {String} result.address_list.member_id 用户ID
  31. * @apiSuccess {String} result.address_list.address_realname 收货人姓名
  32. * @apiSuccess {String} result.address_list.city_id 城市ID
  33. * @apiSuccess {String} result.address_list.area_id 地区ID
  34. * @apiSuccess {String} result.address_list.area_info 地区内容
  35. * @apiSuccess {String} result.address_list.address_detail 详细地址
  36. * @apiSuccess {String} result.address_list.address_tel_phone 座机
  37. * @apiSuccess {String} result.address_list.address_mob_phone 手机
  38. * @apiSuccess {String} result.address_list.address_is_default 是否默认地址
  39. * @apiSuccess {String} result.address_list.address_longitude 经度
  40. * @apiSuccess {String} result.address_list.address_latitude 纬度
  41. */
  42. public function address_list()
  43. {
  44. $address_model = model('address');
  45. $chain_model = model('chain');
  46. $address_list = $address_model->getAddressList(array('member_id' => $this->member_info['member_id']));
  47. foreach ($address_list as $key => $val) {
  48. $address_list[$key]['cityerror'] = '';
  49. if ($val['chain_id'] > 0) {
  50. $condition = array();
  51. $condition[] = array('chain_id', '=', $val['chain_id']);
  52. $chain_info = $chain_model->getChainInfo($condition, 'chain_area_2,chain_area_3');
  53. if ($val['city_id'] !== $chain_info['chain_area_2'] || $val['area_id'] !== $chain_info['chain_area_3']) {
  54. $address_list[$key]['cityerror'] = true;
  55. } else {
  56. $address_list[$key]['cityerror'] = false;
  57. }
  58. } else {
  59. $address_list[$key]['cityerror'] = false;
  60. }
  61. }
  62. ds_json_encode(10000, '', array('address_list' => $address_list));
  63. }
  64. /**
  65. * @api {POST} api/memberaddress/address_info 获取地址详细信息
  66. * @apiVersion 1.0.0
  67. * @apiGroup MemberAddress
  68. *
  69. * @apiHeader {String} X-DS-KEY 用户授权token
  70. *
  71. * @apiParam {String} address_id 地址ID
  72. *
  73. * @apiSuccess {String} code 返回码,10000为成功
  74. * @apiSuccess {String} message 返回消息
  75. * @apiSuccess {Object} result 返回数据
  76. * @apiSuccess {String} result.address_info 地址信息
  77. * @apiSuccess {String} result.address_info.address_id 地址ID
  78. * @apiSuccess {String} result.address_info.member_id 用户ID
  79. * @apiSuccess {String} result.address_info.address_realname 收货人姓名
  80. * @apiSuccess {String} result.address_info.city_id 城市ID
  81. * @apiSuccess {String} result.address_info.area_id 地区ID
  82. * @apiSuccess {String} result.address_info.area_info 地区内容
  83. * @apiSuccess {String} result.address_info.address_detail 详细地址
  84. * @apiSuccess {String} result.address_info.address_tel_phone 座机
  85. * @apiSuccess {String} result.address_info.address_mob_phone 手机
  86. * @apiSuccess {String} result.address_info.address_is_default 是否默认地址
  87. * @apiSuccess {String} result.address_info.address_longitude 经度
  88. * @apiSuccess {String} result.address_info.address_latitude 纬度
  89. */
  90. public function address_info()
  91. {
  92. $address_id = intval(input('post.address_id'));
  93. $address_model = model('address');
  94. $condition = array();
  95. $condition[] = array('member_id', '=', $this->member_info['member_id']);
  96. $condition[] = array('address_id', '=', $address_id);
  97. $address_info = $address_model->getAddressInfo($condition);
  98. if (!empty($address_id) && $address_info['member_id'] == $this->member_info['member_id']) {
  99. ds_json_encode(10000, '', array('address_info' => $address_info));
  100. } else {
  101. ds_json_encode(10001, lang('address_does_not_exist'));
  102. }
  103. }
  104. /**
  105. * @api {POST} api/memberaddress/address_del 删除地址
  106. * @apiVersion 1.0.0
  107. * @apiGroup MemberAddress
  108. *
  109. * @apiHeader {String} X-DS-KEY 用户授权token
  110. *
  111. * @apiParam {String} address_id 地址ID
  112. * @apiParam {String} address_realname 收货人姓名
  113. * @apiParam {String} city_id 市级ID
  114. * @apiParam {String} area_id 地区ID
  115. * @apiParam {String} area_info 地区内容
  116. * @apiParam {String} address_detail 详细地址
  117. * @apiParam {String} address_tel_phone 座机
  118. * @apiParam {String} address_mob_phone 手机
  119. * @apiParam {String} address_is_default 默认收货地址 1默认
  120. * @apiParam {String} address_longitude 经度
  121. * @apiParam {String} address_latitude 纬度
  122. *
  123. * @apiSuccess {String} code 返回码,10000为成功
  124. * @apiSuccess {String} message 返回消息
  125. * @apiSuccess {Object} result 返回数据
  126. */
  127. public function address_del()
  128. {
  129. $address_id = intval(input('post.address_id'));
  130. $address_model = model('address');
  131. $condition = array();
  132. $condition[] = array('address_id', '=', $address_id);
  133. $condition[] = array('member_id', '=', $this->member_info['member_id']);
  134. $address_model->delAddress($condition);
  135. ds_json_encode(10000, '', 1);
  136. }
  137. /**
  138. * @api {POST} api/memberaddress/address_add 新增地址
  139. * @apiVersion 1.0.0
  140. * @apiGroup MemberAddress
  141. *
  142. * @apiHeader {String} X-DS-KEY 用户授权token
  143. *
  144. * @apiParam {String} address_realname 收货人姓名
  145. * @apiParam {String} city_id 市级ID
  146. * @apiParam {String} area_id 地区ID
  147. * @apiParam {String} area_info 地区内容
  148. * @apiParam {String} address_detail 详细地址
  149. * @apiParam {String} address_tel_phone 座机
  150. * @apiParam {String} address_mob_phone 手机
  151. * @apiParam {String} address_is_default 默认收货地址 1默认
  152. * @apiParam {String} address_longitude 经度
  153. * @apiParam {String} address_latitude 纬度
  154. *
  155. * @apiSuccess {String} code 返回码,10000为成功
  156. * @apiSuccess {String} message 返回消息
  157. * @apiSuccess {Object} result 返回数据
  158. */
  159. public function address_add()
  160. {
  161. $address_model = model('address');
  162. $address_info = $this->_address_valid();
  163. $result = $address_model->addAddress($address_info);
  164. if ($result) {
  165. ds_json_encode(10000, '', array('address_id' => $result));
  166. } else {
  167. ds_json_encode(10001, lang('ds_common_op_fail'));
  168. }
  169. }
  170. /**
  171. * @api {POST} api/memberaddress/address_edit 编辑地址
  172. * @apiVersion 1.0.0
  173. * @apiGroup MemberAddress
  174. *
  175. * @apiHeader {String} X-DS-KEY 用户授权token
  176. *
  177. * @apiParam {String} address_id 地址ID
  178. *
  179. * @apiSuccess {String} code 返回码,10000为成功
  180. * @apiSuccess {String} message 返回消息
  181. * @apiSuccess {Object} result 返回数据
  182. */
  183. public function address_edit()
  184. {
  185. $address_id = intval(input('post.address_id'));
  186. $address_model = model('address');
  187. //验证地址是否为本人
  188. $address_info = $address_model->getOneAddress($address_id);
  189. if ($address_info['member_id'] != $this->member_info['member_id']) {
  190. ds_json_encode(10001, lang('param_error'));
  191. }
  192. $address_info = $this->_address_valid();
  193. $result = $address_model->editAddress($address_info, array('address_id' => $address_id, 'member_id' => $this->member_info['member_id']));
  194. if ($result) {
  195. ds_json_encode(10000, '', 1);
  196. } else {
  197. ds_json_encode(10001, lang('ds_common_op_fail'));
  198. }
  199. }
  200. /**
  201. * 验证地址数据
  202. */
  203. private function _address_valid()
  204. {
  205. $data = [
  206. 'address_realname' => input('post.address_realname'),
  207. 'area_info' => input('post.area_info'),
  208. 'address_detail' => input('post.address_detail'),
  209. ];
  210. $memberaddress_validate = ds_validate('memberaddress');
  211. if (!$memberaddress_validate->scene('address_valid')->check($data)) {
  212. ds_json_encode(10001, $memberaddress_validate->getError());
  213. }
  214. //当默认地址为1时,把当前用户的地址设置为非默认地址
  215. $address_is_default = intval(input('post.address_is_default'));
  216. if ($address_is_default == 1) {
  217. model('address')->editAddress(array('address_is_default' => 0), array('member_id' => $this->member_info['member_id']));
  218. }
  219. $data = array();
  220. $data['member_id'] = $this->member_info['member_id'];
  221. $data['address_realname'] = input('post.address_realname');
  222. $data['area_id'] = intval(input('post.area_id'));
  223. $data['city_id'] = intval(input('post.city_id'));
  224. $data['area_info'] = input('post.area_info');
  225. $data['address_detail'] = input('post.address_detail');
  226. $data['address_longitude'] = input('post.address_longitude');
  227. $data['address_latitude'] = input('post.address_latitude');
  228. $data['address_tel_phone'] = !empty(input('post.address_tel_phone')) ? input('post.address_tel_phone') : '';
  229. $data['address_mob_phone'] = !empty(input('post.address_mob_phone')) ? input('post.address_mob_phone') : '';
  230. $data['address_is_default'] = $address_is_default;
  231. return $data;
  232. }
  233. /**
  234. * @api {POST} api/memberaddress/chain_add 添加门店收货地址
  235. * @apiVersion 1.0.0
  236. * @apiGroup MemberAddress
  237. *
  238. * @apiHeader {String} X-DS-KEY 用户授权token
  239. *
  240. * @apiParam {String} chain_id 门店ID
  241. * @apiParam {String} address_realname 收货人姓名
  242. * @apiParam {String} address_tel_phone 座机号码
  243. * @apiParam {String} address_mob_phone 手机号码
  244. * @apiParam {String} address_is_default 是否默认
  245. * @apiParam {String} address_id 地址ID
  246. *
  247. * @apiSuccess {String} code 返回码,10000为成功
  248. * @apiSuccess {String} message 返回消息
  249. * @apiSuccess {Object} result 返回数据
  250. */
  251. public function chain_add()
  252. {
  253. $info = model('chain')->getChainOpenInfo(array(array('chain_id', '=', intval(input('param.chain_id')))));
  254. if (empty($info)) {
  255. ds_json_encode(10001, lang('pick_up_point_exist'));
  256. }
  257. $data = array();
  258. $data['member_id'] = $this->member_info['member_id'];
  259. $data['address_realname'] = input('param.address_realname');
  260. $data['area_id'] = $info['chain_area_3'];
  261. $data['city_id'] = $info['chain_area_2'];
  262. $data['area_info'] = $info['chain_area_info'];
  263. $data['address_detail'] = $info['chain_address'];
  264. $data['address_tel_phone'] = input('param.address_tel_phone', '');
  265. $data['address_mob_phone'] = input('param.address_mob_phone');
  266. $data['chain_id'] = $info['chain_id'];
  267. $data['address_is_default'] = intval(input('post.address_is_default'));
  268. //当默认地址为1时,把当前用户的地址设置为非默认地址
  269. $address_is_default = intval(input('post.address_is_default'));
  270. if ($address_is_default == 1) {
  271. model('address')->editAddress(array('address_is_default' => 0), array('member_id' => $this->member_info['member_id']));
  272. }
  273. if (intval(input('param.address_id'))) {
  274. $result = model('address')->editAddress($data, array('address_id' => intval(input('param.address_id'))));
  275. } else {
  276. $count = model('address')->getAddressCount(array('member_id' => $this->member_info['member_id']));
  277. if ($count >= 20) {
  278. ds_json_encode(10001, lang('valid_addresses_allowed'));
  279. }
  280. $result = model('address')->addAddress($data);
  281. }
  282. if (!$result) {
  283. ds_json_encode(10001, lang('ds_common_op_fail'));
  284. }
  285. ds_json_encode(10000, lang('ds_common_op_succ'));
  286. }
  287. /**
  288. * @api {POST} api/memberaddress/chain_list 展示门店列表
  289. * @apiVersion 1.0.0
  290. * @apiGroup MemberAddress
  291. *
  292. * @apiHeader {String} X-DS-KEY 用户授权token
  293. *
  294. * @apiParam {String} area_id 地区ID
  295. *
  296. * @apiSuccess {String} code 返回码,10000为成功
  297. * @apiSuccess {String} message 返回消息
  298. * @apiSuccess {Object} result 返回数据
  299. */
  300. public function chain_list()
  301. {
  302. $chain_model = model('chain');
  303. $condition = array();
  304. $condition[] = array('chain_area_3', '=', intval(input('param.area_id')));
  305. $chain_list = $chain_model->getChainOpenList($condition, $this->pagesize);
  306. $mobile_page = $chain_model->page_info;
  307. $result = array_merge(array('chain_list' => $chain_list), mobile_page($mobile_page));
  308. ds_json_encode(10000, '', $result);
  309. }
  310. }