MainController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. <?php
  2. namespace backend\controllers;
  3. use biz\order\services\OrderService;
  4. use biz\stat\services\StatIncomeService;
  5. use biz\stat\services\StatUserByDayService;
  6. use biz\stat\services\VisitByDayService;
  7. use biz\user\classes\UserClass;
  8. use biz\user\services\UserService;
  9. use common\components\weixinUtil;
  10. use common\models\xhStatIncome;
  11. use common\models\xhUser;
  12. use common\services\ImageService;
  13. use common\services\xhMerchantAssetService;
  14. use Yii;
  15. use yii\web\Controller;
  16. use common\services\xhAdminService;
  17. use yii\helpers\Json;
  18. use common\services\xhMerchantExtendService;
  19. use common\services\xhMerchantService;
  20. use common\services\xhStatIncomeService;
  21. use common\services\xhStatIncomeMonthService;
  22. use common\components\util;
  23. use common\components\configDict;
  24. use common\components\stringUtil;
  25. use common\services\xhVisitByDayService;
  26. use common\services\xhWxOpenService;
  27. use common\services\xhWxMenuService;
  28. use yii\web\Cookie;
  29. /**
  30. * main controller
  31. */
  32. class MainController extends BackendController
  33. {
  34. public $enableCsrfValidation = false;
  35. public $withoutLogin = ['login', 'logout', 'check-login', 'test', 'rabbitmq'];
  36. //消息队列 shish 2019.9.8
  37. //CMD中执行 yii rabbitmq/consume consumers
  38. public function actionRabbitmq()
  39. {
  40. $producer = \Yii::$app->rabbitmq->getProducer('producers');
  41. $msg = serialize(['dataset_id' => rand(1, 100), 'linked_datasets' => []]);
  42. $producer->publish($msg, 'ex1', 'route1');
  43. echo 'ok';
  44. }
  45. public function actionBindOpen()
  46. {
  47. $merchant = $this->merchant;
  48. $wxAppId = $merchant['wxAppId'];
  49. $miniAppId = $merchant['miniAppId'];
  50. $open_appid = $merchant['openAppId'];
  51. echo "<pre>";
  52. $id = Yii::$app->request->get('id', 0);
  53. //创建平台,并将公众号绑定到平台
  54. if ($id == 1) {
  55. weixinUtil::createOpenAccount($wxAppId, $this->merchant);
  56. $return = weixinUtil::getOpenAccount($wxAppId, $this->merchant);
  57. print_r($return);
  58. if ($return['errcode'] == 0) {
  59. $openAppId = $return['open_appid'];
  60. xhMerchantService::updateById($this->merchantId, ['openAppId' => $openAppId]);
  61. }
  62. }
  63. //将小程序绑定到平台
  64. if ($id == 2) {
  65. $openAppId = $merchant['openAppId'];
  66. $r = weixinUtil::bindOpenAccount($miniAppId, $openAppId, $merchant, true);
  67. print_r($r);
  68. }
  69. //查询公众号 小程序的绑定情况
  70. if ($id == 3) {
  71. $return = weixinUtil::getOpenAccount($wxAppId, $this->merchant);
  72. print_r($return);
  73. $return = weixinUtil::getOpenAccount($miniAppId, $this->merchant, true);
  74. print_r($return);
  75. }
  76. //设置小程序服务器域名
  77. if ($id == 4) {
  78. weixinUtil::setDomain($this->merchant);
  79. weixinUtil::setWebViewDomain($this->merchant);
  80. }
  81. //为授权的小程序帐号上传小程序代码
  82. if ($id == 5) {
  83. $tId = $_GET['tId'];
  84. weixinUtil::miniCommit($this->merchant, $tId);
  85. }
  86. //获取小程序体验码
  87. if ($id == 6) {
  88. weixinUtil::getExperienceQrCode($this->merchant);
  89. }
  90. //获取授权小程序帐号已设置的类目
  91. if ($id == 7) {
  92. weixinUtil::getMiniCategory($this->merchant);
  93. }
  94. //获取小程序的第三方提交代码的页面配置
  95. if ($id == 8) {
  96. weixinUtil::getMiniPage($this->merchant);
  97. }
  98. //将第三方提交的代码包提交审核
  99. if ($id == 9) {
  100. weixinUtil::miniSubmitAudit($this->merchant);
  101. }
  102. //查询最新一次提交的审核状态
  103. if ($id == 10) {
  104. weixinUtil::miniGetLatestAuditStatus($this->merchant);
  105. }
  106. //发布已通过审核的小程序
  107. if ($id == 11) {
  108. weixinUtil::miniRelease($this->merchant);
  109. }
  110. //撤回审核
  111. if ($id == 12) {
  112. weixinUtil::rollbackCheck($this->merchant);
  113. }
  114. //生成 申请会员页 的小程序码
  115. if ($id == 13) {
  116. weixinUtil::generateMemberCode($this->merchant);
  117. }
  118. //解绑小程序和公众号绑定的平台
  119. if ($id == 14) {
  120. weixinUtil::unbindAccount($this->merchant);
  121. weixinUtil::unbindMiniProgram($this->merchant);
  122. }
  123. Yii::$app->end();
  124. $r = weixinUtil::bindOpenAccount($miniAppId, $open_appid, $merchant, true);
  125. print_r($r);
  126. echo $miniAppId . ' ' . $open_appid . ' ';
  127. $return = weixinUtil::getOpenAccount($wxAppId, $this->merchant);
  128. print_r($return);
  129. die;
  130. $return = weixinUtil::createOpenAccount($wxAppId, $this->merchant);
  131. print_r($return);
  132. die;
  133. $r = weixinUtil::bindOpenAccount($miniAppId, $open_appid, $merchant);
  134. print_r($r);
  135. echo $miniAppId . ' ' . $open_appid . ' ';
  136. $return = weixinUtil::getOpenAccount($wxAppId, $this->merchant);
  137. print_r($return);
  138. $return = weixinUtil::getOpenAccount($miniAppId, $this->merchant);
  139. print_r($return);
  140. die;
  141. if ($return['errcode'] == 0) {
  142. $open_appid = isset($return['open_appid']) ? $return['open_appid'] : '';
  143. } else {
  144. $return = weixinUtil::createOpenAccount($wxAppId, $this->merchant);
  145. if ($return['errcode'] == 0) {
  146. $open_appid = isset($return['open_appid']) ? $return['open_appid'] : '';
  147. }
  148. }
  149. echo "<br />open_appid:" . $open_appid . "<br />";
  150. if (!empty($open_appid)) {
  151. xhMerchantService::updateById($this->merchantId, ['openAppId' => $open_appid]);
  152. $r = weixinUtil::bindOpenAccount($miniAppId, $open_appid, $merchant);
  153. echo "绑定小程序结果:<br />";
  154. print_r($r);
  155. }
  156. echo "<br /><br />";
  157. $return = weixinUtil::getOpenAccount($wxAppId, $merchant);
  158. echo "微信appId:{$wxAppId}<br />";
  159. print_r($return);
  160. $return = weixinUtil::getOpenAccount($miniAppId, $merchant);
  161. echo "<br />小程序appId:{$miniAppId}<br />";
  162. print_r($return);
  163. }
  164. public function actionCs()
  165. {
  166. // 忽略获取的header数据
  167. $ignore = ['host', 'accept', 'content-length', 'content-type'];
  168. $headers = [];
  169. foreach ($_SERVER as $key => $value) {
  170. if (substr($key, 0, 5) === 'HTTP_') {
  171. $key = substr($key, 5);
  172. $key = str_replace('_', ' ', $key);
  173. $key = str_replace(' ', '-', $key);
  174. $key = strtolower($key);
  175. if (!in_array($key, $ignore)) {
  176. $headers[$key] = $value;
  177. }
  178. }
  179. }
  180. $token = isset($headers['token']) ? $headers['token'] : '';
  181. }
  182. public function actionIndex()
  183. {
  184. echo 'aaa';die;
  185. $time = strtotime(date("Y-m-d"));
  186. $todayIncome = xhStatIncomeService::getTodayIncome($this->merchantId, $time);
  187. $year = date("Y");
  188. $month = date("n");
  189. $monthIncome = xhStatIncomeMonthService::getMonthIncome($this->merchantId, $year, $month);
  190. $statUser = StatUserByDayService::getTodayNum();
  191. $todayVisit = VisitByDayService::getTodayNum($this->merchantId);
  192. $totalVisit = VisitByDayService::getTotalVisit($this->merchantId);
  193. $latestIncome = xhStatIncomeService::getLatestIncome($this->merchantId);//取最近10天的收入
  194. $dayIncome = xhStatIncome::find()->where(['merchantId' => $this->merchantId])->asArray()->orderBy('time desc')->limit(3)->all();
  195. $income = StatIncomeService::getTodayNum();
  196. //获取最近的订单 shish 2019.8.19
  197. $orderList = OrderService::getLatestOrder();
  198. //今日访问量
  199. $todayVisitNum = VisitByDayService::getTodayNum($this->merchantId);
  200. return $this->render('index', [
  201. 'merchant' => $this->merchant,
  202. 'merchantExtend' => $this->merchantExtend,
  203. 'merchantAsset' => $this->merchantAsset,
  204. 'todayIncome' => $todayIncome,
  205. 'monthIncome' => $monthIncome,
  206. 'statUser' => $statUser,
  207. 'todayVisit' => $todayVisit,
  208. 'totalVisit' => $totalVisit,
  209. 'latestIncome' => $latestIncome,
  210. 'dayIncome' => $dayIncome,
  211. 'orderList' => $orderList,
  212. 'income' => $income,
  213. 'todayVisitNum' => $todayVisitNum,
  214. ]);
  215. }
  216. public function actionTest()
  217. {
  218. }
  219. public function actionLogin()
  220. {
  221. $merchant = xhMerchantService::getById(12358);
  222. $data = ["touser" => 'oTFbYvsjwgVwXggwtlM2ESAKwuN8', "template_id" => 'rxcSb6up1IgKJQY2XcTodJdFweigTu6UPXVZ7wsdWcI',
  223. "url" => 'http://m.huaml.com/order/detail?id=1', "data" => [
  224. "first" => ["value" => '石头付款', "color" => "#173177"],
  225. "keyword1" => ["value" => '10元', "color" => "#173177"],
  226. "keyword2" => ["value" => '客户付款', "color" => "#173177"],
  227. "keyword3" => ["value" => date("Y-m-d H:i"), "color" => "#173177"],
  228. "remark" => ["value" => "点击查看明细", "color" => "#173177"]]];
  229. $accessToken = weixinUtil::getAuthorizerAccessToken($merchant);
  230. $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={$accessToken}";
  231. //$data = urldecode(json_encode($data, JSON_UNESCAPED_UNICODE));
  232. //$curl = new curl\Curl();
  233. //$response = $curl->setOption(CURLOPT_POSTFIELDS, $data)->post($url);
  234. //self::asyncRequest('https://api.weixin.qq.com','/cgi-bin/message/template/send?access_token='.$accessToken,json_encode($data, JSON_UNESCAPED_UNICODE));
  235. //echo 'ok';
  236. //die;
  237. $params = json_encode($data,JSON_UNESCAPED_UNICODE);
  238. $start_time = microtime(true);
  239. $fp = fsockopen('ssl://api.weixin.qq.com', 443, $error, $errstr, 3);
  240. $http = "POST /cgi-bin/message/template/send?access_token={$accessToken} HTTP/1.1\r\nHost: api.weixin.qq.com\r\nContent-type: application/x-www-form-urlencoded\r\nContent-Length: " . strlen($params) . "\r\nConnection:close\r\n\r\n$params\r\n\r\n";
  241. fwrite($fp, $http);
  242. fclose($fp);
  243. die;
  244. sleep(3);
  245. Yii::info('id:'.$_POST['name']);
  246. util::ok();
  247. if ($this->isLogin) {
  248. $this->redirect(['main/index']);
  249. }
  250. return $this->renderPartial('login');
  251. }
  252. private static function asyncRequest($host, $path, $param = []){
  253. $query = isset($param) ? http_build_query($param) : '';
  254. $port = 80;
  255. $errno = 0;
  256. $errstr = '';
  257. $timeout = 30; //连接超时时间(S)
  258. $fp = @fsockopen($host, $port, $errno, $errstr, $timeout);
  259. //$fp = stream_socket_client("tcp://".$host.":".$port, $errno, $errstr, $timeout);
  260. if (!$fp) {
  261. return '连接失败';
  262. }
  263. if ($errno || !$fp) {
  264. return $errstr;
  265. }
  266. stream_set_blocking($fp,0); //非阻塞
  267. stream_set_timeout($fp, 1);//响应超时时间(S)
  268. $out = "POST " . $path . " HTTP/1.1\r\n";
  269. $out .= "host:" . $host . "\r\n";
  270. $out .= "content-length:" . strlen($query) . "\r\n";
  271. $out .= "content-type:application/x-www-form-urlencoded\r\n";
  272. $out .= "connection:close\r\n\r\n";
  273. $out .= $query;
  274. $result = @fputs($fp, $out);
  275. @fclose($fp);
  276. return $result;
  277. }
  278. /**
  279. * 重复登陆,记录登陆名
  280. * @return string
  281. */
  282. public function actionRelogin()
  283. {
  284. return $this->renderPartial('relogin');
  285. }
  286. public function actionLogout()
  287. {
  288. xhAdminService::logout();
  289. $this->redirect(['main/login']);
  290. }
  291. public function actionCheckLogin()
  292. {
  293. $request = Yii::$app->request;
  294. $mobile = $request->post('mobile');
  295. $password = $request->post('password');
  296. $return = xhAdminService::loginByMobile($mobile, $password, true);//true 自动登陆
  297. util::encode($return);
  298. }
  299. public function actionSetLoginPassword()
  300. {
  301. $id = $this->adminId;
  302. $admin = xhAdminService::getById($id);
  303. return $this->render('setLoginPassword', ['admin' => $admin]);
  304. }
  305. public function actionModifyLoginPassword()
  306. {
  307. $post = Yii::$app->request->post();
  308. $prePassword = isset($post['prePassword']) ? $post['prePassword'] : '';
  309. $id = $this->adminId;
  310. $admin = xhAdminService::getById($id);
  311. $savePwd = $admin['password'];
  312. if (empty($savePwd)) {
  313. util::failInfo('请填写密码');
  314. }
  315. if (md5($prePassword) == $savePwd) {
  316. util::failInfo('原密码不正确');
  317. }
  318. xhAdminService::updateById($id, ['password' => md5($post['password'])]);
  319. util::successInfo('修改成功');
  320. }
  321. /**
  322. * 扫码枪扫码
  323. */
  324. public function actionSearch()
  325. {
  326. $get = Yii::$app->request->get();
  327. $content = isset($get['content']) ? $get['content'] : '';
  328. $id = preg_replace('/\D/s', '', $content);
  329. if (strpos($content, configDict::getQrKey('user')) === 0) {
  330. $this->redirect(['/user/detail', 'id' => $id]);
  331. Yii::$app->end();
  332. }
  333. if (strpos($content, configDict::getQrKey('coupon')) === 0) {
  334. $this->redirect(['/coupon/detail', 'id' => $id]);
  335. Yii::$app->end();
  336. }
  337. }
  338. /**
  339. * 审核通过
  340. */
  341. public function actionApprove()
  342. {
  343. $openMerchant = xhWxOpenService::getMerchant();
  344. return $this->render('approve', ['admin' => $this->admin, 'openMerchant' => $openMerchant]);
  345. }
  346. public function actionQuickOrder()
  347. {
  348. $this->redirect(Yii::$app->params['frontUrl'] . '/payment/quick-order?account=' . $this->merchant['id']);
  349. }
  350. public function actionPub()
  351. {
  352. $channelName = 'redis_shi';
  353. $time = time();
  354. Yii::$app->redis->executeCommand('PUBLISH', [$channelName, $time]);
  355. }
  356. public function actionSub()
  357. {
  358. $channelName = 'redis_shi';
  359. print_r(Yii::$app->redis->executeCommand('SUBSCRIBE', [$channelName]));
  360. }
  361. public function actionError()
  362. {
  363. $msg = Yii::$app->request->get('msg');
  364. return $this->render('error', ['msg' => $msg]);
  365. }
  366. }