CommonController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <?php
  2. /**
  3. * 公用API
  4. */
  5. class CommonController extends ApiBaseController {
  6. public function beforeAction($action){
  7. $weixin_use = array('staticsource','getsubcity', 'getallcity');
  8. if(in_array(strtolower($action->id),$weixin_use)){
  9. return true;
  10. }
  11. return $this->verify();
  12. }
  13. //获取全部的城市信息
  14. public function actionGetAllCity(){
  15. $criteria = new EMongoCriteria();
  16. $criteria->sort('parent_province_id', EMongoCriteria::SORT_ASC);
  17. $criteria->sort('parent_city_id', EMongoCriteria::SORT_ASC);
  18. $cursor = CityLib::model()->findAll($criteria);
  19. $res = CommonFn::getRowsFromCursor($cursor);
  20. $province = array();
  21. $city = array();
  22. $area = array();
  23. $keyMap = array();
  24. foreach ($res as $item) {
  25. $keyMap[$item['_id']] = $item['name'];
  26. }
  27. foreach ($res as $item) {
  28. $parent = $item['parent_area_id'];
  29. $level = 4;
  30. if ($parent == 0) {
  31. $parent = $item['parent_city_id'];
  32. $level = 3;
  33. }
  34. if ($parent == 0) {
  35. $parent = $item['parent_province_id'];
  36. $level = 2;
  37. }
  38. if ($parent == 0) {
  39. $level = 1;
  40. }
  41. if ($level == 1) {
  42. $province[] = $item['name'];
  43. } else if ($level == 2) {
  44. $city[$keyMap[$item['parent_province_id']]][] = $item['name'];
  45. } else if ($level == 3) {
  46. $area[$keyMap[$item['parent_city_id']]][] = $item['name'];
  47. }
  48. }
  49. $data = array('p' => $province, 'c' => $city, 'a' => $area);
  50. CommonFn::requestAjax(true, CommonFn::getMessage('message', 'operation_success'), $data, 200);
  51. }
  52. //获取城市级联信息
  53. public function actionGetSubCity(){
  54. $address_info = json_decode(Yii::app()->request->getParam('address_info'),true);
  55. if(!isset($address_info['province']) || $address_info['province'] == '未知'){
  56. $address_info = array();
  57. }
  58. $province = array();
  59. $city = array();
  60. $area = array();
  61. $z_citylib = new ZCityLib();
  62. if(isset($address_info['province'])&&$address_info['province']){
  63. $province = $z_citylib->getSubCity(1);
  64. foreach ($province as $key => $value) {
  65. if($value['name'] == $address_info['province']){
  66. $province[$key]['is_selected'] = 1;
  67. $city = $z_citylib->getSubCity($value['city_code']);
  68. foreach ($city as $sub_key => $sub_value) {
  69. if(isset($address_info['city']) && $sub_value['name'] == $address_info['city']){
  70. $have_selected = true;
  71. $city[$sub_key]['is_selected'] = 1;
  72. $area = $z_citylib->getSubCity($sub_value['city_code']);
  73. foreach ($area as $grand_key => $grand_value) {
  74. if($grand_value['name'] == $address_info['area']){
  75. $area[$grand_key]['is_selected'] = 1;
  76. }else{
  77. $area[$grand_key]['is_selected'] = 0;
  78. }
  79. }
  80. }else{
  81. $city[$sub_key]['is_selected'] = 0;
  82. }
  83. }
  84. if(!isset($have_selected)){
  85. $city[0]['is_selected'] = 1;
  86. $area = $z_citylib->getSubCity($city[0]['city_code']);
  87. foreach ($area as $grand_key => $grand_value) {
  88. if($grand_key == 0){
  89. $area[$grand_key]['is_selected'] = 1;
  90. }else{
  91. $area[$grand_key]['is_selected'] = 0;
  92. }
  93. }
  94. }
  95. }else{
  96. $province[$key]['is_selected'] = 0;
  97. }
  98. }
  99. }else{
  100. $province = $z_citylib->getSubCity(1);
  101. foreach ($province as $key => $value) {
  102. if($value['name'] == '上海市'){
  103. $province[$key]['is_selected'] = 1;
  104. }else{
  105. $province[$key]['is_selected'] = 0;
  106. }
  107. }
  108. foreach ($province as $key => $value) {
  109. if($value['name'] == '上海市'){
  110. $city = $z_citylib->getSubCity($value['city_code']);
  111. foreach ($city as $sub_key => $sub_value) {
  112. if($sub_value['name'] == '上海市'){
  113. $city[$sub_key]['is_selected'] = 1;
  114. }else{
  115. $city[$sub_key]['is_selected'] = 0;
  116. }
  117. }
  118. $area = $z_citylib->getSubCity($city[0]['city_code']);
  119. foreach ($area as $sub_key => $sub_value) {
  120. if($sub_value['name'] == '上海市'){
  121. $area[$sub_key]['is_selected'] = 1;
  122. }else{
  123. $area[$sub_key]['is_selected'] = 0;
  124. }
  125. }
  126. }
  127. }
  128. }
  129. //容错处理,防止客户端上传错误城市
  130. if(empty($province)){
  131. $province = $z_citylib->getSubCity(1);
  132. foreach ($province as $key => $value) {
  133. if($value['name'] == '上海市'){
  134. $province[$key]['is_selected'] = 1;
  135. }else{
  136. $province[$key]['is_selected'] = 0;
  137. }
  138. }
  139. }
  140. if(empty($city)){
  141. foreach ($province as $key => $value) {
  142. if($value['is_selected'] == 1){
  143. $temp_code = $value['city_code'];
  144. }
  145. }
  146. $city = $z_citylib->getSubCity($temp_code);
  147. if(empty($city)){
  148. $temp['name'] = '';
  149. $city[] = $temp;
  150. }
  151. $city[0]['is_selected'] = 1;
  152. }
  153. if(empty($area)){
  154. foreach ($city as $key => $value) {
  155. if($value['is_selected'] == 1){
  156. $temp_code = $value['city_code'];
  157. }
  158. }
  159. $area = $z_citylib->getSubCity($temp_code);
  160. if(empty($area)){
  161. $temp['name'] = '';
  162. $area[] = $temp;
  163. }
  164. $area[0]['is_selected'] = 1;
  165. }
  166. foreach ($province as $key => $value) {
  167. unset($province[$key]['city_code']);
  168. }
  169. foreach ($city as $key => $value) {
  170. unset($city[$key]['city_code']);
  171. }
  172. foreach ($area as $key => $value) {
  173. unset($area[$key]['city_code']);
  174. }
  175. $data['provinces'] = $province;
  176. $data['citys'] = $city;
  177. $data['areas'] = $area;
  178. CommonFn::requestAjax(true,CommonFn::getMessage('message','operation_success'),$data,200);
  179. }
  180. public function actionPay(){
  181. $pay_channel = Yii::app()->getRequest()->getParam("pay_channel");
  182. $order_id = Yii::app()->getRequest()->getParam("order_id");
  183. $callback = Yii::app()->getRequest()->getParam("callback");
  184. $amount = 1;
  185. $result = Service::factory('PayService')->Pay($pay_channel,$amount);
  186. if($callback){
  187. echo $callback."($result)";
  188. die();
  189. }
  190. echo $result;
  191. }
  192. public function actionRetrieve(){
  193. $charge_id = Yii::app()->getRequest()->getParam("charge_id");
  194. $result = Service::factory('PayService')->retrieve($charge_id);
  195. echo $result;
  196. }
  197. public function actionGetUrl(){
  198. $url_id = Yii::app()->getRequest()->getParam("url_id");
  199. $data['url'] = Service::factory('VariableService')->getVariable($url_id);
  200. $success = empty($data) ? false : true;
  201. $message = $success ? '' : 'URL不存在';
  202. CommonFn::requestAjax($success, $message, $data);
  203. }
  204. public function actionCustomeParam() {
  205. $user_id = Yii::app()->request->getParam('user_id', '');
  206. $device_id = Yii::app()->request->getParam('device_id');
  207. $data['new_user_coupons_value'] = Yii::app()->params['new_user_coupons_value']?Yii::app()->params['new_user_coupons_value']:0;
  208. if($user_id){
  209. $cache = new ARedisCache();
  210. $key = 'new_user_coupons_check'.$user_id;
  211. $new_user_coupons_check = $cache->get($key);
  212. if(!$new_user_coupons_check){
  213. if(CommonFn::isMongoId($user_id)){
  214. $user_id = new MongoId($user_id);
  215. $criteria = new EMongoCriteria();
  216. $criteria->user('==',$user_id);
  217. $coupons = UserCoupon::model()->count($criteria);
  218. if($coupons<1){
  219. $data['get_user_coupons'] = 1;
  220. }else{
  221. $data['get_user_coupons'] = 0;
  222. }
  223. }else{
  224. $data['get_user_coupons'] = 0;
  225. }
  226. }else{
  227. $data['get_user_coupons'] = 0;
  228. }
  229. }elseif ($device_id) {
  230. $cache = new ARedisCache();
  231. $key = 'new_user_coupons_check'.$device_id;
  232. $new_user_coupons_check = $cache->get($key);
  233. if(!$new_user_coupons_check){
  234. $criteria = new EMongoCriteria();
  235. $criteria->user_device_id('==',$device_id);
  236. $coupons = UserCoupon::model()->count($criteria);
  237. if($coupons<1){
  238. $data['get_user_coupons'] = 1;
  239. }else{
  240. $data['get_user_coupons'] = 0;
  241. }
  242. }
  243. }else{
  244. $data['get_user_coupons'] = 0;
  245. }
  246. CommonFn::requestAjax(true,'success', $data);
  247. }
  248. public function actionStaticSource() {
  249. $key = Yii::app()->request->getParam('key');
  250. $static = StaticSource::getByKey($key);
  251. if (!$static) {
  252. CommonFn::requestAjax(false, '资源不存在', []);
  253. } else {
  254. CommonFn::requestAjax(true, '', [
  255. 'id' => (string)$static->_id,
  256. 'key' => $static->key,
  257. 'title' => $static->title? $static->title : '',
  258. 'content' => $static->content,
  259. ]);
  260. }
  261. }
  262. }