/** * Finds the WxApp model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return WxApp the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = WxApp::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * 发送消息 * @date 2016-02-27T17:36:12+0800 * @author cnzhangxl@foxmail.com * @return [type] [description] */ public function actionMsgSend() { $sleep = 15; $cache_time = 60; $cachename = 'console_wechat_msg-send'; if (Yii::$app->cache->get($cachename)) { exit; } while (true) { Yii::$app->cache->set($cachename, 1, $cache_time); $wx_app_list = WxApp::find()->all(); foreach ($wx_app_list as $app) { $msg_arr = WxMsgSend::find()->where(['send_num' => 0, 'appid' => $app->id])->all(); $options = ['token' => $app->token, 'encodingaeskey' => $app->encodingaeskey, 'appid' => $app->appid, 'appsecret' => $app->appsecret]; $we_obj = new Wechat($options); $user_last = array(); foreach ($msg_arr as $msg_one) { if (isset($user_last[$msg_one->user_id]) && $user_last[$msg_one->user_id] + 10 > time()) { continue; } $user_last[$msg_one->user_id] = time(); $res = false; $msg_one->scenario = 'update'; switch ($msg_one->msg_type) { case 1: //客服消息 $res = $we_obj->sendCustomMessage(json_decode($msg_one->content, true)); break; default: # code... break; } $msg_one->send_at = time(); $msg_one->send_num += 1; if (!$res) { $msg_one->errcode = "" . $we_obj->errCode; } $msg_one->save(); } } sleep($sleep); } return false; }