Authcompany.php 14 KB

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