Memberaddress.php 14 KB

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