Xxts.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. namespace app\admin\controller\user;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. /**
  6. * 会员管理
  7. *
  8. * @icon fa fa-user
  9. */
  10. class Xxts extends Backend
  11. {
  12. protected $relationSearch = true;
  13. /**
  14. * @var \app\admin\model\User
  15. */
  16. protected $model = null;
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. $this->model = model('Xxts');
  21. }
  22. /**
  23. * 查看
  24. */
  25. public function index()
  26. {
  27. //设置过滤方法
  28. $this->request->filter(['strip_tags']);
  29. if ($this->request->isAjax()) {
  30. //如果发送的来源是Selectpage,则转发到Selectpage
  31. if ($this->request->request('keyField')) {
  32. return $this->selectpage();
  33. }
  34. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  35. $total = $this->model
  36. ->where($where)
  37. ->order($sort, $order)
  38. ->count();
  39. $list = $this->model
  40. ->where($where)
  41. ->order($sort, $order)
  42. ->limit($offset, $limit)
  43. ->select();
  44. $result = array("total" => $total, "rows" => $list);
  45. return json($result);
  46. }
  47. return $this->view->fetch();
  48. }
  49. public function tuis($ids = NULL)
  50. {
  51. $row = $this->model->get($ids);
  52. if (!$row)
  53. $this->error(__('No Results were found'));
  54. $this->view->assign('row',$row);
  55. return $this->view->fetch();
  56. }
  57. public function tuisax()
  58. {
  59. $tdata=$this->model->where(['id'=>input('id')])->find();
  60. Db::startTrans();
  61. try {
  62. $this->model->where(['id'=>$tdata['id']])->update(['type'=>2]);
  63. $list= model('User')->where(['status'=>'normal'])->select();
  64. foreach ($list as $v){
  65. if($v['wxxopenid']){
  66. $url=$tdata['url'].'?id='.$tdata['urlid'];
  67. $this->pushmsg($v['wxxopenid'],$tdata['info'],$tdata['name'],$url);
  68. }
  69. }
  70. Db::commit();
  71. $this->success('推送成功');
  72. }catch (Exception $e){
  73. Db::rollback();
  74. $this->error($e->getMessage());
  75. }
  76. }
  77. /**
  78. * 编辑
  79. */
  80. public function edit($ids = NULL)
  81. {
  82. $row = $this->model->get($ids);
  83. if (!$row)
  84. $this->error(__('No Results were found'));
  85. return parent::edit($ids);
  86. }
  87. public function add()
  88. {
  89. return parent::add();
  90. }
  91. //$uid 用户id $zt提醒状态 $title项目标题 $beizhu备注 $tjtime提交时间 $shtime审核时间 $id跳转id $xcxurl跳转地址
  92. public function pushmsg($wxxopenid,$info,$name,$url)
  93. {
  94. $site=config('site');
  95. $appid =$site['weixinxcx']['template_id'];
  96. $touser=$wxxopenid;
  97. $page=isset($url)?$url:'pages/client/index';
  98. $template_id=$appid; //提醒
  99. $content=[
  100. "thing1"=>['value' => $info],
  101. "thing2"=>['value' => $name],
  102. "thing4"=>['value' => 'admin'],
  103. ];
  104. $rdd=self::sendSubscribeMessage($touser,$template_id,$page,$content);
  105. $arr = json_decode($rdd,true);
  106. $errcode=isset($arr['errcode'])?$arr['errcode']:1;
  107. if($arr['errcode']>0){
  108. $errmsg=isset($arr['errmsg'])?$arr['errmsg']:'1';
  109. $this->error(__($arr['errmsg']));
  110. }
  111. }
  112. //发送订阅消息
  113. public function sendSubscribeMessage($touser,$template_id,$page,$content)
  114. {
  115. //access_token
  116. $access_token = self::getAccessToken() ;
  117. //请求url
  118. $url = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=' . $access_token ;
  119. //发送内容
  120. $data = [] ;
  121. //接收者(用户)的 openid
  122. $data['touser'] = $touser ;
  123. //所需下发的订阅模板id
  124. $data['template_id'] = $template_id ;
  125. //点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数,(示例index?foo=bar)。该字段不填则模板无跳转。
  126. $data['page'] = $page ;
  127. //模板内容,格式形如 { "key1": { "value": any }, "key2": { "value": any } }
  128. $data['data'] = $content;
  129. //跳转小程序类型:developer为开发版;trial为体验版;formal为正式版;默认为正式版
  130. $data['miniprogram_state'] = 'formal' ;
  131. return self::curlPost($url,json_encode($data)) ;
  132. }
  133. /**
  134. * Notes:获取accessToken
  135. * @return mixed
  136. * @throws \think\Exception
  137. * @throws \think\exception\PDOException
  138. */
  139. public function getAccessToken()
  140. {
  141. $site=config('site');
  142. $appid =$site['weixinxcx']['appid'];//小程序的appid
  143. $appSecret = $site['weixinxcx']['appSecret'];// 小程序的$appSecret
  144. //当前时间戳
  145. $now_time = strtotime(date('Y-m-d H:i:s',time())) ;
  146. //失效时间
  147. $timeout = 7200 ;
  148. //判断access_token是否过期
  149. $before_time = $now_time - $timeout ;
  150. //未查找到就为过期
  151. $access_token = Db::name('takeout_access_token')->where('id',1)
  152. ->where('update_time' ,'>',$before_time)
  153. ->value('access_token');
  154. //如果过期
  155. if( !$access_token ) {
  156. //获取新的access_token
  157. $appid = $appid;
  158. $secret = $appSecret;
  159. $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret;
  160. $res = json_decode(file_get_contents($url),true);
  161. $access_token = $res['access_token'] ;
  162. //更新数据库
  163. $update = ['access_token' => $access_token ,'update_time' => $now_time] ;
  164. Db::name('takeout_access_token')->where('id',1)->update($update) ;
  165. }
  166. return $access_token ;
  167. }
  168. //发送post请求
  169. protected function curlPost($url,$data)
  170. {
  171. $ch = curl_init();
  172. $params[CURLOPT_URL] = $url; //请求url地址
  173. $params[CURLOPT_HEADER] = FALSE; //是否返回响应头信息
  174. $params[CURLOPT_SSL_VERIFYPEER] = false;
  175. $params[CURLOPT_SSL_VERIFYHOST] = false;
  176. $params[CURLOPT_RETURNTRANSFER] = true; //是否将结果返回
  177. $params[CURLOPT_POST] = true;
  178. $params[CURLOPT_POSTFIELDS] = $data;
  179. curl_setopt_array($ch, $params); //传入curl参数
  180. $content = curl_exec($ch); //执行
  181. curl_close($ch); //关闭连接
  182. return $content;
  183. }
  184. }