| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- <?php
- /**
- * @link:http://www.goodmall.com/
- * @copyright: Copyright (c) 2020
- * 基础model
- * Author: james
- * Date: 2020-04-08
- * Time: 15:12
- */
- namespace addons\Store\common\models;
- // use app\core\ApiCode;
- class BaseModel extends \yii\base\Model
- {
- const YES = 1;
- const NO = 0;
- const IS_DELETE_YES = self::YES;
- const IS_DELETE_NO = self::NO;
- protected $sign = '';
- public $result = [];
- /**
- * 返回错误数据数组
- * @param array $model
- * @return array
- */
- public function responseErrorInfo($model = [])
- {
- if (!$model) {
- $model = $this;
- }
- $msg = isset($model->errors) ? current($model->errors)[0] : '数据异常!';
- return [
- 'code' => 1,
- 'msg' => $msg
- ];
- }
- /**
- * 返回错误数据文本信息
- * @param array $model
- * @return string
- */
- public function responseErrorMsg($model = null)
- {
- if (!$model) {
- $model = $this;
- }
- // $msg = isset($model->errors) ? current($model->errors)[0] : '数据异常!';
- if(!empty(current($model->errors))){
- $msg = current($model->errors)[0];
- }else{
- $msg = '数据异常!';
- }
- return $msg;
- }
- public function setSign($val)
- {
- $this->sign = $val;
- return $this;
- }
- /**
- * 组装返回的数组
- * @param $code
- * @param $msg
- * @param array $data
- * @return array
- */
- public function returnApiResultData($code = 999,$msg = null,$data = []){
- if($code == 0){
- if(empty($msg)){
- $msg = "数据请求成功";
- }
- }else{
- if(empty($msg)){
- $msg = "数据请求失败";
- }
- }
- $this->result["code"] = $code;
- $this->result["msg"] = $msg;
- if(!empty($data)){
- $this->result["data"] = $data;
- }
- //系统错误或数据验证错误
- if($code == 999){
- $this->result = $this->responseErrorInfo($data);
- }
- return $this->result;
- }
- /**
- * 获取分页数据
- * @param $pagination
- * @return array
- */
- public function getPaginationInfo($pagination){
- $pageData = [];
- $pageData["total_count"] = $pagination->total_count;
- $pageData["page_count"] = $pagination->page_count;
- $pageData["pageSize"] = $pagination->pageSize;
- $pageData["current_page"] = $pagination->current_page;
- return $pageData;
- }
- /**
- * 表单并发限制提交
- * @param int $cacheKey 缓存key
- * @param string $cacheValue 绑在值
- * @param int $duration 当前缓存过期时间,默认为5秒
- * @return bool
- */
- public function formLimitSubmis($cacheKey = 0, $cacheValue = '', $duration = 5)
- {
- if(empty($cacheKey)){
- return false;
- }
- //并发关键值
- empty($cacheValue) ? $cacheValue = md5($cacheKey) : '';
- $cacheData = \Yii::$app->cache->get($cacheKey);
- //若在缓存内缓存值与并发值相同,即当前属于并发情况返回false
- if($cacheData && ($cacheData == $cacheValue)){
- return false;
- }
- $cacheData = \Yii::$app->cache->set($cacheKey, $cacheValue, $duration);
- if(!$cacheData){
- return false;
- }
- return true;
- }
- /**
- * 获取插件sign标记
- * @return mixed
- */
- public function getPluginSign()
- {
- $currentPlugin = \Yii::$app->plugin->getCurrentPlugin();
- try{
- $sign = $currentPlugin->getSignName();
- }catch (\Exception $exception){
- $sign = $currentPlugin->getName();
- }
- return $sign;
- }
- }
|