ShopAdminController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. <?php
  2. namespace ghs\controllers;
  3. use biz\admin\classes\AdminClass;
  4. use biz\shop\classes\ShopClass;
  5. use bizGhs\admin\services\AdminService;
  6. use bizHd\wx\classes\WxOpenClass;
  7. use bizGhs\shop\classes\ShopAdminClass;
  8. use bizGhs\admin\services\AdminRoleService;
  9. use bizGhs\shop\services\ShopAdminService;
  10. use common\components\dict;
  11. use common\components\imgUtil;
  12. use common\components\sms;
  13. use common\components\stringUtil;
  14. use common\components\util;
  15. use Yii;
  16. class ShopAdminController extends BaseController
  17. {
  18. public $guestAccess = ['generate-admin'];
  19. //获取所有在职员工 ssh 20220507
  20. public function actionGetAllStaff()
  21. {
  22. $list = ShopAdminClass::getAllByCondition(['mainId' => $this->mainId, 'delStatus' => 0, 'status' => 1, 'isPt' => 0,], null, 'id,name', null, true);
  23. util::success(['list' => $list]);
  24. }
  25. //获取添加员工的验证码 ssh 20210117
  26. public function actionGetStaffAuthCode()
  27. {
  28. $get = Yii::$app->request->get();
  29. $mobile = $get['mobile'] ?? '';
  30. if (stringUtil::isMobile($mobile) == false) {
  31. util::fail('请输入正确手机号');
  32. }
  33. $rand = rand(11111, 99999);
  34. $minute = 10;
  35. sms::send($mobile . ',' . $rand . ',' . $minute, '您的员工验证码:{$var},{$var}分钟内有效');
  36. $ptStyle = Yii::$app->params['ptStyle'];
  37. $cacheKey = 'add_staff_auth_code_' . $ptStyle . '_' . $mobile;
  38. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 600, $rand]);
  39. util::complete();
  40. }
  41. //添加员工
  42. public function actionAddStaff()
  43. {
  44. $shop = $this->shop;
  45. $join = $shop->join ?? 0;
  46. $default = $shop->default ?? 0;
  47. if ($join == 0 && $default == 0) {
  48. util::fail('请在总店操作');
  49. }
  50. $shopAdmin = $this->shopAdmin;
  51. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  52. util::fail('没有权限');
  53. }
  54. $connection = Yii::$app->db;
  55. $transaction = $connection->beginTransaction();
  56. try {
  57. $post = Yii::$app->request->post();
  58. $authCode = $post['authCode'] ?? '';
  59. $mobile = $post['mobile'] ?? '';
  60. $name = $post['name'] ?? '';
  61. //避免重复提交
  62. util::checkRepeatCommit($mobile, 10);
  63. $ptStyle = Yii::$app->params['ptStyle'];
  64. $cacheKey = 'add_staff_auth_code_' . $ptStyle . '_' . $mobile;
  65. $saveAuthCode = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  66. if (empty($saveAuthCode)) {
  67. util::fail('请填写验证码哦');
  68. }
  69. if (empty($authCode)) {
  70. util::fail('请填写验证码');
  71. }
  72. if ($saveAuthCode != $authCode) {
  73. util::fail('验证码错误');
  74. }
  75. $ptStyle = Yii::$app->params['ptStyle'];
  76. $adminInfo = ['name' => $name, 'mobile' => $mobile, 'style' => $ptStyle, 'currentGhsShopId' => $this->shopId];
  77. $fromApp = dict::getDict('userSourceGetId', 'app', 'id');
  78. $admin = \bizGhs\admin\classes\AdminClass::replaceAdmin($adminInfo, $fromApp);
  79. $post['shopId'] = $this->shopId;
  80. $respond = ShopAdminClass::appGenerateStaff($post, $admin);
  81. $transaction->commit();
  82. util::success($respond);
  83. } catch (\Exception $exception) {
  84. $transaction->rollBack();
  85. Yii::info("失败原因:" . $exception->getMessage());
  86. util::fail('添加失败');
  87. }
  88. }
  89. //创建管理员数据准备 ssh 2020.4.17
  90. public function actionPrepareBind()
  91. {
  92. $post = Yii::$app->request->post();
  93. $shopAdmin = $this->shopAdmin->attributes;
  94. if ($shopAdmin['super'] != 1) {
  95. util::fail('超管才能操作');
  96. }
  97. $roleId = isset($post['roleId']) ? $post['roleId'] : 0;
  98. $role = AdminRoleService::getById($roleId);
  99. if (empty($role) || isset($role['sjId']) != $this->sjId) {
  100. util::fail('请选择角色!');
  101. }
  102. unset($post['userId']);
  103. unset($post['status']);
  104. $post['shopId'] = $this->shopId;
  105. $post['sjId'] = $this->sjId;
  106. $post['adminId'] = $this->adminId;
  107. $post['mainId'] = $this->mainId;
  108. $merchant = WxOpenClass::getGhsWxInfo();
  109. $post['merchant'] = $merchant;
  110. $respond = ShopAdminService::prepareBindAdmin($post);
  111. $miniCode = isset($respond['miniCode']) ? imgUtil::getPrefix() . $respond['miniCode'] : '';
  112. util::success(['miniCode' => $miniCode]);
  113. }
  114. //创建员工 ssh 2020.4.20
  115. public function actionGenerateAdmin()
  116. {
  117. $post = Yii::$app->request->post();
  118. $post['adminId'] = $this->adminId;
  119. $post['mainId'] = $this->mainId;
  120. $admin = $this->admin->attributes;
  121. $currentShopId = $admin['currentGhsShopId'] ?? 0;
  122. if (!empty($currentShopId)) {
  123. $shop = ShopClass::getShopInfo($currentShopId);
  124. //一个人不能同时在二个商家门店下工作
  125. if (isset($shop['sjId']) && $shop['sjId'] != $this->sjId) {
  126. util::fail('您已经是别的商家员工');
  127. }
  128. }
  129. $openShop = $admin['openGhsShop'] ?? AdminClass::OPEN_SHOP_NO;
  130. if ($openShop != AdminClass::OPEN_SHOP_NO) {
  131. util::fail('您已申请开过店,不能成为别人的员工');
  132. }
  133. $respond = ShopAdminService::generateAdmin($post, $admin);
  134. util::success($respond);
  135. }
  136. //收款通知 ssh 2021.1.14
  137. public function actionUpdateRemind()
  138. {
  139. $get = Yii::$app->request->get();
  140. $id = $get['id'] ?? 0;
  141. $remind = $get['remind'] ?? 0;
  142. $relation = ShopAdminClass::getById($id, true);
  143. if (empty($relation)) {
  144. util::fail('没有找到员工');
  145. }
  146. ShopAdminClass::valid($relation->attributes, $this->mainId);
  147. $relation->remind = $remind;
  148. $relation->save();
  149. util::complete();
  150. }
  151. //启用与停用 ssh 2021.1.14
  152. public function actionUpdateStatus()
  153. {
  154. $get = Yii::$app->request->get();
  155. $id = $get['id'] ?? 0;
  156. $status = $get['status'] ?? 0;
  157. $relation = ShopAdminClass::getById($id, true);
  158. if (empty($relation)) {
  159. util::fail('没有找到员工');
  160. }
  161. ShopAdminClass::valid($relation->attributes, $this->mainId);
  162. $relation->status = $status;
  163. $relation->save();
  164. util::complete();
  165. }
  166. //员工列表 ssh 2019.12.8
  167. public function actionList()
  168. {
  169. $where = [];
  170. $where['mainId'] = $this->mainId;
  171. $where['delStatus'] = 0;
  172. $where['isPt'] = 0;
  173. $list = ShopAdminService::getAdminList($where);
  174. util::success($list);
  175. }
  176. //获取员工信息 ssh 2021.1.13
  177. public function actionDetail()
  178. {
  179. $get = Yii::$app->request->get();
  180. $id = isset($get['id']) ? $get['id'] : 0;
  181. $respond = ShopAdminService::getAdminInfo($id);
  182. ShopAdminClass::valid($respond, $this->mainId);
  183. util::success($respond);
  184. }
  185. //修改员工信息
  186. public function actionUpdate()
  187. {
  188. $post = Yii::$app->request->post();
  189. $id = $post['id'] ?? 0;
  190. $shop = $this->shop;
  191. $join = $shop->join ?? 0;
  192. $default = $shop->default ?? 0;
  193. if ($join == 0 && $default == 0) {
  194. util::fail('请在总店操作');
  195. }
  196. $shopAdmin = $this->shopAdmin;
  197. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  198. util::fail('没有权限');
  199. }
  200. if (empty($id)) {
  201. util::fail('请选择员工');
  202. }
  203. $relation = ShopAdminClass::getById($id, true);
  204. if (empty($relation)) {
  205. util::fail('没有找到员工');
  206. }
  207. ShopAdminClass::valid($relation, $this->mainId);
  208. unset($post['id']);
  209. $roleId = $post['roleId'] ?? 0;
  210. $remind = $post['remind'] ?? 0;
  211. $status = $post['status'] ?? 1;
  212. $super = $post['super'] ?? 0;
  213. $founder = $relation->founder ?? 1;
  214. if ($founder == 2 && $super == 0) {
  215. util::fail('不能关闭超管权限');
  216. }
  217. $adminId = $relation->adminId;
  218. $relation->roleId = $roleId;
  219. $relation->remind = $remind;
  220. $relation->status = $status;
  221. $relation->super = $super;
  222. if (isset($post['name']) && !empty($post['name'])) {
  223. $name = $post['name'];
  224. $relation->name = $name;
  225. AdminClass::updateById($adminId, ['name' => $name]);
  226. ShopAdminClass::updateByCondition(['adminId' => $adminId], ['name' => $name]);
  227. }
  228. $relation->save();
  229. //若有传密码,则修改密码
  230. $password = $post['password'] ?? '';
  231. if (!empty($password)) {
  232. if (ctype_alnum($password)) {
  233. util::fail('密码必须是字母、数字和符号的组合');
  234. }
  235. $password = password_hash($password, PASSWORD_BCRYPT);
  236. AdminClass::updateById($adminId, ['password' => $password]);
  237. }
  238. util::complete('修改成功');
  239. }
  240. //删除员工关系 ssh 2021.1.14
  241. public function actionDelete()
  242. {
  243. $id = Yii::$app->request->get('id');
  244. $shop = $this->shop;
  245. $join = $shop->join ?? 0;
  246. $default = $shop->default ?? 0;
  247. if ($join == 0 && $default == 0) {
  248. util::fail('请在总店操作');
  249. }
  250. $shopAdmin = $this->shopAdmin;
  251. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  252. util::fail('没有权限');
  253. }
  254. $connection = Yii::$app->db;
  255. $transaction = $connection->beginTransaction();
  256. try {
  257. $relation = ShopAdminClass::getById($id, true);
  258. \biz\shop\classes\ShopAdminClass::deleteShopAdmin($relation, $this->shopAdminId, $this->mainId);
  259. $transaction->commit();
  260. util::complete();
  261. } catch (\Exception $exception) {
  262. $transaction->rollBack();
  263. Yii::info("失败原因:" . $exception->getMessage());
  264. util::fail('删除失败');
  265. }
  266. }
  267. }