OfficialAccountApi.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. <?php
  2. /**
  3. * OfficialAccountApi.php UTF-8
  4. * 公众号消息API
  5. *
  6. * @date : 2018/9/13 18:03
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HuoMp 1.0
  11. */
  12. namespace huoMpMsg\controller;
  13. use huo\controller\common\Base;
  14. use huoMpMsg\constant\OfficialAccountConst;
  15. use huoMpMsg\status\OfficialAccountStatus;
  16. class OfficialAccountApi extends Base {
  17. /**
  18. * 获取微信服务器IP地址
  19. * https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140187
  20. *
  21. * @param string $wx_app_id 第三方用户唯一凭证
  22. * @param string $wx_app_secret 第三方用户唯一凭证密钥,即appsecret
  23. *
  24. * @return array
  25. */
  26. public function getCallbackIp($wx_app_id, $wx_app_secret) {
  27. $_url = 'https://api.weixin.qq.com/cgi-bin/getcallbackip';
  28. $_access_token = Common::getAccessToken($wx_app_id, $wx_app_secret);
  29. $_url = $_url.'?access_token='.$_access_token;
  30. $_param = '';
  31. $_ret = Common::curl($_url, $_param);
  32. $_rdata = [];
  33. if (Common::isJson($_ret)) {
  34. $_rdata = json_decode($_ret, true);
  35. }
  36. if (isset($_rdata['errcode'])) {
  37. $_code = $_rdata['errcode'];
  38. return $this->huoError($_code, $_rdata['errmsg']);
  39. }
  40. return $_rdata['ip_list'];
  41. }
  42. /**
  43. * 添加客服帐号
  44. * https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140547
  45. *
  46. * @param string $wx_app_id 第三方用户唯一凭证
  47. * @param string $wx_app_secret 第三方用户唯一凭证密钥,即appsecret
  48. *
  49. * @return array
  50. */
  51. public function addFfAccount($wx_app_id, $wx_app_secret, $kf_account, $nickname, $password) {
  52. $_url = 'https://api.weixin.qq.com/customservice/kfaccount/add';
  53. $_access_token = Common::getAccessToken($wx_app_id, $wx_app_secret);
  54. $_url = $_url.'?access_token='.$_access_token;
  55. $_param['kf_account'] = $kf_account;
  56. $_param['nickname'] = $nickname;
  57. $_param['password'] = $password;
  58. $_ret = Common::curl($_url, http_build_query($_param), 'POST');
  59. $_rdata = [];
  60. if (Common::isJson($_ret)) {
  61. $_rdata = json_decode($_ret, true);
  62. }
  63. if (isset($_rdata['errcode']) && '0' != $_rdata['errcode']) {
  64. $_code = $_rdata['errcode'];
  65. return $this->huoError($_code, $_rdata['errmsg']);
  66. }
  67. $_code = OfficialAccountStatus::NO_ERROR;
  68. return $this->huoSuccess($_code, OfficialAccountStatus::getMsg($_code));
  69. }
  70. /**
  71. * 修改客服帐号
  72. * https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140547
  73. *
  74. * @param string $wx_app_id 第三方用户唯一凭证
  75. * @param string $wx_app_secret 第三方用户唯一凭证密钥,即appsecret
  76. *
  77. * @param string $kf_account 客服邮箱
  78. * @param string $nickname 客服昵称
  79. * @param string $password 客服密码
  80. *
  81. * @return array
  82. */
  83. public function updateKfAccount($wx_app_id, $wx_app_secret, $kf_account, $nickname, $password) {
  84. $_url = 'https://api.weixin.qq.com/customservice/kfaccount/update';
  85. $_access_token = Common::getAccessToken($wx_app_id, $wx_app_secret);
  86. $_url = $_url.'?access_token='.$_access_token;
  87. $_param['kf_account'] = $kf_account;
  88. $_param['nickname'] = $nickname;
  89. $_param['password'] = $password;
  90. $_ret = Common::curl($_url, http_build_query($_param), 'POST');
  91. $_rdata = [];
  92. if (Common::isJson($_ret)) {
  93. $_rdata = json_decode($_ret, true);
  94. }
  95. if (isset($_rdata['errcode']) && '0' != $_rdata['errcode']) {
  96. $_code = $_rdata['errcode'];
  97. return $this->huoError($_code, $_rdata['errmsg']);
  98. }
  99. $_code = OfficialAccountStatus::NO_ERROR;
  100. return $this->huoSuccess($_code, OfficialAccountStatus::getMsg($_code));
  101. }
  102. /**
  103. * 删除客服帐号
  104. * https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140547
  105. *
  106. * @param string $wx_app_id 第三方用户唯一凭证
  107. * @param string $wx_app_secret 第三方用户唯一凭证密钥,即appsecret
  108. *
  109. * @param string $kf_account 客服邮箱
  110. * @param string $nickname 客服昵称
  111. * @param string $password 客服密码
  112. *
  113. * @return array
  114. */
  115. public function delKfAccount($wx_app_id, $wx_app_secret, $kf_account, $nickname, $password) {
  116. $_url = 'https://api.weixin.qq.com/customservice/kfaccount/del';
  117. $_access_token = Common::getAccessToken($wx_app_id, $wx_app_secret);
  118. $_url = $_url.'?access_token='.$_access_token;
  119. $_param['kf_account'] = $kf_account;
  120. $_param['nickname'] = $nickname;
  121. $_param['password'] = $password;
  122. $_ret = Common::curl($_url, http_build_query($_param), 'POST');
  123. $_rdata = [];
  124. if (Common::isJson($_ret)) {
  125. $_rdata = json_decode($_ret, true);
  126. }
  127. if (isset($_rdata['errcode']) && '0' != $_rdata['errcode']) {
  128. $_code = $_rdata['errcode'];
  129. return $this->huoError($_code, $_rdata['errmsg']);
  130. }
  131. $_code = OfficialAccountStatus::NO_ERROR;
  132. return $this->huoSuccess($_code, OfficialAccountStatus::getMsg($_code));
  133. }
  134. /**
  135. * 设置客服帐号的头像
  136. * https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140547
  137. *
  138. * @param string $wx_app_id 第三方用户唯一凭证
  139. * @param string $wx_app_secret 第三方用户唯一凭证密钥,即appsecret
  140. *
  141. * @param string $kf_account 客服邮箱
  142. *
  143. * @return array
  144. */
  145. public function uploadKfHeadImg($wx_app_id, $wx_app_secret, $kf_account) {
  146. $_url = 'http://api.weixin.qq.com/customservice/kfaccount/uploadheadimg';
  147. $_code = OfficialAccountStatus::NO_ERROR;
  148. return $this->huoSuccess($_code, OfficialAccountStatus::getMsg($_code));
  149. }
  150. /**
  151. * 获取所有客服账号
  152. * https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140547
  153. *
  154. * @param string $wx_app_id 第三方用户唯一凭证
  155. * @param string $wx_app_secret 第三方用户唯一凭证密钥,即appsecret
  156. *
  157. * @return array
  158. */
  159. public function getKfList($wx_app_id, $wx_app_secret) {
  160. $_url = 'https://api.weixin.qq.com/cgi-bin/customservice/getkflist';
  161. $_access_token = Common::getAccessToken($wx_app_id, $wx_app_secret);
  162. $_url = $_url.'?access_token='.$_access_token;
  163. $_ret = Common::curl($_url, '');
  164. $_rdata = [];
  165. if (Common::isJson($_ret)) {
  166. $_rdata = json_decode($_ret, true);
  167. }
  168. if (isset($_rdata['errcode']) && '0' != $_rdata['errcode']) {
  169. $_code = $_rdata['errcode'];
  170. return $this->huoError($_code, $_rdata['errmsg']);
  171. }
  172. $_code = OfficialAccountStatus::NO_ERROR;
  173. return $this->huoSuccess($_code, OfficialAccountStatus::getMsg($_code), $_rdata);
  174. }
  175. /**
  176. * 获取所有客服账号
  177. * https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140547
  178. *
  179. * @param string $wx_app_id 第三方用户唯一凭证
  180. * @param string $wx_app_secret 第三方用户唯一凭证密钥,即appsecret
  181. *
  182. * @param string $open_id
  183. * @param string $msgtype 消息类型
  184. * @param mixd $data
  185. *
  186. * @return array
  187. */
  188. public function sendMsg($wx_app_id, $wx_app_secret, $open_id, $msgtype, $data = null) {
  189. $_url = 'https://api.weixin.qq.com/cgi-bin/message/custom/send';
  190. $_access_token = Common::getAccessToken($wx_app_id, $wx_app_secret);
  191. $_url = $_url.'?access_token='.$_access_token;
  192. $_param['touser'] = $open_id;
  193. $_param['msgtype'] = $msgtype;
  194. switch ($msgtype) {
  195. case OfficialAccountConst::MSG_TYPE_TEXT:
  196. $_param[$msgtype] = $data;
  197. break;
  198. case OfficialAccountConst::MSG_TYPE_LINK:
  199. case OfficialAccountConst::MSG_TYPE_IMAGE:
  200. case OfficialAccountConst::MSG_TYPE_VOICE:
  201. $_param[$msgtype] = $data;
  202. break;
  203. case OfficialAccountConst::MSG_TYPE_VIDEO:
  204. break;
  205. case OfficialAccountConst::MSG_TYPE_MUSIC:
  206. break;
  207. case OfficialAccountConst::MSG_TYPE_NEWS:
  208. break;
  209. case OfficialAccountConst::MSG_TYPE_MPNEWS:
  210. break;
  211. case OfficialAccountConst::MSG_TYPE_WXCARD:
  212. break;
  213. case OfficialAccountConst::MSG_TYPE_MINIPROGRAMPAGE:
  214. break;
  215. default:
  216. break;
  217. }
  218. $_param_json = json_encode($_param, JSON_UNESCAPED_UNICODE);
  219. $_header = ['Content-Type: application/octet-stream; charset=utf-8'];
  220. $_ret = Common::curl($_url, $_param_json, 'POST', $_header);
  221. $_rdata = [];
  222. if (Common::isJson($_ret)) {
  223. $_rdata = json_decode($_ret, true);
  224. }
  225. if (isset($_rdata['errcode']) && '0' != $_rdata['errcode']) {
  226. $_code = $_rdata['errcode'];
  227. return $this->huoError($_code, $_rdata['errmsg']);
  228. }
  229. $_code = OfficialAccountStatus::NO_ERROR;
  230. return $this->huoSuccess($_code, OfficialAccountStatus::getMsg($_code));
  231. }
  232. /**
  233. * 发送模板消息
  234. * https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433751277
  235. *
  236. * @param string $wx_app_id 第三方用户唯一凭证
  237. * @param string $wx_app_secret 第三方用户唯一凭证密钥,即appsecret
  238. *
  239. * @param string $open_id
  240. * @param string $template_id 模板ID
  241. * @param string $url 模板跳转链接
  242. * @param array $miniprogram 跳小程序所需数据,不需跳小程序可不用传该数据
  243. * @param mixed $data 模板数据
  244. *
  245. * @return array
  246. */
  247. public function sendTemplateMsg(
  248. $wx_app_id, $wx_app_secret, $open_id, $template_id, $url = '', $miniprogram = [], $data
  249. ) {
  250. $_url = 'https://api.weixin.qq.com/cgi-bin/message/template/send';
  251. $_access_token = Common::getAccessToken($wx_app_id, $wx_app_secret);
  252. $_url = $_url.'?access_token='.$_access_token;
  253. $_param['touser'] = $open_id;
  254. $_param['template_id'] = $template_id;
  255. $_param['url'] = $url;
  256. $_param['miniprogram'] = $miniprogram;
  257. $_param['data'] = $data;
  258. $_param_json = json_encode($_param);
  259. $_header = ['Content-Type: application/octet-stream; charset=utf-8'];
  260. $_ret = Common::curl($_url, $_param_json, 'POST', $_header);
  261. $_rdata = [];
  262. if (Common::isJson($_ret)) {
  263. $_rdata = json_decode($_ret, true);
  264. }
  265. if (isset($_rdata['errcode']) && '0' != $_rdata['errcode']) {
  266. $_code = $_rdata['errcode'];
  267. return $this->huoError($_code, $_rdata['errmsg']);
  268. }
  269. $_code = OfficialAccountStatus::NO_ERROR;
  270. return $this->huoSuccess($_code, OfficialAccountStatus::getMsg($_code), $_rdata);
  271. }
  272. /**
  273. * 发送模板消息
  274. * https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433751277
  275. *
  276. * @param string $wx_app_id 第三方用户唯一凭证
  277. * @param string $wx_app_secret 第三方用户唯一凭证密钥,即appsecret
  278. * @param string $file_path 图片绝对路径
  279. *
  280. * @return array
  281. */
  282. public function imageUpload($wx_app_id, $wx_app_secret, $file_path) {
  283. $_url = 'https://api.weixin.qq.com/cgi-bin/media/upload';
  284. $_access_token = Common::getAccessToken($wx_app_id, $wx_app_secret);
  285. $_type = "image";
  286. $_url = $_url.'?access_token='.$_access_token.'&type='.$_type;
  287. //这里声明文件的路径,使用绝对路径
  288. $_file_data = array("media" => new \CURLFile($file_path));
  289. $ch = curl_init();
  290. curl_setopt($ch, CURLOPT_URL, $_url);
  291. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  292. curl_setopt($ch, CURLOPT_POST, 1);
  293. curl_setopt($ch, CURLOPT_POSTFIELDS, $_file_data);
  294. $_ret = curl_exec($ch);//发送请求获取结果
  295. curl_close($ch);//关闭会话
  296. $_rdata = [];
  297. if (Common::isJson($_ret)) {
  298. $_rdata = json_decode($_ret, true);
  299. }
  300. if (isset($_rdata['errcode']) && '0' != $_rdata['errcode']) {
  301. $_code = $_rdata['errcode'];
  302. return $this->huoError($_code, $_rdata['errmsg']);
  303. }
  304. $_code = OfficialAccountStatus::NO_ERROR;
  305. return $this->huoSuccess($_code, OfficialAccountStatus::getMsg($_code), $_rdata);
  306. }
  307. }