store.ctrl.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <?php
  2. /**
  3. * [WeEngine System] Copyright (c) 2014 WE7.CC
  4. * WeEngine is NOT a free software, it under the license terms, visited http://www.we7.cc/ for more details.
  5. */
  6. defined('IN_IA') or exit('Access Denied');
  7. $_W['page']['title'] = '商家设置-粉丝营销';
  8. uni_user_permission_check('activity_store_list');
  9. $dos = array('display', 'post','delete', 'import', 'sync');
  10. $do = in_array($do, $dos) ? $do : 'display';
  11. if($do == 'post') {
  12. $id = intval($_GPC['id']);
  13. if($id > 0) {
  14. $location = pdo_get('activity_stores', array('id' => $id, 'uniacid' => $_W['uniacid']));
  15. if (empty($location)) {
  16. message('商家不存在', referer(), 'info');
  17. }
  18. if (COUPON_TYPE == WECHAT_COUPON) {
  19. $location_info = $coupon_api->LocationGet($location['location_id']);
  20. if(is_error($location_info)) {
  21. message("从微信获取门店信息失败,错误详情:{$location_info['message']}", referer(), 'error');
  22. }
  23. $update_status = $location_info['business']['base_info']['update_status'];
  24. $location['open_time_start'] = '8:00';
  25. $location['open_time_end'] = '24:00';
  26. $open_time = explode('-', $location['open_time']);
  27. if(!empty($open_time)) {
  28. $location['open_time_start'] = $open_time[0];
  29. $location['open_time_end'] = $open_time[1];
  30. }
  31. $location['category'] = iunserializer($location['category']);
  32. $location['categorys'] = $location['category'];
  33. $location['category'] = rtrim(implode('-', $location['category']), '-');
  34. $location['address'] = $location['provice'].$location['city'].$location['district'].$location['address'];
  35. $location['baidumap'] = array('lng' => $location['longitude'], 'lat' => $location['latitude']);
  36. $photo_lists = iunserializer($location['photo_list']);
  37. $location['photo_list'] = array();
  38. if(!empty($photo_lists)) {
  39. foreach($photo_lists as $li) {
  40. if(!empty($li['photo_url'])) {
  41. $location['photo_list'][] = $li['photo_url'];
  42. }
  43. }
  44. }
  45. } else {
  46. $location['category'] = iunserializer($location['category']);
  47. $location['photo_list'] = iunserializer($location['photo_list']);
  48. foreach ($location['photo_list'] as &$photo) {
  49. $photo = $photo['photo_url'];
  50. }
  51. $location['opentime'] = explode('-', $location['open_time']);
  52. $location['open_time_start'] = $location['opentime'][0];
  53. $location['open_time_end'] = $location['opentime'][1];
  54. $item = $location;
  55. }
  56. }else {
  57. $item['open_time_start'] = '8:00';
  58. $item['open_time_end'] = '24:00';
  59. }
  60. if(checksubmit('submit')) {
  61. if (COUPON_TYPE == WECHAT_COUPON && $id) {
  62. if(empty($location['location_id'])) {
  63. message('门店正在审核中或审核未通过,不能更新门店信息', referer(), 'error');
  64. }
  65. if($update_status == 1) {
  66. message('服务信息正在更新中,尚未生效,不允许再次更新', referer(), 'error');
  67. }
  68. $data['telephone'] = trim($_GPC['telephone']) ? trim($_GPC['telephone']) : message('门店电话不能为空');
  69. if(empty($_GPC['photo_list'])) {
  70. message('门店图片不能为空');
  71. } else {
  72. foreach($_GPC['photo_list'] as $val) {
  73. if(empty($val)) continue;
  74. $data['photo_list'][] = array('photo_url' => $val);
  75. }
  76. }
  77. $data['avg_price'] = intval($_GPC['avg_price']);
  78. if(empty($_GPC['open_time_start']) || empty($_GPC['open_time_end'])) {
  79. message('营业时间不能为空');
  80. } else {
  81. $data['open_time'] = $_GPC['open_time_start'] . '-' . $_GPC['open_time_end'];
  82. }
  83. $data['recommend'] = urlencode(trim($_GPC['recommend']));
  84. $data['special'] = trim($_GPC['special']) ? urlencode(trim($_GPC['special'])) : message('特色服务不能为空');
  85. $data['introduction'] = urlencode(trim($_GPC['introduction']));
  86. $data['poi_id'] = $location['location_id'];
  87. $status = $coupon_api->LocationEdit($data);
  88. if(is_error($status)) {
  89. message($status['message'], '', 'error');
  90. } else {
  91. unset($data['poi_id']);
  92. $data['photo_list'] = iserializer($data['photo_list']);
  93. $data['recommend'] = $_GPC['recommend'];
  94. $data['special'] = trim($_GPC['special']);
  95. $data['introduction'] = trim($_GPC['introduction']);
  96. pdo_update('activity_stores', $data, array('uniacid' => $_W['uniacid'], 'id' => $id));
  97. message('门店信息已提交,等待微信审核', url('activity/store'), 'success');
  98. }
  99. }
  100. $store_data = array(
  101. 'business_name' => trim($_GPC['business_name']),
  102. 'branch_name' => trim($_GPC['branch_name']),
  103. 'category' => array(
  104. 'cate' => trim($_GPC['class']['cate']),
  105. 'sub' => trim($_GPC['class']['sub']),
  106. 'clas' => trim($_GPC['class']['clas'])
  107. ),
  108. 'province' => trim($_GPC['reside']['province']),
  109. 'city' => trim($_GPC['reside']['city']),
  110. 'district' => trim($_GPC['reside']['district']),
  111. 'address' => trim($_GPC['address']),
  112. 'longitude' => trim($_GPC['baidumap']['lng']),
  113. 'latitude' => trim($_GPC['baidumap']['lat']),
  114. 'avg_price' => intval($_GPC['avg_price']),
  115. 'telephone' => trim($_GPC['telephone']),
  116. 'open_time' => trim($_GPC['open_time_start']). '-'.trim($_GPC['open_time_end']),
  117. 'recommend' => trim($_GPC['recommend']),
  118. 'special' => trim($_GPC['special']),
  119. 'introduction' => trim($_GPC['introduction']),
  120. );
  121. if (empty($store_data['business_name'])) {
  122. message('门店名称不能为空');
  123. }
  124. if(empty($store_data['category']['cate'])) {
  125. message('门店类目不能为空');
  126. }
  127. if (empty($store_data['province']) || empty($store_data['city']) || empty($store_data['district']) || empty($store_data['address'])) {
  128. message('请设置门店所在省、市、区及详细地址');
  129. }
  130. if (empty($store_data['longitude']) || empty($store_data['latitude'])) {
  131. message('请选择门店所在地理位置坐标');
  132. }
  133. if (empty($store_data['telephone'])) {
  134. message('门店电话不能为空');
  135. }
  136. if(empty($store_data['open_time'])) {
  137. message('请设置营业时间');
  138. }
  139. if(empty($_GPC['photo_list'])) {
  140. message('门店图片不能为空');
  141. }
  142. foreach($_GPC['photo_list'] as $photourl) {
  143. if (!empty($photourl)) {
  144. $store_data['photo_list'][] = array('photo_url' => $photourl);
  145. }
  146. }
  147. if (!empty($id)) {
  148. $insert = $store_data;
  149. unset($insert['sid']);
  150. $insert['source'] = COUPON_TYPE;
  151. $insert['category'] = iserializer($insert['category']);
  152. $insert['photo_list'] = iserializer($insert['photo_list']);
  153. pdo_update('activity_stores',$insert,array('id' => $id, 'uniacid' => $_W['uniacid']));
  154. message('门店信息更新成功', url('activity/store'), 'success');
  155. } else {
  156. $insert = $store_data;
  157. $insert['uniacid'] = $_W['uniacid'];
  158. $insert['source'] = COUPON_TYPE;
  159. unset($insert['sid']);
  160. $insert['category'] = iserializer($insert['category']);
  161. $insert['photo_list'] = iserializer($insert['photo_list']);
  162. $insert['status'] = 1;
  163. $result = pdo_insert('activity_stores', $insert);
  164. if (COUPON_TYPE == WECHAT_COUPON) {
  165. $insert['status'] = 2;
  166. $store_data['sid'] = pdo_insertid();
  167. $status = $coupon_api->LocationAdd($store_data);
  168. if(is_error($status)) {
  169. pdo_delete('activity_stores', array('uniacid' => $_W['uniacid'], 'id' => $store_data['sid']));
  170. message($status['message'], '', 'error');
  171. }
  172. }
  173. }
  174. message('门店添加成功', url('activity/store'), 'success');
  175. }
  176. }
  177. if($do == 'display') {
  178. $pindex = max(1, intval($_GPC['page']));
  179. $psize = 15;
  180. $limit = 'ORDER BY id DESC LIMIT ' . ($pindex - 1) * $psize . ", {$psize}";
  181. $total = pdo_fetchcolumn('SELECT COUNT(*) FROM '. tablename('activity_stores')." WHERE uniacid = :uniacid AND source = :source", array(':uniacid' => $_W['uniacid'], ':source' => COUPON_TYPE));
  182. $list = pdo_getslice('activity_stores', array('uniacid' => $_W['uniacid'], 'source' => COUPON_TYPE), array($pindex, $psize));
  183. $pager = pagination($total,$pindex,$psize);
  184. foreach($list as &$key) {
  185. $key['category'] = iunserializer($key['category']);
  186. $key['category_'] = implode('-', $key['category']);
  187. }
  188. }
  189. if($do =='delete') {
  190. $id = intval($_GPC['id']);
  191. $count = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('activity_clerks') . ' WHERE uniacid = :uniacid AND storeid = :id', array(':id' => $id, ':uniacid' => $_W['uniacid']));
  192. $count = intval($count);
  193. if($count > 0) {
  194. message("该门店下有{$count}名店员.请将店员变更到其他门店后,再进行删除操作", referer(), 'error');
  195. }
  196. pdo_delete('activity_stores',array('id' => $id, 'uniacid' => $_W['uniacid']));
  197. if (COUPON_TYPE == WECHAT_COUPON) {
  198. $location = pdo_fetch('SELECT status,location_id FROM ' . tablename('activity_stores') . ' WHERE uniacid = :aid AND id = :id', array(':aid' => $_W['uniacid'], ':id' => $id));
  199. if(!empty($location['location_id'])) {
  200. $status = $coupon_api->LocationDel($location['location_id']);
  201. }
  202. if(is_error($status)) {
  203. message("删除本地门店数据成功<br>通过微信接口删除微信门店数据失败,请登陆微信公众平台手动删除门店<br>错误原因:{$status['message']}", url('activity/store/list'), 'error');
  204. }
  205. }
  206. message('删除成功',referer(), 'success');
  207. }
  208. if($do == 'import') {
  209. $begin = intval($_GPC['begin']);
  210. $data = $coupon_api->LocationBatchGet(array('begin' => $begin));
  211. if(is_error($data)) {
  212. message($data['message'], referer(), 'error');
  213. }
  214. if (empty($begin)) {
  215. pdo_delete('activity_stores', array('uniacid' => $_W['uniacid'], 'source' => 2));
  216. }
  217. $location = $data['business_list'];
  218. $status2local = array('', 3, 2, 1, 3);
  219. if(!empty($location)) {
  220. foreach($location as $row) {
  221. $isexist = array();
  222. $li = array();
  223. $li = $row['base_info'];
  224. if($li['available_state'] == 3) {
  225. $isexist = pdo_getcolumn('activity_stores', array('uniacid' => $_W['uniacid'], 'location_id' => $li['poi_id']), 'id');
  226. if(empty($isexist)) {
  227. $li['uniacid'] = $_W['uniacid'];
  228. $li['status'] = 1;
  229. $li['location_id'] = $li['poi_id'];
  230. $category_temp = explode(',', $li['categories'][0]);
  231. $li['category'] = iserializer(array('cate' => $category_temp[0], 'sub' => $category_temp[1], 'clas' => $category_temp[2]));
  232. $li['photo_list'] = iserializer($li['photo_list']);
  233. if(empty($li['sid'])) {
  234. unset($li['sid']);
  235. }
  236. $li['source'] = 2;
  237. unset($li['categories'], $li['poi_id'], $li['update_status'], $li['available_state'], $li['sid'],$li['offset_type']);
  238. pdo_insert('activity_stores', $li);
  239. }
  240. } else {
  241. $select_type == 'sid';
  242. if(!empty($li['sid'])) {
  243. $isexist = pdo_getcolumn('activity_stores', array('uniacid' => $_W['uniacid'], 'id' => $li['sid']), 'id');
  244. }
  245. if(empty($isexist)) {
  246. $select_type = 'location';
  247. $isexist = pdo_get('activity_stores', array('uniacid' => $_W['uniacid'], 'location_id' => $li['poi_id']));
  248. }
  249. $li['uniacid'] = $_W['uniacid'];
  250. $li['status'] = $status2local[$li['available_state']];
  251. $li['location_id'] = $li['poi_id'];
  252. $category_temp = explode(',', $li['categories'][0]);
  253. $li['category'] = iserializer(array('cate' => $category_temp[0], 'sub' => $category_temp[1], 'clas' => $category_temp[2]));
  254. $li['photo_list'] = iserializer($li['photo_list']);
  255. $li['type'] = 2;
  256. $li['source'] = 2;
  257. unset($li['categories'], $li['poi_id'], $li['update_status'], $li['available_state'],$li['offset_type'], $li['sid'], $li['type']);
  258. if(empty($isexist)) {
  259. pdo_insert('activity_stores', $li);
  260. } else {
  261. if($selet_type == 'sid') {
  262. pdo_update('activity_stores', $li, array('uniacid' => $_W['uniacid'], 'id' => $li['sid']));
  263. } else {
  264. pdo_update('activity_stores', $li, array('uniacid' => $_W['uniacid'], 'location_id' => $li['poi_id']));
  265. }
  266. }
  267. }
  268. }
  269. message('正在导入微信门店,请不要关闭浏览器...', url('activity/store/import', array('begin' => $begin + 50)), 'success');
  270. }
  271. message('导入门店成功', url('activity/store/display'), 'success');
  272. }
  273. if($do == 'sync') {
  274. $type = trim($_GPC['type']);
  275. if ($type == '1') {
  276. $cachekey = "storesync:{$_W['uniacid']}";
  277. $cache = cache_delete($cachekey);
  278. }
  279. activity_store_sync();
  280. message(error(0, '更新门店信息成功'), url('activity/store'), 'ajax');
  281. }
  282. template('activity/store');