Imagetextmediapush.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. namespace app\admin\controller\messagecustom;
  3. use app\admin\service\AdminConfigService;
  4. use app\admin\service\CommonService;
  5. use app\admin\service\CustomService;
  6. use app\common\constants\Custom;
  7. use app\common\constants\Message;
  8. use app\common\constants\OfficialAccount;
  9. use app\common\controller\Backend;
  10. use app\common\model\BookCategory;
  11. class Imagetextmediapush extends Backend
  12. {
  13. /**
  14. * @var \app\admin\model\CustomMediaPush
  15. */
  16. protected $model = null;
  17. /**
  18. * @var \app\common\model\AdminConfig
  19. */
  20. protected $adminConfigModel = null;
  21. /**
  22. * @var \app\common\model\SubscriptionRelation
  23. */
  24. protected $subscriptionRelationModel = null;
  25. /**
  26. * @var \app\common\model\Subscription
  27. */
  28. protected $subscriptionModel = null;
  29. public function _initialize()
  30. {
  31. parent::_initialize();
  32. $this->model = model('CustomMediaPush');
  33. $this->adminConfigModel = model('AdminConfig');
  34. $this->subscriptionModel = model('Subscription');
  35. $this->subscriptionRelationModel = model('SubscriptionRelation');
  36. }
  37. public function index()
  38. {
  39. //设置过滤方法
  40. $this->request->filter(['strip_tags']);
  41. $status = $this->request->param('status') ?? 0;
  42. if ($this->request->isAjax()) {
  43. //如果发送的来源是Selectpage,则转发到Selectpage
  44. if ($this->request->request('pkey_name')) {
  45. return $this->selectpage();
  46. }
  47. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  48. $filter = $this->request->get("filter", '');
  49. $filter = json_decode($filter, true);
  50. $where = [];
  51. $subId = $adminId = false;
  52. if ($filter) {
  53. foreach ($filter as $k => $v) {
  54. switch ($k) {
  55. case 'sendtime':
  56. $v = str_replace(' - ', ',', $v);
  57. $arr = array_slice(explode(',', $v), 0, 2);
  58. if (stripos($v, ',') === false || !array_filter($arr)) {
  59. continue;
  60. }
  61. $arr[0] = strtotime($arr[0]);
  62. $arr[1] = strtotime($arr[1]);
  63. $where[$k] = ['BETWEEN', "{$arr[0]},{$arr[1]}"];
  64. break;
  65. case 'official_account_type':
  66. $where[$k] = $v;
  67. break;
  68. case 'wechat_name':
  69. if (isset($filter['official_account_type'])) {
  70. if ($filter['official_account_type'] == OfficialAccount::OFFICIAL_ACCOUNT_TYPE_SERVICE) {
  71. $adminId = $this->adminConfigModel->getAdminIdByOfficialName($v);
  72. } elseif ($filter['official_account_type'] == OfficialAccount::OFFICIAL_ACCOUNT_TYPE_SUBSCRIPTION) {
  73. $subId = $this->subscriptionModel->getIdByOfficialName($v);
  74. }
  75. } else {
  76. $adminId = $this->adminConfigModel->getAdminIdByOfficialName($v);
  77. $subId = $this->subscriptionModel->getIdByOfficialName($v);
  78. }
  79. break;
  80. case 'id':
  81. $where[$k] = $v;
  82. break;
  83. case 'message_json$[*].book_name':
  84. $where[$k] = ['like', "%$v%"];
  85. break;
  86. default:
  87. break;
  88. }
  89. }
  90. }
  91. $officialConditions = [];
  92. if ($adminId) {
  93. $officialConditions[] = "find_in_set($adminId,official_account_service_ids)";
  94. }
  95. if ($subId) {
  96. $officialConditions[] = "find_in_set($subId,official_account_subscribe_ids)";
  97. }
  98. $officialCondition = implode(' OR ', $officialConditions);
  99. $countFetchObj = $this->_buildPushObj($where, $officialCondition);
  100. $total = $countFetchObj
  101. ->count();
  102. $listFetchObj = $this->_buildPushObj($where, $officialCondition);
  103. $list = $listFetchObj
  104. ->order($sort, $order)
  105. ->limit($offset, $limit)
  106. ->select();
  107. $result = array("total" => $total, "rows" => $list);
  108. return json($result);
  109. }
  110. $this->assign('status', $status);
  111. $this->assignconfig('status', $status);
  112. return $this->view->fetch();
  113. }
  114. /**
  115. * 删除 客服消息素材推送记录
  116. * @param string $ids
  117. * @throws \think\db\exception\DataNotFoundException
  118. * @throws \think\db\exception\ModelNotFoundException
  119. * @throws \think\exception\DbException
  120. */
  121. public function del($ids = "")
  122. {
  123. if ($ids) {
  124. $customService = new CustomService();
  125. $result = $customService->delCustomMediaPush($ids);
  126. if ($result) {
  127. $this->success();
  128. }else {
  129. $this->error(__('No rows were deleted'));
  130. }
  131. }
  132. $this->error(__('Parameter %s can not be empty', 'ids'));
  133. }
  134. /**
  135. * 查看详情
  136. * @param string $ids
  137. * @return \think\response\Json
  138. * @throws \think\Exception
  139. * @throws \think\exception\DbException
  140. */
  141. public function viewDetail($ids = '')
  142. {
  143. if ($ids) {
  144. $obj_arr = $this->model->get(['id' => $ids]) ->toArray();
  145. if($this->request->isAjax()) {
  146. $json_data = CustomService::instance()->getCustomMediaPushDetail($ids);
  147. return json($json_data);
  148. }
  149. $obj_arr['message_json'] = json_decode($obj_arr['message_json'], true);
  150. $obj_arr['user_json'] = json_decode($obj_arr['user_json'], true);
  151. $this->assign('bookcategorylist', BookCategory::getBookCategoryList());
  152. $this->assign('obj', $obj_arr);
  153. $this->assign('officialSuccess', CustomService::instance()->getOfficialPushStatusList($obj_arr['official_account_type'], $ids, Custom::CUSTOM_STATUE_HIDDEN));
  154. $this->assign('officialFail', CustomService::instance()->getOfficialPushStatusList($obj_arr['official_account_type'], $ids, Custom::CUSTOM_STATUE_FAIL));
  155. return $this->fetch();
  156. }
  157. $this->error(__('Parameter %s can not be empty', 'ids'));
  158. }
  159. /**
  160. * 编辑待发送的图文消息
  161. */
  162. public function editWaitingImgMsg($ids = '')
  163. {
  164. if ($ids) {
  165. if ($this->request->isAjax()) {
  166. $params = $this->request->post();
  167. $customService = new CustomService();
  168. $sendtime = strtotime($params['row']['sendtime']);
  169. $pushId = $params['row']['ids'];
  170. $result = $customService->editCustomMediaPush($pushId, $sendtime);
  171. if ($result) {
  172. $this->success();
  173. } else {
  174. $this->error(__('No rows were deleted'));
  175. }
  176. }
  177. $oMedia = $this->model->find($ids);
  178. if ($oMedia['official_account_type'] == OfficialAccount::OFFICIAL_ACCOUNT_TYPE_SUBSCRIPTION) {
  179. $subList = $this->subscriptionRelationModel->getSubListByAdminId($this->auth->id);
  180. $this->assign("sublist", $subList);
  181. $this->assign("selected", explode(',', $oMedia['official_account_subscribe_ids']));
  182. }
  183. $wechatAccountLimit = CommonService::instance()->getWechatAccountLimit();
  184. $this->assign('sub_limited', $wechatAccountLimit['subLimitNum']);
  185. $this->assign('acc_type', $oMedia['official_account_type']);
  186. $this->assign('admin_id',$this->auth->id);
  187. $this->assign('ids', $ids);
  188. return $this->fetch();
  189. }
  190. $this->error(__('Parameter %s can not be empty', 'ids'));
  191. }
  192. private function _buildPushObj($where, $officialCondition)
  193. {
  194. $status = $this->request->param('status') ?? Custom::CUSTOM_MEDIA_PUSH_STATUS_PENDING;
  195. $channelId = $this->auth->id;
  196. $condition = [
  197. 'created_admin_id' => $channelId,
  198. 'message_type' => Message::MESSAGE_TYPE_IMAGE_TEXT,
  199. 'created_from' => Custom::CUSTOM_CREATED_FROM_GROUP_SEND,
  200. 'status' => $status,
  201. ];
  202. $obj = $this->model
  203. ->where($where)
  204. ->where($condition);
  205. if (!empty($officialCondition)) {
  206. $obj->where($officialCondition);
  207. }
  208. return $obj;
  209. }
  210. }