ShopAdminController.php 8.8 KB

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