Authcoach.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. <?php
  2. namespace app\common\library;
  3. use app\common\model\Coach;
  4. use app\common\model\UserRule;
  5. use fast\Random;
  6. use think\Config;
  7. use think\Db;
  8. use think\Exception;
  9. use think\Hook;
  10. use think\Request;
  11. use think\Validate;
  12. class Authcoach
  13. {
  14. protected static $instance = null;
  15. protected $_error = '';
  16. protected $_logined = false;
  17. protected $_user = null;
  18. protected $_token = '';
  19. //Token默认有效时长
  20. protected $keeptime = 2592000;
  21. protected $requestUri = '';
  22. protected $rules = [];
  23. //默认配置
  24. protected $config = [];
  25. protected $options = [];
  26. //protected $allowFields = ['id', 'name', 'logo','image','contacts', 'mobile','province_name','city_name','area_name','province_id','city_id','area_id','address','full_address','longitude','latitude','aptitude_images','open_hours','staff'];
  27. protected $allowFields = ['id','type','truename','mobile'];
  28. public function __construct($options = [])
  29. {
  30. if ($config = Config::get('coach')) {
  31. $this->config = array_merge($this->config, $config);
  32. }
  33. $this->options = array_merge($this->config, $options);
  34. }
  35. /**
  36. *
  37. * @param array $options 参数
  38. * @return Auth
  39. */
  40. public static function instance($options = [])
  41. {
  42. if (is_null(self::$instance)) {
  43. self::$instance = new static($options);
  44. }
  45. return self::$instance;
  46. }
  47. /**
  48. * 生成不重复的随机数字字母组合
  49. */
  50. function getUinqueNo($length = 8, $nos = [])
  51. {
  52. $newid = Random::build("alnum", $length);
  53. if (in_array($newid, $nos)) {
  54. $newid = $this->getUinqueNo($length, $nos);
  55. }
  56. return $newid;
  57. }
  58. /**
  59. * 获取User模型
  60. * @return User
  61. */
  62. public function getUser()
  63. {
  64. return $this->_user;
  65. }
  66. /**
  67. * 兼容调用user模型的属性
  68. *
  69. * @param string $name
  70. * @return mixed
  71. */
  72. public function __get($name)
  73. {
  74. return $this->_user ? $this->_user->$name : null;
  75. }
  76. /**
  77. * 兼容调用user模型的属性
  78. */
  79. public function __isset($name)
  80. {
  81. return isset($this->_user) ? isset($this->_user->$name) : false;
  82. }
  83. /**
  84. * 根据Token初始化
  85. *
  86. * @param string $token Token
  87. * @return boolean
  88. */
  89. public function init($token)
  90. {
  91. if ($this->_logined) {
  92. return true;
  93. }
  94. if ($this->_error) {
  95. return false;
  96. }
  97. $data = Tokencoach::get($token);
  98. if (!$data) {
  99. return false;
  100. }
  101. $user_id = intval($data['user_id']);
  102. if ($user_id > 0) {
  103. $user = Coach::get($user_id);
  104. if (!$user) {
  105. $this->setError('Account not exist');
  106. return false;
  107. }
  108. if ($user->status != 1) {
  109. $this->setError('Account is locked');
  110. return false;
  111. }
  112. /* if(!$user->company_id){
  113. $this->setError('Account not exist');
  114. return false;
  115. }
  116. $companyinfo = Company::get($user->company_id);
  117. if(!$companyinfo){
  118. $this->setError('Account not exist');
  119. return false;
  120. }
  121. $user->company = $companyinfo;*/
  122. $this->_user = $user;
  123. $this->_logined = true;
  124. $this->_token = $token;
  125. //初始化成功的事件
  126. // Hook::listen("company_init_successed", $this->_user);
  127. return true;
  128. } else {
  129. $this->setError('You are not logged in');
  130. return false;
  131. }
  132. }
  133. /**
  134. * 用户登录
  135. *
  136. * @param string $account 账号,用户名、邮箱、手机号
  137. * @param string $password 密码
  138. * @return boolean
  139. */
  140. public function login($account, $password, $openid)
  141. {
  142. $field = 'mobile';
  143. $user = Coach::get([$field => $account]);
  144. if (!$user) {
  145. $this->setError('Account is incorrect');
  146. return false;
  147. }
  148. if ($user->status != 1) {
  149. $this->setError('Account is locked');
  150. return false;
  151. }
  152. if ($user->password != $this->getEncryptPassword($password, $user->salt)) {
  153. $this->setError('Password is incorrect');
  154. return false;
  155. }
  156. //直接登录员工
  157. return $this->direct($user->id,$openid);
  158. }
  159. /**
  160. * 退出
  161. *
  162. * @return boolean
  163. */
  164. public function logout()
  165. {
  166. if (!$this->_logined) {
  167. $this->setError('You are not logged in');
  168. return false;
  169. }
  170. //设置登录标识
  171. $this->_logined = false;
  172. //删除Token
  173. Tokencoach::delete($this->_token);
  174. //退出成功的事件
  175. Hook::listen("user_logout_successed", $this->_user);
  176. return true;
  177. }
  178. /**
  179. * 修改密码
  180. * @param string $newpassword 新密码
  181. * @param string $oldpassword 旧密码
  182. * @param bool $ignoreoldpassword 忽略旧密码
  183. * @return boolean
  184. */
  185. public function resetpwd($newpassword)
  186. {
  187. if (!$this->_logined) {
  188. $this->setError('You are not logged in');
  189. return false;
  190. }
  191. Db::startTrans();
  192. $salt = Random::alnum();
  193. $newpassword = $this->getEncryptPassword($newpassword, $salt);
  194. //unset($this->_user['company']);
  195. $this->_user->save(['password' => $newpassword, 'salt' => $salt]);
  196. Tokencoach::delete($this->_token);
  197. //同步到admin
  198. $admin = [
  199. 'password' => $newpassword,
  200. 'salt' => $salt,
  201. 'updatetime' => time(),
  202. ];
  203. $admin_rs = Db::name('admin')->where('staff_id',$this->_user->id)->update($admin);
  204. if($admin_rs === false){
  205. Db::rollback();
  206. $this->setError('重置失败');
  207. return false;
  208. }
  209. Db::commit();
  210. return true;
  211. }
  212. /**
  213. * 直接登录账号
  214. * @param int $user_id
  215. * @return boolean
  216. */
  217. public function direct($staff_id,$openid = '')
  218. {
  219. $user = CompanyStaff::get($staff_id);
  220. if ($user) {
  221. if(!$user->company_id){
  222. return false;
  223. }
  224. $companyinfo = Company::get($user->company_id);
  225. if(!$companyinfo){
  226. return false;
  227. }
  228. if($companyinfo->status != 1){
  229. $this->setError('当前门店未通过审核');
  230. return false;
  231. }
  232. Db::startTrans();
  233. try {
  234. if(!empty($openid)){
  235. $user->openid = $openid;
  236. $user->save();
  237. }
  238. $user->openid = '';
  239. $user->company = $companyinfo;// 追加公司
  240. $this->_user = $user;
  241. $this->_token = Random::uuid();
  242. Tokencompany::set($this->_token, $user->id, $this->keeptime);
  243. $this->_logined = true;
  244. //登录成功的事件
  245. Hook::listen("company_login_successed", $this->_user);
  246. Db::commit();
  247. } catch (Exception $e) {
  248. Db::rollback();
  249. $this->setError($e->getMessage());
  250. return false;
  251. }
  252. return true;
  253. } else {
  254. return false;
  255. }
  256. }
  257. /**
  258. * 判断是否登录
  259. * @return boolean
  260. */
  261. public function isLogin()
  262. {
  263. if ($this->_logined) {
  264. return true;
  265. }
  266. return false;
  267. }
  268. /**
  269. * 获取当前Token
  270. * @return string
  271. */
  272. public function getToken()
  273. {
  274. return $this->_token;
  275. }
  276. /**
  277. * 获取会员基本信息
  278. */
  279. public function getUserinfo()
  280. {
  281. $data = $this->_user->toArray();
  282. $allowFields = $this->getAllowFields();
  283. $userinfo = array_intersect_key($data, array_flip($allowFields));
  284. $userinfo = array_merge($userinfo, Tokencompany::get($this->_token));
  285. //追加
  286. $userinfo['company']['image'] = one_domain_image($userinfo['company']['image']);
  287. /////////////////////////////////////
  288. //个人信息
  289. $strattime = strtotime(date('Y-m-d'));
  290. $endtime = $strattime + 86399;
  291. return $userinfo;
  292. }
  293. /**
  294. * 获取当前请求的URI
  295. * @return string
  296. */
  297. public function getRequestUri()
  298. {
  299. return $this->requestUri;
  300. }
  301. /**
  302. * 设置当前请求的URI
  303. * @param string $uri
  304. */
  305. public function setRequestUri($uri)
  306. {
  307. $this->requestUri = $uri;
  308. }
  309. /**
  310. * 获取允许输出的字段
  311. * @return array
  312. */
  313. public function getAllowFields()
  314. {
  315. return $this->allowFields;
  316. }
  317. /**
  318. * 设置允许输出的字段
  319. * @param array $fields
  320. */
  321. public function setAllowFields($fields)
  322. {
  323. $this->allowFields = $fields;
  324. }
  325. /**
  326. * 获取密码加密后的字符串
  327. * @param string $password 密码
  328. * @param string $salt 密码盐
  329. * @return string
  330. */
  331. public function getEncryptPassword($password, $salt = '')
  332. {
  333. return md5(md5($password) . $salt);
  334. }
  335. /**
  336. * 检测当前控制器和方法是否匹配传递的数组
  337. *
  338. * @param array $arr 需要验证权限的数组
  339. * @return boolean
  340. */
  341. public function match($arr = [])
  342. {
  343. $request = Request::instance();
  344. $arr = is_array($arr) ? $arr : explode(',', $arr);
  345. if (!$arr) {
  346. return false;
  347. }
  348. $arr = array_map('strtolower', $arr);
  349. // 是否存在
  350. if (in_array(strtolower($request->action()), $arr) || in_array('*', $arr)) {
  351. return true;
  352. }
  353. // 没找到匹配
  354. return false;
  355. }
  356. /**
  357. * 设置会话有效时间
  358. * @param int $keeptime 默认为永久
  359. */
  360. public function keeptime($keeptime = 0)
  361. {
  362. $this->keeptime = $keeptime;
  363. }
  364. /**
  365. * 渲染用户数据
  366. * @param array $datalist 二维数组
  367. * @param mixed $fields 加载的字段列表
  368. * @param string $fieldkey 渲染的字段
  369. * @param string $renderkey 结果字段
  370. * @return array
  371. */
  372. /*public function render(&$datalist, $fields = [], $fieldkey = 'user_id', $renderkey = 'userinfo')
  373. {
  374. $fields = !$fields ? ['id', 'nickname', 'level', 'avatar'] : (is_array($fields) ? $fields : explode(',', $fields));
  375. $ids = [];
  376. foreach ($datalist as $k => $v) {
  377. if (!isset($v[$fieldkey])) {
  378. continue;
  379. }
  380. $ids[] = $v[$fieldkey];
  381. }
  382. $list = [];
  383. if ($ids) {
  384. if (!in_array('id', $fields)) {
  385. $fields[] = 'id';
  386. }
  387. $ids = array_unique($ids);
  388. $selectlist = User::where('id', 'in', $ids)->column($fields);
  389. foreach ($selectlist as $k => $v) {
  390. $list[$v['id']] = $v;
  391. }
  392. }
  393. foreach ($datalist as $k => &$v) {
  394. $v[$renderkey] = isset($list[$v[$fieldkey]]) ? $list[$v[$fieldkey]] : null;
  395. }
  396. unset($v);
  397. return $datalist;
  398. }*/
  399. /**
  400. * 设置错误信息
  401. *
  402. * @param string $error 错误信息
  403. * @return Auth
  404. */
  405. public function setError($error)
  406. {
  407. $this->_error = $error;
  408. return $this;
  409. }
  410. /**
  411. * 获取错误信息
  412. * @return string
  413. */
  414. public function getError()
  415. {
  416. return $this->_error ? __($this->_error) : '';
  417. }
  418. }