Base.php 13 KB

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