Memberaddress.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <?php
  2. namespace app\home\controller;
  3. use think\facade\View;
  4. use think\facade\Lang;
  5. /**
  6. *
  7. *
  8. * ----------------------------------------------------------------------------
  9. *
  10. * 控制器
  11. */
  12. class Memberaddress extends BaseMember
  13. {
  14. public function initialize()
  15. {
  16. parent::initialize();
  17. Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/memberaddress.lang.php');
  18. }
  19. /*
  20. * 收货地址列表
  21. */
  22. public function index()
  23. {
  24. $address_model = model('address');
  25. $chain_model = model('chain');
  26. $address_list = $address_model->getAddressList(array('member_id' => session('member_id')));
  27. foreach ($address_list as $key => $val) {
  28. $address_list[$key]['cityerror'] = '';
  29. if ($val['chain_id'] > 0) {
  30. $condition = array();
  31. $condition[] = array('chain_id', '=', $val['chain_id']);
  32. $chain_info = $chain_model->getChainInfo($condition, 'chain_area_2,chain_area_3');
  33. if ($val['city_id'] !== $chain_info['chain_area_2'] || $val['area_id'] !== $chain_info['chain_area_3']) {
  34. $address_list[$key]['cityerror'] = lang('city_fail');
  35. }
  36. }
  37. }
  38. View::assign('address_list', $address_list);
  39. /* 设置买家当前菜单 */
  40. $this->setMemberCurMenu('member_address');
  41. /* 设置买家当前栏目 */
  42. $this->setMemberCurItem('my_address');
  43. return View::fetch($this->template_dir . 'index');
  44. }
  45. public function add()
  46. {
  47. if (!request()->isPost()) {
  48. $area_mod = model('area');
  49. $region_list = $area_mod->getAreaList(array('area_parent_id' => '0'));
  50. View::assign('region_list', $region_list);
  51. $address = array(
  52. 'address_realname' => '',
  53. 'area_id' => '',
  54. 'city_id' => '',
  55. 'address_detail' => '',
  56. 'address_tel_phone' => '',
  57. 'address_mob_phone' => '',
  58. 'address_is_default' => '',
  59. 'area_info' => '',
  60. 'address_longitude' => '',
  61. 'address_latitude' => '',
  62. );
  63. View::assign('address', $address);
  64. /* 设置买家当前菜单 */
  65. $this->setMemberCurMenu('member_address');
  66. /* 设置买家当前栏目 */
  67. $this->setMemberCurItem('my_address_add');
  68. View::assign('baidu_ak', config('ds_config.baidu_ak'));
  69. return View::fetch($this->template_dir . 'form');
  70. } else {
  71. $address_is_default = input('post.is_default') == 1 ? 1 : 0;
  72. $data = array(
  73. 'member_id' => session('member_id'),
  74. 'address_realname' => input('post.true_name'),
  75. 'area_id' => input('post.area_id'),
  76. 'city_id' => input('post.city_id'),
  77. 'address_detail' => input('post.address'),
  78. 'address_longitude' => input('post.longitude'),
  79. 'address_latitude' => input('post.latitude'),
  80. 'address_tel_phone' => input('post.tel_phone'),
  81. 'address_mob_phone' => input('post.mob_phone'),
  82. 'address_is_default' => $address_is_default,
  83. 'area_info' => input('post.area_info'),
  84. );
  85. $memberaddress_validate = ds_validate('memberaddress');
  86. if (!$memberaddress_validate->scene('add')->check($data)) {
  87. ds_json_encode(10001, $memberaddress_validate->getError());
  88. }
  89. //当默认地址为1时,把当前用户的地址设置为非默认地址
  90. if ($address_is_default == 1) {
  91. model('address')->editAddress(array('address_is_default' => 0), array('member_id' => session('member_id')));
  92. }
  93. $address_model = model('address');
  94. $result = $address_model->addAddress($data);
  95. if ($result) {
  96. ds_json_encode(10000, lang('ds_common_save_succ'));
  97. } else {
  98. ds_json_encode(10001, lang('ds_common_save_fail'));
  99. }
  100. }
  101. }
  102. public function edit()
  103. {
  104. $address_id = intval(input('param.address_id'));
  105. if (0 >= $address_id) {
  106. ds_json_encode(10001, lang('param_error'));
  107. }
  108. $address_model = model('address');
  109. $address = $address_model->getAddressInfo(array('member_id' => session('member_id'), 'address_id' => $address_id));
  110. if (empty($address)) {
  111. ds_json_encode(10001, lang('address_does_not_exist'));
  112. }
  113. if (!request()->isPost()) {
  114. $area_mod = model('area');
  115. $region_list = $area_mod->getAreaList(array('area_parent_id' => '0'));
  116. View::assign('region_list', $region_list);
  117. View::assign('address', $address);
  118. /* 设置买家当前菜单 */
  119. $this->setMemberCurMenu('member_address');
  120. /* 设置买家当前栏目 */
  121. $this->setMemberCurItem('my_address_edit');
  122. View::assign('baidu_ak', config('ds_config.baidu_ak'));
  123. return View::fetch($this->template_dir . 'form');
  124. } else {
  125. $address_is_default = input('post.is_default') == 1 ? 1 : 0;
  126. $data = array(
  127. 'address_realname' => input('post.true_name'),
  128. 'area_id' => input('post.area_id'),
  129. 'city_id' => input('post.city_id'),
  130. 'address_detail' => input('post.address'),
  131. 'address_longitude' => input('post.longitude'),
  132. 'address_latitude' => input('post.latitude'),
  133. 'address_tel_phone' => input('post.tel_phone'),
  134. 'address_mob_phone' => input('post.mob_phone'),
  135. 'address_is_default' => $address_is_default,
  136. 'area_info' => input('post.area_info'),
  137. );
  138. $memberaddress_validate = ds_validate('memberaddress');
  139. if (!$memberaddress_validate->scene('edit')->check($data)) {
  140. ds_json_encode(10001, $memberaddress_validate->getError());
  141. }
  142. //当默认地址为1时,把当前用户的地址设置为非默认地址
  143. if ($address_is_default == 1) {
  144. model('address')->editAddress(array('address_is_default' => 0), array('member_id' => session('member_id')));
  145. }
  146. $result = $address_model->editAddress($data, array('member_id' => session('member_id'), 'address_id' => $address_id));
  147. if ($result) {
  148. ds_json_encode(10000, lang('ds_common_save_succ'));
  149. } else {
  150. ds_json_encode(10001, lang('ds_common_save_fail'));
  151. }
  152. }
  153. }
  154. public function drop()
  155. {
  156. $address_id = intval(input('param.address_id'));
  157. if (0 >= $address_id) {
  158. ds_json_encode(10001, lang('empty_error'));
  159. }
  160. $address_model = model('address');
  161. $condition = array();
  162. $condition[] = array('address_id', '=', $address_id);
  163. $condition[] = array('member_id', '=', session('member_id'));
  164. $result = $address_model->delAddress($condition);
  165. if ($result) {
  166. ds_json_encode(10000, lang('ds_common_del_succ'));
  167. } else {
  168. ds_json_encode(10001, lang('ds_common_del_fail'));
  169. }
  170. }
  171. /**
  172. * 添加门店收货地址
  173. */
  174. public function chain_add()
  175. {
  176. if (request()->isPost()) {
  177. $info = model('chain')->getChainOpenInfo(array(array('chain_id', '=', intval(input('param.chain_id')))));
  178. if (empty($info)) {
  179. ds_json_encode(10001, lang('pick_up_point_exist'));
  180. }
  181. $data = array();
  182. $data['member_id'] = session('member_id');
  183. $data['address_realname'] = input('param.true_name');
  184. $data['area_id'] = $info['chain_area_3'];
  185. $data['city_id'] = $info['chain_area_2'];
  186. $data['area_info'] = $info['chain_area_info'];
  187. $data['address_detail'] = $info['chain_address'];
  188. $data['address_tel_phone'] = input('param.tel_phone');
  189. $data['address_mob_phone'] = input('param.mob_phone');
  190. $data['chain_id'] = $info['chain_id'];
  191. $data['address_is_default'] = 0;
  192. if (intval(input('param.address_id'))) {
  193. $result = model('address')->editAddress($data, array('address_id' => intval(input('param.address_id'))));
  194. } else {
  195. $count = model('address')->getAddressCount(array('member_id' => session('member_id')));
  196. if ($count >= 20) {
  197. ds_json_encode(10001, lang('valid_addresses_allowed'));
  198. }
  199. $result = model('address')->addAddress($data);
  200. }
  201. if (!$result) {
  202. ds_json_encode(10001, lang('ds_common_save_fail'));
  203. }
  204. ds_json_encode(10000, lang('ds_common_save_succ'));
  205. } else {
  206. if (intval(input('param.address_id')) > 0) {
  207. $address_model = model('address');
  208. $condition = array('address_id' => intval(input('param.address_id')), 'member_id' => session('member_id'));
  209. $address_info = $address_model->getAddressInfo($condition);
  210. //取出省级ID
  211. $area_info = model('area')->getAreaInfo(array('area_id' => $address_info['city_id']));
  212. $address_info['province_id'] = $area_info['area_parent_id'];
  213. View::assign('address_info', $address_info);
  214. }
  215. /* 设置买家当前菜单 */
  216. $this->setMemberCurMenu('member_address');
  217. /* 设置买家当前栏目 */
  218. $this->setMemberCurItem('chain_add');
  219. return View::fetch($this->template_dir . 'chain_add');
  220. }
  221. }
  222. /**
  223. * 展示门店列表
  224. */
  225. public function chain_list()
  226. {
  227. $chain_model = model('chain');
  228. $condition = array();
  229. $condition[] = array('chain_area_3', '=', intval(input('param.area_id')));
  230. $condition[] = array('chain_if_collect', '=', 1);
  231. $chain_list = $chain_model->getChainOpenList($condition, 5);
  232. View::assign('show_page', $chain_model->page_info->render());
  233. View::assign('chain_list', $chain_list);
  234. /* 设置买家当前菜单 */
  235. $this->setMemberCurMenu('member_address');
  236. /* 设置买家当前栏目 */
  237. $this->setMemberCurItem('chain_list');
  238. echo View::fetch($this->template_dir . 'chain_list');
  239. exit;
  240. }
  241. /**
  242. * 栏目菜单
  243. */
  244. function getMemberItemList()
  245. {
  246. $item_list = array(
  247. array(
  248. 'name' => 'my_address',
  249. 'text' => lang('my_address'),
  250. 'url' => (string)url('Memberaddress/index'),
  251. ),
  252. array(
  253. 'name' => 'my_address_add',
  254. 'text' => lang('new_address'),
  255. 'url' => (string)url('Memberaddress/add'),
  256. ),
  257. );
  258. if (request()->action() == 'edit') {
  259. $item_list[] = array(
  260. 'name' => 'my_address_edit',
  261. 'text' => lang('edit_address'),
  262. 'url' => "javascript:void(0)",
  263. );
  264. }
  265. if (request()->action() == 'chain_add') {
  266. $item_list[] = array(
  267. 'name' => 'chain_add',
  268. 'text' => lang('to_set'),
  269. 'url' => "javascript:void(0)",
  270. );
  271. }
  272. return $item_list;
  273. }
  274. }