Authcompany.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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. if($companyinfo->status != 1){
  218. $this->setError('当前门店未通过审核');
  219. return false;
  220. }
  221. Db::startTrans();
  222. try {
  223. $user->company = $companyinfo;// 追加公司
  224. $this->_user = $user;
  225. $this->_token = Random::uuid();
  226. Tokencompany::set($this->_token, $user->id, $this->keeptime);
  227. $this->_logined = true;
  228. //登录成功的事件
  229. Hook::listen("company_login_successed", $this->_user);
  230. Db::commit();
  231. } catch (Exception $e) {
  232. Db::rollback();
  233. $this->setError($e->getMessage());
  234. return false;
  235. }
  236. return true;
  237. } else {
  238. return false;
  239. }
  240. }
  241. /**
  242. * 判断是否登录
  243. * @return boolean
  244. */
  245. public function isLogin()
  246. {
  247. if ($this->_logined) {
  248. return true;
  249. }
  250. return false;
  251. }
  252. /**
  253. * 获取当前Token
  254. * @return string
  255. */
  256. public function getToken()
  257. {
  258. return $this->_token;
  259. }
  260. /**
  261. * 获取会员基本信息
  262. */
  263. public function getUserinfo()
  264. {
  265. $data = $this->_user->toArray();
  266. $allowFields = $this->getAllowFields();
  267. $userinfo = array_intersect_key($data, array_flip($allowFields));
  268. $userinfo = array_merge($userinfo, Tokencompany::get($this->_token));
  269. //追加
  270. $userinfo['company']['image'] = one_domain_image($userinfo['company']['image']);
  271. $userinfo['money'] = model('walletcompany')->getWallet($this->company_id,'money'); //可提现金额
  272. $userinfo['dongjie_money'] = Db::name('order')->where('company_id',$this->company_id)->where('status',2)->where('ordertype',3)->sum('pay_fee');//冻结金额,即出售的套餐,但没使用的
  273. $userinfo['all_money'] = bcadd($userinfo['money'],$userinfo['dongjie_money'],2);
  274. /////////////////////////////////////
  275. //个人信息
  276. $strattime = strtotime(date('Y-m-d'));
  277. $endtime = $strattime + 86399;
  278. //今日开单
  279. $map = [
  280. 'company_id' => $this->company_id,
  281. 'finish_time' => ['BETWEEN',[$strattime,$endtime]], //开单时间用完成时间
  282. 'status' => 3,
  283. ];
  284. if($this->type == 2){
  285. //员工的
  286. $map['staff_id'] = $this->id;
  287. }
  288. $today_pay_fee = Db::name('order')->where($map)->sum('pay_fee');
  289. $today_appen_fee = Db::name('order')->where($map)->sum('appen_fee');
  290. $userinfo['today_order_totalfee'] = $today_appen_fee + $today_pay_fee;
  291. //今日新增客户
  292. $map = [
  293. 'company_id' => $this->company_id,
  294. 'createtime'=>['BETWEEN',[$strattime,$endtime]],
  295. ];
  296. if($this->type == 2){
  297. //员工的
  298. $map['staff_id'] = $this->id;
  299. }
  300. $userinfo['today_newcus'] = Db::name('user_wallet')->where($map)->count();
  301. //今日新增预约
  302. $map = [
  303. 'company_id' => $this->company_id,
  304. 'createtime'=>['BETWEEN',[$strattime,$endtime]],
  305. ];
  306. $userinfo['today_newpre'] = Db::name('pre_order')->where($map)->count();
  307. //待处理订单数量
  308. $map = [
  309. 'company_id' => $this->company_id,
  310. 'status' => 2,
  311. ];
  312. $userinfo['order_unfinish'] = Db::name('order')->where($map)->count();
  313. //昨日订单数量
  314. $starttime = strtotime(date('Y-m-d')) - 86400;
  315. $endtime = strtotime(date('Y-m-d')) - 1;
  316. $map = [
  317. 'company_id' => $this->company_id,
  318. 'status' => 3,
  319. 'finish_time'=> ['BETWEEN',[$starttime,$endtime]],
  320. ];
  321. $userinfo['order_zuori'] = Db::name('order')->where($map)->count();
  322. //7日订单数量
  323. $starttime = strtotime(date('Y-m-d')) - 86400*6;
  324. $endtime = strtotime(date('Y-m-d')) + 86399;
  325. $map = [
  326. 'company_id' => $this->company_id,
  327. 'status' => 3,
  328. 'finish_time'=> ['BETWEEN',[$starttime,$endtime]],
  329. ];
  330. $userinfo['order_qiri'] = Db::name('order')->where($map)->count();
  331. return $userinfo;
  332. }
  333. /**
  334. * 获取当前请求的URI
  335. * @return string
  336. */
  337. public function getRequestUri()
  338. {
  339. return $this->requestUri;
  340. }
  341. /**
  342. * 设置当前请求的URI
  343. * @param string $uri
  344. */
  345. public function setRequestUri($uri)
  346. {
  347. $this->requestUri = $uri;
  348. }
  349. /**
  350. * 获取允许输出的字段
  351. * @return array
  352. */
  353. public function getAllowFields()
  354. {
  355. return $this->allowFields;
  356. }
  357. /**
  358. * 设置允许输出的字段
  359. * @param array $fields
  360. */
  361. public function setAllowFields($fields)
  362. {
  363. $this->allowFields = $fields;
  364. }
  365. /**
  366. * 获取密码加密后的字符串
  367. * @param string $password 密码
  368. * @param string $salt 密码盐
  369. * @return string
  370. */
  371. public function getEncryptPassword($password, $salt = '')
  372. {
  373. return md5(md5($password) . $salt);
  374. }
  375. /**
  376. * 检测当前控制器和方法是否匹配传递的数组
  377. *
  378. * @param array $arr 需要验证权限的数组
  379. * @return boolean
  380. */
  381. public function match($arr = [])
  382. {
  383. $request = Request::instance();
  384. $arr = is_array($arr) ? $arr : explode(',', $arr);
  385. if (!$arr) {
  386. return false;
  387. }
  388. $arr = array_map('strtolower', $arr);
  389. // 是否存在
  390. if (in_array(strtolower($request->action()), $arr) || in_array('*', $arr)) {
  391. return true;
  392. }
  393. // 没找到匹配
  394. return false;
  395. }
  396. /**
  397. * 设置会话有效时间
  398. * @param int $keeptime 默认为永久
  399. */
  400. public function keeptime($keeptime = 0)
  401. {
  402. $this->keeptime = $keeptime;
  403. }
  404. /**
  405. * 渲染用户数据
  406. * @param array $datalist 二维数组
  407. * @param mixed $fields 加载的字段列表
  408. * @param string $fieldkey 渲染的字段
  409. * @param string $renderkey 结果字段
  410. * @return array
  411. */
  412. /*public function render(&$datalist, $fields = [], $fieldkey = 'user_id', $renderkey = 'userinfo')
  413. {
  414. $fields = !$fields ? ['id', 'nickname', 'level', 'avatar'] : (is_array($fields) ? $fields : explode(',', $fields));
  415. $ids = [];
  416. foreach ($datalist as $k => $v) {
  417. if (!isset($v[$fieldkey])) {
  418. continue;
  419. }
  420. $ids[] = $v[$fieldkey];
  421. }
  422. $list = [];
  423. if ($ids) {
  424. if (!in_array('id', $fields)) {
  425. $fields[] = 'id';
  426. }
  427. $ids = array_unique($ids);
  428. $selectlist = User::where('id', 'in', $ids)->column($fields);
  429. foreach ($selectlist as $k => $v) {
  430. $list[$v['id']] = $v;
  431. }
  432. }
  433. foreach ($datalist as $k => &$v) {
  434. $v[$renderkey] = isset($list[$v[$fieldkey]]) ? $list[$v[$fieldkey]] : null;
  435. }
  436. unset($v);
  437. return $datalist;
  438. }*/
  439. /**
  440. * 设置错误信息
  441. *
  442. * @param string $error 错误信息
  443. * @return Auth
  444. */
  445. public function setError($error)
  446. {
  447. $this->_error = $error;
  448. return $this;
  449. }
  450. /**
  451. * 获取错误信息
  452. * @return string
  453. */
  454. public function getError()
  455. {
  456. return $this->_error ? __($this->_error) : '';
  457. }
  458. }