Memberaddress.php 12 KB

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