NoticeAction.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. /*
  8. * @Author: 凯
  9. * @Date: 2021-04-21 11:16:10
  10. * @LastEditTime: 2021-04-23 17:47:22
  11. * @LastEditors: Please set LastEditors
  12. * @Description: In User Settings Edit
  13. * @FilePath: \admin_php\utils\NoticeAction.php
  14. */
  15. namespace app\utils\Notice;
  16. use app\models\Notice;
  17. use app\models\NoticeErr;
  18. use app\models\Option;
  19. use app\models\SmsRecord;
  20. use app\models\SmsSetting;
  21. use app\models\StoreMini;
  22. use app\models\User;
  23. use app\models\WechatConfig;
  24. use app\utils\Sms;
  25. use EasyWeChat\Factory;
  26. use Yii;
  27. use yii\helpers\Json;
  28. use EasyWeChat\Kernel\BaseClient;
  29. class NoticeAction
  30. {
  31. /**
  32. * @description: 获取小程序配置
  33. * @param {*}
  34. * @return {*}
  35. */
  36. public static function getMini($store_id = null)
  37. {
  38. $store_id = $store_id ? $store_id : get_store_id();
  39. $wechat_config = WechatConfig::findOne(['store_id' => $store_id, 'type' => 1]);
  40. $self_mini = Option::get('self_mini', $store_id, 'store', 0)['value'];
  41. if ((int)$store_id === 0 || (\Yii::$app->prod_is_dandianpu() && !$self_mini)) {
  42. $wechat_config = (object)[
  43. 'app_id' => Option::get('one_store_wechat_appid', 0, 'saas')['value'],
  44. 'app_secret' => Option::get('one_store_wechat_secret', 0, 'saas')['value'],
  45. ];
  46. }
  47. if (!$wechat_config) {
  48. throw new \Exception('Wechat App Is Null');
  49. }
  50. if (empty($wechat_config->app_secret)) {
  51. return [
  52. 'code' => 1,
  53. 'msg' => '使用三方配置信息'
  54. ];
  55. } else {
  56. $config = [
  57. 'app_id' => $wechat_config->app_id,
  58. 'secret' => $wechat_config->app_secret,
  59. 'response_type' => 'array'
  60. ];
  61. }
  62. $wechat = Factory::miniProgram($config);
  63. return $wechat;
  64. }
  65. /**
  66. * @description: 获取公众号配置
  67. * @param {*}
  68. * @return {*}
  69. */
  70. public static function getWx($store_id = null)
  71. {
  72. $store_id = $store_id ? $store_id : get_store_id();
  73. $wechat_config = WechatConfig::findOne(['store_id' => $store_id, 'type' => 2]);
  74. if (!$wechat_config) {
  75. throw new \Exception('Wechat App Is Null');
  76. }
  77. $config = [
  78. 'app_id' => $wechat_config->app_id,
  79. 'secret' => $wechat_config->app_secret,
  80. 'response_type' => 'array'
  81. ];
  82. $wechat = Factory::officialAccount($config);
  83. return $wechat;
  84. }
  85. /**
  86. * @description: 拉取小程序模板ID
  87. * @param {int} $tid
  88. * @param {array} $kidList
  89. * @param {string} $sceneDesc 场景描述
  90. * @param {string} $template_id 删除原来的模板ID
  91. * @return {*}
  92. */
  93. public static function getTamplateId($tid, array $kidList, $sceneDesc = '', $template_id = '', $type = 'miapp')
  94. {
  95. $app = $type == 'miapp' ? self::getMini() : self::getWx();
  96. if (isset($app['code']) && $app['code'] === 1) {
  97. $client = self::isv_function();
  98. if (is_array($client) && $client['code'] !== 0) {
  99. return [
  100. 'code' => 1,
  101. 'msg' => '小程序信息错误'
  102. ];
  103. }
  104. $data = [
  105. 'tid' => $tid,
  106. 'kidList' => $kidList,
  107. 'sceneDesc' => $sceneDesc
  108. ];
  109. /*$data = [
  110. 'ids' => '311,2,670',
  111. 'start' => 390,
  112. 'limit' => 30
  113. ];*/
  114. $res = $client->httpPostJson("wxaapi/newtmpl/addtemplate", $data);
  115. //生活服务->百货/超市/便利店 物流服务 => 查件 商家自营 => 3C数码
  116. //生活服务->百货/超市/便利店 物流服务 => 查件 商家自营 => 办公/文具
  117. //$res = $client->httpGet("wxaapi/newtmpl/getcategory", $data);
  118. // $res = $client->httpGet("wxaapi/newtmpl/getpubtemplatetitles", $data);
  119. if ($res['errcode'] == 0 && $template_id) {
  120. $client->httpPostJson("wxaapi/newtmpl/deltemplate", ['priTmplId' => $template_id]);
  121. }
  122. } else {
  123. $res = $app->subscribe_message->addTemplate($tid, $kidList, $sceneDesc);
  124. $res = is_array($res) ? $res : Json::decode($res);
  125. if ($res['errcode'] == 0 && $template_id) {
  126. $app->subscribe_message->deleteTemplate($template_id);
  127. }
  128. }
  129. if ($res['errcode'] == 0 || !empty($res['priTmplId'])) {
  130. return [
  131. 'code' => 0,
  132. 'template_id' => $res['priTmplId']
  133. ];
  134. } else {
  135. return [
  136. 'code' => $res['errcode'],
  137. 'msg' => $res['errmsg']
  138. ];
  139. }
  140. }
  141. /**
  142. * @description: 获取发送模板ID
  143. * @param {array} $sendType
  144. * @param {*} $type wxaapi公众号 miapp小程序
  145. * @return {*}
  146. */
  147. public static function getSendTamplateId( array $sendType, $type = 'miapp')
  148. {
  149. $self_mini = 0;
  150. if (intval(get_store_id()) > 0) {
  151. $self_mini = \app\models\Option::get('self_mini', get_store_id(), 'store', 0)['value'];
  152. }
  153. if ((\Yii::$app->prod_is_dandianpu() && !$self_mini) || (int)get_store_id() === 0) {
  154. $setting = Notice::findOne(['store_id' => 0]);
  155. } else {
  156. $setting = Notice::findOne(['store_id' => get_store_id()]);
  157. }
  158. //$setting = Notice::findOne(['store_id' => get_store_id()]);
  159. if (!in_array($type, ['wxaapi' , 'miapp'])) {
  160. return [];
  161. }
  162. if ($setting) {
  163. $setting_arr = $setting->toArray();
  164. $return_arr = [];
  165. foreach ($setting_arr as $k => $v) {
  166. if ($v && in_array($k, $sendType)) {
  167. $v_arr = Json::decode($v);
  168. if ($v_arr[$type]['is_open']) {
  169. $return_arr[] = $v_arr[$type]['template_id'];
  170. }
  171. }
  172. }
  173. return $return_arr;
  174. } else {
  175. return [];
  176. }
  177. }
  178. public static function sendMine2($user_id, $template_id, $page, array $data_info, $type = 'wxaapi')
  179. {
  180. return self::sendMine($user_id, $template_id, $page, $data_info, $type);
  181. }
  182. /**
  183. * @description: 发送模板消息
  184. * @param int|string $user_id|$openid
  185. * @param int $template_id
  186. * @param string $page
  187. * @param array $data
  188. * @return {*}
  189. */
  190. public static function sendMine($user_id, $template_id, $page, array $data_info, $type = 'miapp')
  191. {
  192. try {
  193. $user = User::findOne($user_id);
  194. if($user && ($user->wechat_open_id || $user->wechat_platform_open_id)){
  195. $openid = $type == 'miapp' ? $user->wechat_open_id : $user->wechat_platform_open_id;
  196. }elseif(!empty($user_id) && is_string($user_id)){
  197. $openid = $user_id;
  198. }
  199. if ($openid) {
  200. $data = [
  201. 'template_id' => $template_id,
  202. 'touser' => $openid,
  203. 'page' => $page,
  204. ];
  205. if ($page) {
  206. $data['page'] = $page;
  207. }
  208. $data['data'] = $data_info;
  209. $app = $type == 'miapp' ? self::getMini($user->store_id) : self::getWx($user->store_id);
  210. if (isset($app['code']) && $app['code'] === 1) {
  211. $client = self::isv_function($user->store_id);
  212. if (is_array($client)) {
  213. throw new \Exception('参数配置错误');
  214. }
  215. $res = $client->httpPostJson("cgi-bin/message/subscribe/send", $data);
  216. } else {
  217. if($type == 'wxaapi'){
  218. $data['page'] = \Yii::$app->request->hostInfo . '/h5/#/' . $page;
  219. $client = new BaseClient($app);
  220. //订阅消息
  221. $res = $client->httpPostJson("cgi-bin/message/subscribe/bizsend", $data);
  222. debug_log([$res, $type, $data], __CLASS__ . '.log');
  223. if ($res['errcode'] != 0) {
  224. //模板消息
  225. $data['url'] = $data['page'];
  226. $res = $client->httpPostJson("cgi-bin/message/template/send", $data);
  227. debug_log([$res, $type, $data], __CLASS__ . '.log');
  228. }
  229. // debug_log([$res, $type, $data], __CLASS__ . '.log');
  230. }else{
  231. $res = $app->subscribe_message->send($data);
  232. $res = is_array($res) ? $res : Json::decode($res);
  233. debug_log([$res, $type, $data], __CLASS__ . '.log');
  234. }
  235. }
  236. if ($res['errcode'] != 0) {
  237. $form = new NoticeErr();
  238. $form->store_id = $user->store_id ?? get_store_id();
  239. $form->user_id = $user_id;
  240. $form->data = Json::encode($data);
  241. $form->err_msg = Json::encode($res);
  242. $form->addtime = time();
  243. $form->type = $type == 'miapp' ? 1 : 2;
  244. $form->save();
  245. }
  246. }
  247. } catch (\Exception $e) {
  248. \Yii::error('line->>>' . $e->getLine());
  249. \Yii::error($e->getMessage());
  250. \Yii::error($e);
  251. \Yii::error('模板消息发送失败');
  252. }
  253. }
  254. /**
  255. * @description: 发送短信
  256. * @param {*} $mobile 手机号
  257. * @param {*} $tpl 短信模板code
  258. * @param {*} $msg 模板变量
  259. * @param {*} $content 变量
  260. * @return array
  261. */
  262. public static function sendSms($mobile = null, $tpl = null,$msg = null, $content = null, $store_id = -1)
  263. {
  264. \Yii::error([$mobile, $tpl,$msg, $content, $store_id]);
  265. $sms_setting = SmsSetting::findOne(['store_id' => $store_id]);
  266. if (!$sms_setting) {
  267. $sms_setting = SmsSetting::findOne(['store_id' => 0]);
  268. if (!$sms_setting) {
  269. $sms_setting = SmsSetting::findOne(['store_id' => -1]);
  270. }
  271. }
  272. if (is_array($msg) && is_array($content)) {
  273. foreach ($msg as $index => $item) {
  274. $content_sms[$item] = $content[$index];
  275. }
  276. } else {
  277. $content_sms[$msg] = $content;
  278. }
  279. /**
  280. * @var EasySms $sms
  281. */
  282. \Yii::error([$sms_setting]);
  283. $sms = Sms::initSms($sms_setting);
  284. try {
  285. $data = [
  286. 'content' => $content,
  287. 'template' => $tpl
  288. ];
  289. if ($msg) {
  290. if (is_array($msg) && is_array($content)) {
  291. foreach ($msg as $index => $item) {
  292. $data['data'][$item] = $content[$index];
  293. }
  294. } else {
  295. $data['data'] = [
  296. $msg => $content
  297. ];
  298. }
  299. }
  300. $result = $sms->send($mobile, $data);
  301. \Yii::error([$result, $mobile, $data]);
  302. if ($result['aliyun']['status'] != 'success') {
  303. $result_twice = $sms->send($mobile, $data);
  304. if ($result_twice['aliyun']['status'] != 'success') {
  305. $form = new NoticeErr();
  306. $form->store_id = $store_id;
  307. $form->user_id = 0;
  308. $form->data = Json::encode($data);
  309. $form->err_msg = Json::encode($result_twice['aliyun']['status']);
  310. $form->type = 3;
  311. $form->addtime = time();
  312. $form->save();
  313. return [
  314. 'code' => 1,
  315. 'msg' => $result_twice['aliyun']['status']['Message']
  316. ];
  317. }
  318. }
  319. } catch (\Exception $e) {
  320. \Yii::error($e);
  321. return [
  322. 'code' => 1,
  323. 'msg' => $e->getMessage()
  324. ];
  325. }
  326. if (is_array($content_sms)) {
  327. foreach ($content_sms as $k => $v) {
  328. $content_sms[$k] = strval($v);
  329. }
  330. $content_sms = json_encode($content_sms, JSON_UNESCAPED_UNICODE);
  331. }
  332. return [
  333. 'code' => 0,
  334. 'msg' => '成功'
  335. ];
  336. }
  337. /**
  338. * @description: 发送短信
  339. * @param {*} $mobile 手机号
  340. * @param {*} $tpl 短信模板code
  341. * @param {*} $msg 模板变量
  342. * @param {*} $content 变量
  343. * @return array
  344. */
  345. public static function sendAdoptSms($mobile = null, $tpl = null,$msg = null, $content = null,$day1 = null,$day2 = null)
  346. {
  347. $sms_setting = SmsSetting::findOne(['store_id' => get_store_id()]);
  348. $content_sms[$msg] = $content;
  349. /**
  350. * @var EasySms $sms
  351. */
  352. $sms = Sms::initSms($sms_setting);
  353. try {
  354. $data = [
  355. 'content' => $content,
  356. 'template' => $tpl
  357. ];
  358. if ($msg) {
  359. $data['data'] = [
  360. $msg => $content,
  361. 'day1'=>$day1,
  362. 'day2'=>$day2,
  363. ];
  364. }
  365. $result = $sms->send($mobile, $data);
  366. if ($result['aliyun']['status'] != 'success') {
  367. $result_twice = $sms->send($mobile, $data);
  368. if ($result_twice['aliyun']['status'] != 'success') {
  369. $form = new NoticeErr();
  370. $form->store_id = get_store_id();
  371. $form->user_id = 0;
  372. $form->data = Json::encode($data);
  373. $form->err_msg = Json::encode($result_twice['aliyun']['status']);
  374. $form->type = 3;
  375. $form->addtime = time();
  376. $form->save();
  377. return [
  378. 'code' => 1,
  379. 'msg' => $result_twice['aliyun']['status']['Message']
  380. ];
  381. }
  382. }
  383. } catch (\Exception $e) {
  384. \Yii::error(['ALIYUN SMS SUPPLIER NOTIFY EXCEPTION <=============> ', $e->getMessage()]);
  385. return [
  386. 'code' => 1,
  387. 'msg' => $e->getMessage()
  388. ];
  389. }
  390. if (is_array($content_sms)) {
  391. foreach ($content_sms as $k => $v) {
  392. $content_sms[$k] = strval($v);
  393. }
  394. $content_sms = json_encode($content_sms, JSON_UNESCAPED_UNICODE);
  395. }
  396. return [
  397. 'code' => 0,
  398. 'msg' => '成功'
  399. ];
  400. }
  401. public static function isv_function($store_id = null)
  402. {
  403. $store_id = $store_id ? $store_id : get_store_id();
  404. try {
  405. $store_mini = StoreMini::find()->where(['store_id' => $store_id, 'is_cancle' => 0, 'is_use' => 1])->orderBy('id desc')->select('appid, authorizer_refresh_token')->one();
  406. if (empty($store_mini->appid) || empty($store_mini->authorizer_refresh_token)) {
  407. return [
  408. 'code' => 1,
  409. 'msg' => "信息配置不完整"
  410. ];
  411. }
  412. $authorizer_refresh_token = json_decode($store_mini->authorizer_refresh_token, true);
  413. //获取三方平台配置信息
  414. $config = [
  415. 'app_id' => Option::get("platform_third_appid", 0, 'saas')['value'],
  416. 'secret' => Option::get("platform_third_secret", 0, 'saas')['value'],
  417. 'token' => Option::get("platform_token", 0, 'saas')['value'],
  418. 'aes_key' => Option::get("platform_encodingAesKey", 0, 'saas')['value']
  419. ];
  420. $openPlatform = Factory::openPlatform($config);
  421. $mini_program = $openPlatform->miniProgram($store_mini->appid, $store_mini->authorizer_refresh_token);
  422. return new BaseClient($mini_program);
  423. } catch (\Exception $e) {
  424. return [
  425. 'code' => 1,
  426. 'msg' => $e->getMessage()
  427. ];
  428. }
  429. }
  430. }