UserController.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  1. <?php
  2. namespace saas\controllers;
  3. use common\services\xhUserAlipayService;
  4. use common\services\xhUserIntegralService;
  5. use Yii;
  6. use common\services\xhTrainClassService;
  7. use common\services\xhOrderService;
  8. use common\services\xhPayToolService;
  9. use common\services\xhRechargeService;
  10. use common\services\xhMerchantExtendService;
  11. use common\services\xhMerchantAssetService;
  12. use common\services\xhUserAssetService;
  13. use common\services\xhCouponService;
  14. use common\services\xhUserTagService;
  15. use common\models\xhUserTag;
  16. use common\services\xhUserTagClassService;
  17. use common\models\xhTrainCourse;
  18. use common\models\xhTrainStudent;
  19. use common\models\xhUser;
  20. use common\models\xhUserAsset;
  21. use common\services\xhAdminService;
  22. use common\services\xhMerchantService;
  23. use common\components\stringUtil;
  24. use common\components\weixinUtil;
  25. use common\services\xhTMessageService;
  26. use common\components\configDict;
  27. use common\components\util;
  28. use common\components\page;
  29. use common\services\xhUserService;
  30. use common\models\xhTrainUserCourse;
  31. use common\services\xhUserByDayService;
  32. use common\models\xhUserAlipay;
  33. use common\services\xhUserUniteService;
  34. use common\services\xhMerchantCapitalService;
  35. class UserController extends PlatformController
  36. {
  37. public function actionList()
  38. {
  39. $get = Yii::$app->request->get();
  40. $query = xhUser::find();
  41. if (isset($get['content']) && !empty($get['content'])) {
  42. $content = $get['content'];
  43. if (stringUtil::isMobieNumber($content)) {
  44. $query->andWhere(['mobile' => $content]);
  45. } else {
  46. $query->andWhere(['like', 'userName', $content]);
  47. }
  48. }
  49. if (isset($get['class']) && $get['class'] == 1) {
  50. $query->andWhere(['isMember' => 1]);
  51. }
  52. $countQuery = clone $query;
  53. $count = $countQuery->count();
  54. $page = isset($get['page']) ? $get['page'] : 1;
  55. $pageSize = 20;
  56. $offset = ($page - 1) * $pageSize;
  57. $models = $query->offset($offset)->orderBy('createTime desc')->limit($pageSize)->asArray()->all();
  58. $page = new page($count, $page, $pageSize);
  59. $paging = $page->fpage();
  60. return $this->render('list', [
  61. 'models' => $models,
  62. 'pages' => $paging,
  63. ]);
  64. }
  65. public function actionAlipayUser()
  66. {
  67. $get = Yii::$app->request->get();
  68. $query = xhUserAlipay::find();
  69. $countQuery = clone $query;
  70. $count = $countQuery->count();
  71. $page = isset($get['page']) ? $get['page'] : 1;
  72. $pageSize = 20;
  73. $offset = ($page - 1) * $pageSize;
  74. $models = $query->offset($offset)->orderBy('updateTime desc')->limit($pageSize)->asArray()->all();
  75. $page = new page($count, $page, $pageSize);
  76. $paging = $page->fpage();
  77. return $this->render('alipayUser', [
  78. 'models' => $models,
  79. 'pages' => $paging,
  80. ]);
  81. }
  82. public function actionAlipayUserDetail()
  83. {
  84. $get = Yii::$app->request->get();
  85. $alipayId = isset($get['alipayId']) ? $get['alipayId'] : '';
  86. $alipay = xhUserAlipayService::getByIds($alipayId, $this->merchantId);
  87. return $this->render('alipayUserDetail', ['alipay' => $alipay]);
  88. }
  89. public function actionDetail()
  90. {
  91. $get = Yii::$app->request->get();
  92. $id = isset($get['id']) ? $get['id'] : 0;
  93. $user = xhUserService::getById($id);
  94. $userAsset = xhUserAssetService::getByUserId($id);
  95. $merchantId = $user['merchantId'];
  96. $merchant = xhMerchantService::getById($merchantId);
  97. $class = xhTrainClassService::getAllByCondition(['merchantId' => $merchantId]);
  98. //取商标的所有用户标签
  99. $merchantTag = xhUserTagClassService::getMerchantTag($merchantId);
  100. $userTag = xhUserTagService::getUserTag($id);
  101. $coupon = xhCouponService::getUserCoupon($id);//取代金劵
  102. $merchantExtend = xhMerchantExtendService::getByMerchantId($merchantId);
  103. $memberIntegral = xhMerchantExtendService::getGradeList($merchantExtend, 'desc');
  104. $freight = configDict::getConfig('freight');//运费计算
  105. $alipayId = 0;
  106. $alipayInfo = [];
  107. if (!empty($user['alipayId'])) {
  108. $alipayId = $user['alipayId'];
  109. $alipayInfo = xhUserAlipayService::getByIds($alipayId, $merchantId);
  110. }
  111. return $this->render('detail', [
  112. 'merchantExtend' => $merchantExtend,
  113. 'userTag' => $userTag,
  114. 'coupon' => $coupon,
  115. 'merchantTag' => $merchantTag,
  116. 'user' => $user,
  117. 'class' => $class,
  118. 'userAsset' => $userAsset,
  119. 'memberIntegral' => $memberIntegral,
  120. 'freight' => $freight,
  121. 'merchant' => $merchant,
  122. 'alipayInfo' => $alipayInfo,
  123. ]);
  124. }
  125. /**
  126. * 升级为会员
  127. */
  128. public function actionUpgrade()
  129. {
  130. $get = Yii::$app->request->get();
  131. $adminId = $this->adminId;
  132. $admin = xhAdminService::getById($adminId);
  133. if (isset($get['confirmPassword']) == false) {
  134. util::failInfo('请填写确认密码');
  135. }
  136. if (isset($get['memberLevel']) == false || empty($get['memberLevel'])) {
  137. util::failInfo('请选择等级数');
  138. }
  139. $userId = isset($get['userId']) ? $get['userId'] : 0;
  140. $memberLevel = $get['memberLevel'];
  141. $user = xhUserService::getById($userId);
  142. if (empty($user) || $user['merchantId'] != $this->merchantId) {
  143. util::failInfo('客户不存在');
  144. }
  145. if (empty($user['mobile'])) {
  146. util::failInfo('客户还没有设置手机号哦');
  147. }
  148. $confirmPassword = $get['confirmPassword'];
  149. if (md5($confirmPassword) != $admin['confirmPassword']) {
  150. util::failInfo('确认密码有误');
  151. }
  152. $grade = xhMerchantExtendService::getGradeList($this->merchantExtend, 'desc');
  153. $newLevelPoint = $grade[$memberLevel]['point'];//升级到当前级别需要的积分数
  154. $userAsset = xhUserAssetService::getByUserId($userId);//用户资产
  155. if (!empty($userAsset['isMember']) && $userAsset['memberLevel'] >= $memberLevel) {
  156. util::failInfo('客户已经达到此级别了');
  157. }
  158. $addPoint = $newLevelPoint - $userAsset['point'];
  159. $upData['point'] = $newLevelPoint;//升级后的积分
  160. $upData['memberLevel'] = $memberLevel;
  161. $upData['isMember'] = 1;
  162. xhUserAssetService::updateByUserId($userId, $upData);
  163. $now = time();
  164. $date = date("Y-m-d H:i", $now);
  165. $cashPay = configDict::getConfig('payWay', 'cashPay');
  166. $allTM = xhTMessageService::getList($this->merchantId);
  167. $openId = $user['openId'];
  168. $shortTempId = 'OPENTM205211943';//升级通知
  169. if (isset($allTM[$shortTempId])) {
  170. $tempId = $allTM[$shortTempId];
  171. $data = [
  172. "touser" => $openId, "template_id" => $tempId, "url" => Yii::$app->params['frontUrl'] . '/center/right?account=' . $this->merchant['id'],
  173. "data" => [
  174. "first" => ["value" => "恭喜,您已经升为{$memberLevel}级会员。", "color" => "#173177"],
  175. "keyword1" => ["value" => $user['userName'], "color" => "#173177"],
  176. "keyword2" => ["value" => $date, "color" => "#173177"],
  177. "remark" => ["value" => "点击查看会员权益", "color" => "#173177"],
  178. ]];
  179. weixinUtil::sendTaskInform($data, $this->merchant);
  180. }
  181. util::successInfo();
  182. }
  183. /**
  184. * 充值
  185. */
  186. public function actionRecharge()
  187. {
  188. $get = Yii::$app->request->get();
  189. $rechargePassword = isset($get['rechargePassword']) ? $get['rechargePassword'] : '';
  190. $adminId = $this->adminId;
  191. $admin = xhAdminService::getById($adminId);
  192. if (empty($admin['confirmPassword'])) {
  193. util::failInfo('您还没有设置确认密码');
  194. }
  195. if (md5($rechargePassword) != $admin['confirmPassword']) {
  196. util::failInfo('确认密码有误哦');
  197. }
  198. $userId = isset($get['userId']) ? $get['userId'] : 0;
  199. $rechargeAmount = isset($get['rechargeAmount']) ? $get['rechargeAmount'] : 1;
  200. $user = xhUserService::getById($userId);
  201. if (empty($user) || $user['merchantId'] != $this->merchantId) {
  202. util::failInfo('访问出错,请联系网站管理员');
  203. }
  204. $openId = $user['openId'];
  205. $payPassword = $user['payPassword'];
  206. $date = date("Y-m-d H:i:s");
  207. $userAsset = xhUserAssetService::getByUserId($userId);
  208. if (empty($userAsset)) {
  209. util::failInfo('操作出错,请与网站管理员联系');
  210. }
  211. $totalBalance = $userAsset['balance'] + $rechargeAmount;
  212. $totalRecharge = $userAsset['totalRecharge'] + $rechargeAmount;
  213. $upData['balance'] = $totalBalance;
  214. $upData['totalRecharge'] = $totalRecharge;
  215. $return = xhRechargeService::recharge($rechargeAmount, $user, $this->adminId, $this->merchant, $this->merchantExtend, $this->merchantAsset);
  216. if ($return['code'] == 'A0001') {
  217. util::successInfo('充值成功');
  218. }
  219. util::successInfo('充值失败');
  220. }
  221. /**
  222. * 现金支付,转店主微信,转店主支付宝
  223. */
  224. public function actionCashPay()
  225. {
  226. $post = Yii::$app->request->post();
  227. $extend = $this->merchantExtend;
  228. //dump($post);die;
  229. $getUid = Yii::$app->request->get('userId');
  230. $userId = isset($getUid) ? $getUid : 0;
  231. $amount = isset($post['consumeAmount']) ? $post['consumeAmount'] : 1;
  232. $couponId = isset($post['couponId']) ? $post['couponId'] : 1;
  233. $user = xhUserService::getById($userId, $this->merchantId);
  234. /*if($user['merchantId'] != $this->merchantId){
  235. util::failInfo('非法访问');
  236. }*/
  237. if (!empty($user) && empty($user['subscribe'])) {
  238. util::failInfo('客户要关注公众号才能结算');
  239. }
  240. /*if(!empty($couponId)){
  241. $coupon = xhCouponService::getById($couponId);
  242. if($coupon['merchantId'] != $this->merchantId){
  243. util::failInfo('非法代金劵');
  244. }
  245. if($coupon['useStatus'] == 1 || time() > $coupon['deadline']){
  246. util::failInfo('代金劵已经失效');
  247. }
  248. if($coupon['meetAmount'] > $amount){
  249. util::failInfo('消费不足'.$coupon['meetAmount'].'元');
  250. }
  251. }*/
  252. $payWay = configDict::getConfig('payWay', 'cashPay');
  253. $payStyle = configDict::getConfig('payStyle', 'cashPay');
  254. $now = time();
  255. $expireTime = $now + 300;//订单5分钟后过期
  256. $payAdd['prePrice'] = $amount;
  257. $payAdd['actPrice'] = $amount;
  258. $payAdd['payWay'] = $payWay;
  259. $payAdd['payStyle'] = $payStyle;
  260. $payAdd['payStatus'] = 0;
  261. $payAdd['userId'] = $userId;
  262. $payAdd['merchantId'] = $this->merchantId;
  263. $payAdd['createTime'] = date("Y-m-d H:i:s", $now);
  264. $payAdd['deadline'] = $expireTime;
  265. xhOrderService::formatPost($payAdd, $post);
  266. $order = xhOrderService::add($payAdd);
  267. $id = $order['id'];
  268. $typeList = configDict::getConfig('capitalType');
  269. $capitalType = $typeList['xhOrder']['id'];
  270. $return = xhPayToolService::cashPay($id, $amount, $capitalType, $couponId);
  271. if ($return['code'] == 'A0001') {
  272. util::successInfo('登记成功');
  273. } else {
  274. Yii::warning(json_encode($return));
  275. }
  276. util::failInfo('登记没有成功');
  277. }
  278. /**
  279. * 重置支付密码
  280. */
  281. public function actionResetPayPassword()
  282. {
  283. $get = Yii::$app->request->get();
  284. $id = isset($get['id']) ? $get['id'] : 0;
  285. $user = xhUserService::getById($id);
  286. if (empty($user) || $user['merchantId'] != $this->merchantId) {
  287. util::failInfo('没有这个客户');
  288. }
  289. if (empty($user['subscribe'])) {
  290. util::failInfo('客户要先关注公众号哦');
  291. }
  292. $rand = stringUtil::getRandNum(6);
  293. xhUserService::updateById($id, ['payPassword' => md5($rand)]);
  294. $openId = $user['openId'];
  295. $allTM = xhTMessageService::getList($this->merchantId);
  296. $shortTempId = 'OPENTM207430125';
  297. $tempId = $allTM[$shortTempId];
  298. $url = Yii::$app->params['frontUrl'] . '/center/password?account=' . $this->merchant['id'];
  299. $data = [
  300. "touser" => $openId, "template_id" => $tempId, "url" => $url,
  301. "data" => ["first" => ["value" => '操作成功', "color" => "#173177"],
  302. "keyword1" => ["value" => "你的支付密码已经重置为:" . $rand, "color" => "#173177"],
  303. "keyword2" => ["value" => date("Y-m-d H:i"), "color" => "#173177"],
  304. "remark" => ["value" => "点此修改成好记的密码哦", "color" => "#173177"]]];
  305. weixinUtil::sendTaskInform($data, $this->merchant);
  306. util::successInfo();
  307. }
  308. /**
  309. * 导出用户,备份用户
  310. */
  311. public function actionExport()
  312. {
  313. $query = xhUserAsset::find();
  314. $query->where(['merchantId' => $this->merchantId])->andWhere(['>', 'integral', 0]);
  315. $userAsset = $query->orderBy('integral desc')->all();
  316. if (empty($userAsset)) {
  317. echo '没有获得积分用户';
  318. Yii::$app->end();
  319. }
  320. $objectPHPExcel = new \PHPExcel();
  321. $objectPHPExcel->setActiveSheetIndex(0);
  322. $memberCount = xhUserAsset::find()->andWhere(['isMember' => 1, 'merchantId' => $this->merchantId])->count('id');//会员数
  323. $allUsercount = xhUserAsset::find()->andWhere(['merchantId' => $this->merchantId])->count('id');//用户数
  324. //报表头的输出
  325. $objectPHPExcel->getActiveSheet()->mergeCells('B1:G1');
  326. $objectPHPExcel->getActiveSheet()->setCellValue('B1', '用户信息表');
  327. $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('B2', '用户信息表');
  328. $objectPHPExcel->setActiveSheetIndex(0)->getStyle('B1')->getFont()->setSize(24);
  329. $objectPHPExcel->setActiveSheetIndex(0)->getStyle('B1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  330. $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('B2', '日期:' . date("Y年m月j日"));
  331. $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('F2', '用户数:' . $allUsercount);
  332. $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('G2', '会员数:' . $memberCount);
  333. $objectPHPExcel->setActiveSheetIndex(0)->getStyle('F2')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);//右居中
  334. $objectPHPExcel->setActiveSheetIndex(0)->getStyle('G2')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);//右居中
  335. //表格头的输出
  336. $objectPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(5);
  337. $objectPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(7);
  338. $objectPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(22);
  339. $objectPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(17);
  340. $objectPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(15);
  341. $objectPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(15);
  342. $objectPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(22);
  343. $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('B3', '编号');
  344. $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('C3', '昵称');
  345. $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('D3', '会员等级');
  346. $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('E3', '余额');
  347. $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('F3', '积分');
  348. $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('G3', '创建日期');
  349. //设置居中
  350. $objectPHPExcel->getActiveSheet()->getStyle('B3:G3')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  351. //设置边框
  352. $objectPHPExcel->getActiveSheet()->getStyle('B3:G3')->getBorders()->getTop()->setBorderStyle(\PHPExcel_Style_Border::BORDER_THIN);
  353. $objectPHPExcel->getActiveSheet()->getStyle('B3:G3')->getBorders()->getLeft()->setBorderStyle(\PHPExcel_Style_Border::BORDER_THIN);
  354. $objectPHPExcel->getActiveSheet()->getStyle('B3:G3')->getBorders()->getRight()->setBorderStyle(\PHPExcel_Style_Border::BORDER_THIN);
  355. $objectPHPExcel->getActiveSheet()->getStyle('B3:G3')->getBorders()->getBottom()->setBorderStyle(\PHPExcel_Style_Border::BORDER_THIN);
  356. $objectPHPExcel->getActiveSheet()->getStyle('B3:G3')->getBorders()->getVertical()->setBorderStyle(\PHPExcel_Style_Border::BORDER_THIN);
  357. //设置颜色
  358. $objectPHPExcel->getActiveSheet()->getStyle('B3:G3')->getFill()->setFillType(\PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setARGB('FF66CCCC');
  359. foreach ($userAsset as $subKey => $subMUser) {
  360. //明细的输出
  361. $objectPHPExcel->getActiveSheet()->setCellValue('B' . ($subKey + 4), $subKey + 1);
  362. $objectPHPExcel->getActiveSheet()->setCellValue('C' . ($subKey + 4), $subMUser->xhUser->userName);
  363. $objectPHPExcel->getActiveSheet()->setCellValue('D' . ($subKey + 4), $subMUser->memberLevel);
  364. $objectPHPExcel->getActiveSheet()->setCellValue('E' . ($subKey + 4), $subMUser->balance);
  365. $objectPHPExcel->getActiveSheet()->setCellValue('F' . ($subKey + 4), $subMUser->integral);
  366. $objectPHPExcel->getActiveSheet()->setCellValue('G' . ($subKey + 4), $subMUser->createTime);
  367. //设置边框
  368. $currentRowNum = $subKey + 4;
  369. $objectPHPExcel->getActiveSheet()->getStyle('B' . ($subKey + 4) . ':G' . $currentRowNum)->getBorders()->getTop()->setBorderStyle(\PHPExcel_Style_Border::BORDER_THIN);
  370. $objectPHPExcel->getActiveSheet()->getStyle('B' . ($subKey + 4) . ':G' . $currentRowNum)->getBorders()->getLeft()->setBorderStyle(\PHPExcel_Style_Border::BORDER_THIN);
  371. $objectPHPExcel->getActiveSheet()->getStyle('B' . ($subKey + 4) . ':G' . $currentRowNum)->getBorders()->getRight()->setBorderStyle(\PHPExcel_Style_Border::BORDER_THIN);
  372. $objectPHPExcel->getActiveSheet()->getStyle('B' . ($subKey + 4) . ':G' . $currentRowNum)->getBorders()->getBottom()->setBorderStyle(\PHPExcel_Style_Border::BORDER_THIN);
  373. $objectPHPExcel->getActiveSheet()->getStyle('B' . ($subKey + 4) . ':G' . $currentRowNum)->getBorders()->getVertical()->setBorderStyle(\PHPExcel_Style_Border::BORDER_THIN);
  374. }
  375. //设置分页显示
  376. //$objectPHPExcel->getActiveSheet()->setBreak( 'I55' , \PHPExcel_Worksheet::BREAK_ROW );
  377. //$objectPHPExcel->getActiveSheet()->setBreak( 'I10' , \PHPExcel_Worksheet::BREAK_COLUMN );
  378. $objectPHPExcel->getActiveSheet()->getPageSetup()->setHorizontalCentered(true);
  379. $objectPHPExcel->getActiveSheet()->getPageSetup()->setVerticalCentered(false);
  380. ob_end_clean();
  381. ob_start();
  382. header('Content-Type : application/vnd.ms-excel');
  383. header('Content-Disposition:attachment;filename="' . $this->merchant['merchantName'] . '会员' . date("Ymj") . '.xls"');
  384. $objWriter = \PHPExcel_IOFactory::createWriter($objectPHPExcel, 'Excel5');
  385. $objWriter->save('php://output');
  386. }
  387. /**
  388. * 报名培训
  389. */
  390. public function actionApplyTrain()
  391. {
  392. $get = Yii::$app->request->get();
  393. $userId = $get['userId'];
  394. $user = xhUserService::getById($userId);
  395. if (empty($user) || $user['merchantId'] != $this->merchantId) {
  396. util::failInfo('学员不存在');
  397. }
  398. if (empty($user['subscribe'])) {
  399. util::failInfo('学员还没有关注公众号哦~');
  400. }
  401. $student = xhTrainStudent::getByCondition(['userId' => $userId, 'merchantId' => $this->merchantId]);
  402. if (!empty($student)) {
  403. util::failInfo('已经报过名了哦~');
  404. }
  405. $classId = isset($get['classId']) ? $get['classId'] : 0;
  406. $class = xhTrainClassService::getById($classId);
  407. if (empty($class)) {
  408. util::failInfo('课程不存在哦~');
  409. }
  410. $nowTime = time();
  411. $stuData = ['userId' => $userId, 'merchantId' => $this->merchantId, 'registerTime' => $nowTime, 'classId' => $class['id'],
  412. 'addTime' => $nowTime, 'modTime' => $nowTime, 'tuition' => $class['tuition'], 'className' => $class['name'], 'lesson' => $class['lesson']];
  413. $student = xhTrainStudent::add($stuData);
  414. $course = xhTrainCourse::getAllByCondition(['classId' => $classId]);
  415. $userCourse = [];
  416. foreach ($course as $key => $val) {
  417. $userCourse[] = ['courseId' => $val['id'], 'courseName' => $val['name'], 'courseIntro' => $val['intro'], 'classId' => $classId, 'studentId' => $student['id'], 'userId' => $userId, 'merchantId' => $this->merchantId];
  418. }
  419. xhTrainUserCourse::batchAdd($userCourse);
  420. xhUserService::updateById($userId, ['identity' => 1]);
  421. $allTM = xhTMessageService::getList($this->merchantId);
  422. $openId = $user['openId'];
  423. $shortTempId = 'OPENTM401027382';
  424. $tempId = $allTM[$shortTempId];
  425. $data = array(
  426. "touser" => $openId,
  427. "template_id" => $tempId,//模板id,从公众平台后台取得
  428. "url" => $this->frontUrl . '/center/student-card?account=' . $this->merchant['id'],
  429. "data" => array(
  430. "first" => array(
  431. "value" => "恭喜,您已经成功报名。",
  432. "color" => "#173177"
  433. ),
  434. "keyword1" => array(
  435. "value" => $class['name'],
  436. "color" => "#173177"
  437. ),
  438. "keyword2" => array(
  439. "value" => date("Y-m-d H:i"),
  440. "color" => "#173177"
  441. ),
  442. "remark" => array(
  443. "value" => "您的学员卡已经激活,点此查看课程内容",
  444. "color" => "#173177"
  445. ),
  446. ),
  447. );
  448. weixinUtil::sendTaskInform($data, $this->merchant);
  449. util::successInfo('报名成功~');
  450. }
  451. public function actionRemark()
  452. {
  453. $get = Yii::$app->request->get();
  454. $userId = isset($get['userId']) ? $get['userId'] : 0;
  455. $user = xhUserService::getById($userId);
  456. if (empty($user) || $user['merchantId'] != $this->merchantId) {
  457. util::failInfo('用户不存在');
  458. }
  459. $remark = isset($get['remark']) ? $get['remark'] : '';
  460. xhUserService::updateById($userId, ['remark' => $remark]);
  461. util::successInfo();
  462. }
  463. public function actionTagUserList()
  464. {
  465. $get = Yii::$app->request->get();
  466. $tagId = isset($get['tagId']) ? $get['tagId'] : 0;
  467. $tagInfo = xhUserTagClassService::getByCondition(['tagId' => $tagId]);
  468. if (empty($tagInfo) || $tagInfo['merchantId'] != $this->merchantId) {
  469. util::stop('不合法标签');
  470. }
  471. $query = xhUserTag::find();
  472. $query->where(['tagId' => $tagId]);
  473. $query->orderBy('id desc');
  474. $countQuery = clone $query;
  475. $count = $countQuery->count();
  476. $page = isset($get['page']) ? $get['page'] : 1;
  477. $pageSize = 20;
  478. $offset = ($page - 1) * $pageSize;
  479. $models = $query->offset($offset)->orderBy('id desc')->limit($pageSize)->asArray()->all();
  480. $page = new page($count, $page, $pageSize);
  481. $paging = $page->fpage();
  482. $merchantTag = xhUserTagClassService::getMerchantTag($this->merchantId);
  483. return $this->render('tagUserList', ['models' => $models, 'paging' => $paging, 'merchantTag' => $merchantTag, 'tagInfo' => $tagInfo]);
  484. }
  485. public function actionTag()
  486. {
  487. $tag = xhUserTagClassService::getAllByCondition(['merchantId' => $this->merchantId]);
  488. return $this->render('tag', ['tag' => $tag]);
  489. }
  490. /**
  491. * 添加用户标签
  492. */
  493. public function actionTagAdd()
  494. {
  495. $post = Yii::$app->request->post();
  496. if (isset($post['tagName']) == false || empty($post['tagName'])) {
  497. util::failInfo('请填写标签名称');
  498. }
  499. $name = $post['tagName'];
  500. $tagArr = xhUserTagClassService::getMerchantTag($this->merchantId);
  501. if (!empty($tagArr) && in_array($name, $tagArr)) {
  502. util::failInfo('标签已经存在了');
  503. }
  504. $return = weixinUtil::createTag($this->merchant, $name);
  505. if (isset($return['errcode']) == false) {
  506. $tagId = $return['tag']['id'];
  507. $data = ['tagId' => $tagId, 'name' => $name, 'merchantId' => $this->merchantId];
  508. xhUserTagClassService::add($data, $this->merchantId);
  509. util::successInfo();
  510. }
  511. $errCode = $return['errcode'];
  512. $errList = weixinUtil::$errCodes;
  513. $msg = $errList[$errCode];
  514. util::failInfo($msg);
  515. }
  516. /**
  517. * 删除用户标签
  518. */
  519. public function actionTagDel()
  520. {
  521. $get = Yii::$app->request->get();
  522. if (isset($get['tagId']) == false || empty($get['tagId'])) {
  523. util::failInfo('请选择你要删除的标签');
  524. }
  525. $tagId = $get['tagId'];
  526. $status = xhUserTagService::getUserByTagId($tagId);
  527. if ($status) {
  528. util::failInfo('标签下还有用户,无法删除');
  529. }
  530. $tag = xhUserTagClassService::getByCondition(['tagId' => $tagId, 'merchantId' => $this->merchantId]);
  531. if (empty($tag)) {
  532. util::failInfo('标签不存在');
  533. }
  534. if ($tag['merchantId'] != $this->merchantId) {
  535. util::failInfo('非法操作');
  536. }
  537. $wxTagId = $tag['tagId'];
  538. $return = weixinUtil::delTag($this->merchant, $wxTagId);
  539. if (isset($return['errcode']) && $return['errcode'] == 0) {
  540. xhUserTagClassService::deleteByCondition(['tagId' => $tagId, 'merchantId' => $this->merchantId], $this->merchantId);
  541. util::successInfo();
  542. }
  543. $errCode = $return['errcode'];
  544. $errList = weixinUtil::$errCodes;
  545. $msg = $errList[$errCode];
  546. util::failInfo($msg);
  547. }
  548. /**
  549. * 修改标签
  550. */
  551. public function actionTagMod()
  552. {
  553. $get = Yii::$app->request->get();
  554. if (isset($get['id']) == false || empty($get['id'])) {
  555. util::failInfo('请选择标签');
  556. }
  557. $id = $get['id'];
  558. $tag = xhUserTagClassService::getByCondition(['id' => $id]);
  559. if (empty($tag)) {
  560. util::failInfo('标签已删除');
  561. }
  562. if ($tag['merchantId'] != $this->merchantId) {
  563. util::failInfo('非法访问');
  564. }
  565. $tagId = $tag['tagId'];
  566. $name = $get['tagName'];
  567. $return = weixinUtil::modTag($this->merchant, $tagId, $name);
  568. if (isset($return['errcode']) && $return['errcode'] == 0) {
  569. $data = ['name' => $name];
  570. xhUserTagClassService::updateById($id, $data, $this->merchantId);
  571. util::successInfo('操作成功');
  572. }
  573. $errCode = $return['errcode'];
  574. $errList = weixinUtil::$errCodes;
  575. $msg = $errList[$errCode];
  576. util::failInfo($msg);
  577. }
  578. /**
  579. * 对每个用户设置标签
  580. */
  581. public function actionTagSet()
  582. {
  583. $get = Yii::$app->request->get();
  584. $userId = isset($get['userId']) ? $get['userId'] : 0;
  585. $user = xhUserService::getById($userId);
  586. if (empty($user) || $user['merchantId'] != $this->merchantId) {
  587. util::failInfo('非法用户');
  588. }
  589. $tagId = isset($get['tagId']) ? $get['tagId'] : '';
  590. $merchantTag = xhUserTagClassService::getMerchantTag($this->merchantId);
  591. $mTag = array_flip($merchantTag);
  592. //防止非法标签id
  593. if (in_array($tagId, $mTag) == false) {
  594. util::failInfo('非法标签');
  595. }
  596. $userTag = xhUserTagService::getUserTag($userId);
  597. if (count($userTag) >= 3) {
  598. util::failInfo('最多只能设置三个标签哦');
  599. }
  600. if (in_array($tagId, $userTag)) {
  601. util::failInfo('此标签已经添加过了');
  602. }
  603. $openIdList = [$user['openId']];
  604. $return = weixinUtil::membersBatchTag($this->merchant, $openIdList, $tagId);
  605. if (isset($return['errcode']) == false) {
  606. util::failInfo('操作未成功,微信服务器没有返回');
  607. }
  608. if ($return['errcode'] != 0) {
  609. $errCode = $return['errcode'];
  610. $errList = weixinUtil::$errCodes;
  611. $msg = isset($errList[$errCode]) ? $errList[$errCode] : '';
  612. util::failInfo($msg);
  613. }
  614. $addTagData = ['merchantId' => $this->merchantId, 'tagId' => $tagId, 'userId' => $userId];
  615. xhUserTagService::add($addTagData);
  616. util::successInfo();
  617. }
  618. /**
  619. * 删除用户的标签
  620. */
  621. public function actionTagRemove()
  622. {
  623. $get = Yii::$app->request->get();
  624. $userId = isset($get['userId']) ? $get['userId'] : 0;
  625. $user = xhUserService::getById($userId);
  626. if (empty($user) || $user['merchantId'] != $this->merchantId) {
  627. util::failInfo('非法用户');
  628. }
  629. $tagId = isset($get['tagId']) ? $get['tagId'] : '';
  630. $merchantTag = xhUserTagClassService::getMerchantTag($this->merchantId);
  631. $mTag = array_flip($merchantTag);
  632. //防止非法标签id
  633. if (in_array($tagId, $mTag) == false) {
  634. util::failInfo('非法标签');
  635. }
  636. $openIdList = [$user['openId']];
  637. $return = weixinUtil::membersCancelTag($this->merchant, $openIdList, $tagId);
  638. if (isset($return['errcode']) == false) {
  639. util::failInfo('操作未成功,微信服务器没有返回');
  640. }
  641. if ($return['errcode'] != 0) {
  642. $errCode = $return['errcode'];
  643. $errList = weixinUtil::$errCodes;
  644. $msg = isset($errList[$errCode]) ? $errList[$errCode] : '';
  645. util::failInfo($msg);
  646. }
  647. xhUserTagService::deleteByCondition(['tagId' => $tagId, 'merchantId' => $this->merchantId, 'userId' => $userId]);
  648. util::successInfo('操作成功');
  649. }
  650. /**
  651. * 给多个用户添加标签
  652. */
  653. public function actionBatchAddUserTag()
  654. {
  655. $get = Yii::$app->request->get();
  656. if (isset($get['tagId']) == false || empty($get['tagId'])) {
  657. util::failInfo('请选择标签');
  658. }
  659. if (isset($get['userIdStr']) == false || empty($get['userIdStr'])) {
  660. util::failInfo('请选择用户');
  661. }
  662. $tagId = $get['tagId'];
  663. $userIdStr = trim($get['userIdStr']);
  664. $userIdList = explode(',', $userIdStr);
  665. $userIds = xhUserTagService::getUserIdByTagId($tagId, $this->merchantId);//取标签下的用户
  666. $toDoIds = array_diff($userIdList, $userIds);
  667. if (empty($toDoIds)) {
  668. util::successInfo('全部用户已经设置此标签了~');
  669. }
  670. $openIdList = [];
  671. $toSaveData = [];
  672. foreach ($toDoIds as $doKey => $doUserId) {
  673. $userInfo = xhUserService::getById($doUserId);
  674. if ($userInfo['merchantId'] == $this->merchantId) {
  675. $openIdList[] = $userInfo['openId'];
  676. $toSaveData[] = ['merchantId' => $this->merchantId, 'tagId' => $tagId, 'userId' => $doUserId];
  677. }
  678. }
  679. $return = weixinUtil::membersBatchTag($this->merchant, $openIdList, $tagId);//批量为用户打标签
  680. if (isset($return['errcode']) == false) {
  681. util::failInfo('操作失败');
  682. }
  683. if ($return['errcode'] == 0) {
  684. xhUserTagService::batchAdd($toSaveData);
  685. util::successInfo('操作成功');
  686. }
  687. $errCode = $return['errcode'];
  688. $errList = weixinUtil::$errCodes;
  689. $msg = isset($errList[$errCode]) ? $errList[$errCode] : '操作失败';
  690. util::failInfo($msg);
  691. }
  692. /**
  693. * 发放代金劵
  694. */
  695. public function actionGrantCoupon()
  696. {
  697. $get = Yii::$app->request->get();
  698. $userId = isset($get['userId']) ? $get['userId'] : 0;
  699. $userInfo = xhUserService::getById($userId);
  700. $merchantId = isset($userInfo['merchantId']) ? $userInfo['merchantId'] : 0;
  701. if ($this->merchantId != $merchantId) {
  702. util::failInfo('不是你的用户');
  703. }
  704. $duration = isset($get['duration']) && !empty($get['duration']) ? $get['duration'] : 60;
  705. $amount = isset($get['amount']) && !empty($get['amount']) ? $get['amount'] : 5;
  706. $meetAmount = isset($get['meetAmount']) && !empty($get['meetAmount']) ? $get['meetAmount'] : 150;
  707. if ($meetAmount <= $amount) {
  708. util::failInfo('消费金额要大于代金劵金额');
  709. }
  710. $get['beginTime'] = isset($get['beginTime']) && !empty($get['beginTime']) ? strtotime($get['beginTime']) : time();
  711. $get['deadline'] = $get['beginTime'] + $duration * 86400;
  712. $get['amount'] = $amount;
  713. $get['meetAmount'] = $meetAmount;
  714. $get['createTime'] = date("Y-m-d H:i:s");
  715. $get['merchantId'] = $this->merchantId;
  716. $get['userId'] = $userId;
  717. $get['number'] = 'CO' . stringUtil::buildOrderNo($userId);//代金劵编号
  718. xhCouponService::add($get, $userId);
  719. $allTM = xhTMessageService::getList($this->merchantId);
  720. $shortTempId = 'OPENTM207112032';//收入提醒
  721. $tempId = $allTM[$shortTempId];
  722. $openId = $userInfo['openId'];
  723. $data = ["touser" => $openId, "template_id" => $tempId,
  724. "url" => $this->frontUrl . '/center/coupon?account=' . $this->merchant['id'],
  725. "data" => [
  726. "first" => ["value" => "亲,您获得一张代金劵哦。", "color" => "#173177"],
  727. "keyword1" => ["value" => '代金劵', "color" => "#173177"],
  728. "keyword2" => ["value" => $amount . '元', "color" => "#173177"],
  729. "keyword3" => ["value" => date("Y-m-d H:i"), "color" => "#173177"],
  730. "remark" => ["value" => "点击查看详情", "color" => "#173177"]]];
  731. weixinUtil::sendTaskInform($data, $this->merchant);
  732. util::successInfo();
  733. }
  734. public function actionTrend()
  735. {
  736. $fans = xhUserByDayService::getLatestNum($this->merchantId, 29);
  737. return $this->render('trend', ['fans' => $fans,]);
  738. }
  739. /**
  740. * 将支付宝关联上微信号,积分迁移到微信号上
  741. */
  742. public function actionRelateUser()
  743. {
  744. $get = Yii::$app->request->get();
  745. $userId = isset($get['userId']) ? $get['userId'] : 0;
  746. $alipayId = isset($get['alipayId']) ? $get['alipayId'] : 0;
  747. $relation = xhUserAlipayService::getByIds($alipayId, $this->merchantId);
  748. if (empty($relation)) {
  749. util::failInfo('消费信息不存在');
  750. }
  751. if (isset($relation['userId']) && !empty($relation['userId'])) {
  752. util::failInfo('帐号已经绑定过了');
  753. }
  754. $user = xhUserService::getById($userId);
  755. if ($user['merchantId'] != $this->merchantId) {
  756. util::failInfo('不是您的用户');
  757. }
  758. $openId = $user['openId'];
  759. $userName = $user['userName'];
  760. if (!empty($user['alipayId'])) {
  761. util::failInfo($userName . '已经绑定过支付宝了');
  762. }
  763. xhUserAlipayService::updateByIds($alipayId, $this->merchantId, ['openId' => $openId, 'userId' => $userId, 'status' => 1]);
  764. $merchant = $this->merchant;
  765. $merchantExtend = $this->merchantExtend;
  766. $merchantId = $this->merchantId;
  767. $date = date("Y-m-d H:i:s");
  768. $now = time();
  769. $userAsset = xhUserAssetService::getByUserId($userId);
  770. $capitalTypeList = configDict::getConfig('capitalType');
  771. $capitalType = $capitalTypeList['xhUserUnite']['id'];
  772. $point = $relation['point'];
  773. $addIntegral = $point;
  774. $addPoint = $point;
  775. $totalExpend = $relation['totalExpend'];
  776. $uniteData = [
  777. 'alipayId' => $alipayId,
  778. 'merchantId' => $this->merchantId,
  779. 'userId' => $userId,
  780. 'point' => $point,
  781. 'totalExpend' => $totalExpend,
  782. 'operateId' => $this->adminId,
  783. 'addTime' => time(),
  784. 'createTime' => $date,
  785. ];
  786. $order = xhUserUniteService::add($uniteData);
  787. $orderId = $order['id'];
  788. $payWay = configDict::getConfig('payWay', 'alipay');
  789. $userIntegral = stringUtil::calcAdd($userAsset['integral'], $point);
  790. $userPoint = stringUtil::calcAdd($userAsset['point'], $point);
  791. $totalExpend = stringUtil::calcAdd($userAsset['totalExpend'], $totalExpend);
  792. $upAssetData = [];
  793. $upAssetData['integral'] = $userIntegral;
  794. $upAssetData['point'] = $userPoint;
  795. $upAssetData['totalExpend'] = $totalExpend;
  796. xhUserAssetService::ioChangeAsset($user, $userAsset, $upAssetData, $merchant, $merchantExtend, $order, $capitalType);//用户资产改变
  797. xhUserService::updateById($userId, ['alipayId' => $alipayId]);
  798. $integralData = [
  799. 'userId' => $userId,
  800. 'userName' => $userName,
  801. 'merchantId' => $merchantId,
  802. 'relateId' => (string)$orderId,
  803. 'integral' => $userIntegral,
  804. 'num' => $addIntegral,
  805. 'io' => 1,
  806. 'payWay' => $payWay,
  807. 'alipayId' => $alipayId,
  808. 'event' => '绑定支付宝帐号获得积分',
  809. 'operatorId' => $userId,
  810. 'createTime' => $date,
  811. 'addTime' => $now,
  812. 'capitalType' => $capitalType,
  813. ];
  814. xhUserIntegralService::add($integralData);//用户兑换型积分流水增加
  815. xhMerchantCapitalService::updateByCondition(['alipayId' => $alipayId, 'merchantId' => $this->merchantId], ['userId' => $userId, 'userName' => $userName]);
  816. xhOrderService::updateByCondition(['alipayId' => $alipayId, 'merchantId' => $this->merchantId], ['userId' => $userId]);
  817. util::successInfo();
  818. }
  819. /**
  820. * 关联前获取用户信息
  821. */
  822. public function actionGetUser()
  823. {
  824. $get = Yii::$app->request->get();
  825. $id = isset($get['id']) ? $get['id'] : 0;
  826. $user = xhUserService::getById($id);
  827. if ($user['subscribe'] == 0) {
  828. util::failInfo('没有关注公众号');
  829. }
  830. if ($user['merchantId'] != $this->merchantId) {
  831. util::failInfo('非法操作');
  832. }
  833. $largeImg = $user['avatar'];
  834. $avatarList = xhUserService::getAvatarList($largeImg);
  835. $avatar = $avatarList['small'];
  836. util::successInfo('操作成功', 'A0001', ['id' => $id, 'avatar' => $avatar, 'userName' => $user['userName']]);
  837. }
  838. }