Linktextmediapush.php 8.4 KB

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