QyWechatRequestService.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 Exception;
  12. use EasyWeChat\Factory;
  13. use app\common\facades\EasyWeChat;
  14. class QyWechatRequestService
  15. {
  16. public $business_id;
  17. public $config;
  18. public $easyWechat;
  19. public $base_data;
  20. public $callback_data;
  21. public $return_data;
  22. public $return_columns_handle;
  23. public $return_columns;
  24. public $unset_columns;
  25. public $function;
  26. public $require_data;
  27. public $function_list = [
  28. 'getDepartmentList' => ['name' => '获取部门列表'],
  29. 'createDepartment' => ['name' => '创建部门'],
  30. 'updateDepartment' => ['name' => '修改部门'],
  31. 'deleteDepartment' => ['name' => '删除部门'],
  32. 'getStaffList' => ['name' => '获取部门成员列表'],
  33. 'getStaffDetailList' => ['name' => '获取部门成员详情列表'],
  34. 'getStaffDetail' => ['name' => '获取部门成员详情'],
  35. 'createMember' => ['name' => '创建成员'],
  36. 'updateMember' => ['name' => '编辑成员'],
  37. 'deleteMember' => ['name' => '删除成员'],
  38. 'getMediaId' => ['name' => '上传临时素材'],
  39. 'getStaffTicket'=>['name'=>'成员个人数据授权'],
  40. 'getUserDetail'=>['name'=>'成员个人数据详情'],
  41. 'departmentIds'=>['name'=>'获取企业微信部门ID'],
  42. 'departmentDetail'=>['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', 'contact_secret'];
  49. foreach ($column_arr as $v) {
  50. if (!$this->config[$v]) return;
  51. }
  52. $this->easyWechat = EasyWeChat::work([
  53. // $this->easyWechat = Factory::work([
  54. 'corp_id' => $this->config['corpid'],
  55. 'agent_id' => $this->config['agentid'],
  56. // 'secret' => $this->config['agent_secret'],
  57. 'secret' => $this->config['contact_secret'],
  58. ]);
  59. }
  60. public function request($function, $base_data = [])
  61. {
  62. try {
  63. $this->callback_data = [];
  64. $this->return_data = [];
  65. $this->return_columns_handle = 0;
  66. $this->return_columns = [];
  67. $this->unset_columns = [];
  68. if (!$this->easyWechat) {
  69. throw new Exception('尚未配置企业微信信息');
  70. }
  71. $function_arr = [
  72. 'getDepartmentList', 'departmentIds', 'departmentDetail',
  73. 'getStaffList', 'getStaffDetailList', 'getStaffDetail',
  74. 'getStaffTicket', 'getUserDetail',];
  75. if (in_array($function,$function_arr)){
  76. $this->easyWechat = EasyWeChat::work([
  77. 'corp_id' => $this->config['corpid'],
  78. 'agent_id' => $this->config['agentid'],
  79. 'secret' => $this->config['agent_secret'],
  80. ]);
  81. }
  82. if (!method_exists($this, $function) || !isset($this->function_list[$function])) {
  83. throw new Exception('未知请求方法');
  84. }
  85. $this->base_data = $base_data;
  86. $this->function = $function;
  87. $this->$function();
  88. $this->handleCallback();
  89. } catch (Exception $e) {
  90. return ['result' => 0, 'msg' => $this->function_list[$function]['name'] . '失败,' . $e->getMessage(), 'data' => []];
  91. }
  92. return ['result' => 1, 'msg' => $this->function_list[$function]['name'] . '成功', 'data' => $this->return_data];
  93. }
  94. private function checkRequestData()
  95. {
  96. if ($this->require_data) {
  97. foreach ($this->require_data as $v) {
  98. if (!isset($this->base_data[$v])) {
  99. throw new Exception($v . '为空');
  100. }
  101. }
  102. }
  103. }
  104. //返回数据判断处理
  105. private function handleCallback()
  106. {
  107. $err_msg = $this->callback_data['errmsg'] ?: '未知原因';
  108. if (!$this->callback_data || $this->callback_data['errcode'] !== 0) {
  109. throw new Exception("请求失败,code:{$this->callback_data['errcode']},msg:{$err_msg}");
  110. }
  111. $after_function = 'after' . strtoupper($this->function);
  112. if (method_exists($this, $after_function)) { //自定义返回数据处理方法
  113. $this->$after_function();
  114. } else { //默认返回数据处理方法
  115. $this->defaultAfter();
  116. }
  117. }
  118. private function defaultAfter($callback_data = [])
  119. {
  120. $callback_data = $callback_data ?: $this->callback_data;
  121. if ($this->return_columns_handle) {
  122. if ($this->return_columns) {
  123. foreach ($this->return_columns as $v) {
  124. $this->return_data[$v] = $callback_data[$v];
  125. }
  126. } elseif ($this->unset_columns) {
  127. $return_data = $callback_data;
  128. foreach ($this->unset_columns as $v) {
  129. unset($return_data[$v]);
  130. }
  131. $this->return_data = $return_data;
  132. }
  133. } else {
  134. $this->return_data = $callback_data;
  135. }
  136. }
  137. private function getUserDetail()
  138. {
  139. $this->require_data = ['user_ticket'];
  140. $this->checkRequestData();
  141. $this->callback_data = $this->easyWechat->user->getUserDetail(['user_ticket' => $this->base_data['user_ticket']]);
  142. }
  143. private function getStaffTicket()
  144. {
  145. $this->require_data = ['code'];
  146. $this->checkRequestData();
  147. $this->callback_data = $this->easyWechat->user->getUserInfo(['code' => $this->base_data['code']]);
  148. }
  149. private function getMediaId()
  150. {
  151. $this->require_data = ['path'];
  152. $this->callback_data = $this->easyWechat->media->uploadImage($this->base_data['path']);
  153. }
  154. private function departmentIds()
  155. {
  156. $this->callback_data = $this->easyWechat->department->departmentIds();
  157. }
  158. private function departmentDetail(){
  159. $this->callback_data = $this->easyWechat->department->departmentDetail($this->base_data['id']);
  160. }
  161. private function getDepartmentList()
  162. {
  163. $this->callback_data = $this->easyWechat->department->list();
  164. $this->return_columns_handle = 1;
  165. $this->return_columns = ['department'];
  166. }
  167. private function getStaffDetailList()
  168. {
  169. $this->require_data = ['department_id'];
  170. $this->checkRequestData();
  171. $this->callback_data = $this->easyWechat->user->getDetailedDepartmentUsers($this->base_data['department_id'], true);
  172. $this->return_columns_handle = 1;
  173. $this->return_columns = ['userlist'];
  174. }
  175. private function getStaffDetail()
  176. {
  177. $this->require_data = ['userid'];
  178. $this->checkRequestData();
  179. $this->callback_data = $this->easyWechat->user->get($this->base_data['userid']);
  180. $this->return_columns_handle = 1;
  181. $this->unset_columns = ['errcode', 'errmsg'];
  182. }
  183. private function getStaffList()
  184. {
  185. $this->require_data = ['department_id'];
  186. $this->checkRequestData();
  187. $this->callback_data = $this->easyWechat->user->getDepartmentUsers($this->base_data['department_id'], true);
  188. $this->return_columns_handle = 1;
  189. $this->return_columns = ['userlist'];
  190. }
  191. private function createDepartment()
  192. {
  193. $this->require_data = ['name', 'parent_id'];
  194. $this->checkRequestData();
  195. $request_data = [
  196. 'name' => trim($this->base_data['name']),
  197. 'parentid' => $this->base_data['parent_id'],
  198. 'order' => intval($this->base_data['order']) ?: 10000,
  199. ];
  200. $this->callback_data = $this->easyWechat->department->create($request_data);
  201. $this->return_columns_handle = 1;
  202. $this->return_columns = ['id'];
  203. }
  204. private function updateDepartment()
  205. {
  206. $this->require_data = ['name', 'parent_id', 'id'];
  207. $this->checkRequestData();
  208. $request_data = [
  209. 'name' => trim($this->base_data['name']),
  210. 'order' => intval($this->base_data['order']) ?: 10000,
  211. ];
  212. if ($this->base_data['parent_id']) {
  213. $request_data['parentid'] = $this->base_data['parent_id'];
  214. }
  215. $this->callback_data = $this->easyWechat->department->update($this->base_data['id'], $request_data);
  216. }
  217. private function deleteDepartment()
  218. {
  219. $this->require_data = ['id'];
  220. $this->checkRequestData();
  221. $this->callback_data = $this->easyWechat->department->delete($this->base_data['id']);
  222. }
  223. private function createMember()
  224. {
  225. $this->require_data = ['name', 'userid', 'mobile'];
  226. $this->checkRequestData();
  227. $request_data = $this->base_data;
  228. $this->callback_data = $this->easyWechat->user->create($request_data);
  229. }
  230. private function updateMember()
  231. {
  232. $this->require_data = ['name', 'userid', 'mobile'];
  233. $this->checkRequestData();
  234. $request_data = $this->base_data;
  235. unset($request_data['userid']);
  236. $this->callback_data = $this->easyWechat->user->update($this->base_data['userid'], $request_data);
  237. }
  238. private function deleteMember()
  239. {
  240. $this->require_data = ['userid'];
  241. $this->checkRequestData();
  242. $this->callback_data = $this->easyWechat->user->delete($this->base_data['userid']);
  243. }
  244. }