123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- <?php
- /**
- *
- * @author: xaboy<365615158@qq.com>
- * @day: 2017/11/02
- */
- namespace crmeb\basic;
- use think\facade\Db;
- use think\Model;
- class BaseModel extends Model
- {
- private static $errorMsg;
- private static $transaction = 0;
- private static $DbInstance = [];
- const DEFAULT_ERROR_MSG = '操作失败,请稍候再试!';
- /**
- * 设置错误信息
- * @param string $errorMsg
- * @return bool
- */
- protected static function setErrorInfo($errorMsg = self::DEFAULT_ERROR_MSG, $rollback = false)
- {
- if ($rollback) self::rollbackTrans();
- self::$errorMsg = $errorMsg;
- return false;
- }
- /**
- * 获取错误信息
- * @param string $defaultMsg
- * @return string
- */
- public static function getErrorInfo($defaultMsg = self::DEFAULT_ERROR_MSG)
- {
- return !empty(self::$errorMsg) ? self::$errorMsg : $defaultMsg;
- }
- /**
- * 开启事务
- */
- public static function beginTrans()
- {
- Db::startTrans();
- }
- /**
- * 提交事务
- */
- public static function commitTrans()
- {
- Db::commit();
- }
- /**
- * 关闭事务
- */
- public static function rollbackTrans()
- {
- Db::rollback();
- }
- /**
- * 根据结果提交滚回事务
- * @param $res
- */
- public static function checkTrans($res)
- {
- if ($res) {
- self::commitTrans();
- } else {
- self::rollbackTrans();
- }
- }
- /**
- * 获取ID
- * @param string $key 键名
- * @return mixed
- */
- public static function getkeytoid(string $key)
- {
- $rs = Db::query(sprintf("call keytoid('%s')",$key));
- return $rs[0][0]['cus_no'];
- }
- }
|