| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- <?php
- namespace common\traits;
- use app\core\ApiCode;
- use common\enums\AddonsEnum;
- use common\enums\ApiStatusEnum;
- use common\helpers\ArrayHelper;
- use Yii;
- use yii\web\Response;
- trait BaseControllerTrait
- {
- /**
- *
- * 请求成功统消息格式化处理
- * @Author bing
- * @DateTime 2020-10-27 15:43:16
- * @copyright: Copyright (c) 2020 广东七件事集团
- * @param string $msg
- * @param array $data
- * @param [type] $code
- * @return void|mixed
- */
- public function success($msg = '操作成功', $data = [], $code = ApiStatusEnum::CODE_SUCCESS, $flag = JSON_NUMERIC_CHECK)
- {
- if (is_object($data)) $data = ArrayHelper::toArray($data);
- $data = $this->_utf8ize($data);
- $ret = json_encode(array(
- 'code' => $code,
- 'msg' => $msg,
- 'data' => $data
- ), $flag | JSON_UNESCAPED_UNICODE);
- if (property_exists($this, 'mall_id') && strpos($_SERVER['DOCUMENT_ROOT'], "/api/web") !== false &&
- !str_ends_with($_SERVER['REQUEST_URI'], '/mall/config')) { // 仅限API前端
- if (str_starts_with($_SERVER['REQUEST_URI'], '/store/')) { // 门店API请求
- $basic = \Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_STORE, 'basic');
- } else {
- $basic = Yii::$app->services->setting->mall->get($this->mall_id, 'basic');
- }
- $dollars_symbol = !empty($basic['is_enable_dollars_symbol']) ? $basic['dollars_symbol'] : '¥';
- $ret = str_replace('¥', $dollars_symbol, $ret);
- }
- Yii::$app->controller->asJson(json_decode($ret, true))->send();
- }
- /**
- * 请求失败消息格式化处理
- * @Author bing
- * @DateTime 2020-10-27 15:44:05
- * @copyright: Copyright (c) 2020 广东七件事集团
- * @param string $msg
- * @param array $data
- * @param [type] $code
- * @return void|mixed
- */
- public function error($msg = '操作失败', $data = [], $code = ApiStatusEnum::CODE_FAIL, $flag = JSON_NUMERIC_CHECK)
- {
- if (is_object($data)) $data = ArrayHelper::toArray($data);
- Yii::$app->controller->asJson(json_decode(json_encode(array(
- 'code' => $code,
- 'msg' => $msg,
- 'data' => $data
- ), $flag), true));
- }
- /**
- * 检查必须传送的参数
- * @Author bing
- * @DateTime 2021-08-12 18:40:15
- * @copyright: Copyright (c) 2020 广东七件事集团
- * @param [type] $parmas
- * @param array $require_key
- * @return void
- */
- public static function checkRequireParam($parmas, $require_key = [])
- {
- foreach ($require_key as $key) {
- if (!isset($parmas[$key])) {
- self::setStaticError('缺少必填参数' . $key);
- return false;
- }
- }
- return true;
- }
- /**
- * 返回字符串
- * @Author: lun
- * @DateTime: 2023/3/25 0025 9:26
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param string $msg
- * @param array $data
- * @param int $code
- * @return void|mixed
- */
- public function successStr($msg = '操作成功', $data = [], $code = ApiStatusEnum::CODE_SUCCESS)
- {
- if (is_object($data)) $data = ArrayHelper::toArray($data);
- Yii::$app->controller->asJson(json_decode(json_encode(array(
- 'code' => $code,
- 'msg' => $msg,
- 'data' => $data
- )), true))->send();
- }
- /**
- * 递归转换所有字符串为UTF-8
- *
- * @param array $data
- * @return array
- */
- private function _utf8ize($data)
- {
- if (is_array($data)) {
- foreach ($data as $k => $v) {
- $data[$k] = $this->_utf8ize($v);
- }
- } elseif (is_string($data) && !mb_check_encoding($data, 'UTF-8')) {
- return mb_convert_encoding($data, 'UTF-8', 'auto');
- }
- return $data;
- }
- }
|