BaseModel.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/11/02
  6. */
  7. namespace crmeb\basic;
  8. use think\facade\Db;
  9. use think\Model;
  10. class BaseModel extends Model
  11. {
  12. private static $errorMsg;
  13. private static $transaction = 0;
  14. private static $DbInstance = [];
  15. const DEFAULT_ERROR_MSG = '操作失败,请稍候再试!';
  16. /**
  17. * 设置错误信息
  18. * @param string $errorMsg
  19. * @return bool
  20. */
  21. protected static function setErrorInfo($errorMsg = self::DEFAULT_ERROR_MSG, $rollback = false)
  22. {
  23. if ($rollback) self::rollbackTrans();
  24. self::$errorMsg = $errorMsg;
  25. return false;
  26. }
  27. /**
  28. * 获取错误信息
  29. * @param string $defaultMsg
  30. * @return string
  31. */
  32. public static function getErrorInfo($defaultMsg = self::DEFAULT_ERROR_MSG)
  33. {
  34. return !empty(self::$errorMsg) ? self::$errorMsg : $defaultMsg;
  35. }
  36. /**
  37. * 开启事务
  38. */
  39. public static function beginTrans()
  40. {
  41. Db::startTrans();
  42. }
  43. /**
  44. * 提交事务
  45. */
  46. public static function commitTrans()
  47. {
  48. Db::commit();
  49. }
  50. /**
  51. * 关闭事务
  52. */
  53. public static function rollbackTrans()
  54. {
  55. Db::rollback();
  56. }
  57. /**
  58. * 根据结果提交滚回事务
  59. * @param $res
  60. */
  61. public static function checkTrans($res)
  62. {
  63. if ($res) {
  64. self::commitTrans();
  65. } else {
  66. self::rollbackTrans();
  67. }
  68. }
  69. }