SubMsg.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. namespace app\store\service\wxapp;
  3. use app\store\model\Wxapp as WxappModel;
  4. use app\store\model\Setting as SettingModel;
  5. use app\common\library\helper;
  6. use app\common\service\Basics;
  7. use app\common\library\wechat\WxSubMsg;
  8. use app\common\exception\BaseException;
  9. /**
  10. * 小程序订阅消息服务类
  11. * Class SubMsg
  12. * @package app\store\service\wxapp
  13. */
  14. class SubMsg extends Basics
  15. {
  16. /* @var $WxSubMsg WxSubMsg 小程序订阅消息api类 */
  17. private $WxSubMsg;
  18. /**
  19. * 构造方法
  20. * SubMsg constructor.
  21. * @param null $wxappId
  22. * @throws \app\common\exception\BaseException
  23. * @throws \think\Exception
  24. * @throws \think\exception\DbException
  25. */
  26. public function __construct($wxappId = null)
  27. {
  28. // 小程序订阅消息api类
  29. $wxConfig = WxappModel::getWxappCache($wxappId);
  30. $this->WxSubMsg = new WxSubMsg($wxConfig['app_id'], $wxConfig['app_secret']);
  31. }
  32. /**
  33. * 一键添加订阅消息
  34. * @return bool
  35. * @throws BaseException
  36. * @throws \think\exception\DbException
  37. */
  38. public function shuttle()
  39. {
  40. // 拉取我的模板列表
  41. $myList = $this->getMyTemplateList();
  42. // 筛选出未添加的模板
  43. $addedList = $this->getNotAddedTemplates($myList);
  44. // 批量添加订阅消息模板
  45. $newList = $this->onBatchAdd($addedList);
  46. // 全部模板列表
  47. $tplList = array_merge($newList, $myList);
  48. // 保存全部模板id
  49. return $this->saveAll($tplList);
  50. }
  51. /**
  52. * 保存全部模板id
  53. * @param $tplList
  54. * @return bool
  55. * @throws \think\exception\DbException
  56. */
  57. private function saveAll($tplList)
  58. {
  59. // 整理模板id
  60. $data = SettingModel::getItem('submsg');
  61. foreach ($data as &$group) {
  62. foreach ($group as &$item) {
  63. if (!isset($item['title'])) continue;
  64. $tpl = helper::arraySearch($tplList, 'title', $item['title']);
  65. $tpl != false && $item['template_id'] = $tpl['priTmplId'];
  66. }
  67. }
  68. // 保存数据
  69. return (new SettingModel)->edit('submsg', $data);
  70. }
  71. /**
  72. * 批量添加订阅消息模板
  73. * [并且记录返回的priTmplId]
  74. * @param $newList
  75. * @return array
  76. * @throws BaseException
  77. */
  78. private function onBatchAdd($newList)
  79. {
  80. foreach ($newList as &$item) {
  81. // 请求微信api, 添加模板记录
  82. $response = $this->WxSubMsg->addTemplate($item['tid'], $item['kidList'], $item['sceneDesc']);
  83. if ($response === false) {
  84. throw new BaseException(['msg' => "添加模板[{$item['sceneDesc']}]失败:" . $this->WxSubMsg->getError()]);
  85. }
  86. // 记录template_id
  87. $item['priTmplId'] = $response['priTmplId'];
  88. }
  89. return $newList;
  90. }
  91. /**
  92. * 筛选出未添加的模板
  93. * @param $myList
  94. * @return array
  95. */
  96. private function getNotAddedTemplates($myList)
  97. {
  98. // 所有订阅消息模板列表
  99. $templateLists = $this->getTemplates();
  100. if (empty($myList)) return $templateLists;
  101. // 整理未添加的
  102. $data = [];
  103. foreach ($templateLists as $item) {
  104. if (helper::arraySearch($myList, 'title', $item['title']) === false) {
  105. $data[] = $item;
  106. }
  107. }
  108. return $data;
  109. }
  110. /**
  111. * 所有订阅消息模板列表
  112. * @return array
  113. */
  114. private function getTemplates()
  115. {
  116. return [
  117. // 支付成功通知
  118. [
  119. 'tid' => 9344,
  120. 'title' => '新订单提醒',
  121. 'kidList' => [1, 2, 4, 3],
  122. 'sceneDesc' => '新订单提醒',
  123. ],
  124. // 订单发货通知
  125. [
  126. 'tid' => 855,
  127. 'title' => '订单发货通知',
  128. 'kidList' => [1, 2, 12, 11, 17],
  129. 'sceneDesc' => '订单发货通知',
  130. ],
  131. // 售后状态通知
  132. [
  133. 'tid' => 5049,
  134. 'title' => '售后状态通知',
  135. 'kidList' => [1, 6, 2, 3, 4],
  136. 'sceneDesc' => '售后状态通知',
  137. ],
  138. // 拼团进度通知
  139. [
  140. 'tid' => 5008,
  141. 'title' => '拼团进度通知',
  142. 'kidList' => [1, 5, 7, 3, 6],
  143. 'sceneDesc' => '拼团进度通知',
  144. ],
  145. // 分销商入驻审核通知
  146. [
  147. 'tid' => 4050,
  148. 'title' => '代理商入驻审核通知',
  149. 'kidList' => [1, 2, 3, 4],
  150. 'sceneDesc' => '分销商入驻审核通知',
  151. ],
  152. // 提现成功通知
  153. [
  154. 'tid' => 2001,
  155. 'title' => '提现成功通知',
  156. 'kidList' => [1, 3, 4],
  157. 'sceneDesc' => '提现成功通知',
  158. ],
  159. // 提现失败通知
  160. [
  161. 'tid' => 3173,
  162. 'title' => '提现失败通知',
  163. 'kidList' => [1, 3, 4],
  164. 'sceneDesc' => '提现失败通知',
  165. ],
  166. ];
  167. }
  168. /**
  169. * 拉取我的模板列表
  170. * @return mixed
  171. * @throws BaseException
  172. */
  173. private function getMyTemplateList()
  174. {
  175. $response = $this->WxSubMsg->getTemplateList();
  176. if ($response === false) {
  177. throw new BaseException(['msg' => '拉取模板列表失败:' . $this->WxSubMsg->getError()]);
  178. }
  179. return $response['data'];
  180. }
  181. }