EActiveRecord.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <?php
  2. //model 的重写基类
  3. namespace app\common\models;
  4. use app\models\Linkmenu;
  5. use app\models\ContentModel;
  6. use app\modules\admin\models\Sysconfig;
  7. use app\modules\admin\models\Role;
  8. use app\modules\ucenter\models\UserGroup;
  9. use app\modules\ad\models\AdInd;
  10. use app\modules\ad\models\AdSpace;
  11. use Yii;
  12. use yii\db\ActiveRecord;
  13. class EActiveRecord extends ActiveRecord
  14. {
  15. public static function shortTableName()
  16. {
  17. $table_name = self::tableName();
  18. return str_replace(array('{{%','}}'),"",$table_name);
  19. }
  20. /**
  21. * 获取模型验证的第一个错误
  22. */
  23. public function returnFirstError()
  24. {
  25. $errors = $this->getErrors();
  26. if(!empty($errors)){
  27. $firstError = array_splice($errors, 0,1);
  28. $attribute = key($firstError);
  29. return $this->getFirstError($attribute);
  30. }
  31. return null;
  32. }
  33. //获取最后插入ID
  34. public static function getLastInsertId()
  35. {
  36. return Yii::$app->db->getLastInsertID();
  37. }
  38. /**
  39. * @返回主键.
  40. */
  41. public static function modelPrimaryKey()
  42. {
  43. $pkInfo = parent::primaryKey();
  44. return $pkInfo[0];
  45. }
  46. //禁用状态
  47. public static function disableOptions($k=null)
  48. {
  49. $options = array('1'=>'禁用','0'=>'启用');
  50. if(is_numeric($k))return $options[$k];
  51. return $options;
  52. }
  53. //是否选择
  54. public static function boolOptions($k=null)
  55. {
  56. $options = array('1'=>'是','0'=>'否');
  57. if(is_numeric($k))return $options[$k];
  58. return $options;
  59. }
  60. //返回某个表(传入表名不含前缀)的主键
  61. public static function getPrikey($table=null)
  62. {
  63. if($table==null)$table = self::shortTableName();
  64. //查询表的主键
  65. $dsnInfo = explode(";",Yii::$app->db->dsn);
  66. $dbInfo = explode("=",$dsnInfo['1']);
  67. //$sql = "SELECT k.column_name,k.table_name FROM information_schema.table_constraints t JOIN information_schema.key_column_usage k USING ( constraint_name, table_schema, table_name ) WHERE t.constraint_type = 'PRIMARY KEY' AND k.table_name = '".Yii::$app->db->tablePrefix.$table."' AND t.table_schema = '$dbInfo[1]'";
  68. $sql = "SELECT column_name FROM information_schema.key_column_usage WHERE table_name='".Yii::$app->db->tablePrefix.$table."' AND constraint_name LIKE '%_pkey'";
  69. $result = Yii::$app->db->createCommand($sql)->queryOne();
  70. return $result['column_name'];
  71. }
  72. //返回属性名称
  73. public static function getAttributeName($field)
  74. {
  75. if($field=='operate')
  76. {
  77. return Yii::t('attr',$field);
  78. }
  79. else
  80. {
  81. $table_name = self::shortTableName();
  82. return Yii::t('attr',$table_name.'.'.$field);
  83. }
  84. }
  85. //后台角色
  86. public static function roleOptions($k=0)
  87. {
  88. $resultList = Role::find()->where("disabled=0")->all();
  89. foreach($resultList as $result)
  90. {
  91. $options[$result->role_id] = $result->role_name;
  92. }
  93. if($k>0)return $options[$k];
  94. return $options;
  95. }
  96. //联动菜单
  97. public static function linkmenuOptions($k=0)
  98. {
  99. $resultList = Linkmenu::find()->where("key_id=0")->all();
  100. foreach($resultList as $result)
  101. {
  102. $options[$result->menu_id] = $result->name;
  103. }
  104. if($k>0)return $options[$k];
  105. return $options;
  106. }
  107. //系统参数选项
  108. public static function sysconfigOptions($name,$k=null)
  109. {
  110. $sysconfig = Sysconfig::find()->where("name='$name'")->one();
  111. $resultList = json_decode($sysconfig->value,true);
  112. if($sysconfig->type==1)
  113. {
  114. if(is_array($resultList))foreach($resultList as $id=>$name)
  115. {
  116. $options[$id] = $name;
  117. }
  118. if(!is_null($k))return $options[$k];
  119. }
  120. else
  121. {
  122. if(is_array($resultList))foreach($resultList as $name)
  123. {
  124. $options[] = $name;
  125. }
  126. if($k!='') return $k;
  127. }
  128. return $options;
  129. }
  130. //是否选择(字段管理页面)
  131. public static function fieldBoolOptions($k=null)
  132. {
  133. $options = array('0'=>'否','1'=>'是');
  134. if(is_numeric($k))return $options[$k];
  135. return $options;
  136. }
  137. //返回广告行业选项
  138. public function indOptions($k=NULL)
  139. {
  140. $resultList = AdInd::find()->where("disabled=0")->orderBy(['list_order'=>SORT_ASC])->all();
  141. if(is_array($resultList))foreach($resultList as $result)
  142. {
  143. $options[$result->ind_id] = $result->name;
  144. }
  145. if($k>0)return $options[$k];
  146. return $options;
  147. }
  148. //返回广告位选项
  149. public function adspaceOptions($k=NULL)
  150. {
  151. $resultList = AdSpace::find()->where("disabled=0")->orderBy(['list_order'=>SORT_ASC])->all();
  152. if(is_array($resultList))foreach($resultList as $result)
  153. {
  154. $options[$result->space_id] = $result->name."(".$result->space_no.")";
  155. }
  156. if($k>0)return $options[$k];
  157. return $options;
  158. }
  159. //返回用户组选项
  160. public static function userGroupOptions($k=NULL)
  161. {
  162. $resultList = UserGroup::find()->where("disabled=0")->orderBy(['list_order'=>SORT_ASC])->all();
  163. if(is_array($resultList))foreach($resultList as $result)
  164. {
  165. $options[$result->group_id] = $result->group_name;
  166. }
  167. if($k>0)return $options[$k];
  168. return $options;
  169. }
  170. //返回VIP类型选择
  171. public static function viptypeOptions($k=null,$cat=null)
  172. {
  173. if($cat==null)
  174. {
  175. $vipTypeList = \app\modules\ucenter\models\UserVipType::find()->where("status=1")->orderBy(['vip_cat'=>SORT_ASC,'list_order'=>SORT_ASC])->all();
  176. }
  177. else
  178. {
  179. $vipTypeList = \app\modules\ucenter\models\UserVipType::find()->where("vip_cat='".$cat."' and status=1")->orderBy(['list_order'=>SORT_ASC])->all();
  180. }
  181. if(is_array($vipTypeList))foreach($vipTypeList as $vipType)
  182. {
  183. $options[$vipType->id] = $vipType->title;
  184. }
  185. if($k>0)return $options[$k];
  186. return $options;
  187. }
  188. //返回用户等级选项
  189. public static function userGroupLevelOptions($group_id,$k=NULL)
  190. {
  191. $levelList = \app\modules\ucenter\models\UserGroupLevel::find()->where("user_group_id=".$group_id."")->orderBy(['id'=>SORT_ASC])->all();
  192. $options = [];
  193. if(is_array($levelList))foreach($levelList as $levelInfo)
  194. {
  195. $options[$levelInfo['id']] = $levelInfo['name'];
  196. }
  197. if($k>0)return $options[$k];
  198. return $options;
  199. }
  200. //返回用户模型选项
  201. public static function userModelOptions($k=NULL)
  202. {
  203. $resultList = ContentModel::find()->where("type=2 and disabled=0")->all();
  204. if(is_array($resultList))foreach($resultList as $result)
  205. {
  206. $options[$result->model_id] = $result->name;
  207. }
  208. if($k>0)return $options[$k];
  209. return $options;
  210. }
  211. //返回当前模型ID
  212. public static function getContentModelId()
  213. {
  214. $table_name = self::shortTableName();
  215. $contentModel = ContentModel::find()->where("table_name='".$table_name."'")->one();
  216. return $contentModel->model_id;
  217. }
  218. //返回标签链接
  219. public static function getTagLinksByModel($tags,$model_id)
  220. {
  221. if(empty($tags))return[];
  222. $tags = explode(",",$tags);
  223. if(is_array($tags))foreach($tags as $tag)
  224. {
  225. $tagModel = \app\models\Tag::find()->where("tag='".$tag."'")->one();
  226. if(!empty($tagModel))
  227. {
  228. $links[] = ['title'=>$tagModel->tag,'link'=>\app\common\components\SiteUrl::tagLink($tagModel->pinyin,$model_id)];
  229. }
  230. }
  231. return $links;
  232. }
  233. //返回内容首个标签
  234. public static function getFirstTag($tags)
  235. {
  236. $tags = explode(",",$tags);
  237. return $tags[0]?$tags[0]:'无';
  238. }
  239. //文本型选项
  240. public static function boxOptions($str,$k=null)
  241. {
  242. $typeList = explode("\n",$str);
  243. if(!empty($typeList))foreach($typeList as $_k) {
  244. $v = explode("|",$_k);
  245. if(!empty($v[0])){
  246. $options[trim($v[1])] = $v[0];
  247. }
  248. }
  249. if($k>0)return $options[$k];
  250. return $options;
  251. }
  252. //售后步骤
  253. public static function refundStepOptions($type,$k=null)
  254. {
  255. $type==1?$options = array('1'=>'申请退货退款','2'=>'卖家处理','3'=>'买家寄回商品','4'=>'买家收货','5'=>'退款完毕'):$options = array('1'=>'申请退款','2'=>'卖家处理','3'=>'退款完毕');
  256. if(is_numeric($k))return $options[$k];
  257. return $options;
  258. }
  259. }