Memberaddress.php 14 KB

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