Authcompany.php 12 KB

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