Kefu.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. <?php
  2. namespace app\api\controller;
  3. use think\Db;
  4. use fast\Random;
  5. use think\Config;
  6. use think\Session;
  7. use think\Validate;
  8. use EasyWeChat\Factory;
  9. use app\common\controller\Api;
  10. use addons\kefu\library\Common;
  11. use addons\kefu\library\Captcha;
  12. /**
  13. * KeFu 接口
  14. */
  15. class Kefu extends Api
  16. {
  17. // 无需登录的接口,*表示全部
  18. protected $noNeedLogin = ['acceptWxMsg', 'goodsList', 'orderList', 'login', 'captcha', 'captchaPre'];// 实际使用中请去除`goodsList`和`orderList`
  19. // 无需鉴权的接口,*表示全部
  20. protected $noNeedRight = ['*'];
  21. protected $wxBizMsg; // 消息加解密和辅助类实例
  22. /*
  23. * 获取未读消息数量
  24. */
  25. public function getUnreadMessagesCount()
  26. {
  27. $user = $this->auth->getUser();
  28. // 验证为客服用户
  29. $kefu_user_info = Common::checkKefuUser(false, $user->id);
  30. // 获取与客服的会话
  31. $kefu_session_id = Db::name('kefu_session')->where('user_id', $kefu_user_info['id'])->value('id');
  32. if ($kefu_session_id) {
  33. $unread_msg_count = Db::name('kefu_record')
  34. ->where('session_id', $kefu_session_id)
  35. ->where('sender_identity', 0)
  36. ->where('status', 0)
  37. ->count('id');
  38. } else {
  39. $unread_msg_count = 0;
  40. }
  41. $this->success('ok', $unread_msg_count);
  42. }
  43. /*
  44. * 获取最后一条未读消息
  45. */
  46. public function getUnreadMessages()
  47. {
  48. $user = $this->auth->getUser();
  49. $kefu_user_info = Common::checkKefuUser(false, $user->id);// 验证为客服用户
  50. $this->success('ok', Common::getUnreadMessages($kefu_user_info['user_id']));
  51. }
  52. /*
  53. * 演示用订单列表接口
  54. */
  55. public function orderList()
  56. {
  57. // $user = $this->auth->getUser();
  58. $logo = cdnurl('/assets/addons/kefu/img/buoy1.png', true);
  59. $order_list = [
  60. [
  61. 'id' => 1,
  62. 'subject' => '接口演示订单标题-这是一个演示订单,我来自接口/api/KeFu/orderList',
  63. 'logo' => $logo,
  64. 'note' => '接口:/api/KeFu/orderList',
  65. 'price' => '99',
  66. 'number' => 1
  67. ],
  68. [
  69. 'id' => 2,
  70. 'subject' => '小米9耳机正品type-c适用于8se/10半入耳式mix3 7pro note3/5原装',
  71. 'logo' => $logo,
  72. 'note' => '订单属性订单属性',
  73. 'price' => '101',
  74. 'number' => 2
  75. ],
  76. [
  77. 'id' => 3,
  78. 'subject' => '小米9正品耳机等3件商品',
  79. 'logo' => $logo,
  80. 'note' => '颜色:红色;礼盒:不要礼盒',
  81. 'price' => '100',
  82. 'number' => 3
  83. ]
  84. ];
  85. $this->success('ok', $order_list);
  86. }
  87. /*
  88. * 演示用商品列表接口
  89. * 此接口用于返回客服前台可用的商品列表
  90. */
  91. public function goodsList()
  92. {
  93. // $user = $this->auth->getUser();
  94. $logo = cdnurl('/assets/addons/kefu/img/buoy1.png', true);
  95. $goods_list = [
  96. [
  97. 'id' => 1,
  98. 'subject' => '接口演示商品名称-这是一个演示商品,我来自接口/api/KeFu/goodsList',
  99. 'logo' => $logo,
  100. 'note' => '接口:/api/KeFu/goodsList',
  101. 'price' => '99'
  102. ],
  103. [
  104. 'id' => 2,
  105. 'subject' => '小米9耳机正品type-c适用于8se/10半入耳式mix3 7pro note3/5原装',
  106. 'logo' => $logo,
  107. 'note' => '小米通用',
  108. 'price' => '101'
  109. ],
  110. [
  111. 'id' => 3,
  112. 'subject' => '小米9耳机正品type-c适用于8se/10半入耳式mix3 7pro note3/5原装',
  113. 'logo' => $logo,
  114. 'note' => '小米通用',
  115. 'price' => '100'
  116. ]
  117. ];
  118. $this->success('ok', $goods_list);
  119. }
  120. /*
  121. * 接受/处理来自微信的消息
  122. */
  123. public function acceptWxMsg()
  124. {
  125. $echostr = $this->request->get('echostr');
  126. $data = $this->request->only(['msg_signature', 'timestamp', 'nonce', 'Encrypt']);
  127. if ($echostr) {
  128. if ($this->checkSignature()) {
  129. echo $echostr;
  130. return;
  131. }
  132. }
  133. // 获取微信小程序配置
  134. $wechat_temp = Db::name('kefu_config')
  135. ->whereIn('name', 'wechat_app_id,wechat_app_secret,wechat_token,wechat_encodingkey')
  136. ->select();
  137. foreach ($wechat_temp as $key => $value) {
  138. $wechat_config[$value['name']] = $value['value'];
  139. }
  140. $config = [
  141. 'app_id' => $wechat_config['wechat_app_id'],
  142. 'secret' => $wechat_config['wechat_app_secret'],
  143. 'token' => $wechat_config['wechat_token'],
  144. 'aes_key' => $wechat_config['wechat_encodingkey'],
  145. /*'log' => [
  146. 'level' => 'debug',
  147. 'file' => RUNTIME_PATH . 'log/kefu_wechat.log',
  148. ],*/
  149. ];
  150. $app = Factory::miniProgram($config);
  151. $service = $app->customer_service;
  152. $msg = '';
  153. $this->wxBizMsg = new \addons\kefu\library\WechatCrypto\WXBizMsgCrypt();
  154. $errCode = $this->wxBizMsg->decryptMsg($data['msg_signature'], $data['timestamp'], $data['nonce'], $data['Encrypt'], $msg);
  155. if ($errCode == 0) {
  156. $msg = json_decode($msg, true);
  157. if (!$msg) {
  158. \think\Log::record('微信客服消息解析出错,消息内容:' . $msg, 'notice');
  159. echo "success";
  160. return;
  161. }
  162. if (!empty($msg['MsgType']) && in_array($msg['MsgType'], ["text", "image"])) {
  163. if ($msg['MsgType'] == "image") {
  164. $dlImg = $this->wxBizMsg->saveImg($msg['MediaId']); // 保存图片
  165. $content = request()->domain() . $dlImg;
  166. $message_type = 1;
  167. } else {
  168. $content = $msg['Content'];
  169. $message_type = 0;
  170. }
  171. $session = $this->wxBizMsg->userInitialize($msg['FromUserName']);
  172. if ($session['code'] == 1 || $session['code'] == 2) {
  173. if (Db::name('kefu_blacklist')->where('user_id', $session['kefu_user']['id'])->value('id')) {
  174. $this->wxBizMsg->sendMessage('您的消息被拒收了,请注意您的发言~', $msg['FromUserName']);
  175. return;
  176. }
  177. if ($session['session']) {
  178. // 通知客服新消息
  179. $res = Common::socketMessage($session['session']['id'], $content, $message_type, $session['session']['user_id'] . '||user');
  180. } else {
  181. $user_info = Common::userInfo($session['kefu_user']['id'] . '||user');
  182. $last_leave_message_time = Db::name('kefu_leave_message')
  183. ->where('user_id', $user_info['id'])
  184. ->order('createtime desc')
  185. ->value('createtime');
  186. if ($last_leave_message_time && ($last_leave_message_time + 20) > time()) {
  187. $this->wxBizMsg->sendMessage('由于当前无客服代表在线,请不要频繁发送消息,感谢您的支持!', $msg['FromUserName']);
  188. echo "success";
  189. return;
  190. }
  191. $leave_message = [
  192. 'user_id' => $user_info['id'],
  193. 'name' => $user_info['nickname'],
  194. 'message' => $content,
  195. 'createtime' => time(),
  196. ];
  197. if (Db::name('kefu_leave_message')->insert($leave_message)) {
  198. $leave_message_id = Db::name('kefu_leave_message')->getLastInsID();
  199. // 记录轨迹
  200. $trajectory = [
  201. 'user_id' => $user_info['id'],
  202. 'csr_id' => 0,
  203. 'log_type' => 6,
  204. 'note' => $leave_message_id,
  205. 'url' => '',
  206. 'referrer' => '',
  207. 'createtime' => time(),
  208. ];
  209. Db::name('kefu_trajectory')->insert($trajectory);
  210. $this->wxBizMsg->sendMessage('留言成功!', $msg['FromUserName']);
  211. }
  212. }
  213. } elseif ($session['code'] == 0) {
  214. $this->wxBizMsg->sendMessage($session['msg'], $msg['FromUserName']);
  215. }
  216. } else {
  217. $session = $this->wxBizMsg->userInitialize($msg['FromUserName']);
  218. if ($session['code'] == 1 || $session['code'] == 0) {
  219. $this->wxBizMsg->sendMessage($session['msg'], $msg['FromUserName']);
  220. }
  221. }
  222. } else {
  223. \think\Log::record('微信客服消息解析出错,消息内容 errCode:' . $errCode, 'notice');
  224. }
  225. echo "success";
  226. return;
  227. }
  228. /*是否是验证消息*/
  229. private function checkSignature()
  230. {
  231. $wechat_token = Db::name('kefu_config')->where('name', 'wechat_token')->value('value');
  232. $signature = $this->request->get('signature');
  233. $timestamp = $this->request->get('timestamp');
  234. $nonce = $this->request->get('nonce');
  235. $tmpArr = [$wechat_token, $timestamp, $nonce];
  236. sort($tmpArr, SORT_STRING);
  237. $tmpStr = implode($tmpArr);
  238. $tmpStr = sha1($tmpStr);
  239. if ($tmpStr == $signature) {
  240. return true;
  241. } else {
  242. return false;
  243. }
  244. }
  245. public function login()
  246. {
  247. $username = $this->request->post('username');
  248. $password = $this->request->post('password');
  249. $captcha = $this->request->post('captcha');
  250. $captchaId = $this->request->post('captcha_id');
  251. $rule = [
  252. 'captcha' => 'require|length:4,6',
  253. 'username' => 'require|length:3,30',
  254. 'password' => 'require|length:6,30',
  255. 'captcha_id' => 'require'
  256. ];
  257. $msg = [
  258. 'captcha.require' => '请输入验证码',
  259. 'captcha.length' => '请输入正确的验证码~',
  260. 'username.require' => '用户名不能为空',
  261. 'username.length' => '用户名必须为3到30个字符',
  262. 'password.require' => '密码不能为空',
  263. 'password.length' => '密码必须为3到30个字符',
  264. 'captcha_id.require' => '参数缺失!'
  265. ];
  266. $data = [
  267. 'username' => $username,
  268. 'password' => $password,
  269. 'captcha' => $captcha,
  270. 'captcha_id' => $captchaId
  271. ];
  272. $validate = new Validate($rule, $msg);
  273. $result = $validate->check($data);
  274. if (!$result) {
  275. $this->error(__($validate->getError()));
  276. }
  277. $captchaObj = new Captcha();
  278. if (!$captchaObj->check($captcha, $captchaId)) {
  279. $this->error(__('请输入正确的验证码~'));
  280. }
  281. // 检查为管理员
  282. $userInfo = $this->checkAdminByPassword($username, $password);
  283. if (!$userInfo) {
  284. $this->error('账号或密码错误,请重试!');
  285. }
  286. $userInfo = Common::checkAdmin(false, $userInfo->id);
  287. if ($userInfo) {
  288. $keeptime = 864000;
  289. $expiretime = time() + $keeptime;
  290. // 原规则为单纯的id,若需修改附加的字符串,请将`Common::checkAdmin`方法里边的附加字符串一起修改
  291. $sign = $userInfo['id'] . 'kefu_admin_sign_additional';
  292. $key = md5(md5($sign) . md5($keeptime) . md5($expiretime) . $userInfo['token']);
  293. $tokenData = [$userInfo['id'], $keeptime, $expiretime, $key];
  294. $userInfo['token'] = implode('|', $tokenData);
  295. } else {
  296. $this->error('登录失败,请确认用户是客服代表!');
  297. }
  298. $this->success('ok', [
  299. 'userinfo' => $userInfo
  300. ]);
  301. }
  302. public function captchaPre()
  303. {
  304. $captchaId = md5(Random::uuid());
  305. $this->success('', [
  306. 'captcha_id' => $captchaId
  307. ]);
  308. }
  309. public function captcha()
  310. {
  311. $captchaId = $this->request->request('captcha_id');
  312. $config = array(
  313. 'codeSet' => '123456789', // 验证码字符集合
  314. 'fontSize' => 22, // 验证码字体大小(px)
  315. 'useCurve' => false, // 是否画混淆曲线
  316. 'useNoise' => true, // 是否添加杂点
  317. 'length' => 4, // 验证码位数
  318. 'bg' => array(255, 255, 255), // 背景颜色
  319. );
  320. $captcha = new Captcha($config);
  321. return $captcha->entry($captchaId);
  322. }
  323. private function checkAdminByPassword($username, $password)
  324. {
  325. $admin = \app\admin\model\Admin::get(['username' => $username]);
  326. if (!$admin) {
  327. return false;
  328. }
  329. if ($admin['status'] == 'hidden') {
  330. return false;
  331. }
  332. if (Config::get('fastadmin.login_failure_retry') && $admin->loginfailure >= 10 && time() - $admin->updatetime < 86400) {
  333. return false;
  334. }
  335. if ($admin->password != md5(md5($password) . $admin->salt)) {
  336. $admin->loginfailure++;
  337. $admin->save();
  338. return false;
  339. }
  340. // 不重置管理员token
  341. $admin->loginfailure = 0;
  342. $admin->logintime = time();
  343. $admin->loginip = request()->ip();
  344. if (!$admin->token) {
  345. $admin->token = \fast\Random::uuid();
  346. }
  347. $admin->save();
  348. return $admin;
  349. }
  350. }