BaseModel.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /**
  3. * @link:http://www.goodmall.com/
  4. * @copyright: Copyright (c) 2020
  5. * 基础model
  6. * Author: james
  7. * Date: 2020-04-08
  8. * Time: 15:12
  9. */
  10. namespace addons\Store\common\models;
  11. // use app\core\ApiCode;
  12. class BaseModel extends \yii\base\Model
  13. {
  14. const YES = 1;
  15. const NO = 0;
  16. const IS_DELETE_YES = self::YES;
  17. const IS_DELETE_NO = self::NO;
  18. protected $sign = '';
  19. public $result = [];
  20. /**
  21. * 返回错误数据数组
  22. * @param array $model
  23. * @return array
  24. */
  25. public function responseErrorInfo($model = [])
  26. {
  27. if (!$model) {
  28. $model = $this;
  29. }
  30. $msg = isset($model->errors) ? current($model->errors)[0] : '数据异常!';
  31. return [
  32. 'code' => 1,
  33. 'msg' => $msg
  34. ];
  35. }
  36. /**
  37. * 返回错误数据文本信息
  38. * @param array $model
  39. * @return string
  40. */
  41. public function responseErrorMsg($model = null)
  42. {
  43. if (!$model) {
  44. $model = $this;
  45. }
  46. // $msg = isset($model->errors) ? current($model->errors)[0] : '数据异常!';
  47. if(!empty(current($model->errors))){
  48. $msg = current($model->errors)[0];
  49. }else{
  50. $msg = '数据异常!';
  51. }
  52. return $msg;
  53. }
  54. public function setSign($val)
  55. {
  56. $this->sign = $val;
  57. return $this;
  58. }
  59. /**
  60. * 组装返回的数组
  61. * @param $code
  62. * @param $msg
  63. * @param array $data
  64. * @return array
  65. */
  66. public function returnApiResultData($code = 999,$msg = null,$data = []){
  67. if($code == 0){
  68. if(empty($msg)){
  69. $msg = "数据请求成功";
  70. }
  71. }else{
  72. if(empty($msg)){
  73. $msg = "数据请求失败";
  74. }
  75. }
  76. $this->result["code"] = $code;
  77. $this->result["msg"] = $msg;
  78. if(!empty($data)){
  79. $this->result["data"] = $data;
  80. }
  81. //系统错误或数据验证错误
  82. if($code == 999){
  83. $this->result = $this->responseErrorInfo($data);
  84. }
  85. return $this->result;
  86. }
  87. /**
  88. * 获取分页数据
  89. * @param $pagination
  90. * @return array
  91. */
  92. public function getPaginationInfo($pagination){
  93. $pageData = [];
  94. $pageData["total_count"] = $pagination->total_count;
  95. $pageData["page_count"] = $pagination->page_count;
  96. $pageData["pageSize"] = $pagination->pageSize;
  97. $pageData["current_page"] = $pagination->current_page;
  98. return $pageData;
  99. }
  100. /**
  101. * 表单并发限制提交
  102. * @param int $cacheKey 缓存key
  103. * @param string $cacheValue 绑在值
  104. * @param int $duration 当前缓存过期时间,默认为5秒
  105. * @return bool
  106. */
  107. public function formLimitSubmis($cacheKey = 0, $cacheValue = '', $duration = 5)
  108. {
  109. if(empty($cacheKey)){
  110. return false;
  111. }
  112. //并发关键值
  113. empty($cacheValue) ? $cacheValue = md5($cacheKey) : '';
  114. $cacheData = \Yii::$app->cache->get($cacheKey);
  115. //若在缓存内缓存值与并发值相同,即当前属于并发情况返回false
  116. if($cacheData && ($cacheData == $cacheValue)){
  117. return false;
  118. }
  119. $cacheData = \Yii::$app->cache->set($cacheKey, $cacheValue, $duration);
  120. if(!$cacheData){
  121. return false;
  122. }
  123. return true;
  124. }
  125. /**
  126. * 获取插件sign标记
  127. * @return mixed
  128. */
  129. public function getPluginSign()
  130. {
  131. $currentPlugin = \Yii::$app->plugin->getCurrentPlugin();
  132. try{
  133. $sign = $currentPlugin->getSignName();
  134. }catch (\Exception $exception){
  135. $sign = $currentPlugin->getName();
  136. }
  137. return $sign;
  138. }
  139. }