WechatCustomerRequestService.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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\customers;
  11. use app\common\facades\EasyWeChat;
  12. use Exception;
  13. use EasyWeChat\Factory;
  14. use business\common\services\SettingService;
  15. class WechatCustomerRequestService
  16. {
  17. public $business_id;
  18. public $config;
  19. public $easyWechat;
  20. public $base_data;
  21. public $callback_data;
  22. public $return_data;
  23. public $return_columns_handle;
  24. public $return_columns;
  25. public $unset_columns;
  26. public $function;
  27. public $require_data;
  28. public $function_list = [
  29. 'getCustomerList' => ['name' => '获取企业客户标签列表'],
  30. 'addTag' => ['name' => '新建标签'],
  31. 'editTag' => ['name' => '修改标签'],
  32. 'deleteTag' => ['name' => '删除标签'],
  33. 'addTagGroup' => ['name' => '新建标签组'],
  34. 'editTagGroup' => ['name' => '修改标签组'],
  35. 'changeMemberTag' => ['name' => '修改客户关联标签'],
  36. 'customerForeverSingleCode' => ['name' => '新增单人联系我(永久)'],
  37. 'deleteCustomerCode' => ['name' => '删除联系我'],
  38. 'sendWelcomeWord' => ['name' => '发送企业客户欢迎语'],
  39. 'addGroupCode' => ['name' => '新增企业微信群邀请码'],
  40. 'editGroupCode' => ['name' => '更新企业微信群邀请码'],
  41. 'deleteGroupCode' => ['name' => '删除企业微信群邀请码'],
  42. 'getGroupCode' => ['name' => '获取企业微信群邀请码'],
  43. ];
  44. public function __construct($business_id = 0)
  45. {
  46. $business_id = intval($business_id) ?: SettingService::getBusinessId();
  47. $this->config = SettingService::getQyWxSetting($business_id);
  48. $column_arr = ['corpid', 'customer_secret'];
  49. foreach ($column_arr as $v) {
  50. if (!$this->config[$v]) return;
  51. }
  52. $this->easyWechat = EasyWeChat::work( [
  53. 'corp_id' => $this->config['corpid'],
  54. 'secret' => $this->config['customer_secret'],
  55. ]);
  56. // $this->easyWechat = Factory::work([
  57. // 'corp_id' => $this->config['corpid'],
  58. // 'secret' => $this->config['customer_secret'],
  59. // ]);
  60. }
  61. public function request($function, $base_data = [])
  62. {
  63. try {
  64. $this->callback_data = [];
  65. $this->return_data = [];
  66. $this->return_columns_handle = 0;
  67. $this->return_columns = [];
  68. $this->unset_columns = [];
  69. if (!$this->easyWechat) {
  70. throw new Exception('尚未配置企业微信信息');
  71. }
  72. if (!method_exists($this, $function) || !isset($this->function_list[$function])) {
  73. throw new Exception('未知请求方法');
  74. }
  75. $this->base_data = $base_data;
  76. $this->function = $function;
  77. $this->$function();
  78. $this->handleCallback();
  79. } catch (Exception $e) {
  80. return ['result' => 0, 'msg' => $this->function_list[$function]['name'] . '失败,' . $e->getMessage(), 'data' => []];
  81. }
  82. return ['result' => 1, 'msg' => $this->function_list[$function]['name'] . '成功', 'data' => $this->return_data];
  83. }
  84. protected function getGroupCode()
  85. {
  86. $this->callback_data = $this->easyWechat->contact_way->getGroupCode($this->base_data['config_id'] ?: '');
  87. }
  88. protected function addGroupCode()
  89. {
  90. $params = [
  91. 'scene' => isset($this->base_data['scene']) ? $this->base_data['scene'] : 2,
  92. 'remark' => isset($this->base_data['remark']) ? $this->base_data['remark'] : '',
  93. 'auto_create_room' => isset($this->base_data['auto_create_room']) ? $this->base_data['auto_create_room'] : 1,
  94. 'chat_id_list' => $this->base_data['chat_id_list'],
  95. ];
  96. if (isset($this->base_data['state'])) {
  97. $params['state'] = $this->base_data['state'];
  98. }
  99. if ($params['auto_create_room']) {
  100. $params['room_base_name'] = isset($this->base_data['room_base_name']) ? trim($this->base_data['room_base_name']) : '客户群';
  101. $params['room_base_id'] = isset($this->base_data['room_base_id']) ? intval($this->base_data['room_base_id']) : 2;
  102. }
  103. $this->callback_data = $this->easyWechat->contact_way->addGroupCode($params);
  104. }
  105. protected function editGroupCode()
  106. {
  107. $params = [
  108. 'config_id' => $this->base_data['config_id'],
  109. 'scene' => isset($this->base_data['scene']) ? $this->base_data['scene'] : 2,
  110. 'remark' => isset($this->base_data['remark']) ? $this->base_data['remark'] : '',
  111. 'auto_create_room' => isset($this->base_data['auto_create_room']) ? $this->base_data['auto_create_room'] : 1,
  112. 'chat_id_list' => $this->base_data['chat_id_list'],
  113. ];
  114. if (isset($this->base_data['state'])) {
  115. $params['state'] = $this->base_data['state'];
  116. }
  117. if ($params['auto_create_room']) {
  118. $params['room_base_name'] = isset($this->base_data['room_base_name']) ? trim($this->base_data['room_base_name']) : '客户群';
  119. $params['room_base_id'] = isset($this->base_data['room_base_id']) ? intval($this->base_data['room_base_id']) : 2;
  120. }
  121. $this->callback_data = $this->easyWechat->contact_way->editGroupCode($params);
  122. }
  123. protected function deleteGroupCode()
  124. {
  125. $this->callback_data = $this->easyWechat->contact_way->deleteGroupCode($this->base_data['config_id'] ?: '');
  126. if ($this->callback_data['errcode'] == 41044) {
  127. $this->callback_data['errcode'] = 0;
  128. }
  129. }
  130. protected function sendWelcomeWord()
  131. {
  132. $this->callback_data = $this->easyWechat->external_contact_message->sendWelcome($this->base_data['welcome_code'], $this->base_data);
  133. }
  134. protected function customerForeverSingleCode()
  135. {
  136. $request_data = [
  137. 'remark' => $this->base_data['remark'] ?: '',
  138. 'skip_verify' => true,
  139. 'state' => $this->base_data['state'] ?: '',
  140. 'user' => $this->base_data['user'],
  141. ];
  142. if ($this->base_data['is_temp']){
  143. $request_data['is_temp'] = true;
  144. }
  145. $this->callback_data = $this->easyWechat->contact_way->create(1, 2, $request_data);
  146. }
  147. protected function deleteCustomerCode()
  148. {
  149. $this->callback_data = $this->easyWechat->contact_way->delete($this->base_data['config_id']);
  150. if ($this->callback_data['errcode'] == 41044) {
  151. $this->callback_data['errcode'] = 0;
  152. }
  153. }
  154. protected function changeMemberTag()
  155. {
  156. $this->require_data = ['userid', 'external_userid'];
  157. $this->checkRequestData();
  158. if (!$this->base_data['add_tag'] && !$this->base_data['remove_tag']) {
  159. throw new Exception('请选择要变动的标签');
  160. }
  161. $this->callback_data = $this->easyWechat->external_contact->markTags($this->base_data);
  162. }
  163. protected function addTag()
  164. {
  165. $this->require_data = ['name_arr'];
  166. $this->checkRequestData();
  167. if (!$this->base_data['group_id'] && !$this->base_data['group_name']) {
  168. throw new Exception('请输入标签组信息');
  169. }
  170. $request_data = [
  171. 'group_id' => $this->base_data['group_id'],
  172. 'group_name' => $this->base_data['group_name'],
  173. 'order' => intval($this->base_data['order']) ?: 0,
  174. 'tag' => []
  175. ];
  176. foreach ($this->base_data['name_arr'] as $k => $v) {
  177. $request_data['tag'][] = [
  178. 'name' => $v,
  179. 'order' => intval($this->base_data['order_arr'][$k]) ?: 0
  180. ];
  181. }
  182. $this->callback_data = $this->easyWechat->external_contact->addCorpTag($request_data);
  183. if ($this->callback_data['errcode'] == '40071') {
  184. throw new Exception('code:40071,msg:企业微信当前标签组下存在同名标签');
  185. }
  186. $this->return_columns_handle = 1;
  187. $this->return_columns = ['tag_group'];
  188. }
  189. protected function editTag()
  190. {
  191. $this->require_data = ['id', 'name'];
  192. $this->checkRequestData();
  193. $order = intval($this->base_data['order']);
  194. $this->callback_data = $this->easyWechat->external_contact->updateCorpTag($this->base_data['id'], $this->base_data['name'], $order);
  195. if ($this->callback_data['errcode'] == '40068') {
  196. throw new Exception('code:40068,msg:请求的企业微信标签不存在或已删除,请先同步企业微信标签,tagId:' . $this->base_data['id']);
  197. }
  198. }
  199. protected function deleteTag()
  200. {
  201. if (!$this->base_data['tag_id'] && !$this->base_data['group_id']) {
  202. throw new Exception('请选择要删除的标签或标签组');
  203. }
  204. $this->callback_data = $this->easyWechat->external_contact->deleteCorpTag($this->base_data['tag_id'] ?: [], $this->base_data['group_id'] ?: []);
  205. }
  206. protected function getCustomerList()
  207. {
  208. $this->callback_data = $this->easyWechat->external_contact->getCorpTags($this->base_data['tag_id'] ?: [], $this->base_data['group_id'] ?: []);
  209. $this->return_columns_handle = 1;
  210. $this->return_columns = ['tag_group'];
  211. }
  212. protected function checkRequestData()
  213. {
  214. if ($this->require_data) {
  215. foreach ($this->require_data as $v) {
  216. if (!isset($this->base_data[$v])) {
  217. throw new Exception($v . '为空');
  218. }
  219. }
  220. }
  221. }
  222. //返回数据判断处理
  223. protected function handleCallback()
  224. {
  225. $err_msg = $this->callback_data['errmsg'] ?: '未知原因';
  226. if (!$this->callback_data || $this->callback_data['errcode'] !== 0) {
  227. throw new Exception("请求失败,code:{$this->callback_data['errcode']},msg:{$err_msg}");
  228. }
  229. $after_function = 'after' . strtoupper($this->function);
  230. if (method_exists($this, $after_function)) { //自定义返回数据处理方法
  231. $this->$after_function();
  232. } else { //默认返回数据处理方法
  233. $this->defaultAfter();
  234. }
  235. }
  236. protected function defaultAfter($callback_data = [])
  237. {
  238. $callback_data = $callback_data ?: $this->callback_data;
  239. if ($this->return_columns_handle) {
  240. if ($this->return_columns) {
  241. foreach ($this->return_columns as $v) {
  242. $this->return_data[$v] = $callback_data[$v];
  243. }
  244. } elseif ($this->unset_columns) {
  245. $return_data = $callback_data;
  246. foreach ($this->unset_columns as $v) {
  247. unset($return_data[$v]);
  248. }
  249. $this->return_data = $return_data;
  250. } else {
  251. $this->return_data = $this->callback_data;
  252. }
  253. } else {
  254. $this->return_data = $this->callback_data;
  255. }
  256. }
  257. }