123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350 |
- <?php
- // +----------------------------------------------------------------------
- // | ThinkAdmin
- // +----------------------------------------------------------------------
- // | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
- // +----------------------------------------------------------------------
- // | 官方网站: http://demo.thinkadmin.top
- // +----------------------------------------------------------------------
- // | 开源协议 ( https://mit-license.org )
- // +----------------------------------------------------------------------
- // | gitee 代码仓库:https://gitee.com/zoujingli/ThinkAdmin
- // | github 代码仓库:https://github.com/zoujingli/ThinkAdmin
- // +----------------------------------------------------------------------
- namespace app\api\controller;
- use AlibabaCloud\SDK\Dingtalk\Vworkflow_1_0\Models\GetProcessConfigResponseBody\result\commentConf;
- use app\common\constant\CommonConstant;
- use app\common\model\User;
- use Firebase\JWT\JWT;
- use hg\apidoc\providers\CommonService;
- use think\Controller;
- use think\Db;
- use think\Exception;
- use think\exception\HttpResponseException;
- use think\Loader;
- use think\Response;
- use think\Validate;
- use function AlibabaCloud\Client\value;
- /**
- * 会员管理基类
- * Class Member
- * @package app\store\controller\api
- */
- class Base extends Controller
- {
- protected $user_id;
- protected $user;
- protected $page; // 页数
- protected $page_num;// 每页多少
- protected $off_set;
- protected $is_commit = true; // 事务是否提交
- protected $ret_msg = ''; // 返回提示信息
- protected $need_login = [];
- public function initialize()
- {
- $modulename = $this->request->module();
- $controllername = Loader::parseName($this->request->controller());
- $actionname = strtolower($this->request->action());
- $this->checking($modulename, $controllername, $actionname);
- $this->page = input('page') ?: 1;
- $this->page_num = input('page_num') ?: 20;
- $this->off_set = ($this->page * $this->page_num) - $this->page_num;
- $path = explode('/', $this->request->path());
- if (!empty($this->need_login) && in_array(end($path), $this->need_login)) {
- $this->checkLogin();
- }
- }
- /**
- * 校验jwt权限API
- **/
- protected function checkLogin()
- {
- $authorization = app()->request->header('Authorization');
- if (!$authorization) {
- $this->error('请登录后操作', null, 0, -1);
- }
- try {
- $key = md5(config('app.jwt'));
- $check_authorization = JWT::decode($authorization, $key, array('HS256'));
- if ($check_authorization['code'] != 200) {
- $this->exception($check_authorization['msg']);
- }
- $authInfo = json_decode(json_encode($check_authorization['data']), true);
- if (!$authInfo) {
- $this->error('Token验证不通过,账号不存在', null, 0, -1);
- }
- $member = User::field('is_deleted', true)
- ->where('is_deleted', CommonConstant::IS_DELETED_0)
- ->find($authInfo['uid']);
- if (!$member) {
- $this->error('账号不存在', null, 0, -1);
- }
- if (!$member['status']) {
- $this->error('账号已经被禁用', null, 0, -1);
- }
- $this->user_id = $authInfo['uid'];
- $this->user = $member;
- return $this->user_id;
- } catch (\Exception $e) {
- $this->error($e->getMessage(), '', 0, -1);
- }
- }
- /**
- * token加密
- **/
- public function createJwt($uid, $facility_code = '')
- {
- $key = md5(config('app.jwt')); //jwt的签发密钥,验证token的时候需要用到
- $time = time(); //签发时间
- $expire = $time + config('app.jwt_time'); //过期时间
- $token = array(
- "uid" => $uid,
- "iss" => "https://zain.com",//签发组织
- "aud" => "https://zain.com", //签发作者
- "iat" => $time,
- "nbf" => $time,
- "exp" => $expire,
- // "facility_code" => $facility_code,
- );
- $jwt = JWT::encode($token, $key);
- return $jwt;
- }
- protected function setUid()
- {
- $authorization = app()->request->header('Authorization');
- $key = md5(config('app.jwt'));
- if (empty($authorization) || $authorization == null) return false;
- try {
- $check_authorization = JWT::decode($authorization, $key, array('HS256'));
- if ($check_authorization['code'] != 200) $this->exception($check_authorization['msg']);
- $authInfo = json_decode(json_encode($check_authorization['data']), true);
- if (!empty($authInfo['uid'])) {
- $member = Db::name('store_member')->field('status')->where('id', $authInfo['uid'])->find();
- if ($member['status']) {
- $this->user_id = $authInfo['uid'];
- $this->user_id;
- return true;
- }
- }
- } catch (\Exception $e) {
- return false;
- //$this->error($e->getMessage(),'',0,-1);
- }
- }
- // 验证短信验证码
- protected function checkPhoneCode($phone, $code)
- {
- //return true;
- $sel_time = date('Y-m-d H:i:s', time() - 600);
- $store_member_sms = Db::name('store_member_sms')
- ->field('id,code')->where('phone', $phone)
- ->where('create_at', '> time', $sel_time)
- ->where('used', 0)->order('id desc')->find();
- return !empty($store_member_sms) && $store_member_sms['code'] == $code ? $store_member_sms['id'] : 0;
- }
- // 更新验证码状态
- protected function updatePhoneCode($code_id)
- {
- Db::name('store_member_sms')->where('id', $code_id)->update(['used' => 1]);
- }
- /**
- * 操作成功返回的数据
- * @param string $msg 提示信息
- * @param mixed $data 要返回的数据
- * @param int $code 错误码,默认为1
- * @param string $type 输出类型
- * @param array $header 发送的 Header 信息
- */
- protected function success($msg = 'ok', $data = null, $is_login = 1, $code = 1, $type = null, array $header = [])
- {
- $this->results($msg, $data, $is_login, $code, $type, $header);
- }
- /**
- * 操作失败返回的数据
- * @param string $msg 提示信息
- * @param mixed $data 要返回的数据
- * @param int $code 错误码,默认为0
- * @param string $type 输出类型
- * @param array $header 发送的 Header 信息
- */
- protected function error($msg = '', $data = null, $is_login = 1, $code = 0, $type = null, array $header = [])
- {
- if (empty($this->user_id)) {
- $is_login = 0;
- }
- $this->results($msg, $data, $is_login, $code, $type, $header);
- }
- /**
- * 返回封装后的 API 数据到客户端
- * @access protected
- * @param mixed $msg 提示信息
- * @param mixed $data 要返回的数据
- * @param int $code 错误码,默认为0
- * @param string $type 输出类型,支持json/xml/jsonp
- * @param array $header 发送的 Header 信息
- * @return void
- * @throws HttpResponseException
- */
- protected function results($msg, $data = null, $is_login, $code = 0, $type = null, array $header = [])
- {
- $result = [
- 'code' => $code,
- 'is_login' => $is_login,
- 'msg' => $msg,
- 'time' => \think\facade\Request::instance()->server('REQUEST_TIME'),
- 'data' => $data,
- ];
- // 如果未设置类型则自动判断
- $type = $type ? $type : 'json';
- if (isset($header['statuscode'])) {
- $code = $header['statuscode'];
- unset($header['statuscode']);
- } else {
- //未设置状态码,根据code值判断
- $code = $code >= 1000 || $code < 200 ? 200 : $code;
- }
- $response = Response::create($result, $type, $code)->header($header);
- throw new HttpResponseException($response);
- }
- protected function exception($msg)
- {
- throw new Exception($msg);
- }
- // 事务返回
- protected function transReturn($data = [])
- {
- $this->is_commit ? $this->success($this->ret_msg, $data) : $this->error($this->ret_msg);
- }
- protected function checking($modulename, $controllername, $action_name)
- {
- $params = $this->request->post();
- if ($controllername == 'approveflow') {
- if ($action_name == 'get_data') {
- $get_module_list = CommonConstant::get_module_list();
- $rule = [
- 'module|模块类型' => 'require|in:' . implode(',', array_keys($get_module_list)),
- ];
- $message = [
- 'module.in' => '请选择正确的模块类型',
- ];
- $validate = new Validate($rule, $message);
- if (!$validate->check($params)) {
- $this->error($validate->getError());
- }
- }
- }
- if ($controllername == 'approveinfo') {
- if ($action_name == 'create') {
- $get_way_list = CommonConstant::way;
- $get_module_list = CommonConstant::get_module_list();
- $rule = [
- 'way|方式' => 'require|in:' . implode(',', array_keys($get_way_list)),
- 'module|模块类型' => 'require|in:' . implode(',', array_keys($get_module_list)),
- ];
- $message = [
- 'way.in' => '请选择正确的方式',
- 'module.in' => '请选择正确的模块类型',
- ];
- $validate = new Validate($rule, $message);
- if (!$validate->check($params)) {
- $this->error($validate->getError());
- }
- $way = $this->request->post('way');
- $id = $this->request->post('id');
- if (!$id && in_array($way, [CommonConstant::update, CommonConstant::edit])) {
- $this->error('申请参数不能为空');
- }
- $module = $this->request->post('module');
- $validates = CommonConstant::get_module_validate_list();
- $validate = $validates[$module];
- $validate = new $validate;
- if (!$validate->check($params, [], $get_way_list[$way])) {
- $this->error($validate->getError());
- }
- }
- if (in_array($action_name, ['get_detail', 'get_info', 'urging', 'cancel','download'])) {
- $rule = [
- 'id|申请参数' => 'require|gt:0',
- ];
- $message = [];
- $validate = new Validate($rule, $message);
- if (!$validate->check($params)) {
- $this->error($validate->getError());
- }
- }
- if ($action_name == 'comment') {
- $rule = [
- 'id|申请参数' => 'require|gt:0',
- 'comment_score|满意程度' => 'require',
- 'comment|评价内容' => 'require',
- ];
- $message = [
- 'comment_score.require' => '请选择满意程度',
- 'comment.require' => '请输入评价内容',
- ];
- $validate = new Validate($rule, $message);
- if (!$validate->check($params)) {
- $this->error($validate->getError());
- }
- }
- }
- if ($controllername == 'approve') {
- if (in_array($action_name, ['get_detail', 'get_info'])) {
- $rule = [
- 'approve_id|审批参数' => 'require|gt:0',
- ];
- $message = [];
- $validate = new Validate($rule, $message);
- if (!$validate->check($params)) {
- $this->error($validate->getError());
- }
- }
- if ($action_name == 'audit') {
- $rule = [
- 'approve_id|审批参数' => 'require|gt:0',
- 'status|审批状态' => 'require|in:' . CommonConstant::STATUS_3 . ',' . CommonConstant::STATUS_4,
- ];
- $message = [
- 'status.in' => '请选择正确的审批状态',
- ];
- $validate = new Validate($rule, $message);
- if (!$validate->check($params)) {
- $this->error($validate->getError());
- }
- }
- if ($action_name == 'feedback') {
- $rule = [
- 'approve_id|审批参数' => 'require|gt:0',
- 'feedback|反馈结果' => 'require',
- ];
- $message = [
- 'feedback.require' => '请输入反馈结果',
- ];
- $validate = new Validate($rule, $message);
- if (!$validate->check($params)) {
- $this->error($validate->getError());
- }
- }
- }
- }
- }
|