Base.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://demo.thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | gitee 代码仓库:https://gitee.com/zoujingli/ThinkAdmin
  12. // | github 代码仓库:https://github.com/zoujingli/ThinkAdmin
  13. // +----------------------------------------------------------------------
  14. namespace app\api\controller;
  15. use app\common\constant\CommonConstant;
  16. use app\common\model\User;
  17. use Firebase\JWT\JWT;
  18. use think\Controller;
  19. use think\Db;
  20. use think\Exception;
  21. use think\exception\HttpResponseException;
  22. use think\Loader;
  23. use think\Response;
  24. use think\Validate;
  25. use function AlibabaCloud\Client\value;
  26. /**
  27. * 会员管理基类
  28. * Class Member
  29. * @package app\store\controller\api
  30. */
  31. class Base extends Controller
  32. {
  33. protected $user_id;
  34. protected $user;
  35. protected $page; // 页数
  36. protected $page_num;// 每页多少
  37. protected $off_set;
  38. protected $is_commit = true; // 事务是否提交
  39. protected $ret_msg = ''; // 返回提示信息
  40. protected $is_test = false;
  41. protected $need_login = [];
  42. public function initialize()
  43. {
  44. $modulename = $this->request->module();
  45. $controllername = Loader::parseName($this->request->controller());
  46. $actionname = strtolower($this->request->action());
  47. $this->checking($modulename, $controllername, $actionname);
  48. $this->page = input('page', 1);
  49. $this->page_num = input('page_num', 20);
  50. $this->off_set = $this->page * $this->page_num - $this->page_num;
  51. $this->is_test = input('test', 0);// 测试用的
  52. $path = explode('/', $this->request->path());
  53. if (input('user_id')) $this->user_id = input('user_id');
  54. if (input('user')) $this->user_id = input('user');
  55. if (!empty($this->need_login) && in_array(end($path), $this->need_login)) $this->checkLogin();
  56. }
  57. //校验jwt权限API
  58. protected function checkLogin()
  59. {
  60. $authorization = app()->request->header('Authorization');
  61. if (empty($authorization) || $authorization == null) {
  62. if ($this->is_test == 1) {
  63. $this->user_id = input('user_id') ? input('user_id') : 22;
  64. if (input('user')) $this->user_id = input('user');
  65. return true;
  66. }
  67. $this->error('Token不存在,拒绝访问--', '', 0, -1);
  68. }
  69. $key = md5(config('app.jwt'));
  70. try {
  71. $check_authorization = JWT::decode($authorization, $key, array('HS256'));
  72. if ($check_authorization['code'] != 200) $this->exception($check_authorization['msg']);
  73. $authInfo = json_decode(json_encode($check_authorization['data']), true);
  74. if (!empty($authInfo['uid'])) {
  75. $member = User::find($authInfo['uid']);
  76. if (empty($member) || $member['is_deleted']) $this->error('会员不存在', '', 0);
  77. //if(!empty($authInfo['facility_code']) && !in_array($authInfo['facility_code'],[$member['facility_1'],$member['facility_2'],$member['facility_3'],$member['facility_4'],$member['facility_5']])) $this->error('该设备已被禁用','',0,-1);
  78. if ($member['status']) {
  79. $this->user_id = $authInfo['uid'];
  80. $this->user = $member;
  81. return $this->user_id;
  82. } else {
  83. $this->error('该会员已被禁用', '', 0, -1);
  84. }
  85. } else {
  86. $this->error('Token验证不通过,用户不存在', '', 0, -1);
  87. }
  88. } catch (\Exception $e) {
  89. $this->error($e->getMessage(), '', 0, -1);
  90. }
  91. }
  92. protected function setUid()
  93. {
  94. $authorization = app()->request->header('Authorization');
  95. $key = md5(config('app.jwt'));
  96. if (empty($authorization) || $authorization == null) return false;
  97. try {
  98. $check_authorization = JWT::decode($authorization, $key, array('HS256'));
  99. if ($check_authorization['code'] != 200) $this->exception($check_authorization['msg']);
  100. $authInfo = json_decode(json_encode($check_authorization['data']), true);
  101. if (!empty($authInfo['uid'])) {
  102. $member = Db::name('store_member')->field('status')->where('id', $authInfo['uid'])->find();
  103. if ($member['status']) {
  104. $this->user_id = $authInfo['uid'];
  105. $this->user_id;
  106. return true;
  107. }
  108. }
  109. } catch (\Exception $e) {
  110. return false;
  111. //$this->error($e->getMessage(),'',0,-1);
  112. }
  113. }
  114. // 获取用户信息
  115. function userInfo($field = '')
  116. {
  117. if (!$this->user_id) return [];
  118. return $field ? Db::name('store_member')->field($field)->find($this->user_id) : Db::name('store_member')->find($this->user_id);
  119. }
  120. // 验证短信验证码
  121. protected function checkPhoneCode($phone, $code)
  122. {
  123. //return true;
  124. $sel_time = date('Y-m-d H:i:s', time() - 600);
  125. $store_member_sms = Db::name('store_member_sms')
  126. ->field('id,code')->where('phone', $phone)
  127. ->where('create_at', '> time', $sel_time)
  128. ->where('used', 0)->order('id desc')->find();
  129. return !empty($store_member_sms) && $store_member_sms['code'] == $code ? $store_member_sms['id'] : 0;
  130. }
  131. // 更新验证码状态
  132. protected function updatePhoneCode($code_id)
  133. {
  134. Db::name('store_member_sms')->where('id', $code_id)->update(['used' => 1]);
  135. }
  136. /**
  137. * 操作成功返回的数据
  138. * @param string $msg 提示信息
  139. * @param mixed $data 要返回的数据
  140. * @param int $code 错误码,默认为1
  141. * @param string $type 输出类型
  142. * @param array $header 发送的 Header 信息
  143. */
  144. protected function success($msg = 'ok', $data = null, $is_login = 1, $code = 1, $type = null, array $header = [])
  145. {
  146. $this->results($msg, $data, $is_login, $code, $type, $header);
  147. }
  148. /**
  149. * 操作失败返回的数据
  150. * @param string $msg 提示信息
  151. * @param mixed $data 要返回的数据
  152. * @param int $code 错误码,默认为0
  153. * @param string $type 输出类型
  154. * @param array $header 发送的 Header 信息
  155. */
  156. protected function error($msg = '', $data = null, $is_login = 1, $code = 0, $type = null, array $header = [])
  157. {
  158. if (empty($this->user_id)) {
  159. $is_login = 0;
  160. }
  161. $this->results($msg, $data, $is_login, $code, $type, $header);
  162. }
  163. /**
  164. * 返回封装后的 API 数据到客户端
  165. * @access protected
  166. * @param mixed $msg 提示信息
  167. * @param mixed $data 要返回的数据
  168. * @param int $code 错误码,默认为0
  169. * @param string $type 输出类型,支持json/xml/jsonp
  170. * @param array $header 发送的 Header 信息
  171. * @return void
  172. * @throws HttpResponseException
  173. */
  174. protected function results($msg, $data = null, $is_login, $code = 0, $type = null, array $header = [])
  175. {
  176. $result = [
  177. 'code' => $code,
  178. 'is_login' => $is_login,
  179. 'msg' => $msg,
  180. 'time' => \think\facade\Request::instance()->server('REQUEST_TIME'),
  181. 'data' => $data,
  182. ];
  183. // 如果未设置类型则自动判断
  184. $type = $type ? $type : 'json';
  185. if (isset($header['statuscode'])) {
  186. $code = $header['statuscode'];
  187. unset($header['statuscode']);
  188. } else {
  189. //未设置状态码,根据code值判断
  190. $code = $code >= 1000 || $code < 200 ? 200 : $code;
  191. }
  192. $response = Response::create($result, $type, $code)->header($header);
  193. throw new HttpResponseException($response);
  194. }
  195. //token加密
  196. public function createJwt($uid, $facility_code = '')
  197. {
  198. $key = md5(config('app.jwt')); //jwt的签发密钥,验证token的时候需要用到
  199. $time = time(); //签发时间
  200. $expire = $time + config('app.jwt_time'); //过期时间
  201. $token = array(
  202. "uid" => $uid,
  203. "iss" => "https://zain.com",//签发组织
  204. "aud" => "https://zain.com", //签发作者
  205. "iat" => $time,
  206. "nbf" => $time,
  207. "exp" => $expire,
  208. "facility_code" => $facility_code,
  209. );
  210. $jwt = JWT::encode($token, $key);
  211. return $jwt;
  212. }
  213. protected function exception($msg)
  214. {
  215. throw new Exception($msg);
  216. }
  217. // 事务返回
  218. protected function transReturn($data = [])
  219. {
  220. $this->is_commit ? $this->success($this->ret_msg, $data) : $this->error($this->ret_msg);
  221. }
  222. protected function checking($modulename, $controllername, $action_name)
  223. {
  224. $params = $this->request->post();
  225. if (in_array($action_name, ['get_data', 'create', 'edit'])) {
  226. $get_module_list = CommonConstant::get_module_list();
  227. $rule = [
  228. 'module|模块类型' => 'require|in:' . implode(',', array_keys($get_module_list)),
  229. ];
  230. $message = [
  231. 'module.in' => '请选择正确的模块类型',
  232. ];
  233. $validate = new Validate($rule, $message);
  234. if (!$validate->check($params)) {
  235. $this->error($validate->getError());
  236. }
  237. if ($action_name == 'create') {
  238. $id = $this->request->post('id');
  239. $module = $this->request->post('module');
  240. $validates = CommonConstant::get_module_validate_list();
  241. $validate = $validates[$module];
  242. $validate = new $validate;
  243. if (!$validate->check($params, [], $id > 0 ? 'update' : 'create')) {
  244. $this->error($validate->getError());
  245. }
  246. }
  247. if ($action_name == 'edit') {
  248. $module = $this->request->post('module');
  249. $validates = CommonConstant::get_module_validate_list();
  250. $validate = $validates[$module];
  251. $validate = new $validate;
  252. if (!$validate->check($params, [], 'edit')) {
  253. $this->error($validate->getError());
  254. }
  255. }
  256. }
  257. if ($controllername == 'approveinfo') {
  258. if (in_array($action_name, ['get_detail', 'get_info', 'urging', 'cancel'])) {
  259. $rule = [
  260. 'id|申请参数' => 'require|gt:0',
  261. ];
  262. $message = [];
  263. $validate = new Validate($rule, $message);
  264. if (!$validate->check($params)) {
  265. $this->error($validate->getError());
  266. }
  267. }
  268. }
  269. if ($controllername == 'approve') {
  270. if (in_array($action_name, ['get_detail', 'get_info'])) {
  271. $rule = [
  272. 'approve_id|审批参数' => 'require|gt:0',
  273. ];
  274. $message = [];
  275. $validate = new Validate($rule, $message);
  276. if (!$validate->check($params)) {
  277. $this->error($validate->getError());
  278. }
  279. }
  280. if ($action_name == 'audit') {
  281. $rule = [
  282. 'approve_id|审批参数' => 'require|gt:0',
  283. 'status|审批状态' => 'require|in:' . CommonConstant::STATUS_3 . ',' . CommonConstant::STATUS_4,
  284. ];
  285. $message = [
  286. 'status.in' => '请选择正确的审批状态',
  287. ];
  288. $validate = new Validate($rule, $message);
  289. if (!$validate->check($params)) {
  290. $this->error($validate->getError());
  291. }
  292. }
  293. }
  294. }
  295. }