BaseModel.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. /**
  70. * 获取ID
  71. * @param string $key 键名
  72. * @return mixed
  73. */
  74. public static function getkeytoid(string $key)
  75. {
  76. // $rs = Db::query(sprintf("call keytoid('%s')",$key));
  77. // return $rs[0][0]['cus_no'];
  78. $rs = self::order('uid DESC')->find();
  79. return $rs['uid']+1;
  80. }
  81. }