Appapi.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /*
  3. * FecShop file.
  4. *
  5. * @link http://www.fecshop.com/
  6. * @copyright Copyright (c) 2016 FecShop Software LLC
  7. * @license http://www.fecshop.com/license/
  8. */
  9. namespace fecshop\services\helper;
  10. use fecshop\services\Service;
  11. use Yii;
  12. /**
  13. * 该类主要是给appserver端的api,返回的数据做格式输出,规范输出的各种状态。
  14. * @author Terry Zhao <2358269014@qq.com>
  15. * @since 1.0
  16. */
  17. class Appapi extends Service
  18. {
  19. /**
  20. * 公共状态码
  21. */
  22. public $status_success = 200;
  23. public $status_unknown = 1000000; // 程序内部错误:未知错误
  24. public $status_mysql_disconnect = 1000001; // 程序内部错误:mysql连接错误
  25. public $status_mongodb_disconnect = 1000002; // 程序内部错误:mongodb连接错误
  26. public $status_redis_disconnect = 1000003; // 程序内部错误:redis连接错误
  27. public $status_invalid_token = 1000004; // 无效数据:token无效
  28. public $status_invalid_request_url = 1000005; // 无效请求:该url不存在
  29. public $status_invalid_email = 1000006; // 格式错误:邮箱格式无效
  30. public $status_invalid_captcha = 1000007; // 无效数据:验证码错误
  31. public $status_invalid_param = 1000008; // 无效参数
  32. public $status_miss_param = 1000009; // 参数丢失
  33. public $status_limit_beyond = 1000010; // 超出限制
  34. public $status_data_repeat = 1000011; // 数据重复
  35. public $status_attack = 1000012; // 确定为攻击返回的状态
  36. public $status_invalid_code = 1000013; // 程序内部错误:传递的无效code
  37. /**
  38. * 用户部分的状态码
  39. */
  40. public $account_no_login_or_login_token_timeout = 1100003; // 登录:账户的token已经过期,或者没有登录
  41. /**
  42. * @param $code | String 状态码
  43. * @param $data | 混合状态,可以是数字,数组等格式,用于做返回给前端的数组。
  44. * @param $message | String ,选填,如果不填写,则使用 函数 返回的内容作为message
  45. */
  46. public function getResponseData($code, $data, $message = '')
  47. {
  48. if (!$message) {
  49. $message = $this->getMessageByCode($code);
  50. }
  51. if ($message) {
  52. return [
  53. 'code' => $code,
  54. 'message' => $message,
  55. 'data' => $data,
  56. ];
  57. } else { // 如果不存在,则说明系统内部调用不存在的code,报错。
  58. $code = $this->status_invalid_code;
  59. $message = $this->getMessageByCode($code);
  60. return [
  61. 'code' => $code,
  62. 'message' => $message,
  63. 'data' => '',
  64. ];
  65. }
  66. }
  67. /**
  68. * @param $code | String ,状态码
  69. * 得到 code 对应 message的数组
  70. */
  71. public function getMessageByCode($code)
  72. {
  73. $messageArr = $this->getMessageArr();
  74. return isset($messageArr[$code]['message']) ? $messageArr[$code]['message'] : '';
  75. }
  76. /**
  77. * 得到 code 对应 message的数组
  78. */
  79. public function getMessageArr()
  80. {
  81. $arr = [
  82. /**
  83. * 公共状态码
  84. */
  85. $this->status_success => [
  86. 'message' => 'process success',
  87. ],
  88. $this->status_unknown => [
  89. 'message' => 'unknown errors',
  90. ],
  91. $this->status_mysql_disconnect => [
  92. 'message' => 'mysql connect timeout',
  93. ],
  94. $this->status_mongodb_disconnect => [
  95. 'message' => 'mongodb connect timeout',
  96. ],
  97. $this->status_redis_disconnect => [
  98. 'message' => 'redis connect timeout',
  99. ],
  100. $this->status_invalid_token => [
  101. 'message' => 'token is timeout or invalid',
  102. ],
  103. $this->status_invalid_request_url => [
  104. 'message' => 'the request url is not exist',
  105. ],
  106. $this->status_invalid_email => [
  107. 'message' => 'email format is not correct',
  108. ],
  109. $this->status_invalid_captcha => [
  110. 'message' => 'captcha is not correct',
  111. ],
  112. $this->status_invalid_param => [
  113. 'message' => 'incorrect request parameter',
  114. ],
  115. $this->status_invalid_code => [
  116. 'message' => 'system error, invalid code',
  117. ],
  118. $this->status_miss_param => [
  119. 'message' => 'required parameter does not exist',
  120. ],
  121. $this->status_limit_beyond => [
  122. 'message' => 'beyond maximum limit',
  123. ],
  124. $this->status_data_repeat => [
  125. 'message' => 'insert data is repeat',
  126. ],
  127. $this->status_attack => [
  128. 'message' => 'access exception, the visit to determine the attack behavior',
  129. ],
  130. /**
  131. * 用户部分的状态码
  132. */
  133. $this->account_no_login_or_login_token_timeout => [
  134. 'message' => 'account not login or token timeout',
  135. ],
  136. ];
  137. return $arr;
  138. }
  139. }