SettingService.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Name: 芸众商城系统
  5. * Author: 广州市芸众信息科技有限公司
  6. * Profile: 广州市芸众信息科技有限公司位于国际商贸中心的广州,专注于移动电子商务生态系统打造,拥有芸众社交电商系统、区块链数字资产管理系统、供应链管理系统、电子合同等产品/服务。官网 :www.yunzmall.com www.yunzshop.com
  7. * Date: 2021/9/22
  8. * Time: 13:37
  9. */
  10. namespace business\common\services;
  11. use app\common\facades\Setting;
  12. use app\common\helpers\QrCodeHelper;
  13. use app\common\services\Session;
  14. use business\common\models\Business;
  15. use business\common\models\PlatLog;
  16. use business\common\models\StaffTicketCode;
  17. use EasyWeChat\Kernel\Support;
  18. class SettingService
  19. {
  20. const BUSINESS_PLUGIN_KEY = 'business_plugin_application';
  21. static $business_id;
  22. const PLUGIN_LIST = [
  23. 'group-develop-user' => ['key' => 'GroupDevelopUser', 'name' => '群拓客'],
  24. 'wechat-chat-sidebar' => ['key' => 'WechatChatSidebar', 'name' => '聊天侧边栏'],
  25. 'wechat-customers' => ['key' => 'WechatCustomers', 'name' => '企业客户管理'],
  26. 'yun-chat' => ['key' => 'YunChat', 'name' => '在线客服'],
  27. 'yun-chat-sub' => ['key' => 'YunChatSub', 'name' => '在线客服客服端'],
  28. 'work-wechat-tag' => ['key' => 'WorkWechatTag', 'name' => '企业微信标签'],
  29. 'customer-increase' => ['key' => 'CustomerIncrease', 'name' => '企业微信好友裂变'],
  30. 'group-reward' => ['key' => 'GroupReward', 'name' => '群拓客活动奖励'],
  31. 'sop-task' => ['key' => 'SopTask', 'name' => 'sop任务'],
  32. 'speechcraft-library' => ['key' => 'SpeechcraftLibrary', 'name' => '话术库'],
  33. 'discount-harvest-fans' => ['key' => 'DiscountHarvestFans', 'name' => '让利涨粉'],
  34. 'shop-pos' => ['key' => 'ShopPos', 'name' => 'pos收银'],
  35. 'customer-radar' => ['key' => 'CustomerRadar', 'name' => '拓客雷达'],
  36. 'project-manager' => ['key' => 'ProjectManager', 'name' => '项目管理'],
  37. 'lawyer-platform' => ['key' => 'LawyerPlatform', 'name' => '律所管理'],
  38. 'customer-manage' => ['key' => 'CustomerManage', 'name' => '客户管理'],
  39. 'outbound-system' => ['key' => 'OutboundSystem', 'name' => '外呼电销系统'],
  40. 'drainage-code' => ['key' => 'DrainageCode', 'name' => '爆客码'],
  41. 'crowdfunding' => ['key' => 'Crowdfunding', 'name' => '众筹活动-渠道端'],
  42. 'staff-audit' => ['key' => 'StaffAudit', 'name' => '员工审批'],
  43. 'welcome-words' => ['key' => 'WelcomeWords', 'name' => '欢迎语'],
  44. 'opportunity-management' => ['key' => 'OpportunityManagement', 'name' => '商机管理']
  45. ];
  46. public static function needExamine()
  47. {
  48. if (Setting::get('plugin.work-wechat-platform.create_business_examine')) {
  49. return true;
  50. }
  51. return false;
  52. }
  53. public static function bindBusinessId($business_id = null)
  54. {
  55. $bind_crop_id = app('plugins')->isEnabled('work-wechat-platform') ? Setting::get('plugin.work-wechat-platform')['bind_open_crop_id'] : 0;
  56. if ($business_id !== null) {
  57. return $bind_crop_id == $business_id && $business_id;
  58. }
  59. return $bind_crop_id;
  60. }
  61. /*
  62. * 将插件名从大驼峰转换成-分隔
  63. */
  64. public static function changePluginName($name)
  65. {
  66. //用正则将所有的大写字母替换成 `_字母`,如 `OrderDetail` 替换成 `_Order_Detail`
  67. $name = preg_replace('/[A-Z]/', '-\\0', $name); //_Order_Detail 说明:\\0为反向引用
  68. $name = strtolower($name);
  69. $name = ltrim($name, '-');
  70. return $name;
  71. }
  72. /*
  73. * 获取路由和页面菜单
  74. */
  75. public static function getMenu($business_id = 0)
  76. {
  77. return (new \business\admin\menu\BusinessMenu())->getMenu($business_id);
  78. }
  79. /*
  80. * 判断该企业被后台-企业平台管理插件授权的插件
  81. */
  82. public static function getEnablePlugins($business_id = 0)
  83. {
  84. if (!$business_id) $business_id = self::getBusinessId();
  85. $plat_setting = Business::uniacid()->find($business_id);
  86. return $plat_setting->auth_plugins ? unserialize($plat_setting->auth_plugins) : [];
  87. }
  88. /*
  89. * 判断是否开启了企业微信同步
  90. */
  91. public static function EnabledQyWx()
  92. {
  93. $setting = self::getQyWxSetting();
  94. return $setting['open_state'] ? true : false;
  95. }
  96. /*
  97. * 设置管理的企业ID
  98. */
  99. public static function setBusinessId($business_id)
  100. {
  101. Session::set('business_id', $business_id);
  102. $plat_log = PlatLog::getPlatLog();
  103. if ($plat_log->id) { //记录用户最后管理的企业
  104. $plat_log->final_plat_id = $business_id;
  105. $plat_log->save();
  106. }
  107. }
  108. /*
  109. * 获取当前管理的企业ID
  110. */
  111. public static function getBusinessId()
  112. {
  113. return Session::get('business_id') ?: self::$business_id ?: 0;
  114. }
  115. public static function staffTicketUrl($business_id = 0)
  116. {
  117. $business_id = $business_id ?: self::getBusinessId();
  118. $link_url = request()->getSchemeAndHttpHost().yzApiUrl('plugin.work-wechat.frontend.ticket.index',['i'=>\YunShop::app()->uniacid]);
  119. $link_url = urlencode($link_url);
  120. // $url = 'https://open.weixin.qq.com/connect/oauth2/authorize?';
  121. $setting = self::getQyWxSetting($business_id);
  122. if (!$setting['corpid'] || !$setting['agentid']) {
  123. return '';
  124. }
  125. $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$setting['corpid']}&redirect_uri={$link_url}&response_type=code&scope=snsapi_privateinfo&state={$business_id}&agentid={$setting['agentid']}#wechat_redirect";
  126. return $url;
  127. }
  128. public static function getStaffTicketCodeUrl($business_id = 0)
  129. {
  130. $business_id = $business_id ?: self::getBusinessId();
  131. $res = [
  132. 'ticket_url' => self::staffTicketUrl($business_id),
  133. 'code_url' => '',
  134. ];
  135. if (!$res['ticket_url']) {
  136. return $res;
  137. }
  138. if (!$log = StaffTicketCode::uniacid()->business($business_id)->ticketUrl($res['ticket_url'])->first()) {
  139. $codeClass = new QrCodeHelper($res['ticket_url'], 'app/public/qr/business_ticket_code/' . $business_id);
  140. if (!$codeClass->url()) {
  141. return $res;
  142. }
  143. $log = StaffTicketCode::create([
  144. 'uniacid' => \YunShop::app()->uniacid,
  145. 'code_path' => $codeClass->filePath(),
  146. 'code_url' => $codeClass->url(),
  147. 'ticket_url' => $res['ticket_url'],
  148. 'business_id' => $business_id,
  149. ]);
  150. }
  151. $res['code_url'] = $log->code_url;
  152. return $res;
  153. }
  154. /*
  155. * 获取企业微信设置
  156. */
  157. public static function getQyWxSetting($business_id = 0)
  158. {
  159. $business_id = $business_id ?: self::getBusinessId();
  160. $key = 'business.qy_wx_setting_' . $business_id;
  161. $setting = Setting::get($key) ?: [];
  162. $data = [
  163. 'open_state' => $setting['open_state'] ? 1 : 0,
  164. 'corpid' => $setting['corpid'] ?: '',
  165. 'agentid' => $setting['agentid'] ?: '',
  166. 'agent_secret' => $setting['agent_secret'] ?: '',
  167. 'contact_secret' => $setting['contact_secret'] ?: '',
  168. 'contact_token' => $setting['contact_token'] ?: '',
  169. 'contact_aes_key' => $setting['contact_aes_key'] ?: '',
  170. 'customer_secret' => $setting['customer_secret'] ?: '',
  171. 'customer_token' => $setting['customer_token'] ?: '',
  172. 'customer_aes_key' => $setting['customer_aes_key'] ?: '',
  173. 'work_session_secret' => $setting['work_session_secret'] ?: '',
  174. 'work_session_aes_key' => $setting['work_session_aes_key'] ?: '',
  175. 'work_session_aes_key_version' => $setting['work_session_aes_key_version'] ?: '',
  176. 'contact_notice_url' => request()->getSchemeAndHttpHost() . "/business/" . \YunShop::app()->uniacid . "/frontend/qyWxCallback?business_id=" . $business_id,
  177. 'customer_notice_url' => \app\common\helpers\Url::absoluteApi('plugin.work-wechat.wevent.change-ext-contact.receive', ['type' => 5, 'crop_id' => $business_id]),
  178. ];
  179. return $data;
  180. }
  181. /*
  182. * 设置企业微信设置
  183. */
  184. public static function setQyWxSetting($request_data, $business_id = 0)
  185. {
  186. $business_id = $business_id ?: self::getBusinessId();
  187. $key = 'business.qy_wx_setting_' . $business_id;
  188. $data = [
  189. 'open_state' => $request_data->open_state ? 1 : 0,
  190. 'corpid' => trim($request_data->corpid) ?: '',
  191. 'agentid' => trim($request_data->agentid) ?: '',
  192. 'agent_secret' => trim($request_data->agent_secret) ?: '',
  193. 'contact_secret' => trim($request_data->contact_secret) ?: '',
  194. 'contact_token' => trim($request_data->contact_token) ?: '',
  195. 'contact_aes_key' => trim($request_data->contact_aes_key) ?: '',
  196. 'customer_secret' => trim($request_data->customer_secret) ?: '',
  197. 'customer_token' => trim($request_data->customer_token) ?: '',
  198. 'customer_aes_key' => trim($request_data->customer_aes_key) ?: '',
  199. 'work_session_secret' => trim($request_data->work_session_secret) ?: '',
  200. 'work_session_aes_key' => trim($request_data->work_session_aes_key) ?: '',
  201. 'work_session_aes_key_version' => trim($request_data->work_session_aes_key_version) ?: '',
  202. ];
  203. return Setting::set($key, $data);
  204. }
  205. public static function getToken($business_id = 0, $config = array())
  206. {
  207. if (!$config) {
  208. $settings = static::getQyWxSetting($business_id);
  209. $config = [
  210. 'corp_id' => $settings['corpid'],
  211. 'agent_id' => $settings['agentid'],
  212. 'secret' => $settings['agent_secret'],
  213. ];
  214. }
  215. $app = \app\common\facades\EasyWeChat::work($config);//获取配置信息
  216. $js = $app->jssdk;
  217. $url = \YunShop::request()->url;
  218. if ($url and ($end = strpos($url, '#')) !== false) {
  219. $url = substr($url, 0, $end);
  220. }
  221. $ticket = $js->getTicket();
  222. $getAgentTicket = $js->getAgentTicket();
  223. $timestamp = time();
  224. $noncestr = Support\Str::quickRandom(10);
  225. $str = sha1('jsapi_ticket=' . $ticket['ticket'] . '&noncestr=' . $noncestr . '&timestamp=' . $timestamp . '&url=' . $url);//企业的token
  226. $str2 = sha1('jsapi_ticket=' . $getAgentTicket['ticket'] . '&noncestr=' . $noncestr . '&timestamp=' . $timestamp . '&url=' . $url);//应用的token
  227. $data = [
  228. 'corpid' => $config['corp_id'],
  229. 'agentid' => $config['agent_id'],
  230. 'jsapi_ticket' => $ticket['ticket'],
  231. 'timestamp' => $timestamp,
  232. 'noncestr' => $noncestr,
  233. 'url' => $url,
  234. 'token' => $str,
  235. 'app_token' => $str2
  236. ];
  237. return $data;
  238. }
  239. }