WxController.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: xiaofeng
  5. * Date: 2018/5/2
  6. * Time: 上午10:09
  7. */
  8. namespace backend\controllers;
  9. use backend\base\CommonController;
  10. use backend\base\Help;
  11. use common\models\WxJc;
  12. use common\models\WxRand;
  13. use common\models\WxUserJc;
  14. use Yii;
  15. use common\models\WxUser;
  16. class WxController extends CommonController
  17. {
  18. /*
  19. * 首页-view
  20. * */
  21. public function actionHome()
  22. {
  23. return $this->render('home');
  24. }
  25. /*
  26. * 首页数据
  27. * */
  28. public function actionHomeform()
  29. {
  30. $model = new WxUser();
  31. $result = $model->getList(Yii::$app->request->post());
  32. if($result != null)
  33. {
  34. foreach ($result as &$val)
  35. {
  36. $val['create_at'] = date('Y-m-d H:i',$val['create_at']);
  37. }
  38. return Help::JsonData(0,'成功',$model->Total(Yii::$app->request->post()),$result);
  39. }
  40. return Help::JsonCode(Help::ERROR,'暂无数据');
  41. }
  42. /*
  43. * 添加页面
  44. * */
  45. public function actionCreate()
  46. {
  47. return $this->render('create');
  48. }
  49. /*
  50. * 添加数据
  51. * */
  52. public function actionCreateform()
  53. {
  54. $input = Yii::$app->request->post('data');
  55. $model = new WxUser();
  56. $model->name = $input['name'];
  57. $model->openid = 'a';
  58. $model->key = 'a';
  59. $Transaction = Yii::$app->db->beginTransaction();
  60. $result = $model->save();
  61. $jc = new WxUserJc();
  62. $jc->uid = $model->attributes['id'];
  63. $jc->jc = 0;
  64. $rand = WxRand::find()->andWhere(['state'=>1])->one();
  65. $rand->state = 2;
  66. $jc->rand = $rand->id;
  67. if($jc->save() == true && $result == true && $rand->save())
  68. {
  69. $Transaction->commit();
  70. return Help::JsonCode(Help::SUCCESS,'添加成功',['rand'=>$jc->rand]);
  71. }
  72. $Transaction->rollBack();
  73. return Help::JsonCode(Help::ERROR,'添加失败,请重新添加');
  74. }
  75. //===============奖次相关
  76. public function actionJchome()
  77. {
  78. return $this->render('jchome');
  79. }
  80. /*
  81. *
  82. * */
  83. public function actionJchomeform()
  84. {
  85. $model = new WxJc();
  86. $result = $model->getList(Yii::$app->request->post());
  87. $query = \common\models\WxUserJc::find();
  88. $groupJc = $query->andWhere(['<>','jc',0])->select(["count('js') as jcnum",'jc'])->groupBy('jc')->asArray()->all();
  89. $jc = array_column($groupJc,'jcnum','jc');
  90. if($result != null)
  91. {
  92. foreach ($result as &$val)
  93. {
  94. if(isset($jc[$val['id']]))
  95. {
  96. $val['state'] = '已抽';
  97. $val['total'] = $jc[$val['id']];
  98. }
  99. else
  100. {
  101. $val['state'] = '未抽';
  102. $val['total'] = 0;
  103. }
  104. $val['create_at'] = date('Y-m-d H:i',$val['create_at']);
  105. }
  106. return Help::JsonData(0,'成功',$model->Total(),$result);
  107. }
  108. return Help::JsonCode(Help::ERROR,'失败');
  109. }
  110. public function actionJccreate()
  111. {
  112. return $this->render('jccreate');
  113. }
  114. public function actionJccreateform()
  115. {
  116. $model = new WxJc();
  117. $input = Yii::$app->request->post();
  118. $one = $model::find()->andWhere(['name'=>$input['name']])->andWhere(['del'=>1])->one();
  119. if($one != null) return Help::JsonCode(Help::ERROR,'该奖次已存在');
  120. $model->name = $input['name'];
  121. if($model->save() == true) return Help::JsonCode(Help::SUCCESS,Yii::t('app','add_success'));
  122. return Help::JsonCode(Help::ERROR,Yii::t('app','add_error'));
  123. }
  124. public function actionJcedit()
  125. {
  126. $model = new WxJc();
  127. $input = Yii::$app->request->get();
  128. $row = $model->FindByOne($input['id']);
  129. return $this->render('jcedit',['model'=>$row]);
  130. }
  131. public function actionJceditform()
  132. {
  133. $model = new WxJc();
  134. $input = Yii::$app->request->post();
  135. $row = $model->FindByOne($input['id']);
  136. if($row != null)
  137. {
  138. $row->name = $input['name'];
  139. if($row->save() == true) return Help::JsonCode(Help::SUCCESS,Yii::t('app','edit_success'));
  140. }
  141. return Help::JsonCode(Help::SUCCESS,Yii::t('app','edit_error'));
  142. }
  143. public function actionJcdel()
  144. {
  145. $model = new WxJc();
  146. $input = Yii::$app->request->post();
  147. $row = $model->FindByOne($input['id']);
  148. if($row != null)
  149. {
  150. $row->del = 2;
  151. if($row->save() == true) return Help::JsonCode(Help::SUCCESS,Yii::t('app','del_success'));
  152. }
  153. return Help::JsonCode(Help::SUCCESS,Yii::t('app','del_error'));
  154. }
  155. /*
  156. * 获奖人员名单
  157. * */
  158. public function actionAwardnamelist()
  159. {
  160. $input = Yii::$app->request->get();
  161. $rows = WxUserJc::find()->select(['pfg_wx_user.name','pfg_wx_user.photo','pfg_wx_user_jc.rand'])->andWhere(['pfg_wx_user_jc.jc'=>$input['id']])->leftJoin('pfg_wx_user','pfg_wx_user_jc.uid=pfg_wx_user.id')->asArray()->all();
  162. if($rows != null)
  163. {
  164. return $this->render('awardnamelist',['model'=>$rows]);
  165. }
  166. }
  167. /*
  168. * 清除奖次,重新抽奖
  169. * */
  170. public function actionJcclear()
  171. {
  172. $input = Yii::$app->request->post();
  173. $jcRows = WxUserJc::find()->andWhere(['jc'=>$input['id']])->asArray()->all();
  174. if($jcRows == null) return Help::JsonCode(Help::ERROR,'暂无该奖次记录');
  175. $beginTransaction = Yii::$app->db->beginTransaction();
  176. foreach ($jcRows as &$val)
  177. {
  178. $val['create_at'] = $_SERVER['REQUEST_TIME'];
  179. $val['update_at'] = $_SERVER['REQUEST_TIME'];
  180. $val['session_id'] = Yii::$app->session['user_info']['uid'];
  181. }
  182. $result = Yii::$app->db->createCommand()->batchInsert('pfg_wxjc_record',['jc','state','create_at','update_at','uid','rand','session_id'],$jcRows)->execute();
  183. $res = WxUserJc::updateAll(['jc'=>0,'state'=>1],['jc'=>$input['id']]);
  184. if($res > 0 && $result >0 )
  185. {
  186. $beginTransaction->commit();
  187. return Help::JsonCode(Help::SUCCESS,'操作成功');
  188. }
  189. $beginTransaction->rollBack();
  190. return Help::JsonCode(Help::ERROR,'操作失败');
  191. }
  192. //语录
  193. public function actionYhome()
  194. {
  195. return $this->render('yhome');
  196. }
  197. public function actionYhomeform()
  198. {
  199. $model = new \common\models\WxJt();
  200. $rows = $model->getList(Yii::$app->request->post());
  201. if($rows != null)
  202. {
  203. return Help::JsonData(0,'成功',$model->Total(),$rows);
  204. }
  205. }
  206. public function actionYcreate()
  207. {
  208. return $this->render('ycreate');
  209. }
  210. public function actionYcreateform()
  211. {
  212. $model = new \common\models\WxJt();
  213. $input = Yii::$app->request->post('data');
  214. $model->text = $input['name'];
  215. if($model->save()) return Help::JsonCode(Help::SUCCESS,'添加成功');
  216. return Help::JsonCode(Help::ERROR,'添加失败');
  217. }
  218. public function actionYedit()
  219. {
  220. $row = \common\models\WxJt::findOne(Yii::$app->request->get('id'));
  221. if($row != null)
  222. {
  223. return $this->render('yedit',['model'=>$row]);
  224. }
  225. }
  226. public function actionYeditform()
  227. {
  228. $input = Yii::$app->request->post('data');
  229. $row = \common\models\WxJt::findOne($input['id']);
  230. if($row != null)
  231. {
  232. $row->text = $input['name'];
  233. if($row->save()) return Help::JsonCode(Help::SUCCESS,'操作成功');
  234. return Help::JsonCode(Help::ERROR,'操作失败');
  235. }
  236. }
  237. public function actionYdel()
  238. {
  239. $row = \common\models\WxJt::findOne(Yii::$app->request->post('id'));
  240. if($row != null)
  241. {
  242. if($row->delete() == true) return Help::JsonCode(Help::SUCCESS,'操作成功');
  243. return Help::JsonCode(Help::ERROR,'操作失败');
  244. }
  245. }
  246. }