BaseAjaxController.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <?php
  2. namespace common\components;
  3. use common\services\AdminLogService;
  4. use common\util\ActiveRecordHelper;
  5. use frontend\modules\api\components\BaseAdminController;
  6. use Yii;
  7. use yii\base\InvalidConfigException;
  8. use yii\helpers\ArrayHelper;
  9. use yii\web\Controller;
  10. use yii\db\ActiveQuery;
  11. use yii\db\ActiveRecord;
  12. class BaseAjaxController extends Controller
  13. {
  14. use ActiveRecordHelper;
  15. public $enableCsrfValidation = false;
  16. public function runAction($id, $params = [])
  17. {
  18. $params = ArrayHelper::merge(Yii::$app->request->post(), $params);
  19. if ($this instanceof BaseAdminController) {
  20. //update
  21. $url = substr(Yii::$app->controller->getRoute() . "/" . $id, strlen($this->module->id));
  22. $adminLog = AdminLogService::saveLog($this->userInfo, $url, $params);
  23. if (in_array($url, AdminLogService::$staffTitle)) {
  24. if ($adminLog) {
  25. $_GET['adminLogId'] = $adminLog->id;
  26. $_GET['adminLogModel'] = $adminLog;
  27. }
  28. }
  29. }
  30. return parent::runAction($id, $params);
  31. }
  32. public function asJson($data = [], $r = 0, $msg = "成功")
  33. {
  34. if ($r !== 0) {
  35. $resultData['r'] = 1;
  36. } else {
  37. $resultData['r'] = 0;
  38. }
  39. if ($msg !== "成功") {
  40. $resultData['msg'] = $msg;
  41. } else {
  42. $resultData['msg'] = "成功";
  43. }
  44. $resultData['data'] = $data;
  45. return parent::asJson($resultData);
  46. }
  47. /**
  48. * 从Get参数里增加检索条件
  49. */
  50. public function addJoinConditionToQuery($name, $getName, $notEqual, ActiveQuery $query, $isLike = true)
  51. {
  52. if (isset($_GET[$getName]) && $_GET[$getName] != $notEqual) {
  53. if ($isLike) {
  54. $query = $query->andWhere(["like", $name, $_GET[$getName]]);
  55. } else {
  56. $query = $query->andWhere([$name => $_GET[$getName]]);
  57. }
  58. }
  59. return $query;
  60. }
  61. public function addArrayConditionToQuery($name, ActiveQuery $query, $unionArray = [])
  62. {
  63. if ($unionArray != []) {
  64. $query = $query->andWhere([$name => $unionArray]);
  65. }
  66. return $query;
  67. }
  68. /**
  69. * @param $name
  70. * @param ActiveQuery $query
  71. * @param bool $isLike
  72. * @return ActiveQuery
  73. */
  74. public function addConditionToQuery($name, ActiveQuery $query, bool $isLike = false): ActiveQuery
  75. {
  76. $paramsValue = $this->getParams($name);
  77. if ($paramsValue === null || $paramsValue === "") {
  78. return $query;
  79. }
  80. if ($isLike) {
  81. $query = $query->andWhere(["like", $name, $paramsValue]);
  82. } else {
  83. $query = $query->andWhere([$name => $paramsValue]);
  84. }
  85. return $query;
  86. }
  87. /**
  88. * @param $name
  89. * @param ActiveQuery $query
  90. * @param bool $isLike
  91. * @return ActiveQuery
  92. * @throws InvalidConfigException
  93. */
  94. public function addConditionToQueryNew($name, $paramName, ActiveQuery $query, bool $isLike = false): ActiveQuery
  95. {
  96. $paramsValue = $this->getParams($paramName);
  97. if ($paramsValue === null || $paramsValue === "") {
  98. return $query;
  99. }
  100. if ($isLike) {
  101. $query = $query->andWhere(["like", $name, $paramsValue]);
  102. } else {
  103. $query = $query->andWhere([$name => $paramsValue]);
  104. }
  105. return $query;
  106. }
  107. /**
  108. * 从Get和Post参数里增加检索条件
  109. * @param $name
  110. * @param ActiveQuery $query
  111. * @param bool $timestamp
  112. * @param array $values
  113. * @return ActiveQuery
  114. * @throws InvalidConfigException
  115. */
  116. public function addStartEndConditionToQueryNew($name, $paramName, ActiveQuery $query, bool $timestamp = false, array $values = []): ActiveQuery
  117. {
  118. $exist = true;
  119. $array = [];
  120. if ($values === []) {
  121. $values = $this->getParams($paramName);
  122. }
  123. if (isset($values[0]) && isset($values[1])) {
  124. $array = $values;
  125. } else {
  126. $exist = false;
  127. }
  128. if ($exist) {
  129. if ($timestamp === true) {
  130. $array[0] = strtotime($array[0]);
  131. $array[1] = strtotime($array[1]);//替换掉
  132. }
  133. $query = $query->andWhere([">=", $name, $array[0]])->andWhere(["<=", $name, $array[1]]);
  134. }
  135. return $query;
  136. }
  137. /**
  138. * 从Get和Post参数里增加检索条件
  139. * @param $name
  140. * @param ActiveQuery $query
  141. * @param bool $timestamp
  142. * @param array $values
  143. * @return ActiveQuery
  144. */
  145. public function addStartEndConditionToQuery($name, ActiveQuery $query, bool $timestamp = false, array $values = []): ActiveQuery
  146. {
  147. $exist = true;
  148. $array = [];
  149. if ($values === []) {
  150. $values = $this->getParams($name);
  151. }
  152. if (isset($values[0]) && isset($values[1])) {
  153. $array = $values;
  154. } else {
  155. $exist = false;
  156. }
  157. if ($exist) {
  158. if ($timestamp === true) {
  159. $array[0] = strtotime($array[0]);
  160. $array[1] = strtotime($array[1]);//替换掉
  161. }
  162. $query = $query->andWhere([">=", $name, $array[0]])->andWhere(["<=", $name, $array[1]]);
  163. }
  164. return $query;
  165. }
  166. public function log($info)
  167. {
  168. Yii::info($info, get_class($this));
  169. }
  170. public function returnError($msg, $r = -1)
  171. {
  172. return parent::asJson(['r' => $r, 'msg' => $msg, 'data' => []]);
  173. }
  174. /**
  175. * 生成唯一字符串
  176. * @return string
  177. */
  178. function uniqueNum(): string
  179. {
  180. return strtoupper(dechex(date('m'))) . date(
  181. 'd'
  182. ) . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf(
  183. '%02d',
  184. rand(111, 999)
  185. );
  186. }
  187. /**
  188. * 从Get参数里增加更新内容(单一)
  189. */
  190. public function addUpdateToModel(ActiveRecord $model, $name): void
  191. {
  192. $value = $this->getParams($name);
  193. if ($value && $model->hasAttribute($name)) {
  194. $model->setAttribute($name, $value);
  195. }
  196. }
  197. /**
  198. * @return string
  199. */
  200. public function getImageBasePath()
  201. {
  202. $http = $this->is_ssl() ? 'https://' : 'http://';
  203. if (YII_ENV_DEV) {
  204. $host = $_SERVER['HTTP_HOST'];
  205. } else {
  206. //案例库存储服务器
  207. $host = "172.20.41.109:8080";
  208. }
  209. return $http . $host;
  210. }
  211. /**
  212. * 判断是否SSL协议
  213. * @return bool
  214. */
  215. public function is_ssl()
  216. {
  217. if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] = "https") {
  218. return true;
  219. } else if (isset($_SERVER['HTTPS']) && ('1' == $_SERVER['HTTPS'] || 'on' == strtolower($_SERVER['HTTPS']))) {
  220. return true;
  221. } elseif (isset($_SERVER['SERVER_PORT']) && ('443' == $_SERVER['SERVER_PORT'])) {
  222. return true;
  223. }
  224. return false;
  225. }
  226. /**
  227. * @param ActiveQuery $query
  228. * @param string $orderBy
  229. * @param string $sequenceKey
  230. * @return void
  231. */
  232. public function initSequence(ActiveQuery $query, string $orderBy = "id desc", string $sequenceKey = "sequence"): void
  233. {
  234. $sequenceRaw = $this->getParams($sequenceKey);
  235. if (!$sequenceRaw) {
  236. $query->orderBy($orderBy);
  237. return;
  238. }
  239. $sequence = is_array($sequenceRaw) ? $sequenceRaw : json_decode($sequenceRaw, true);
  240. if (!is_null($sequence['prop'])) {
  241. if (isset($sequence['order']) && $sequence['order'] == 'descending') {
  242. $sort = 'desc';
  243. } else {
  244. $sort = 'asc';
  245. }
  246. if ($sequence['prop'] === "start_time" && $sort === "desc") {
  247. $query->orderBy("start_time desc,id desc");
  248. } else {
  249. $query->orderBy("{$sequence['prop']} {$sort}");
  250. }
  251. } else {
  252. if ($orderBy === "start_time desc") {
  253. $orderBy = "start_time desc,id desc";
  254. }
  255. $query->orderBy($orderBy);
  256. }
  257. }
  258. }