Memberaddress.php 12 KB

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