Message.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use app\admin\model\Template;
  5. use app\admin\model\UserGroup;
  6. use Exception;
  7. use think\exception\DbException;
  8. use think\exception\PDOException;
  9. use think\exception\ValidateException;
  10. use think\response\Json;
  11. use DateTime;
  12. use onesignal\client\api\DefaultApi;
  13. use onesignal\client\Configuration;
  14. use onesignal\client\model\GetNotificationRequestBody;
  15. use onesignal\client\model\Notification;
  16. use onesignal\client\model\StringMap;
  17. use onesignal\client\model\Player;
  18. use onesignal\client\model\UpdatePlayerTagsRequestBody;
  19. use onesignal\client\model\ExportPlayersRequestBody;
  20. use onesignal\client\model\Segment;
  21. use onesignal\client\model\FilterExpressions;
  22. use GuzzleHttp;
  23. use app\common\model\User;
  24. use think\Db;
  25. use think\Queue;
  26. use think\Request;
  27. /**
  28. *
  29. *
  30. * @icon fa fa-circle-o
  31. */
  32. class Message extends Backend
  33. {
  34. /**
  35. * Message模型对象
  36. * @var \app\admin\model\Message
  37. */
  38. protected $model = null;
  39. public function _initialize()
  40. {
  41. parent::_initialize();
  42. $this->model = new \app\admin\model\Message;
  43. $this->view->assign("statusList", $this->model->getStatusList());
  44. $url = $_SERVER['HTTP_HOST'];
  45. if (strpos($url, 'westkiss') !== false) {
  46. $appKeyToken='MDMwZThjYWMtOWQ4ZC00YzU3LWIwNjktOGYzNTA2NjA3NTBh';
  47. $userKeyToken='YTdmZGE5MGEtN2Y3Yi00Mzk0LTgyOGQtMGMzNDkzYTcxYmJh';
  48. }elseif (strpos($url, 'asteriahair') !== false) {
  49. $appKeyToken='NGU4ZTU0OGUtMGU2Yy00NjNmLTgwNWItZTVmYjQxODg5Yzhh';
  50. $userKeyToken='NjQwYWRlYWEtODlmZS00ZDFhLThmZmMtN2E0ZTQwZWI2NmY5';
  51. }else{
  52. $appKeyToken='NzRhMTYyODUtYzczYi00Yjg5LWI3NzktODFmMmY0MGUyODIx';
  53. $userKeyToken='NWU3OGMxNzgtZjEzNS00M2JmLThmNzItZjY5YzIxOWU5YTQ3';
  54. }
  55. $config= Configuration::getDefaultConfiguration()
  56. ->setAppKeyToken($appKeyToken)
  57. ->setUserKeyToken($userKeyToken);
  58. $this->apiInstance = new DefaultApi(
  59. new GuzzleHttp\Client(),
  60. $config
  61. );
  62. }
  63. /**
  64. * 查看
  65. *
  66. * @return string|Json
  67. * @throws \think\Exception
  68. * @throws DbException
  69. */
  70. public function index()
  71. {
  72. //设置过滤方法
  73. $this->request->filter(['strip_tags', 'trim']);
  74. if (false === $this->request->isAjax()) {
  75. return $this->view->fetch();
  76. }
  77. //如果发送的来源是 Selectpage,则转发到 Selectpage
  78. if ($this->request->request('keyField')) {
  79. return $this->selectpage();
  80. }
  81. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  82. $list = $this->model
  83. ->where($where)
  84. ->order($sort, $order)
  85. ->paginate($limit);
  86. foreach ($list as $v) {
  87. $wheres['message_id']=$v->id;
  88. $count = Db('message_read')->where($wheres)->count();
  89. $v['counts'] = $count;
  90. }
  91. $result = ['total' => $list->total(), 'rows' => $list->items()];
  92. return json($result);
  93. }
  94. /**
  95. * 添加
  96. *
  97. * @return string
  98. * @throws \think\Exception
  99. */
  100. public function add()
  101. {
  102. if (false === $this->request->isPost()) {
  103. return $this->view->fetch();
  104. }
  105. $params = $this->request->post('row/a');
  106. $template_id = $params['template_id'];
  107. $template= Template::getById($template_id);
  108. $params['template_name']=$template->name;
  109. $params['template_dsc']=$template->description;
  110. $group_id =$params['group_id'];
  111. $group= UserGroup::getById($group_id);
  112. $params['template_image']=$template->image;
  113. $params['group_name']=$group->name;
  114. if (empty($params)) {
  115. $this->error(__('Parameter %s can not be empty', ''));
  116. }
  117. $params = $this->preExcludeFields($params);
  118. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  119. $params[$this->dataLimitField] = $this->auth->id;
  120. }
  121. $result = false;
  122. Db::startTrans();
  123. try {
  124. //是否采用模型验证
  125. if ($this->modelValidate) {
  126. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  127. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  128. $this->model->validateFailException()->validate($validate);
  129. }
  130. $result = $this->model->allowField(true)->save($params);
  131. Db::commit();
  132. } catch (ValidateException|PDOException|Exception $e) {
  133. Db::rollback();
  134. $this->error($e->getMessage());
  135. }
  136. if ($result === false) {
  137. $this->error(__('No rows were inserted'));
  138. }
  139. $this->success();
  140. }
  141. /**
  142. * 编辑
  143. *
  144. * @param $ids
  145. * @return string
  146. * @throws DbException
  147. * @throws \think\Exception
  148. */
  149. public function edit($ids = null)
  150. {
  151. $row = $this->model->get($ids);
  152. if (!$row) {
  153. $this->error(__('No Results were found'));
  154. }
  155. $adminIds = $this->getDataLimitAdminIds();
  156. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  157. $this->error(__('You have no permission'));
  158. }
  159. if (false === $this->request->isPost()) {
  160. $this->view->assign('row', $row);
  161. return $this->view->fetch();
  162. }
  163. $params = $this->request->post('row/a');
  164. if (empty($params)) {
  165. $this->error(__('Parameter %s can not be empty', ''));
  166. }
  167. if($row->status==2){
  168. $this->error(__('该消息已经发送,不能修改!'));
  169. }
  170. $params = $this->preExcludeFields($params);
  171. $template_id = $params['template_id'];
  172. $template= Template::getById($template_id);
  173. $params['template_name']=$template->name;
  174. $params['template_dsc']=$template->description;
  175. $group_id =$params['group_id'];
  176. $group= UserGroup::getById($group_id);
  177. $params['template_image']=$template->image;
  178. $params['group_name']=$group->name;
  179. $result = false;
  180. Db::startTrans();
  181. try {
  182. //是否采用模型验证
  183. if ($this->modelValidate) {
  184. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  185. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  186. $row->validateFailException()->validate($validate);
  187. }
  188. $result = $row->allowField(true)->save($params);
  189. Db::commit();
  190. } catch (ValidateException|PDOException|Exception $e) {
  191. Db::rollback();
  192. $this->error($e->getMessage());
  193. }
  194. if (false === $result) {
  195. $this->error(__('No rows were updated'));
  196. }
  197. $this->success();
  198. }
  199. public function getType()
  200. {
  201. $list[0]['id']=0;
  202. $list[0]['name']='URL';
  203. $list[1]['id']=1;
  204. $list[1]['name']='商品列表';
  205. $list[2]['id']=2;
  206. $list[2]['name']='订单列表';
  207. $list[3]['id']=3;
  208. $list[3]['name']='商品详情';
  209. $list[4]['id']=4;
  210. $list[4]['name']='购物车';
  211. $list[5]['id']=5;
  212. $list[5]['name']='消息列表';
  213. $list[6]['id']=6;
  214. $list[6]['name']='explore首页';
  215. $list[7]['id']=7;
  216. $list[7]['name']='explore详情';
  217. $list[8]['id']=8;
  218. $list[8]['name']='我的积分';
  219. $list[9]['id']=9;
  220. $list[9]['name']='koc';
  221. $list[10]['id']=10;
  222. $list[10]['name']='vip Center';
  223. $list[11]['id']=11;
  224. $list[11]['name']='我的优惠券';
  225. $list[12]['id']=12;
  226. $list[12]['name']='登录注册';
  227. $list[13]['id']=13;
  228. $list[13]['name']='签到';
  229. $list[14]['id']=14;
  230. $list[14]['name']='首页';
  231. $i=$this->request->request('keyValue');
  232. if ($i) {
  233. return json(['list' => $list[$i], 'total' => count($list[$i])]);
  234. }
  235. return json(['list' => $list, 'total' => count($list)]);
  236. }
  237. /**
  238. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  239. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  240. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  241. */
  242. public function test()
  243. {
  244. $data =input();
  245. $id=$data['ids'];
  246. $wheres['id']=$id;
  247. $one_push = Db::name('message')->where($wheres)->find();
  248. if (empty($one_push)) {
  249. echo '没有查到推送队列';
  250. exit;
  251. }
  252. $result =$this->sendTest($one_push);
  253. $this->success();
  254. }
  255. function createNotification($one_push){
  256. $template_dsc =$one_push['template_dsc'];
  257. $title =$one_push['template_name'];
  258. $type =$one_push['type'];
  259. $url =$one_push['para'];
  260. $content = new StringMap();
  261. $content->setEn($template_dsc);
  262. $notification = new Notification();
  263. $notification->setContents($content);
  264. $notification->setIosBadgeType('Increase');
  265. $notification->setIosBadgeCount(1);
  266. $notification->setIsIos(true);
  267. $notification->setContentAvailable(true);
  268. $data['push_para']=$url;
  269. $data['push_type']=$type;
  270. $data['title']=$title;
  271. $data['message_id']=$one_push['id'];
  272. $notification->setData($data);
  273. $notification->setMutableContent(true);
  274. $apns_alert['title']=$title;
  275. $notification->setApnsAlert($apns_alert);
  276. if($one_push['template_image']){
  277. $request = Request::instance();
  278. $domain=$request->domain();
  279. $images['id'] = $domain.$one_push['template_image'];
  280. $notification->setIosAttachments($images);
  281. }
  282. return $notification;
  283. }
  284. public function sendTest($one_push){
  285. $appId =$this->getAppId();
  286. $notification = $this->createNotification($one_push);
  287. $notification->setAppId($appId);
  288. $notification->setIncludedSegments(['TestUsers']);
  289. return $this->apiInstance->createNotification($notification);
  290. }
  291. public function getAppId(){
  292. $url = $_SERVER['HTTP_HOST'];
  293. if (strpos($url, 'westkiss') !== false) {
  294. $appId ='6bbd561f-4d8e-4d04-a6c4-dbef1bf99694';
  295. }elseif (strpos($url, 'asteriahair') !== false) {
  296. $appId ='618fe580-bc97-4bf4-b2bb-5039f9dbbc82';
  297. }else{
  298. $appId ='b3124d44-7bc7-4965-95dc-0ecb502fdaea';
  299. }
  300. return $appId;
  301. }
  302. }