miniUtil.php 82 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044
  1. <?php
  2. namespace common\components;
  3. use bizHd\wx\services\WxBaseService;
  4. use bizHd\wx\services\WxMiniBaseService;
  5. use common\services\xhMerchantExtendService;
  6. use Yii;
  7. use yii\helpers\Json;
  8. use linslin\yii2\curl;
  9. use common\services\xhWxOpenService;
  10. use common\services\xhMerchantService;
  11. /**
  12. * 小程序开发文楼
  13. */
  14. class miniUtil
  15. {
  16. public $access_token;
  17. public function __construct($merchant = '', $ptStyle = 1)
  18. {
  19. $this->access_token = !empty($merchant) ? self::getMiniProgramAccessToken($merchant, $ptStyle) : '';
  20. }
  21. //获取用户基本信息
  22. public static function userInfo($openId, $merchant, $ptStyle = 0)
  23. {
  24. $accessToken = self::getAuthorizerAccessToken($merchant, $ptStyle);
  25. $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=$accessToken&openid=$openId";
  26. $curl = new curl\Curl();
  27. $result = $curl->get($url);
  28. $result = Json::decode($result);
  29. if (isset($result['errcode'])) {
  30. } else {
  31. return $result;
  32. }
  33. }
  34. //创建开放平台帐号
  35. public static function createOpenAccount($appId, $merchant, $ptStyle = 0)
  36. {
  37. $accessToken = self::getAuthorizerAccessToken($merchant, $ptStyle);
  38. $url = "https://api.weixin.qq.com/cgi-bin/open/create?access_token=" . $accessToken;
  39. $curl = new curl\Curl();
  40. $data = ['appid' => $appId];
  41. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  42. $result = Json::decode($result);
  43. return $result;
  44. }
  45. //绑定到开放平台
  46. public static function bindOpenAccount($appId, $openAppId, $merchant, $isMiniProgram, $ptStyle = 0)
  47. {
  48. if ($isMiniProgram) {
  49. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  50. } else {
  51. $accessToken = self::getAuthorizerAccessToken($merchant, $ptStyle);
  52. }
  53. $url = 'https://api.weixin.qq.com/cgi-bin/open/bind?access_token=' . $accessToken;
  54. $curl = new curl\Curl();
  55. $data = ['appid' => $appId, 'open_appid' => $openAppId];
  56. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  57. $result = Json::decode($result);
  58. return $result;
  59. }
  60. //获取公众号、小程序绑定的开放平台帐号
  61. public static function getOpenAccount($appId, $merchant, $isMiniProgram, $ptStyle = 0)
  62. {
  63. if ($isMiniProgram) {
  64. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  65. } else {
  66. $accessToken = self::getAuthorizerAccessToken($merchant, $ptStyle);
  67. }
  68. $url = "https://api.weixin.qq.com/cgi-bin/open/get?access_token=" . $accessToken;
  69. $curl = new curl\Curl();
  70. $data = ['appid' => $appId];
  71. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  72. $result = Json::decode($result);
  73. return $result;
  74. }
  75. //解绑公众号绑定的平台 ssh 2019.9.15
  76. public static function unbindAccount($merchant, $ptStyle = 0)
  77. {
  78. $accessToken = self::getAuthorizerAccessToken($merchant, $ptStyle);
  79. $url = "https://api.weixin.qq.com/cgi-bin/open/unbind?access_token=" . $accessToken;
  80. $curl = new curl\Curl();
  81. $openAppId = $merchant['openAppId'];
  82. $appId = $merchant['wxAppId'];
  83. $data = ['appid' => $appId, 'open_appid' => $openAppId];
  84. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  85. $result = Json::decode($result);
  86. return $result;
  87. }
  88. //解绑小程序绑定的平台 ssh 2019.9.15
  89. public static function unbindMiniProgram($merchant, $ptStyle = 0)
  90. {
  91. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  92. $url = "https://api.weixin.qq.com/cgi-bin/open/unbind?access_token=" . $accessToken;
  93. $curl = new curl\Curl();
  94. $openAppId = $merchant['openAppId'];
  95. $appId = $merchant['miniAppId'];
  96. $data = ['appid' => $appId, 'open_appid' => $openAppId];
  97. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  98. $result = Json::decode($result);
  99. return $result;
  100. }
  101. public static function getSubscribeUserList($merchant, $nextOpenId = '', $ptStyle = 1)
  102. {
  103. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  104. $url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=$accessToken&next_openid=$nextOpenId";
  105. $curl = new curl\Curl();
  106. $result = $curl->get($url);
  107. return Json::decode($result);
  108. }
  109. public static function batchGetUserInfo($merchant, $openIdList, $ptStyle)
  110. {
  111. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  112. $url = "https://api.weixin.qq.com/cgi-bin/user/info/batchget?access_token=" . $accessToken;
  113. $format = [];
  114. foreach ($openIdList as $val) {
  115. $format[] = ['openid' => $val, 'lang' => "zh-CN"];
  116. }
  117. $newOpenIdList = ["user_list" => $format];
  118. $curl = new curl\Curl();
  119. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($newOpenIdList))->post($url);
  120. $result = Json::decode($result);
  121. return $result;
  122. }
  123. //查询所有分组
  124. public static function groupList($accessToken)
  125. {
  126. $url = "https://api.weixin.qq.com/cgi-bin/groups/get?access_token=$accessToken";
  127. $curl = new curl\Curl();
  128. $result = $curl->get($url);
  129. $result = Json::decode($result);
  130. if (isset($result['errcode'])) {
  131. return null;
  132. } else {
  133. return $result;
  134. }
  135. }
  136. /**
  137. * 提交自定义菜单到微信平台
  138. */
  139. public function userMenuToWeiXin($menu)
  140. {
  141. $jsonData = urldecode(json_encode($menu, JSON_UNESCAPED_UNICODE));
  142. $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" . $this->access_token;
  143. $curl = new curl\Curl();
  144. $response = $curl->setOption(CURLOPT_POSTFIELDS, $jsonData)->post($url);
  145. return $response;
  146. }
  147. /**
  148. * 上传图片多媒体文件
  149. */
  150. public static function uploadImgMedia($merchant, $fullFile)
  151. {
  152. $access_token = self::getMiniProgramAccessToken($merchant, $ptStyle);
  153. $url = "http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=" . $access_token . "&type=image";
  154. $curl = curl_init();
  155. if (class_exists('\CURLFile')) {//关键是判断curlfile,官网推荐php5.5或更高的版本使用curlfile来实例文件
  156. $data = array('media' => new \CURLFile($fullFile));
  157. } else {
  158. $data = array('media' => file_get_contents($fullFile));
  159. }
  160. if (class_exists('\CURLFile')) {
  161. curl_setopt($curl, CURLOPT_SAFE_UPLOAD, true);
  162. } else {
  163. if (defined('CURLOPT_SAFE_UPLOAD')) {
  164. curl_setopt($curl, CURLOPT_SAFE_UPLOAD, false);
  165. }
  166. }
  167. curl_setopt($curl, CURLOPT_URL, $url);
  168. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  169. curl_setopt($curl, CURLOPT_POST, true);
  170. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  171. $result = curl_exec($curl);
  172. curl_close($curl);
  173. return json_decode($result);
  174. }
  175. /**
  176. * 上传视频文件
  177. * @param unknown_type
  178. * @param $merchant
  179. * @param $fullFile
  180. * @return mixed
  181. */
  182. public function uploadVideoMedia($merchant, $fullFile)
  183. {
  184. $access_token = self::getMiniProgramAccessToken($merchant, $ptStyle);
  185. $url = "http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=" . $access_token . "&type=video";
  186. $curl = curl_init();
  187. if (class_exists('\CURLFile')) {//关键是判断curlfile,官网推荐php5.5或更高的版本使用curlfile来实例文件
  188. $data = array('media' => new \CURLFile($fullFile));
  189. } else {
  190. $data = array('media' => file_get_contents($fullFile));
  191. }
  192. if (class_exists('\CURLFile')) {
  193. curl_setopt($curl, CURLOPT_SAFE_UPLOAD, true);
  194. } else {
  195. if (defined('CURLOPT_SAFE_UPLOAD')) {
  196. curl_setopt($curl, CURLOPT_SAFE_UPLOAD, false);
  197. }
  198. }
  199. curl_setopt($curl, CURLOPT_URL, $url);
  200. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  201. curl_setopt($curl, CURLOPT_POST, true);
  202. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  203. $result = curl_exec($curl);
  204. curl_close($curl);
  205. return json_decode($result);
  206. }
  207. /**
  208. * 下载多媒体文件
  209. */
  210. public static function uploadMedia($media_id, $access_token)
  211. {
  212. $url = 'http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=' . $access_token . '&media_id=' . $media_id;
  213. $result = CurlUtil::httpsGet($url);
  214. if (isset($result['errcode'])) {
  215. return false;
  216. } else {
  217. return $result;
  218. }
  219. }
  220. /**
  221. * 获取素材总数
  222. * 图片和图文消息素材(包括单图文和多图文)的总数上限为5000,其他素材的总数上限为1000
  223. */
  224. public function getMaterialCount($merchant, $ptStyle)
  225. {
  226. $token = self::getMiniProgramAccessToken($merchant, $ptStyle);
  227. $url = "https://api.weixin.qq.com/cgi-bin/material/get_materialcount?access_token={$token}";
  228. $curl = new curl\Curl();
  229. $response = $curl->get($url);
  230. return $response;
  231. }
  232. /**
  233. * 获取素材列表 图文列表
  234. * $data = ['type' => 'image','offset' => 0,'count' => 100];
  235. * type:news image video voice
  236. */
  237. public function batchgetMaterial($merchant, $data, $ptStyle)
  238. {
  239. $token = self::getMiniProgramAccessToken($merchant, $ptStyle);
  240. $url = "https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token={$token}";
  241. $curl = new curl\Curl();
  242. $response = $curl->setOption(CURLOPT_POSTFIELDS, json_encode($data))->post($url);
  243. return json_decode($response, true);
  244. }
  245. /**
  246. * 解除绑定删除自定义菜单
  247. */
  248. public static function menuDelete($merchant, $ptStyle)
  249. {
  250. $token = self::getMiniProgramAccessToken($merchant, $ptStyle);
  251. $url = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=$token";
  252. $curl = new curl\Curl();
  253. $response = $curl->get($url);
  254. return $response;
  255. }
  256. //微信api不支持中文转义的json结构
  257. static function json_encode($arr)
  258. {
  259. if (count($arr) == 0) return "[]";
  260. $parts = array();
  261. $is_list = false;
  262. //Find out if the given array is a numerical array
  263. $keys = array_keys($arr);
  264. $max_length = count($arr) - 1;
  265. if (($keys [0] === 0) && ($keys [$max_length] === $max_length)) { //See if the first key is 0 and last key is length - 1
  266. $is_list = true;
  267. for ($i = 0; $i < count($keys); $i++) { //See if each key correspondes to its position
  268. if ($i != $keys [$i]) { //A key fails at position check.
  269. $is_list = false; //It is an associative array.
  270. break;
  271. }
  272. }
  273. }
  274. foreach ($arr as $key => $value) {
  275. if (is_array($value)) { //Custom handling for arrays
  276. if ($is_list)
  277. $parts [] = self::json_encode($value); /* :RECURSION: */
  278. else
  279. $parts [] = '"' . $key . '":' . self::json_encode($value); /* :RECURSION: */
  280. } else {
  281. $str = '';
  282. if (!$is_list)
  283. $str = '"' . $key . '":';
  284. //Custom handling for multiple data types
  285. if (!is_string($value) && is_numeric($value) && $value < 2000000000)
  286. $str .= $value; //Numbers
  287. elseif ($value === false)
  288. $str .= 'false'; //The booleans
  289. elseif ($value === true)
  290. $str .= 'true';
  291. else
  292. $str .= '"' . addslashes($value) . '"'; //All other things
  293. $parts [] = $str;
  294. }
  295. }
  296. $json = implode(',', $parts);
  297. if ($is_list)
  298. return '[' . $json . ']'; //Return numerical JSON
  299. return '{' . $json . '}'; //Return associative JSON
  300. }
  301. /**
  302. * 创建临时二维码 ssh 2019.9.4
  303. */
  304. public static function qrcodeCreate($merchant, $sceneId, $expireSeconds = null, $ptStyle = 1)
  305. {
  306. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  307. $url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=$accessToken";
  308. //有效期默认一天
  309. $expireSeconds = isset($expireSeconds) ? $expireSeconds : 86400;
  310. $post = array('expire_seconds' => $expireSeconds, 'action_name' => 'QR_STR_SCENE', "action_info" => array('scene' => array('scene_str' => $sceneId)));
  311. $curl = new curl\Curl();
  312. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($post))->post($url);
  313. $result = Json::decode($result);
  314. return $result;
  315. }
  316. /**
  317. * 创建永久二维码
  318. */
  319. public static function qrcodePermanentCreate($merchant, $sceneId, $ptStyle = 1)
  320. {
  321. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  322. $url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=$accessToken";
  323. $post = array('action_name' => 'QR_LIMIT_STR_SCENE', "action_info" => array('scene' => array('scene_str' => $sceneId)));
  324. $curl = new curl\Curl();
  325. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($post))->post($url);
  326. $result = Json::decode($result);
  327. return $result;
  328. }
  329. /**
  330. * 微信授权获取code
  331. */
  332. public static function getCode($urlEncode, $merchant, $scope, $ptStyle)
  333. {
  334. $host = Yii::$app->request->getHostInfo();
  335. $REDIRECT_URI = $host . '/auth/get-user-info?url=' . $urlEncode . '&authScope=' . $scope;
  336. Yii::info($REDIRECT_URI);
  337. $appId = $merchant['wxAppId'];
  338. $open = [];
  339. if ($ptStyle == 1) {
  340. $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => dict::getDict('ptStyle', 'hd')]);
  341. }
  342. if ($ptStyle == 2) {
  343. $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => dict::getDict('ptStyle', 'ghs')]);
  344. }
  345. if ($ptStyle == 3) {
  346. $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => dict::getDict('ptStyle', 'mall')]);
  347. }
  348. if (empty($open)) {
  349. util::fail('没有找到平台');
  350. }
  351. $component_app_id = $open['appId'];
  352. $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" . $appId . "&redirect_uri=" . urlencode($REDIRECT_URI) . "&response_type=code&scope=" . $scope . "&state=1&component_appid={$component_app_id}#wechat_redirect";
  353. header("Location:" . $url);
  354. //如果这里没有exit(),在apache服务器下无法实现跳转
  355. exit();
  356. }
  357. /**
  358. * 获取openId
  359. */
  360. public static function getOpenId($code, $merchant, $ptStyle)
  361. {
  362. $appId = $merchant['wxAppId'];
  363. $open = [];
  364. if ($ptStyle == 1) {
  365. $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => dict::getDict('ptStyle', 'hd')]);
  366. }
  367. if ($ptStyle == 2) {
  368. $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => dict::getDict('ptStyle', 'ghs')]);
  369. }
  370. if ($ptStyle == 3) {
  371. $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => dict::getDict('ptStyle', 'mall')]);
  372. }
  373. if (empty($open)) {
  374. util::fail('没有找到平台');
  375. }
  376. $component_app_id = $open['appId'];
  377. $componentAccessToken = wxUtil::getComponentAccessToken($open);
  378. $url = "https://api.weixin.qq.com/sns/oauth2/component/access_token?appid={$appId}&code={$code}&grant_type=authorization_code&component_appid={$component_app_id}&component_access_token={$componentAccessToken}";
  379. $curl = new curl\Curl();
  380. $result = $curl->get($url);
  381. $data = Json::decode($result);
  382. if (isset($data['openid']) == false) {
  383. Yii::warning('error:' . json_encode($data) . ' code:' . $code, __METHOD__);
  384. return false;
  385. }
  386. return $data;
  387. }
  388. /**
  389. * 授权获取自己的信息
  390. */
  391. public function authGetUserInfo($openid, $access_token)
  392. {
  393. $url = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid";
  394. $curl = new curl\Curl();
  395. $result = $curl->get($url);
  396. $data = Json::decode($result);
  397. return $data;
  398. }
  399. /**
  400. * 自动获取自己的信息(公众号的粉丝才能获取)
  401. */
  402. public static function autoGetUserInfo($openid, $merchant, $ptStyle)
  403. {
  404. $access_token = self::getMiniProgramAccessToken($merchant, $ptStyle);
  405. $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=$access_token&openid=$openid&lang=zh_CN";
  406. $curl = new curl\Curl();
  407. $result = $curl->get($url);
  408. $data = Json::decode($result);
  409. return $data;
  410. }
  411. /**
  412. * 网址接入验证
  413. */
  414. public static function signatureValidation()
  415. {
  416. $signature = $_GET['signature'];
  417. $timestamp = $_GET['timestamp'];
  418. $nonce = $_GET['nonce'];
  419. $account = $_GET['account'];
  420. $merchant = xhMerchantService::getByAccount($account);
  421. if ($merchant) {
  422. $token = $merchant['wxToken'];
  423. // 3个数值组成数组验证
  424. $tmpArr = array(
  425. $token,
  426. $timestamp,
  427. $nonce
  428. );
  429. // 字典排序
  430. sort($tmpArr, SORT_STRING);
  431. $tmpStr = implode($tmpArr);
  432. // sha1加密
  433. $tmpStr = sha1($tmpStr);
  434. if ($signature == $tmpStr) {
  435. return true;
  436. } else {
  437. return false;
  438. }
  439. } else {
  440. return false;
  441. }
  442. }
  443. //客服接口发消息
  444. public static function sendMsg($content, $merchant, $openId, $ptStyle)
  445. {
  446. $token = self::getMiniProgramAccessToken($merchant, $ptStyle);
  447. $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={$token}";
  448. $data = ['touser' => $openId, 'msgtype' => 'text', 'text' => ['content' => $content]];
  449. $curl = new curl\Curl();
  450. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  451. $result = Json::decode($result);
  452. return $result;
  453. }
  454. public static function sendImgMsg($mediaId, $merchant, $openId, $ptStyle)
  455. {
  456. $token = self::getMiniProgramAccessToken($merchant, $ptStyle);
  457. $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={$token}";
  458. $data = ['touser' => $openId, 'msgtype' => 'image', 'image' => ['media_id' => $mediaId]];
  459. $curl = new curl\Curl();
  460. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  461. $result = Json::decode($result);
  462. return $result;
  463. }
  464. public static function sendVideoMsg($mediaId, $merchant, $openId, $ptStyle)
  465. {
  466. $token = self::getMiniProgramAccessToken($merchant, $ptStyle);
  467. $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={$token}";
  468. $data = ['touser' => $openId, 'msgtype' => 'video', 'video' => ['media_id' => $mediaId,
  469. 'thumb_media_id' => $mediaId,
  470. 'title' => '因爱而生',
  471. 'description' => "情定原生,爱在今生;\n坐标:浙江,绍兴上虞;\n看到过一句话:“一个城市不过是几条巷道上,几间房子和几个人的组合。如果没有了这些,一个城市如同陨落,只剩下悲凉的记忆;”\n万事万物持续流变,珍爱的东西尤其不可能一直存在,如朝霞,如春花,如爱情;\n无论什么时候,不管天气好坏,都能拿出某些个让人喜欢的东西。",
  472. ]];
  473. $curl = new curl\Curl();
  474. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  475. $result = Json::decode($result);
  476. return $result;
  477. }
  478. /**
  479. * 消息模板,发送任务通知
  480. * @param unknown $data
  481. * @return mixed
  482. */
  483. public static function sendTaskInform($data, $merchant, $ptStyle)
  484. {
  485. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  486. $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={$accessToken}";
  487. $data = urldecode(json_encode($data, JSON_UNESCAPED_UNICODE));
  488. $curl = new curl\Curl();
  489. $response = $curl->setOption(CURLOPT_POSTFIELDS, $data)->post($url);
  490. $arr = json_decode($response);
  491. return $arr;
  492. }
  493. /**
  494. * 创建模板消息
  495. * @param unknown $accessToken
  496. * @param unknown $template_id_short
  497. * @return unknown
  498. */
  499. public static function tMessageCreate($merchant, $shortTemplateId, $ptStyle)
  500. {
  501. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  502. $url = "https://api.weixin.qq.com/cgi-bin/template/api_add_template?access_token={$accessToken}";
  503. $post = array('template_id_short' => $shortTemplateId);
  504. $curl = new curl\Curl();
  505. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($post))->post($url);
  506. $result = Json::decode($result);
  507. return $result;
  508. }
  509. /**
  510. * 删除模板消息
  511. */
  512. public static function delTMessage($merchant, $templateId, $ptStyle)
  513. {
  514. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  515. $url = "https://api.weixin.qq.com/cgi-bin/template/del_private_template?access_token={$accessToken}";
  516. $post = array('template_id' => $templateId);
  517. $curl = new curl\Curl();
  518. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($post))->post($url);
  519. $result = Json::decode($result);
  520. return $result;
  521. }
  522. /**
  523. * 取所有模板消息
  524. */
  525. public function getAllTMessage($merchant, $ptStyle)
  526. {
  527. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  528. $url = "https://api.weixin.qq.com/cgi-bin/template/get_all_private_template?access_token={$accessToken}";
  529. $curl = new curl\Curl();
  530. $result = $curl->get($url);
  531. $result = Json::decode($result);
  532. return $result;
  533. }
  534. /**
  535. * 设置模板消息的行业
  536. */
  537. public function setTMIndustry($merchant, $industry_id1, $industry_id2, $ptStyle)
  538. {
  539. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  540. $url = "https://api.weixin.qq.com/cgi-bin/template/api_set_industry?access_token={$accessToken}";
  541. $post = array('industry_id1' => $industry_id1, 'industry_id2' => $industry_id2);
  542. $curl = new curl\Curl();
  543. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($post))->post($url);
  544. $result = Json::decode($result);
  545. return $result;
  546. }
  547. /**
  548. * 获取公众号的accessToken 公众号调用凭据
  549. */
  550. public static function getAuthorizerAccessToken($merchant, $ptStyle = 0)
  551. {
  552. $sjId = $merchant['id'];
  553. $open = [];
  554. if ($ptStyle == 1) {
  555. $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => dict::getDict('ptStyle', 'hd')]);
  556. }
  557. if ($ptStyle == 2) {
  558. $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => dict::getDict('ptStyle', 'ghs')]);
  559. }
  560. if ($ptStyle == 3) {
  561. $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => dict::getDict('ptStyle', 'mall')]);
  562. }
  563. if (empty($open)) {
  564. util::fail('没有找到平台');
  565. }
  566. $component_app_id = $open['appId'];
  567. $componentAccessToken = wxUtil::getComponentAccessToken($open);
  568. $appId = $merchant['wxAppId'];
  569. $wxAccessToken = $merchant['wxAccessToken'];
  570. $wxAccessTokenTime = $merchant['wxAccessTokenTime'];
  571. $authorize_refresh_token = $merchant['wxRefreshToken'];
  572. $now = time();
  573. if ($now > strtotime($wxAccessTokenTime)) {
  574. $url = "https://api.weixin.qq.com/cgi-bin/component/api_authorizer_token?component_access_token={$componentAccessToken}";
  575. $data = ['component_appid' => $component_app_id, 'authorizer_appid' => $appId, 'authorizer_refresh_token' => $authorize_refresh_token];
  576. $curl = new curl\Curl();
  577. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  578. $result = Json::decode($result);
  579. if (isset($result['authorizer_access_token'])) {
  580. $authorize_access_token = $result['authorizer_access_token'];
  581. $expires_in = $result['expires_in'];
  582. $authorize_refresh_token = $result['authorizer_refresh_token'];
  583. $uData['wxRefreshToken'] = $authorize_refresh_token;;
  584. $uData['wxAccessToken'] = $authorize_access_token;
  585. $uData['wxAccessTokenTime'] = date("Y-m-d H:i:s", (time() + $expires_in - 100));
  586. if ($ptStyle == 1) {
  587. $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => dict::getDict('ptStyle', 'hd')]);
  588. $wxBaseId = isset($open['wxBaseId']) ? $open['wxBaseId'] : 0;
  589. if (!empty($wxBaseId)) {
  590. WxBaseService::updateById($wxBaseId, $uData);
  591. }
  592. } elseif ($ptStyle == 2) {
  593. $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => dict::getDict('ptStyle', 'ghs')]);
  594. $wxBaseId = isset($open['wxBaseId']) ? $open['wxBaseId'] : 0;
  595. if (!empty($wxBaseId)) {
  596. WxBaseService::updateById($wxBaseId, $uData);
  597. }
  598. } elseif ($ptStyle == 3) {
  599. $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => dict::getDict('ptStyle', 'mall')]);
  600. $wxBaseId = isset($open['wxBaseId']) ? $open['wxBaseId'] : 0;
  601. if (!empty($wxBaseId)) {
  602. WxBaseService::updateById($wxBaseId, $uData);
  603. }
  604. } else {
  605. xhMerchantService::updateById($sjId, $uData);
  606. }
  607. return $authorize_access_token;
  608. }
  609. return;
  610. } else {
  611. return $wxAccessToken;
  612. }
  613. }
  614. /**
  615. * 获取小程序的accessToken
  616. */
  617. public static function getMiniProgramAccessToken($merchant, $ptStyle = 0)
  618. {
  619. $sjId = isset($merchant['id']) ? $merchant['id'] : 0;
  620. //$id = dict::getDict('openId');
  621. $open = [];
  622. if ($ptStyle == 1) {
  623. $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => dict::getDict('ptStyle', 'hd')]);
  624. }
  625. if ($ptStyle == 2) {
  626. $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => dict::getDict('ptStyle', 'ghs')]);
  627. }
  628. if ($ptStyle == 3) {
  629. $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => dict::getDict('ptStyle', 'mall')]);
  630. }
  631. if (empty($open)) {
  632. util::fail('没有找到平台信息');
  633. }
  634. $component_app_id = $open['appId'];
  635. $componentAccessToken = wxUtil::getComponentAccessToken($open);
  636. $appId = $merchant['miniAppId'];
  637. if ($ptStyle == 1) {
  638. $miniAccessToken = $merchant['miniAccessToken'];
  639. $miniAccessTokenTime = $merchant['miniAccessTokenTime'];
  640. $miniRefreshToken = $merchant['miniRefreshToken'];
  641. } elseif ($ptStyle == 2) {
  642. $miniAccessToken = $merchant['miniAccessToken'];
  643. $miniAccessTokenTime = $merchant['miniAccessTokenTime'];
  644. $miniRefreshToken = $merchant['miniRefreshToken'];
  645. } elseif ($ptStyle == 3) {
  646. $miniAccessToken = $merchant['miniAccessToken'];
  647. $miniAccessTokenTime = $merchant['miniAccessTokenTime'];
  648. $miniRefreshToken = $merchant['miniRefreshToken'];
  649. } else {
  650. $extend = xhMerchantExtendService::getBySjId($sjId);
  651. $miniAccessToken = $extend['miniAccessToken'];
  652. $miniAccessTokenTime = $extend['miniAccessTokenTime'];
  653. $miniRefreshToken = $extend['miniRefreshToken'];
  654. }
  655. $now = time();
  656. if ($now > strtotime($miniAccessTokenTime)) {
  657. $url = "https://api.weixin.qq.com/cgi-bin/component/api_authorizer_token?component_access_token={$componentAccessToken}";
  658. $data = ['component_appid' => $component_app_id, 'authorizer_appid' => $appId, 'authorizer_refresh_token' => $miniRefreshToken];
  659. $curl = new curl\Curl();
  660. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  661. $result = Json::decode($result);
  662. Yii::info('重新获取mini accesstoken:' . json_encode($result));
  663. if (isset($result['authorizer_access_token'])) {
  664. $expires_in = $result['expires_in'];
  665. $miniAccessToken = $result['authorizer_access_token'];
  666. $uData['miniRefreshToken'] = $result['authorizer_refresh_token'];
  667. $uData['miniAccessToken'] = $miniAccessToken;
  668. $uData['miniAccessTokenTime'] = date("Y-m-d H:i:s", (time() + $expires_in - 100));
  669. if ($ptStyle == 1) {
  670. //零售
  671. $wxMiniBaseId = isset($open['wxMiniBaseId']) ? $open['wxMiniBaseId'] : 0;
  672. if (!empty($wxMiniBaseId)) {
  673. WxMiniBaseService::updateById($wxMiniBaseId, $uData);
  674. }
  675. } elseif ($ptStyle == 2) {
  676. //供货商
  677. $wxMiniBaseId = isset($open['wxMiniBaseId']) ? $open['wxMiniBaseId'] : 0;
  678. if (!empty($wxMiniBaseId)) {
  679. WxMiniBaseService::updateById($wxMiniBaseId, $uData);
  680. }
  681. } elseif ($ptStyle == 3) {
  682. //商城
  683. $wxMiniBaseId = isset($open['wxMiniBaseId']) ? $open['wxMiniBaseId'] : 0;
  684. if (!empty($wxMiniBaseId)) {
  685. WxMiniBaseService::updateById($wxMiniBaseId, $uData);
  686. }
  687. } else {
  688. //其它
  689. xhMerchantExtendService::updateBySjId($sjId, $uData);
  690. }
  691. return $miniAccessToken;
  692. }
  693. Yii::info('accessToken失效:sjId:' . $sjId);
  694. }
  695. return $miniAccessToken;
  696. }
  697. public static function saveWeixinImg($userId, $url, $class = 0)
  698. {
  699. $className = [
  700. '0' => 'weixinUserAvatar',//微信头像
  701. '1' => 'weixinUserImg',//微信公众号二维码
  702. '2' => 'weixinMpQrcode',
  703. '3' => 'weixinMpAvatar',
  704. ];
  705. $folder = Yii::getAlias('@webroot') . '/../../images';
  706. $pre = '/' . $className[$class] . '/' . date('Y') . '/' . date('m') . '/' . date("d") . '/';
  707. if (!file_exists($folder . $pre))
  708. mkdir($folder . $pre, 0777, true);
  709. $randString = stringUtil::buildOrderNo();
  710. $name = md5($userId . $randString) . '.jpg';
  711. $fullFileName = $folder . $pre . $name;
  712. $fileName = $pre . $name;
  713. $curl = new curl\Curl();
  714. $result = $curl->get($url);
  715. $fp2 = @fopen($fullFileName, 'a');//文件大小
  716. fwrite($fp2, $result);
  717. fclose($fp2);
  718. return $fileName;
  719. }
  720. /**
  721. * 下载学员作品图片
  722. */
  723. public static function downloadStudentWorksImg($merchant, $mediaId, $savePathType = 'wxUserImg', $ptStyle = 1)
  724. {
  725. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  726. $url = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token={$accessToken}&media_id={$mediaId}";
  727. $ch = curl_init($url);
  728. curl_setopt($ch, CURLOPT_HEADER, 0);
  729. curl_setopt($ch, CURLOPT_NOBODY, 0);//对body进行输出。
  730. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  731. $body = curl_exec($ch);
  732. $httpinfo = curl_getinfo($ch);
  733. curl_close($ch);
  734. preg_match('/\w\/(\w+)/i', $httpinfo["content_type"], $extmatches);//求出文件格式
  735. $fileExt = $extmatches[1];
  736. $imgSavePath = dict::getDict('imgSavePath');
  737. $comImgSavePath = $imgSavePath[$savePathType];
  738. $folder = Yii::getAlias('@webroot') . '/../../images';
  739. $pre = '/' . $comImgSavePath . '/' . date('Y') . '/' . date('m') . '/' . date("d") . '/';
  740. if (!file_exists($folder . $pre)) {
  741. mkdir($folder . $pre, 0777, true);
  742. }
  743. $randString = stringUtil::buildOrderNo();
  744. $fullFileName = $folder . $pre . $randString . '.jpg';
  745. $fileName = $pre . $randString . '.jpg';
  746. $fp2 = @fopen($fullFileName, 'a');//文件大小
  747. fwrite($fp2, $body);
  748. fclose($fp2);
  749. $srcImg = $fullFileName;
  750. $dstImg300 = $folder . $pre . $randString . "_300.jpg";//生成300px缩略图,不保存到数据库
  751. util::img2thumb($srcImg, $dstImg300, $width = 300, 0, 0, 0);
  752. $dstImg100 = $folder . $pre . $randString . "_100.jpg";//生成50px缩略图,不保存到数据库
  753. util::img2thumb($srcImg, $dstImg100, $width = 100, 0, 0, 0);
  754. return $fileName;
  755. }
  756. /**
  757. * 获取media_id里的图片保存到服务器
  758. */
  759. public static function downloadmediaIdImg($merchant, $mediaId, $savePathType = 'wxUserImg', $ptStyle)
  760. {
  761. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  762. $url = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token={$accessToken}&media_id={$mediaId}";
  763. $ch = curl_init($url);
  764. curl_setopt($ch, CURLOPT_HEADER, 0);
  765. curl_setopt($ch, CURLOPT_NOBODY, 0);//对body进行输出。
  766. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  767. $body = curl_exec($ch);
  768. $httpinfo = curl_getinfo($ch);
  769. curl_close($ch);
  770. preg_match('/\w\/(\w+)/i', $httpinfo["content_type"], $extmatches);//求出文件格式
  771. $fileExt = $extmatches[1];
  772. $imgSavePath = dict::getDict('imgSavePath');
  773. $comImgSavePath = $imgSavePath[$savePathType];
  774. $folder = Yii::getAlias('@webroot') . '/../../images';
  775. $pre = '/' . $comImgSavePath . '/' . date('Y') . '/' . date('m') . '/' . date("d") . '/';
  776. if (!file_exists($folder . $pre)) {
  777. mkdir($folder . $pre, 0777, true);
  778. }
  779. $randString = stringUtil::buildOrderNo();
  780. $fullFileName = $folder . $pre . $randString . '.jpg';
  781. $fileName = $pre . $randString . '.jpg';
  782. $fp2 = @fopen($fullFileName, 'a');//文件大小
  783. fwrite($fp2, $body);
  784. fclose($fp2);
  785. $srcImg = $fullFileName;
  786. $mediumImg = $pre . $randString . "_200.jpg";
  787. $dstImg200 = $folder . $mediumImg;//生成200px缩略图,不保存到数据库
  788. util::img2thumb($srcImg, $dstImg200, $width = 200, 0, 0, 0);
  789. $smallImg = $pre . $randString . "_50.jpg";
  790. $dstImg50 = $folder . $smallImg;//生成50px缩略图,不保存到数据库
  791. util::img2thumb($srcImg, $dstImg50, $width = 50, 0, 0, 0);
  792. return ['large' => $fileName, 'medium' => $mediumImg, 'small' => $smallImg];
  793. }
  794. public static function createTag($merchant, $name, $ptStyle)
  795. {
  796. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  797. $url = "https://api.weixin.qq.com/cgi-bin/tags/create?access_token={$accessToken}";
  798. $post = ['tag' => ['name' => $name]];
  799. $curl = new curl\Curl();
  800. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($post))->post($url);
  801. $result = Json::decode($result);
  802. return $result;
  803. }
  804. public static function delTag($merchant, $id, $ptStyle)
  805. {
  806. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  807. $url = "https://api.weixin.qq.com/cgi-bin/tags/delete?access_token={$accessToken}";
  808. $post = ['tag' => ['id' => $id]];
  809. $curl = new curl\Curl();
  810. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($post))->post($url);
  811. $result = Json::decode($result);
  812. return $result;
  813. }
  814. public static function modTag($merchant, $id, $name, $ptStyle)
  815. {
  816. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  817. $url = "https://api.weixin.qq.com/cgi-bin/tags/update?access_token={$accessToken}";
  818. $post = ['tag' => ['id' => $id, 'name' => $name]];
  819. $curl = new curl\Curl();
  820. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($post))->post($url);
  821. $result = Json::decode($result);
  822. return $result;
  823. }
  824. /**
  825. * 获取商家所有标签
  826. */
  827. public static function getTag($merchant, $ptStyle)
  828. {
  829. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  830. $url = "https://api.weixin.qq.com/cgi-bin/tags/get?access_token={$accessToken}";
  831. $curl = new curl\Curl();
  832. $result = $curl->get($url);
  833. $result = Json::decode($result);
  834. return $result;
  835. }
  836. public function getUserTag($tagId, $nextOpenId = '')
  837. {
  838. $url = "https://api.weixin.qq.com/cgi-bin/user/tag/get?access_token=" . $this->access_token;
  839. $post = ['tagid' => $tagId, 'next_openid' => $nextOpenId];
  840. $curl = new curl\Curl();
  841. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($post))->post($url);
  842. $result = Json::decode($result);
  843. return $result;
  844. }
  845. /**
  846. * 批量为用户打标签
  847. */
  848. public static function membersBatchTag($merchant, $openIdList, $tagId, $ptStyle)
  849. {
  850. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  851. $url = "https://api.weixin.qq.com/cgi-bin/tags/members/batchtagging?access_token={$accessToken}";
  852. $post = ['openid_list' => $openIdList, 'tagid' => $tagId];
  853. $curl = new curl\Curl();
  854. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($post))->post($url);
  855. $result = Json::decode($result);
  856. return $result;
  857. }
  858. /**
  859. * 批量取消用户标签
  860. */
  861. public static function membersCancelTag($merchant, $openIdList, $tagId, $ptStyle)
  862. {
  863. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  864. $url = "https://api.weixin.qq.com/cgi-bin/tags/members/batchuntagging?access_token={$accessToken}";
  865. $post = ['openid_list' => $openIdList, 'tagid' => $tagId];
  866. $curl = new curl\Curl();
  867. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($post))->post($url);
  868. $result = Json::decode($result);
  869. return $result;
  870. }
  871. /**
  872. * 新增临时素材
  873. */
  874. public static function uploadTempMedia($merchant, $fullPathImg, $ptStyle, $type = 'image')
  875. {
  876. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  877. $url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token={$accessToken}&type=" . $type;
  878. $ch = curl_init();
  879. if (class_exists('CURLFile')) {// 这里用特性检测判断php版本
  880. curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
  881. $postData = array('media' => new \CURLFile(realpath($fullPathImg)));//>=5.5
  882. } else {
  883. if (defined('CURLOPT_SAFE_UPLOAD')) {
  884. curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
  885. $postData = array('media' => '@' . realpath($fullPathImg));//<=5.5
  886. }
  887. }
  888. curl_setopt($ch, CURLOPT_URL, $url);
  889. curl_setopt($ch, CURLOPT_POST, 1);
  890. curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
  891. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  892. $response = curl_exec($ch);
  893. $result = Json::decode($response);
  894. return $result;
  895. }
  896. /**
  897. * 获取临时素材
  898. */
  899. public static function getTempMaterial($merchant, $mediaId, $ptStyle)
  900. {
  901. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  902. $url = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token={$accessToken}&media_id=" . $mediaId;
  903. $fileInfo = self::getTempMaterialInfo($url);
  904. if (empty($fileInfo) || isset($fileInfo['errcode'])) {
  905. $msg = '未错误哦~';
  906. return ['code' => 'A0002', 'msg' => $msg];
  907. }
  908. $fileName = substr(md5($mediaId), -4) . stringUtil::buildOrderNo() . '.amr';
  909. $preFolder = Yii::getAlias('@webroot') . '/../../images';
  910. $folder = '/weixinChatVoice/' . date('Y') . '/' . date('m') . '/' . date("d") . '/';
  911. if (!file_exists($preFolder . $folder))
  912. mkdir($preFolder . $folder, 0777, true);
  913. $saveFile = $preFolder . $folder . $fileName;
  914. if (file_exists($saveFile) == false) {
  915. self::readTempMaterial($saveFile, $fileInfo["body"]);
  916. }
  917. return ['code' => 'A0001', 'msg' => 'success', 'data' => ['url' => $folder . $fileName]];
  918. }
  919. /**
  920. * 获取临时素材的头信息等
  921. */
  922. public static function getTempMaterialInfo($url)
  923. {
  924. $ch = curl_init($url);
  925. curl_setopt($ch, CURLOPT_HEADER, 0);
  926. curl_setopt($ch, CURLOPT_NOBODY, 0);//只取body头
  927. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  928. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  929. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  930. $package = curl_exec($ch);
  931. $httpinfo = curl_getinfo($ch);
  932. curl_close($ch);
  933. $imageAll = ['header' => $httpinfo, 'body' => $package];
  934. return $imageAll;
  935. }
  936. /**
  937. * 读取临时素材的资源
  938. */
  939. function readTempMaterial($filename, $fileContent)
  940. {
  941. $localFile = fopen($filename, 'w');
  942. if (false !== $localFile) {
  943. if (false !== fwrite($localFile, $fileContent)) {
  944. fclose($localFile);
  945. }
  946. }
  947. }
  948. /**
  949. * 创建门店
  950. */
  951. public function createStore($info)
  952. {
  953. $jsonData = urldecode(json_encode($info, JSON_UNESCAPED_UNICODE));
  954. $url = "http://api.weixin.qq.com/cgi-bin/poi/addpoi?access_token=" . $this->access_token;
  955. $curl = new curl\Curl();
  956. return $curl->setOption(CURLOPT_POSTFIELDS, $jsonData)->post($url);
  957. }
  958. /**
  959. * 查询门店信息
  960. */
  961. public function getStoreInfo($poi_id)
  962. {
  963. $data = ['poi_id' => $poi_id];
  964. $jsonData = urldecode(json_encode($data, JSON_UNESCAPED_UNICODE));
  965. $url = "http://api.weixin.qq.com/cgi-bin/poi/getpoi?access_token=" . $this->access_token;
  966. $curl = new curl\Curl();
  967. return $curl->setOption(CURLOPT_POSTFIELDS, $jsonData)->post($url);
  968. }
  969. /**
  970. * 查询门店列表
  971. */
  972. public function getStoreList($begin = 0, $count = 20)
  973. {
  974. $data = ["begin" => $begin, "limit" => $count];
  975. $jsonData = urldecode(json_encode($data, JSON_UNESCAPED_UNICODE));
  976. $url = "http://api.weixin.qq.com/cgi-bin/poi/getpoilist?access_token=" . $this->access_token;
  977. $curl = new curl\Curl();
  978. return $curl->setOption(CURLOPT_POSTFIELDS, $jsonData)->post($url);
  979. }
  980. /**
  981. * 查询门店列表 -- 重复 可删除
  982. */
  983. function getShopList($data)
  984. {
  985. $url = "https://api.weixin.qq.com/cgi-bin/poi/getpoilist?access_token=" . $this->access_token;
  986. $curl = new curl\Curl();
  987. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  988. $result = Json::decode($result);
  989. return $result;
  990. }
  991. /**
  992. * 修改门店服务信息
  993. */
  994. public function updateStore(array $buffer)
  995. {
  996. $jsonData = urldecode(json_encode($buffer, JSON_UNESCAPED_UNICODE));
  997. $url = "http://api.weixin.qq.com/cgi-bin/poi/updatepoi?access_token=" . $this->access_token;
  998. $curl = new curl\Curl();
  999. return $curl->setOption(CURLOPT_POSTFIELDS, $jsonData)->post($url);
  1000. }
  1001. /**
  1002. * 删除门店
  1003. * @param $poi_id
  1004. * @return mixed
  1005. */
  1006. public function delStore($poi_id)
  1007. {
  1008. $data = ['poi_id' => $poi_id];
  1009. $jsonData = urldecode(json_encode($data, JSON_UNESCAPED_UNICODE));
  1010. $url = "http://api.weixin.qq.com/cgi-bin/poi/delpoi?access_token=" . $this->access_token;
  1011. $curl = new curl\Curl();
  1012. return $curl->setOption(CURLOPT_POSTFIELDS, $jsonData)->post($url);
  1013. }
  1014. /**
  1015. * 门店类目表
  1016. */
  1017. public function storeTypeList()
  1018. {
  1019. $url = "http://api.weixin.qq.com/cgi-bin/poi/getwxcategory?access_token=" . $this->access_token;
  1020. $curl = new curl\Curl();
  1021. return $curl->get($url);
  1022. }
  1023. /******************************* wifi *************************************/
  1024. /**
  1025. * 获取wifi门店列表
  1026. */
  1027. function getShopWifiList($pageindex = 1, $pagesize = 10)
  1028. {
  1029. $data = ["pageindex" => $pageindex, "pagesize" => $pagesize];
  1030. $url = "https://api.weixin.qq.com/bizwifi/shop/list?access_token=" . $this->access_token;
  1031. $curl = new curl\Curl();
  1032. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1033. $result = Json::decode($result);
  1034. return $result;
  1035. }
  1036. /**
  1037. * 添加密码型设备
  1038. * @param $shopId 门店ID
  1039. * @param $ssid 无线网络设备的ssid
  1040. * @param $pwd 无线网络设备的密码
  1041. * @return mixed
  1042. */
  1043. function addPasswordDevice($shopId, $ssid, $pwd)
  1044. {
  1045. $data = ["shop_id" => $shopId, "ssid" => $ssid, "password" => $pwd];
  1046. $url = "https://api.weixin.qq.com/bizwifi/device/add?access_token=" . $this->access_token;
  1047. $curl = new curl\Curl();
  1048. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1049. $result = Json::decode($result);
  1050. return $result;
  1051. }
  1052. /**
  1053. * 修改门店Wi-Fi(修改门店网络信息)
  1054. * @param $shopId
  1055. * @param $oldSsid
  1056. * @param $ssid
  1057. * @param string $pwd
  1058. * @return mixed
  1059. */
  1060. function updateDevice($shopId, $oldSsid, $ssid, $pwd = '')
  1061. {
  1062. $data = ["shop_id" => $shopId, "old_ssid" => $oldSsid, "ssid" => $ssid];
  1063. if (!empty($pwd)) {
  1064. $data["password"] = $pwd;
  1065. }
  1066. $url = "https://api.weixin.qq.com/bizwifi/shop/update?access_token=" . $this->access_token;
  1067. $curl = new curl\Curl();
  1068. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1069. $result = Json::decode($result);
  1070. return $result;
  1071. }
  1072. /**
  1073. * 删除密码型设备
  1074. * @param $bssid 无线网络设备无线mac地址
  1075. * @return mixed
  1076. */
  1077. function delPasswordDevice($bssid)
  1078. {
  1079. $data = ["bssid" => $bssid];
  1080. $url = "https://api.weixin.qq.com/bizwifi/device/delete?access_token=" . $this->access_token;
  1081. $curl = new curl\Curl();
  1082. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1083. $result = Json::decode($result);
  1084. return $result;
  1085. }
  1086. /**
  1087. * 查询门店Wi-Fi信息
  1088. * @param $shopId 门店ID
  1089. * @return mixed
  1090. */
  1091. function getShopWifi($shopId)
  1092. {
  1093. $data = ["shop_id" => $shopId];
  1094. $url = "https://api.weixin.qq.com/bizwifi/shop/get?access_token=" . $this->access_token;
  1095. $curl = new curl\Curl();
  1096. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1097. $result = Json::decode($result);
  1098. return $result;
  1099. }
  1100. /**
  1101. * 获取物料二维码
  1102. * @param $shopId 门店ID
  1103. * @param $ssid 无线网络设备的ssid
  1104. * @param $img_id 物料样式编号(0-纯二维码,可用于自由设计宣传材料;1-二维码物料,155mm×215mm(宽×高),可直接张贴)
  1105. * @return mixed
  1106. */
  1107. function getWifiQrCode($shopId, $ssid, $img_id)
  1108. {
  1109. //$data = ["shop_id" => 429620, "ssid" => "WX567", "img_id" => $img_id];
  1110. $data = ["shop_id" => $shopId, "ssid" => $ssid, "img_id" => $img_id];
  1111. $url = "https://api.weixin.qq.com/bizwifi/qrcode/get?access_token=" . $this->access_token;
  1112. $curl = new curl\Curl();
  1113. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1114. $result = Json::decode($result);
  1115. return $result;
  1116. }
  1117. function getWifiUrl()
  1118. {
  1119. $url = "https://api.weixin.qq.com/bizwifi/account/get_connecturl?access_token=" . $this->access_token;
  1120. $curl = new curl\Curl();
  1121. $result = $curl->get($url);
  1122. $result = Json::decode($result);
  1123. return $result;
  1124. }
  1125. function generateShortUrl($longUrl)
  1126. {
  1127. $data = ["action" => "long2short", "long_url" => $longUrl,];
  1128. $url = "https://api.weixin.qq.com/cgi-bin/shorturl?access_token=" . $this->access_token;
  1129. $curl = new curl\Curl();
  1130. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1131. $result = Json::decode($result);
  1132. if (isset($result['errcode']) && $result['errcode'] == 0) {
  1133. return ['code' => 'A0001', 'data' => ['shortUrl' => $result['short_url']]];
  1134. }
  1135. return ['code' => 'A0002', 'data' => []];
  1136. }
  1137. //长链接转短链接 ssh 2019.9.26
  1138. public static function urlLongToShort($longUrl, $merchant)
  1139. {
  1140. $accessToken = self::getMiniProgramAccessToken($merchant);
  1141. $data = ["action" => "long2short", "long_url" => $longUrl,];
  1142. $url = "https://api.weixin.qq.com/cgi-bin/shorturl?access_token=" . $accessToken;
  1143. $curl = new curl\Curl();
  1144. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1145. $result = Json::decode($result);
  1146. $url = '';
  1147. if (isset($result['errcode']) && $result['errcode'] == 0) {
  1148. $url = $result['short_url'];
  1149. }
  1150. return ['url' => $url];
  1151. }
  1152. /**
  1153. * 拉取门店所有的设备信息
  1154. * @return array
  1155. */
  1156. public function getShopWifiListAll()
  1157. {
  1158. $startPage = 1;
  1159. $list = [];
  1160. while (true) {
  1161. $reList = $this->getShopWifiList($startPage, 20);
  1162. if ($startPage <= $reList['data']['pagecount']) {
  1163. $list = array_merge($list, $reList['data']['records']);
  1164. ++$startPage;
  1165. } else {
  1166. break;
  1167. }
  1168. }
  1169. return $list;
  1170. }
  1171. /**
  1172. * 通过 poi_id 从Wi-Fi门店列表中 获取 shopId
  1173. * @param $poiId
  1174. * @return bool
  1175. */
  1176. public function getShopId($poiId, $list = [])
  1177. {
  1178. if (empty($list)) {
  1179. $list = $this->getShopWifiListAll();
  1180. }
  1181. foreach ($list as $shop) {
  1182. if ($shop['poi_id'] == $poiId)
  1183. return (string)$shop['shop_id'];
  1184. }
  1185. return false;
  1186. }
  1187. //设置小程序服务器域名
  1188. public static function setDomain($merchant, $ptStyle = 0)
  1189. {
  1190. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  1191. $url = "https://api.weixin.qq.com/wxa/modify_domain?access_token=" . $accessToken;
  1192. $curl = new curl\Curl();
  1193. $mallHost = Yii::$app->params['mallHost'];
  1194. if ($ptStyle == 1) {
  1195. $mallHost = Yii::$app->params['hdHost'];
  1196. }
  1197. $has = strpos($mallHost, 'https');
  1198. //如果不是https则换成https
  1199. if ($has === false) {
  1200. $mallHost = str_replace('http', 'https', $mallHost);
  1201. }
  1202. $data = [
  1203. "action" => "set",
  1204. "requestdomain" => [$mallHost],
  1205. "wsrequestdomain" => [$mallHost],
  1206. "uploaddomain" => [$mallHost],
  1207. "downloaddomain" => [$mallHost],
  1208. ];
  1209. print_r($data);
  1210. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1211. $respond = Json::decode($result);
  1212. print_r($respond);
  1213. }
  1214. //设置业务域名
  1215. public static function setWebViewDomain($merchant, $ptStyle = 0)
  1216. {
  1217. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  1218. $url = "https://api.weixin.qq.com/wxa/setwebviewdomain?access_token=" . $accessToken;
  1219. $curl = new curl\Curl();
  1220. $mallHost = Yii::$app->params['mallHost'];
  1221. if ($ptStyle == 1) {
  1222. $mallHost = Yii::$app->params['hdHost'];
  1223. }
  1224. $has = strpos($mallHost, 'https');
  1225. //如果不是https则换成https
  1226. if ($has === false) {
  1227. $mallHost = str_replace('http', 'https', $mallHost);
  1228. }
  1229. $data = [
  1230. "action" => "set",
  1231. "webviewdomain" => [$mallHost],
  1232. ];
  1233. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1234. $respond = Json::decode($result);
  1235. print_r($respond);
  1236. }
  1237. //为授权的小程序帐号上传小程序代码
  1238. public static function miniCommit($merchant, $templateId, $ptStyle = 0)
  1239. {
  1240. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  1241. $url = 'https://api.weixin.qq.com/wxa/commit?access_token=' . $accessToken;
  1242. $curl = new curl\Curl();
  1243. $apiUrl = Yii::$app->params['mallHost'];
  1244. if ($ptStyle == 1) {
  1245. $apiUrl = Yii::$app->params['hdHost'];
  1246. }
  1247. $imgUrl = imgUtil::getPrefix();
  1248. //如果不是https则换成https
  1249. $has = strpos($apiUrl, 'https');
  1250. if ($has === false) {
  1251. $apiUrl = str_replace('http', 'https', $apiUrl);
  1252. }
  1253. //如果不是https则换成https
  1254. $has = strpos($imgUrl, 'https');
  1255. if ($has === false) {
  1256. $imgUrl = str_replace('http', 'https', $imgUrl);
  1257. }
  1258. $account = isset($merchant['id']) ? $merchant['id'] : 0;
  1259. $name = isset($merchant['name']) ? $merchant['name'] : '';
  1260. $open = [];
  1261. if ($ptStyle == 1) {
  1262. $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => dict::getDict('ptStyle', 'hd')]);
  1263. }
  1264. if ($ptStyle == 2) {
  1265. $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => dict::getDict('ptStyle', 'ghs')]);
  1266. }
  1267. if ($ptStyle == 3) {
  1268. $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => dict::getDict('ptStyle', 'mall')]);
  1269. }
  1270. if (empty($open)) {
  1271. util::fail('没有找到平台');
  1272. }
  1273. $name = isset($open['name']) ? $open['name'] : '';
  1274. $ext = [
  1275. 'extAppid' => $merchant['miniAppId'],
  1276. 'ext' => [
  1277. 'account' => $account,
  1278. 'apiHost' => $apiUrl,
  1279. 'socketApiHost' => str_replace("https://", "", $apiUrl),
  1280. 'imgHost' => $imgUrl,
  1281. 'name' => $name,
  1282. ],
  1283. ];
  1284. echo '<pre>';
  1285. print_r($ext);
  1286. Yii::info("ext:" . json_encode($ext));
  1287. $extJson = json_encode($ext);
  1288. $data = [
  1289. "template_id" => $templateId,
  1290. "ext_json" => $extJson,
  1291. "user_version" => date("mdHis"),
  1292. "user_desc" => date("mdHis")
  1293. ];
  1294. print_r($data);
  1295. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1296. $respond = Json::decode($result);
  1297. print_r($respond);
  1298. }
  1299. //获取体验小程序的体验二维码
  1300. public static function getExperienceQrCode($merchant, $page, $ptStyle = 0)
  1301. {
  1302. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  1303. $path = urlencode($page);
  1304. $url = "https://api.weixin.qq.com/wxa/get_qrcode?access_token={$accessToken}&path=" . $path;
  1305. $curl = new curl\Curl();
  1306. $result = $curl->get($url);
  1307. $file = dirUtil::getImgDir() . 'retail/miniExperience/' . $merchant['id'] . '.jpg';
  1308. file_put_contents($file, $result);
  1309. echo "<b /><img src='" . imgUtil::getPrefix() . "/retail/miniExperience/" . $merchant['id'] . ".jpg' />";
  1310. }
  1311. //获取授权小程序帐号已设置的类目
  1312. public static function getMiniCategory($merchant, $ptStyle = 0)
  1313. {
  1314. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  1315. $url = "https://api.weixin.qq.com/wxa/get_category?access_token=" . $accessToken;
  1316. $curl = new curl\Curl();
  1317. $result = $curl->get($url);
  1318. $respond = Json::decode($result);
  1319. echo "<pre>";
  1320. print_r($respond);
  1321. }
  1322. //小程序支付成功后获取唯一ID ssh 2020.5.12
  1323. public static function getPaidUnionId($merchant, $miniOpenId, $mcId, $orderId, $ptStyle = 0)
  1324. {
  1325. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  1326. $url = "https://api.weixin.qq.com/wxa/getpaidunionid?access_token={$accessToken}&openid={$miniOpenId}&mch_id={$mcId}&out_trade_no=" . (string)$orderId;
  1327. $curl = new curl\Curl();
  1328. $result = $curl->get($url);
  1329. $respond = Json::decode($result);
  1330. $unionId = isset($respond['unionid']) && !empty($respond['unionid']) ? $respond['unionid'] : '';
  1331. return $unionId;
  1332. }
  1333. //小程序加急审核 ssh 2020.4.20
  1334. public static function speedupAudit($merchant, $id, $ptStyle = 0)
  1335. {
  1336. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  1337. $url = "https://api.weixin.qq.com/wxa/speedupaudit?access_token=" . $accessToken;
  1338. $curl = new curl\Curl();
  1339. $data = [
  1340. "auditid" => $id
  1341. ];
  1342. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1343. $respond = Json::decode($result);
  1344. print_r($respond);
  1345. }
  1346. //获取小程序的第三方提交代码的页面配置
  1347. public static function getMiniPage($merchant, $ptStyle = 0)
  1348. {
  1349. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  1350. $url = "https://api.weixin.qq.com/wxa/get_page?access_token=" . $accessToken;
  1351. $curl = new curl\Curl();
  1352. $result = $curl->get($url);
  1353. $respond = Json::decode($result);
  1354. echo "<pre>";
  1355. print_r($respond);
  1356. }
  1357. //将第三方提交的代码包提交审核
  1358. public static function miniSubmitAudit($merchant, $page, $ptStyle = 0)
  1359. {
  1360. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  1361. $url = "https://api.weixin.qq.com/wxa/submit_audit?access_token=" . $accessToken;
  1362. $curl = new curl\Curl();
  1363. if ($ptStyle == 1) {
  1364. $data = [
  1365. "item_list" => [
  1366. [
  1367. "address" => $page,
  1368. "tag" => "花卉宝 花店",
  1369. "first_class" => "IT科技",
  1370. "second_class" => "软件服务提供商",
  1371. "first_id" => 210,
  1372. "second_id" => 413,
  1373. "title" => "花卉宝",
  1374. ],
  1375. ],
  1376. "feedback_info" => "",
  1377. "feedback_stuff" => "",
  1378. ];
  1379. } else {
  1380. $data = [
  1381. "item_list" => [
  1382. [
  1383. "address" => $page,
  1384. "tag" => "鲜花 花店",
  1385. "first_class" => "商家自营",
  1386. "second_class" => "鲜花/园艺/工艺品",
  1387. "first_id" => 304,
  1388. "second_id" => 323,
  1389. "title" => "商城",
  1390. ],
  1391. ],
  1392. "feedback_info" => "",
  1393. "feedback_stuff" => "",
  1394. ];
  1395. }
  1396. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1397. $respond = Json::decode($result);
  1398. echo "<pre>";
  1399. print_r($respond);
  1400. }
  1401. //查询最新一次提交的审核状态
  1402. public static function miniGetLatestAuditStatus($merchant, $ptStyle = 0)
  1403. {
  1404. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  1405. $url = "https://api.weixin.qq.com/wxa/get_latest_auditstatus?access_token=" . $accessToken;
  1406. $curl = new curl\Curl();
  1407. $result = $curl->get($url);
  1408. $respond = Json::decode($result);
  1409. echo "<pre>";
  1410. print_r($respond);
  1411. }
  1412. //发布已通过审核的小程序
  1413. public static function miniRelease($merchant, $ptStyle = 0)
  1414. {
  1415. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  1416. $url = "https://api.weixin.qq.com/wxa/release?access_token=" . $accessToken;
  1417. $curl = new curl\Curl();
  1418. $result = $curl->setOption(CURLOPT_POSTFIELDS, '{}')->post($url);
  1419. $respond = Json::decode($result);
  1420. return $respond;
  1421. }
  1422. //撤回审核
  1423. public static function rollbackCheck($merchant, $ptStyle = 0)
  1424. {
  1425. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  1426. $url = "https://api.weixin.qq.com/wxa/undocodeaudit?access_token=" . $accessToken;
  1427. $curl = new curl\Curl();
  1428. $result = $curl->get($url);
  1429. $respond = Json::decode($result);
  1430. echo "<pre>";
  1431. print_r($respond);
  1432. }
  1433. //申请会员的小程序码
  1434. public static function generateMemberCode($merchant, $shopId, $ptStyle = 0)
  1435. {
  1436. $fileName = $merchant['id'] . '_' . $shopId . '_apply_member.jpg';
  1437. $behind = 'retail/mimiMemberCode/';
  1438. $filePath = dirUtil::getImgDir() . $behind;
  1439. if (!file_exists($filePath)) {
  1440. mkdir($filePath, 0777, true);
  1441. }
  1442. $file = $filePath . $fileName;
  1443. if (file_exists($file)) {
  1444. return $behind . $fileName;
  1445. }
  1446. $path = 'pages/home/user?scan=1&shopId=' . $shopId;
  1447. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  1448. $url = "https://api.weixin.qq.com/wxa/getwxacode?access_token={$accessToken}";
  1449. $curl = new curl\Curl();
  1450. $data = [
  1451. "path" => $path,
  1452. "width" => 1000,
  1453. ];
  1454. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1455. file_put_contents($file, $result);
  1456. return $behind . $fileName;
  1457. }
  1458. public static function generateShortLink($merchant, $pageUrl, $pageTitle, $ptStyle)
  1459. {
  1460. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  1461. $url = "https://api.weixin.qq.com/wxa/genwxashortlink?access_token={$accessToken}";
  1462. $curl = new curl\Curl();
  1463. $data = [
  1464. "page_url" => $pageUrl,
  1465. "page_title" => $pageTitle,
  1466. 'is_permanent' => false,
  1467. ];
  1468. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1469. $result = Json::decode($result);
  1470. return $result;
  1471. }
  1472. /**
  1473. * 记录 getwxacodeunlimit 响应(成功为 JPEG 二进制,失败为 JSON,不可 json_encode 整段 body)
  1474. */
  1475. private static function logWxacodeUnlimitResponse(array $context, $result)
  1476. {
  1477. $body = is_string($result) ? $result : '';
  1478. $bytes = strlen($body);
  1479. $contextJson = Json::encode($context);
  1480. if ($bytes > 0 && isset($body[0]) && $body[0] === '{') {
  1481. try {
  1482. $err = Json::decode($body, true);
  1483. if (is_array($err)) {
  1484. Yii::error('小程序码接口返回错误 context=' . $contextJson . ' response=' . Json::encode($err), __METHOD__);
  1485. return;
  1486. }
  1487. } catch (\Exception $e) {
  1488. Yii::warning('小程序码接口返回疑似 JSON 但解析失败 context=' . $contextJson . ' preview=' . substr($body, 0, 200), __METHOD__);
  1489. return;
  1490. }
  1491. }
  1492. $isJpeg = $bytes >= 2 && substr($body, 0, 2) === "\xFF\xD8";
  1493. Yii::info('小程序码接口返回图片 context=' . $contextJson . ' bytes=' . $bytes . ' is_jpeg=' . ($isJpeg ? 1 : 0), __METHOD__);
  1494. }
  1495. //小程序码生成,永久有效,数量暂无限制 ssh 2020.4.17
  1496. public static function normalizeMiniEnvVersion($envVersion = 'release')
  1497. {
  1498. $envVersion = is_string($envVersion) ? strtolower(trim($envVersion)) : 'release';
  1499. if (!in_array($envVersion, ['develop', 'trial', 'release'], true)) {
  1500. return 'release';
  1501. }
  1502. return $envVersion;
  1503. }
  1504. public static function generateUnlimitedMiniCode($merchant, $page, $scene = '', $ptStyle = 0, $envVersion = 'release')
  1505. {
  1506. $envVersion = self::normalizeMiniEnvVersion($envVersion);
  1507. $id = isset($merchant['id']) ? $merchant['id'] : 0;
  1508. $fileName = md5($page . '_' . $scene . '_' . $envVersion) . '.jpg';
  1509. $behind = 'retail/mimiCode/' . date("Ym") . '/' . date("d") . '/' . $id . '/';
  1510. $filePath = dirUtil::getImgDir() . $behind;
  1511. if (!file_exists($filePath)) {
  1512. mkdir($filePath, 0777, true);
  1513. }
  1514. $file = $filePath . $fileName;
  1515. if (file_exists($file)) {
  1516. return $behind . $fileName;
  1517. }
  1518. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  1519. $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={$accessToken}";
  1520. $curl = new curl\Curl();
  1521. $data = [];
  1522. if (!empty($page)) {
  1523. $data["page"] = $page;
  1524. }
  1525. $data["scene"] = $scene;
  1526. if ($envVersion !== 'release') {
  1527. $data["env_version"] = $envVersion;
  1528. }
  1529. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1530. self::logWxacodeUnlimitResponse([
  1531. 'merchantId' => $id,
  1532. 'page' => $page,
  1533. 'scene' => $scene,
  1534. 'ptStyle' => $ptStyle,
  1535. 'env_version' => $envVersion,
  1536. 'saveAs' => $behind . $fileName,
  1537. ], $result);
  1538. file_put_contents($file, $result);
  1539. //上传oss
  1540. $obj = $behind . $fileName;
  1541. $obj = ltrim($obj, "/");
  1542. oss::uploadImage($obj, $file);
  1543. return $behind . $fileName;
  1544. }
  1545. //生成充值码,永久有效,数量暂无限制 ssh 20250307
  1546. public static function generateUnlimitedRechargeMiniCode($merchant, $page, $scene = '', $ptStyle = 0)
  1547. {
  1548. $id = isset($merchant['id']) ? $merchant['id'] : 0;
  1549. $fileName = md5($page . '_' . $scene) . '.jpg';
  1550. $behind = 'retail/rechargeCode/' . date("Ym") . '/' . date("d") . '/' . $id . '/';
  1551. $filePath = dirUtil::getImgDir() . $behind;
  1552. if (!file_exists($filePath)) {
  1553. mkdir($filePath, 0777, true);
  1554. }
  1555. $file = $filePath . $fileName;
  1556. if (file_exists($file)) {
  1557. return $behind . $fileName;
  1558. }
  1559. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  1560. $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={$accessToken}";
  1561. $curl = new curl\Curl();
  1562. $data = [];
  1563. if (!empty($page)) {
  1564. $data["page"] = $page;
  1565. }
  1566. $data["scene"] = $scene;
  1567. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1568. self::logWxacodeUnlimitResponse([
  1569. 'merchantId' => $id,
  1570. 'page' => $page,
  1571. 'scene' => $scene,
  1572. 'ptStyle' => $ptStyle,
  1573. 'saveAs' => $behind . $fileName,
  1574. ], $result);
  1575. file_put_contents($file, $result);
  1576. //上传oss
  1577. $obj = $behind . $fileName;
  1578. $obj = ltrim($obj, "/");
  1579. oss::uploadImage($obj, $file);
  1580. return $behind . $fileName;
  1581. }
  1582. /**
  1583. * 获取小程序手机号 (新接口) ssh 2025.07.28
  1584. * @param $merchant
  1585. * @param $code
  1586. * @param int $ptStyle
  1587. * @return mixed
  1588. */
  1589. public static function getPhoneNumber($merchant, $code, $ptStyle = 3)
  1590. {
  1591. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  1592. if (empty($accessToken)) {
  1593. return ['errcode' => -1, 'errmsg' => '获取微信令牌失败'];
  1594. }
  1595. $url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token={$accessToken}";
  1596. $data = ['code' => $code];
  1597. $curl = new curl\Curl();
  1598. $curl->setOption(CURLOPT_SSL_VERIFYPEER, false);
  1599. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1600. return Json::decode($result);
  1601. }
  1602. //微信开放平台获取component_access_token
  1603. public static function getComponentAccessToken($open)
  1604. {
  1605. $id = $open['id'];
  1606. $componentAccessToken = $open['componentAccessToken'];
  1607. $componentAccessTokenTime = $open['componentAccessTokenTime'];
  1608. $now = time();
  1609. if ($now > strtotime($componentAccessTokenTime)) {
  1610. $appId = $open['appId'];
  1611. $appSecret = $open['appSecret'];
  1612. $verifyTicket = $open['verifyTicket'];
  1613. $data = ['component_appid' => $appId, 'component_appsecret' => $appSecret, 'component_verify_ticket' => $verifyTicket];
  1614. $url = 'https://api.weixin.qq.com/cgi-bin/component/api_component_token';
  1615. $curl = new curl\Curl();
  1616. $curl->setOption(CURLOPT_SSL_VERIFYPEER, false);
  1617. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1618. if (empty($result)) {
  1619. util::stop('没有取到ComponentAccessToken');
  1620. }
  1621. $result = Json::decode($result);
  1622. if (isset($result['component_access_token']) == false || empty($result['component_access_token'])) {
  1623. Yii::info('get component access token error:' . json_encode($result));
  1624. Yii::$app->end();
  1625. }
  1626. $component_access_token = $result['component_access_token'];
  1627. $expiresIn = $result['expires_in'];
  1628. $oData = [];
  1629. $oData['componentAccessToken'] = $component_access_token;
  1630. $oData['componentAccessTokenTime'] = date("Y-m-d H:i:s", ($now + $expiresIn - 100));//安全起见,将过期时间设置得比微信里还短
  1631. xhWxOpenService::updateById($id, $oData);
  1632. return $component_access_token;
  1633. } else {
  1634. return $componentAccessToken;
  1635. }
  1636. }
  1637. /**
  1638. * 微信开放平台获取预授权码pre_auth_code
  1639. */
  1640. public static function getPreAuthCode($open)
  1641. {
  1642. $id = $open['id'];
  1643. $preAuthCode = $open['preAuthCode'];
  1644. $preAuthCodeTime = $open['preAuthCodeTime'];
  1645. $now = time();
  1646. if ($now > strtotime($preAuthCodeTime)) {
  1647. $token = self::getComponentAccessToken($open);
  1648. $appId = $open['appId'];
  1649. $url = "https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode?component_access_token={$token}";
  1650. $curl = new curl\Curl();
  1651. $data = ['component_appid' => $appId];
  1652. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1653. $result = Json::decode($result);
  1654. Yii::info($result);
  1655. if (isset($result['pre_auth_code']) == false || empty($result['pre_auth_code'])) {
  1656. echo "没有取到pre_auth_code,原因:" . json_encode($result) . " appid:" . $appId . " token:" . $token;
  1657. Yii::$app->end();
  1658. }
  1659. $preAuthCode = $result['pre_auth_code'];
  1660. $expiresIn = $result['expires_in'];
  1661. $oData = [];
  1662. $oData['preAuthCode'] = $preAuthCode;
  1663. $oData['preAuthCodeTime'] = date("Y-m-d H:i:s", ($now + $expiresIn - 100));//安全起见,将过期时间设置得比微信里还短100秒
  1664. xhWxOpenService::updateById($id, $oData);
  1665. return $preAuthCode;
  1666. } else {
  1667. return $preAuthCode;
  1668. }
  1669. }
  1670. /**
  1671. * 微信开放平台使用授权码换取公众号的接口调用凭据和授权信息
  1672. * authorizer_access_token authorizer_refresh_token
  1673. */
  1674. public static function getAuthorizer($code, $ptStyle)
  1675. {
  1676. $open = [];
  1677. if ($ptStyle == 1) {
  1678. $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => dict::getDict('ptStyle', 'hd')]);
  1679. }
  1680. if ($ptStyle == 2) {
  1681. $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => dict::getDict('ptStyle', 'ghs')]);
  1682. }
  1683. if ($ptStyle == 3) {
  1684. $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => dict::getDict('ptStyle', 'mall')]);
  1685. }
  1686. if (empty($open)) {
  1687. util::fail('没有找到平台');
  1688. }
  1689. $appId = $open['appId'];
  1690. $accessToken = self::getComponentAccessToken($open);
  1691. $url = "https://api.weixin.qq.com/cgi-bin/component/api_query_auth?component_access_token={$accessToken}";
  1692. $curl = new curl\Curl();
  1693. $data = ['authorization_code' => $code, 'component_appid' => $appId];
  1694. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1695. $result = Json::decode($result);
  1696. return $result;
  1697. }
  1698. //微信开放平台获取授权方的公众号帐号基本信息 ssh 2020.3.11
  1699. public static function getAuthorizerInfo($authorizeAppId, $open)
  1700. {
  1701. $appId = $open['appId'];
  1702. $accessToken = self::getComponentAccessToken($open);
  1703. $url = "https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_info?component_access_token={$accessToken}";
  1704. $curl = new curl\Curl();
  1705. $data = ['component_appid' => $appId, 'authorizer_appid' => $authorizeAppId];
  1706. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1707. $result = Json::decode($result);
  1708. return $result;
  1709. }
  1710. /**************************即时配送相关接口 START *****************************************/
  1711. //开通即时配送权限
  1712. public static function openLocalExpress($merchant, $ptStyle = 0)
  1713. {
  1714. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  1715. $url = "https://api.weixin.qq.com/cgi-bin/express/local/business/open?access_token=" . $accessToken;
  1716. $curl = new curl\Curl();
  1717. $result = $curl->get($url);
  1718. $respond = Json::decode($result);
  1719. echo "<pre>";
  1720. print_r($respond);
  1721. }
  1722. //异常件退回商家商家确认收货接口
  1723. //场景:当订单配送异常,骑手把货物退还给商家,商家收货以后调用本接口返回确认收货
  1724. /*
  1725. * $data
  1726. * {
  1727. "shopid": "123456",
  1728. "shop_order_id": "123456",
  1729. "shop_no": "shop_no_111"
  1730. "waybill_id": "123456",
  1731. "remark": "remark",
  1732. "delivery_sign": "123456"
  1733. }
  1734. */
  1735. public static function abnormalConfirm($data, $merchant, $ptStyle = 0)
  1736. {
  1737. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  1738. $url = 'https://api.weixin.qq.com/cgi-bin/express/local/business/order/confirm_return?access_token=' . $accessToken;
  1739. $curl = new curl\Curl();
  1740. $data = Json::encode($data);
  1741. $result = $curl->setOption(CURLOPT_POSTFIELDS, $data)->post($url);
  1742. $respond = Json::decode($result);
  1743. return $respond;
  1744. }
  1745. //即时配送,拉取已绑定账号 ssh 2021.2.1
  1746. public static function getBindAccount($merchant, $ptStyle = 0)
  1747. {
  1748. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  1749. $url = 'https://api.weixin.qq.com/cgi-bin/express/local/business/shop/get?access_token=' . $accessToken;
  1750. $curl = new curl\Curl();
  1751. $result = $curl->setOption(CURLOPT_POSTFIELDS, '{}')->post($url);
  1752. $respond = Json::decode($result);
  1753. return $respond;
  1754. }
  1755. //获取已支持的配送公司列表接口 lqh 2021.2.2
  1756. public static function getAllImmeDelivery($merchant, $ptStyle = 0)
  1757. {
  1758. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  1759. $url = 'https://api.weixin.qq.com/cgi-bin/express/local/business/delivery/getall?access_token=' . $accessToken;
  1760. $curl = new curl\Curl();
  1761. $result = $curl->setOption(CURLOPT_POSTFIELDS, '{}')->post($url);
  1762. $respond = Json::decode($result);
  1763. return $respond;
  1764. }
  1765. //下配送单接口 lqh 2021.2.2
  1766. //场景:
  1767. /*
  1768. * 商家可调用本接口向配送公司请求下配送单,配送公司会返回这一单的配送单号、配送费、预计骑手接单时间等信息。
  1769. 如遇下单错误,请先确认一下编码方式,python建议 json.dumps(b, ensure_ascii=False),php建议 json_encode($arr, JSON_UNESCAPED_UNICODE) 可预约时间:达达:72小时内,闪送2小时以后,48小时以内
  1770. {
  1771. "resultcode": 0,
  1772. "resultmsg": "ok",
  1773. "fee": 10, 实际运费(单位:元),运费减去优惠券费用
  1774. "deliverfee": 10, 运费(单位:元)
  1775. "couponfee": 0, 优惠券费用(单位:元)
  1776. "tips": 0, 小费(单位:元)
  1777. "insurancfee": 0, 保价费(单位:元)
  1778. "distance": 1000, 配送距离(整数单位:米)
  1779. "waybill_id": "123456789", 配送单号
  1780. "order_status": 101, 配送状态。
  1781. "finish_code": 1024, 收货码
  1782. "pickup_code": 2048, 取货码
  1783. "dispatch_duration": 300 预计骑手接单时间,单位秒,比如5分钟,就填300, 无法预计填0
  1784. }
  1785. */
  1786. public static function addOrder($data, $merchant, $ptStyle = 0)
  1787. {
  1788. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  1789. $url = 'https://api.weixin.qq.com/cgi-bin/express/local/business/order/add?access_token=' . $accessToken;
  1790. $curl = new curl\Curl();
  1791. $data = Json::encode($data);
  1792. $result = $curl->setOption(CURLOPT_POSTFIELDS, $data)->post($url);
  1793. $respond = Json::decode($result);
  1794. return $respond;
  1795. }
  1796. //可以对待接单状态的订单增加小费。需要注意:订单的小费,以最新一次加小费动作的金额为准,故下一次增加小费额必须大于上一次小费额
  1797. /*
  1798. * {
  1799. "shopid": "123456",
  1800. "shop_order_id": "123456",
  1801. "waybill_id": "123456",
  1802. "tips": 5,
  1803. "remark": "gogogo",
  1804. "delivery_sign": "123456",
  1805. "shop_no": "shop_no_111"
  1806. }
  1807. */
  1808. public static function addTip($data, $merchant, $ptStyle = 0)
  1809. {
  1810. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  1811. $url = 'https://api.weixin.qq.com/cgi-bin/express/local/business/order/addtips?access_token=' . $accessToken;
  1812. $curl = new curl\Curl();
  1813. $data = Json::encode($data);
  1814. $result = $curl->setOption(CURLOPT_POSTFIELDS, $data)->post($url);
  1815. $respond = Json::decode($result);
  1816. return $respond;
  1817. }
  1818. //取消配送单接口 lqh 2021.2.3
  1819. public static function cancelOrder($data, $merchant, $ptStyle = 0)
  1820. {
  1821. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  1822. $url = 'https://api.weixin.qq.com/cgi-bin/express/local/business/order/cancel?access_token=' . $accessToken;
  1823. $curl = new curl\Curl();
  1824. $data = Json::encode($data);
  1825. $result = $curl->setOption(CURLOPT_POSTFIELDS, $data)->post($url);
  1826. $respond = Json::decode($result);
  1827. return $respond;
  1828. }
  1829. //拉取配送单信息 lqh 2021.2.3
  1830. public static function getOrder($data, $merchant, $ptStyle = 0)
  1831. {
  1832. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  1833. $url = 'https://api.weixin.qq.com/cgi-bin/express/local/business/order/get?access_token=' . $accessToken;
  1834. $curl = new curl\Curl();
  1835. $data = Json::encode($data);
  1836. $result = $curl->setOption(CURLOPT_POSTFIELDS, $data)->post($url);
  1837. $respond = Json::decode($result);
  1838. return $respond;
  1839. }
  1840. //模拟配送公司更新配送单状态, 该接口只用于沙盒环境,即订单并没有真实流转到运力方
  1841. public static function mockUpdateOrder($data, $merchant, $ptStyle = 0)
  1842. {
  1843. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  1844. $url = 'https://api.weixin.qq.com/cgi-bin/express/local/business/test_update_order?access_token=' . $accessToken;
  1845. $curl = new curl\Curl();
  1846. $data = Json::encode($data);
  1847. $result = $curl->setOption(CURLOPT_POSTFIELDS, $data)->post($url);
  1848. $respond = Json::decode($result);
  1849. return $respond;
  1850. }
  1851. //预下配送单接口
  1852. //在用户提交外卖订单时,商家可调用本接口查询配送公司是否可接单、预计多久接单、运费预估等。
  1853. //预估运费可作为展示给用户的运费参考值。
  1854. public static function preAddOrder($data, $merchant, $ptStyle = 0)
  1855. {
  1856. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  1857. $url = 'https://api.weixin.qq.com/cgi-bin/express/local/business/order/pre_add?access_token=' . $accessToken;
  1858. $curl = new curl\Curl();
  1859. $data = Json::encode($data);
  1860. $result = $curl->setOption(CURLOPT_POSTFIELDS, $data)->post($url);
  1861. $respond = Json::decode($result);
  1862. return $respond;
  1863. }
  1864. //预取消配送单接口
  1865. //在正式取消配送单前,商家可调用本接口查询该订单是否可以取消,取消订单配送公司需要扣除的费用是多少
  1866. public static function preCancelOrder($data, $merchant, $ptStyle = 0)
  1867. {
  1868. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  1869. $url = 'https://api.weixin.qq.com/cgi-bin/express/local/business/order/precancel?access_token=' . $accessToken;
  1870. $curl = new curl\Curl();
  1871. $data = Json::encode($data);
  1872. $result = $curl->setOption(CURLOPT_POSTFIELDS, $data)->post($url);
  1873. $respond = Json::decode($result);
  1874. return $respond;
  1875. }
  1876. //重新下单
  1877. //在调用下配送单接口、订单被取消、过期或者投递异常的情况下,可调用此接口,重新下单,需要保持orderid一致。
  1878. //备注:美团不支持重新下单接口,如果订单被取消商家需要重新下单,请修改orderid之后,调用下配送单接口下单。
  1879. public static function reOrder($data, $merchant, $ptStyle = 0)
  1880. {
  1881. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  1882. $url = 'https://api.weixin.qq.com/cgi-bin/express/local/business/order/readd?access_token=' . $accessToken;
  1883. $curl = new curl\Curl();
  1884. $data = Json::encode($data);
  1885. $result = $curl->setOption(CURLOPT_POSTFIELDS, $data)->post($url);
  1886. $respond = Json::decode($result);
  1887. return $respond;
  1888. }
  1889. /**************************即时配送相关接口 END *****************************************/
  1890. //发送订阅消息通知 ssh 2023511
  1891. public static function commonSendSubscribeMessage($merchant, $ptStyle, $msgId, $miniOpenId, $page, $msg)
  1892. {
  1893. $wxNotice = dict::getDict('wxNotice');
  1894. if($wxNotice == 0) {
  1895. return [];
  1896. }
  1897. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  1898. $url = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=' . $accessToken;
  1899. $curl = new curl\Curl();
  1900. $data = ['template_id' => $msgId, 'page' => $page, 'touser' => $miniOpenId, 'data' => $msg];
  1901. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1902. Yii::info('订阅消息通知返回:' . $result);
  1903. $result = Json::decode($result);
  1904. return $result;
  1905. }
  1906. }