Base.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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\model\UserFacility;
  16. use app\common\service\UserSynth;
  17. use Firebase\JWT\JWT;
  18. use think\cache\driver\Redis;
  19. use think\Controller;
  20. use think\Db;
  21. use think\Exception;
  22. use think\exception\HttpResponseException;
  23. use think\Request;
  24. use think\Response;
  25. use function AlibabaCloud\Client\value;
  26. use think\facade\Hook;
  27. /**
  28. * 会员管理基类
  29. * Class Member
  30. * @package app\store\controller\api
  31. */
  32. class Base extends Controller
  33. {
  34. protected $user_id;// 权限id
  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. protected $account_id;// 账号id
  43. protected $login_type = 0;//设备类型1:安卓手机设备,2:ios手机设备,3:安卓ipad,4:iosipad,5:h5,6pc电脑设备号
  44. protected $source_type = 1;//设备类型1h5,2:安卓手机设备,3:ios手机设备,
  45. protected $user_info = ['id'=>'','name'=>'','headimg'=>''];
  46. public function initialize(){
  47. $this->page = input('page',1) > 0 ? input('page',1) : 1;
  48. $this->page_num = input('page_num',20);
  49. $this->off_set = $this->page * $this->page_num - $this->page_num;
  50. $this->is_test = input('test',0);// 测试用的 // 上线需要注释
  51. $path = explode('/',$this->request->path());
  52. if( input('user_id')) $this->user_id = input('user_id');// 测试用的 // 上线需要注释
  53. if(!empty($this->need_login) && in_array(end($path),$this->need_login)) $this->checkLogin();
  54. !defined("APPNAME") && define('APPNAME',sysconf('app_name'));
  55. !defined("APPLOGO") && define('APPLOGO',sysconf('app_logo'));
  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. $this->account_id = $this->user_id;
  65. return true;
  66. }
  67. $this->error('Token不存在,拒绝访问--',null,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. $this->account_id = $authInfo['uid'];
  76. if(!empty($authInfo['login_type'])) $this->login_type = $authInfo['login_type'];
  77. //设备号验证 start
  78. if(empty($authInfo['facility_code']) || empty($authInfo['login_type'])) $this->exception('请重新登录');
  79. $redis = new Redis();
  80. if(!in_array($authInfo['login_type'],[1,2,3,4])) {// H5 redis验证
  81. $check_redis = $redis->get('TOKEN_'.$authInfo['uid'].'_'.$authInfo['login_type']);
  82. if($authorization != $check_redis) $this->exception('请重新登录');
  83. }else{
  84. $redis = new Redis();
  85. $check_redis = $redis->get('TOKEN_'.$authInfo['uid'].'_'.$authInfo['login_type']);
  86. if(!$check_redis) {
  87. $facility_id = UserFacility::where(['user_id'=>$authInfo['uid'],'type'=>$authInfo['login_type'],'facility'=>$authInfo['facility_code']])->value('id');
  88. if(!$facility_id) $this->exception('该设备已被禁用');
  89. $redis->set('TOKEN_'.$authInfo['uid'].'_'.$authInfo['login_type'],$authorization,600);// 设置10分钟缓存
  90. }
  91. }
  92. // 设备号验证end
  93. $member = Db::name('store_member')->where('id',$authInfo['uid'])->find();
  94. $this->user_info = $member;
  95. if(empty($member) || $member['is_deleted']) $this->exception('会员不存在');
  96. // 企业用户绑定验证
  97. if($member['account_type'] == 1 && empty($member['bind_id']) && $this->request->action() != 'modifyphone') $this->exception('请绑定手机号111');
  98. if($member['status']){
  99. if($member['bind_id']){
  100. $this->user_id = $member['bind_id'];
  101. }else{
  102. $this->user_id = $authInfo['uid'];
  103. }
  104. // 会员活跃度增加
  105. if(!empty($authInfo['login_type'])) UserSynth::vitality( $this->user_id,$authInfo['login_type']);
  106. return $this->user_id;
  107. }else{
  108. $this->exception('该会员已被禁用');
  109. }
  110. } else {
  111. $this->exception('Token验证不通过,用户不存在');
  112. }
  113. } catch (\Exception $e) {
  114. $this->error($e->getMessage(),null,0,-1);
  115. }
  116. //if($this->user_id == 97) $this->user_id = 83;
  117. }
  118. protected function setUid(){
  119. $authorization = app()->request->header('Authorization');
  120. $key = md5(config('app.jwt'));
  121. if(empty($authorization) || $authorization == null) return false;
  122. try {
  123. $check_authorization = JWT::decode($authorization, $key, array('HS256'));
  124. if($check_authorization['code'] !=200) $this->exception($check_authorization['msg']);
  125. $authInfo = json_decode(json_encode($check_authorization['data']), true);
  126. if (!empty($authInfo['uid'])) {
  127. $this->account_id = $authInfo['uid'];
  128. if(!empty($authInfo['login_type'])) $this->login_type = $authInfo['login_type'];
  129. $member = Db::name('store_member')->where('id',$authInfo['uid'])->find();
  130. $this->user_info = $member;
  131. if($member['status']){
  132. if($member['bind_id']){
  133. $this->user_id = $member['bind_id'];
  134. }else{
  135. $this->user_id = $authInfo['uid'];
  136. }
  137. return true;
  138. }
  139. }
  140. } catch (\Exception $e) {
  141. return false;
  142. //$this->error($e->getMessage(),'',0,-1);
  143. }
  144. }
  145. // 获取用户信息
  146. function userInfo($field =''){
  147. if(!$this->user_id) return ['name'=>''];
  148. return $field ? Db::name('store_member')->field($field)->find($this->user_id) : Db::name('store_member')->find($this->user_id);
  149. }
  150. // 验证短信验证码
  151. protected function checkPhoneCode($phone,$code,$phone_pre = 86){
  152. return true; // 上线需要注释
  153. $sel_time =date('Y-m-d H:i:s',time()-600);
  154. $store_member_sms = Db::name('store_member_sms')
  155. ->field('id,code')
  156. ->where('phone',$phone)
  157. ->where('phone_pre',$phone_pre)
  158. ->where('create_at','> time',$sel_time)
  159. ->where('used',0)->order('id desc')->find();
  160. return !empty($store_member_sms) && $store_member_sms['code'] == $code ? $store_member_sms['id'] :0;
  161. }
  162. // 更新验证码状态
  163. protected function updatePhoneCode($code_id){
  164. Db::name('store_member_sms')->where('id',$code_id)->update(['used'=>1]);
  165. }
  166. /**
  167. * 操作成功返回的数据
  168. * @param string $msg 提示信息
  169. * @param mixed $data 要返回的数据
  170. * @param int $code 错误码,默认为1
  171. * @param string $type 输出类型
  172. * @param array $header 发送的 Header 信息
  173. */
  174. protected function success($msg = 'ok', $data = null , $is_login = 1, $code = 1, $type = null, array $header = [])
  175. {
  176. $this->results($msg, $data, $is_login, $code, $type, $header);
  177. }
  178. /**
  179. * 操作失败返回的数据
  180. * @param string $msg 提示信息
  181. * @param mixed $data 要返回的数据
  182. * @param int $code 错误码,默认为0
  183. * @param string $type 输出类型
  184. * @param array $header 发送的 Header 信息
  185. */
  186. protected function error($msg = '', $data = null, $is_login = 1, $code = 0, $type = null, array $header = [])
  187. {
  188. if(empty($this->user_id)){
  189. $is_login = 0;
  190. }
  191. $this->results($msg, $data, $is_login, $code, $type, $header);
  192. }
  193. /**
  194. * 返回封装后的 API 数据到客户端
  195. * @access protected
  196. * @param mixed $msg 提示信息
  197. * @param mixed $data 要返回的数据
  198. * @param int $code 错误码,默认为0
  199. * @param string $type 输出类型,支持json/xml/jsonp
  200. * @param array $header 发送的 Header 信息
  201. * @return void
  202. * @throws HttpResponseException
  203. */
  204. protected function results($msg, $data = null, $is_login, $code = 0, $type = null, array $header = [])
  205. {
  206. $result = [
  207. 'code' => $code,
  208. 'is_login' => $is_login,
  209. 'msg' => $msg,
  210. 'time' => \think\facade\Request::instance()->server('REQUEST_TIME'),
  211. 'data' => $data,
  212. ];
  213. // 如果未设置类型则自动判断
  214. $type = $type ? $type : 'json';
  215. if (isset($header['statuscode']))
  216. {
  217. $code = $header['statuscode'];
  218. unset($header['statuscode']);
  219. }
  220. else
  221. {
  222. //未设置状态码,根据code值判断
  223. $code = $code >= 1000 || $code < 200 ? 200 : $code;
  224. }
  225. $response = Response::create($result, $type, $code)->header($header);
  226. throw new HttpResponseException($response);
  227. }
  228. /**
  229. * 生成token
  230. * @param $uid 会有id
  231. * @param string $facility_code 设备号
  232. * @param int $port 登录类型
  233. * @return string
  234. */
  235. public function createJwt($uid,$facility_code='',$login_type=0)
  236. {
  237. $key = md5(config('app.jwt')); //jwt的签发密钥,验证token的时候需要用到
  238. $time = time(); //签发时间
  239. $expire = $time + config('app.jwt_time'); //过期时间
  240. $token = array(
  241. "uid" => $uid,
  242. "iss" => "https://zain.com",//签发组织
  243. "aud" => "https://zain.com", //签发作者
  244. "iat" => $time,
  245. "nbf" => $time,
  246. "exp" => $expire,
  247. "facility_code" => $facility_code,
  248. "login_type" => $login_type,
  249. );
  250. $jwt = JWT::encode($token, $key);
  251. return $jwt;
  252. }
  253. protected function exception($msg){
  254. throw new Exception($msg);
  255. }
  256. // 事务返回
  257. protected function transReturn($data = []){
  258. $this->is_commit ? $this->success($this->ret_msg,$data):$this->error($this->ret_msg);
  259. }
  260. }