| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- <?php
- // +----------------------------------------------------------------------
- // | likeshop开源商城系统
- // +----------------------------------------------------------------------
- // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
- // | gitee下载:https://gitee.com/likeshop_gitee
- // | github下载:https://github.com/likeshop-github
- // | 访问官网:https://www.likeshop.cn
- // | 访问社区:https://home.likeshop.cn
- // | 访问手册:http://doc.likeshop.cn
- // | 微信公众号:likeshop技术社区
- // | likeshop系列产品在gitee、github等公开渠道开源版本可免费商用,未经许可不能去除前后端官方版权标识
- // | likeshop系列产品收费版本务必购买商业授权,购买去版权授权后,方可去除前后端官方版权标识
- // | 禁止对系统程序代码以任何目的,任何形式的再发布
- // | likeshop团队版权所有并拥有最终解释权
- // +----------------------------------------------------------------------
- // | author: likeshop.cn.team
- // +----------------------------------------------------------------------
- namespace app\kefuapi\logic;
- use app\common\basics\Logic;
- use app\common\enum\KefuEnum;
- use app\common\model\Admin;
- use app\common\model\kefu\KefuSession;
- use app\common\model\shop\ShopAdmin;
- use app\common\server\UrlServer;
- use think\facade\Config;
- /**
- * 客服登录逻辑
- * Class LoginLogic
- * @package app\shopapi\logic
- */
- class LoginLogic extends Logic
- {
- /**
- * @notes 账号密码登录
- * @param $params
- * @return mixed
- * @throws \think\Exception
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @author 段誉
- * @date 2021/11/9 16:37
- */
- public static function accountLogin($params)
- {
- if (KefuEnum::TYPE_SHOP == $params['type']) {
- $kefu = (new ShopAdmin())->alias('a')
- ->field(['k.id', 'k.nickname', 'k.avatar', 'k.shop_id', 'a.account'])
- ->join('kefu k', 'a.id = k.admin_id and a.shop_id = k.shop_id')
- ->where(['a.account' => $params['account'], 'k.del' => 0])
- ->findOrEmpty()->toArray();
- } else {
- $kefu = (new Admin())->alias('a')
- ->field(['k.id', 'k.nickname', 'k.avatar', 'k.shop_id', 'a.account'])
- ->join('kefu k', 'a.id = k.admin_id')
- ->where(['a.account' => $params['account'], 'k.shop_id' => 0, 'k.del' => 0])
- ->findOrEmpty()->toArray();
- }
- $kefu['avatar'] = !empty($kefu['avatar']) ? UrlServer::getFileUrl($kefu['avatar']) : "";
- $kefu['token'] = self::createSession($kefu['id'], $kefu['shop_id'], $params['client']);
- return $kefu;
- }
- /**
- * @notes 退出登录
- * @param $user_id
- * @param $client
- * @return KefuSession
- * @author 段誉
- * @date 2021/11/23 17:14
- */
- public static function logout($user_id, $client)
- {
- return (new KefuSession())
- ->where(['kefu_id' => $user_id, 'client' => $client])
- ->update(['update_time' => time(), 'expire_time' => time()]);
- }
- /**
- * @notes 创建会话
- * @param $admin_id
- * @param $client
- * @return string
- * @author 段誉
- * @date 2021/11/9 16:38
- */
- public static function createSession($kefu_id, $shop_id, $client)
- {
- $result = KefuSession::where(['kefu_id' => $kefu_id, 'client' => $client])->findOrEmpty();
- $time = time();
- $expire_time = $time + Config::get('project.token_expire_time');
- // 新token
- $token = md5($kefu_id . $client . $time);
- $data = [
- 'shop_id' => $shop_id,
- 'kefu_id' => $kefu_id,
- 'token' => $token,
- 'client' => $client,
- 'update_time' => $time,
- 'expire_time' => $expire_time,
- ];
- if ($result->isEmpty()) {
- KefuSession::create($data);
- } else {
- KefuSession::where([
- 'kefu_id' => $kefu_id,
- 'shop_id' => $shop_id,
- 'client' => $client,
- ])->update($data);
- }
- return $token;
- }
- }
|