Authcompany.php 14 KB

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