MemberController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. <?php
  2. namespace addons\Member\merchant\controllers;
  3. use Yii;
  4. use common\models\base\SearchModel;
  5. use common\enums\StatusEnum;
  6. use common\enums\MemberTypeEnum;
  7. use common\enums\AccessTokenGroupEnum;
  8. use common\enums\CreditsLogTypeEnum;
  9. use common\enums\GenderEnum;
  10. use common\forms\MemberForm as Member;
  11. use common\helpers\BcHelper;
  12. use common\helpers\ExcelHelper;
  13. use common\helpers\ResultHelper;
  14. use common\traits\MemberMobileSelect;
  15. use common\traits\MerchantCurd;
  16. use common\models\member\CreditsLog;
  17. use common\models\member\Account;
  18. use addons\Member\merchant\forms\RechargeForm;
  19. use addons\Member\merchant\forms\MemberEditForm;
  20. use addons\Member\merchant\forms\MemberCreateForm;
  21. use PhpOffice\PhpSpreadsheet\Shared\Date;
  22. /**
  23. * 会员管理
  24. *
  25. * Class MemberController
  26. * @package addons\Member\merchant\controllers
  27. * @author jianyan74 <751393839@qq.com>
  28. */
  29. class MemberController extends BaseController
  30. {
  31. use MerchantCurd, MemberMobileSelect;
  32. /**
  33. * @var Member
  34. */
  35. public $modelClass = Member::class;
  36. /**
  37. * 首页
  38. *
  39. * @return string
  40. * @throws \yii\web\NotFoundHttpException
  41. */
  42. public function actionIndex()
  43. {
  44. $startTime = Yii::$app->request->get('start_time');
  45. $endTime = Yii::$app->request->get('end_time');
  46. $searchModel = new SearchModel([
  47. 'model' => $this->modelClass,
  48. 'scenario' => 'default',
  49. 'partialMatchAttributes' => ['realname', 'mobile'], // 模糊查询
  50. 'defaultOrder' => [
  51. 'id' => SORT_DESC
  52. ],
  53. 'pageSize' => $this->pageSize
  54. ]);
  55. $dataProvider = $searchModel
  56. ->search(Yii::$app->request->queryParams);
  57. $dataProvider->query
  58. ->andWhere([
  59. 'type' => MemberTypeEnum::MEMBER,
  60. 'status' => StatusEnum::ENABLED
  61. ])
  62. ->andFilterWhere(['merchant_id' => $this->getMerchantId()])
  63. ->andFilterWhere(['between', 'created_at', !empty($startTime) ? strtotime($startTime) : '', !empty($endTime) ? strtotime($endTime) : ''])
  64. ->with(['account', 'memberLevel', 'tag']);
  65. $models = $dataProvider->getModels();
  66. $pageAccountTotal = [
  67. 'user_money' => 0,
  68. 'user_integral' => 0,
  69. 'user_growth' => 0,
  70. ];
  71. foreach ($models as $model) {
  72. $pageAccountTotal['user_money'] = BcHelper::add($pageAccountTotal['user_money'], $model->account->user_money);
  73. $pageAccountTotal['user_integral'] = BcHelper::add($pageAccountTotal['user_integral'], $model->account->user_integral);
  74. $pageAccountTotal['user_growth'] = BcHelper::add($pageAccountTotal['user_growth'], $model->account->user_growth);
  75. }
  76. return $this->render($this->action->id, [
  77. 'dataProvider' => $dataProvider,
  78. 'searchModel' => $searchModel,
  79. 'startTime' => $startTime,
  80. 'endTime' => $endTime,
  81. 'pageAccountTotal' => $pageAccountTotal,
  82. 'levelMap' => Yii::$app->services->memberLevel->getMap(),
  83. ]);
  84. }
  85. /**
  86. * 编辑/创建
  87. *
  88. * @return mixed
  89. */
  90. public function actionEdit()
  91. {
  92. $id = Yii::$app->request->get('id', null);
  93. $model = $this->findEditForm($id);
  94. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  95. return $this->referrer();
  96. }
  97. return $this->render($this->action->id, [
  98. 'model' => $model,
  99. ]);
  100. }
  101. /**
  102. * 编辑/创建
  103. *
  104. * @return mixed|string|\yii\web\Response
  105. * @throws \yii\base\Exception
  106. * @throws \yii\base\ExitException
  107. * @throws \yii\base\InvalidConfigException
  108. */
  109. public function actionAjaxEdit()
  110. {
  111. $id = Yii::$app->request->get('id');
  112. $model = $this->findCreateForm($id);
  113. $model->merchant_id = Yii::$app->services->merchant->getNotNullId();
  114. $model->type = MemberTypeEnum::MEMBER;
  115. // ajax 校验
  116. $this->activeFormValidate($model);
  117. if ($model->load(Yii::$app->request->post())) {
  118. // 记录行为
  119. Yii::$app->services->actionLog->create('memberUpdate', '更新用户账号密码', 0, [], false);
  120. // 验证密码
  121. if (!empty($model->password_hash)) {
  122. $model->password_hash = Yii::$app->security->generatePasswordHash($model->password_hash);
  123. }
  124. return $model->save()
  125. ? $this->message('操作成功', $this->redirect(Yii::$app->request->referrer))
  126. : $this->message($this->getError($model), $this->redirect(Yii::$app->request->referrer), 'error');
  127. }
  128. $model->password_hash = '';
  129. return $this->renderAjax($this->action->id, [
  130. 'model' => $model,
  131. ]);
  132. }
  133. /**
  134. * @param $id
  135. * @return string
  136. */
  137. public function actionView($id)
  138. {
  139. $type = Yii::$app->request->get('type', CreditsLogTypeEnum::USER_MONEY);
  140. $searchModel = new SearchModel([
  141. 'model' => CreditsLog::class,
  142. 'scenario' => 'default',
  143. 'partialMatchAttributes' => [], // 模糊查询
  144. 'defaultOrder' => [
  145. 'id' => SORT_DESC
  146. ],
  147. 'pageSize' => $this->pageSize
  148. ]);
  149. $dataProvider = $searchModel
  150. ->search(Yii::$app->request->queryParams);
  151. $dataProvider->query
  152. ->andWhere(['member_id' => $id])
  153. ->andWhere(['>=', 'status', StatusEnum::DISABLED])
  154. ->andWhere(['type' => $type, 'member_type' => MemberTypeEnum::MEMBER])
  155. ->andFilterWhere(['merchant_id' => $this->getMerchantId()])
  156. ->with(['member']);
  157. return $this->render($this->action->id, [
  158. 'member' => $this->findModel($id),
  159. 'dataProvider' => $dataProvider,
  160. 'searchModel' => $searchModel,
  161. 'type' => $type,
  162. 'id' => $id,
  163. ]);
  164. }
  165. /**
  166. * 修改等级
  167. *
  168. * @return mixed|string|\yii\web\Response
  169. * @throws \yii\base\ExitException
  170. */
  171. public function actionUpdateLevel()
  172. {
  173. $id = Yii::$app->request->get('id');
  174. $model = $this->findModel($id);
  175. // ajax 校验
  176. $this->activeFormValidate($model);
  177. if ($model->load(Yii::$app->request->post())) {
  178. // 记录行为
  179. Yii::$app->services->actionLog->create('memberUpdateLevel', '更新用户等级');
  180. return $model->save()
  181. ? $this->redirect(Yii::$app->request->referrer)
  182. : $this->message($this->getError($model), $this->redirect(Yii::$app->request->referrer), 'error');
  183. }
  184. return $this->renderAjax($this->action->id, [
  185. 'model' => $model,
  186. 'levelMap' => Yii::$app->services->memberLevel->getMap(),
  187. ]);
  188. }
  189. /**
  190. * 积分/余额变更
  191. *
  192. * @param $id
  193. * @return mixed|string
  194. * @throws \yii\base\ExitException
  195. * @throws \yii\base\InvalidConfigException
  196. * @throws \yii\db\Exception
  197. */
  198. public function actionRecharge($id)
  199. {
  200. $rechargeForm = new RechargeForm();
  201. $member = Yii::$app->services->member->findById($id);
  202. // ajax 校验
  203. $this->activeFormValidate($rechargeForm);
  204. if ($rechargeForm->load(Yii::$app->request->post())) {
  205. // 记录行为
  206. Yii::$app->services->actionLog->create('memberRecharge', '更新用户余额/积分/成长值');
  207. if (!$rechargeForm->save($member)) {
  208. return $this->message($this->getError($rechargeForm), $this->redirect(Yii::$app->request->referrer), 'error');
  209. }
  210. return $this->message('充值成功', $this->redirect(Yii::$app->request->referrer));
  211. }
  212. return $this->renderAjax($this->action->id, [
  213. 'model' => $member,
  214. 'rechargeForm' => $rechargeForm,
  215. ]);
  216. }
  217. /**
  218. * 黑名单
  219. *
  220. * @param $id
  221. * @return mixed
  222. */
  223. public function actionBlacklist($id)
  224. {
  225. if (!($model = $this->modelClass::findOne($id))) {
  226. return $this->message("找不到数据", $this->redirect(Yii::$app->request->referrer), 'error');
  227. }
  228. $model->status = StatusEnum::DISABLED;
  229. if ($model->save()) {
  230. return $this->message("拉入黑名单成功", $this->redirect(Yii::$app->request->referrer));
  231. }
  232. return $this->message("拉入黑名单失败", $this->redirect(Yii::$app->request->referrer), 'error');
  233. }
  234. /**
  235. * 全部备货完成
  236. *
  237. * @param $id
  238. * @return mixed|string
  239. */
  240. public function actionImportMember()
  241. {
  242. if (Yii::$app->request->isPost) {
  243. $transaction = Yii::$app->db->beginTransaction();
  244. try {
  245. $file = $_FILES['excelFile'];
  246. if (empty($file['tmp_name'])) {
  247. return $this->message('请上传需要导入的文件', $this->redirect(['member/index']), 'error');
  248. }
  249. $defaultData = ExcelHelper::import($file['tmp_name'], 2);
  250. foreach ($defaultData as $datum) {
  251. $member = new Member();
  252. $member->merchant_id = Yii::$app->services->merchant->getNotNullId();
  253. $member->nickname = $datum[0];
  254. $member->mobile = $datum[1];
  255. $member->password = $datum[2];
  256. $member->realname = $datum[3];
  257. $member->gender = $datum[4] == '男' ? GenderEnum::MAN : GenderEnum::WOMAN;
  258. $member->head_portrait = $datum[5];
  259. $member->qq = $datum[6];
  260. $member->email = $datum[7];
  261. $member->birthday = date('Y-m-d', Date::excelToTimestamp($datum[8]));
  262. $member->source = AccessTokenGroupEnum::EXCEL_IMPORT;
  263. $member->save();
  264. Account::updateAll(['user_money' => (float)$datum[9], 'user_integral' => (int)$datum[10], 'user_growth' => (int)$datum[11]], ['member_id' => $member->id]);
  265. }
  266. $transaction->commit();
  267. return $this->message('导入成功', $this->redirect(['member/index']));
  268. } catch (\Exception $e) {
  269. $transaction->rollBack();
  270. return ResultHelper::json(422, $e->getMessage());
  271. }
  272. }
  273. return $this->renderAjax($this->action->id);
  274. }
  275. /**
  276. * 下载模板
  277. */
  278. public function actionTemplateDownload()
  279. {
  280. $path = Yii::getAlias('@addons') . '/Member/common/file/member.xls';
  281. Yii::$app->response->sendFile($path, '一键导入会员模板_' . time() . '.xls');
  282. }
  283. /**
  284. * 返回模型
  285. *
  286. * @param $id
  287. * @return \yii\db\ActiveRecord
  288. */
  289. protected function findCreateForm($id)
  290. {
  291. /* @var $model \yii\db\ActiveRecord */
  292. if (empty($id) || empty(($model = MemberCreateForm::findOne(['id' => $id, 'merchant_id' => $this->getMerchantId()])))) {
  293. $model = new MemberCreateForm();
  294. return $model->loadDefaultValues();
  295. }
  296. return $model;
  297. }
  298. /**
  299. * 返回模型
  300. *
  301. * @param $id
  302. * @return \yii\db\ActiveRecord
  303. */
  304. protected function findEditForm($id)
  305. {
  306. /* @var $model \yii\db\ActiveRecord */
  307. if (empty($id) || empty(($model = MemberEditForm::findOne(['id' => $id, 'merchant_id' => $this->getMerchantId()])))) {
  308. $model = new MemberEditForm();
  309. return $model->loadDefaultValues();
  310. }
  311. return $model;
  312. }
  313. }
粤ICP备19079148号