ShopAdminController.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 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. $ptStyle = Yii::$app->params['ptStyle'];
  64. $adminInfo = ['name' => $name, 'mobile' => $mobile, 'style' => $ptStyle, 'currentGhsShopId' => $this->shopId];
  65. $fromApp = dict::getDict('userSourceGetId', 'app', 'id');
  66. $admin = \bizGhs\admin\classes\AdminClass::replaceAdmin($adminInfo, $fromApp);
  67. $post['shopId'] = $this->shopId;
  68. $respond = ShopAdminService::appGenerateStaff($post, $admin);
  69. $transaction->commit();
  70. util::success($respond);
  71. } catch (\Exception $exception) {
  72. $transaction->rollBack();
  73. Yii::info("失败原因:" . $exception->getMessage());
  74. util::fail('添加失败');
  75. }
  76. }
  77. //创建管理员数据准备 ssh 2020.4.17
  78. public function actionPrepareBind()
  79. {
  80. $post = Yii::$app->request->post();
  81. $shopAdmin = $this->shopAdmin->attributes;
  82. if ($shopAdmin['super'] != 1) {
  83. util::fail('超管才能操作');
  84. }
  85. $roleId = isset($post['roleId']) ? $post['roleId'] : 0;
  86. $role = AdminRoleService::getById($roleId);
  87. if (empty($role) || isset($role['sjId']) != $this->sjId) {
  88. util::fail('请选择角色!');
  89. }
  90. unset($post['userId']);
  91. unset($post['status']);
  92. $post['shopId'] = $this->shopId;
  93. $post['sjId'] = $this->sjId;
  94. $post['adminId'] = $this->adminId;
  95. $post['mainId'] = $this->mainId;
  96. $merchant = WxOpenClass::getGhsWxInfo();
  97. $post['merchant'] = $merchant;
  98. $respond = ShopAdminService::prepareBindAdmin($post);
  99. $miniCode = isset($respond['miniCode']) ? imgUtil::getPrefix() . $respond['miniCode'] : '';
  100. util::success(['miniCode' => $miniCode]);
  101. }
  102. //创建员工 ssh 2020.4.20
  103. public function actionGenerateAdmin()
  104. {
  105. $post = Yii::$app->request->post();
  106. $post['adminId'] = $this->adminId;
  107. $post['mainId'] = $this->mainId;
  108. $admin = $this->admin->attributes;
  109. $currentShopId = $admin['currentGhsShopId'] ?? 0;
  110. if (!empty($currentShopId)) {
  111. $shop = ShopClass::getShopInfo($currentShopId);
  112. //一个人不能同时在二个商家门店下工作
  113. if (isset($shop['sjId']) && $shop['sjId'] != $this->sjId) {
  114. util::fail('您已经是别的商家员工');
  115. }
  116. }
  117. $openShop = $admin['openGhsShop'] ?? AdminClass::OPEN_SHOP_NO;
  118. if ($openShop != AdminClass::OPEN_SHOP_NO) {
  119. util::fail('您已申请开过店,不能成为别人的员工');
  120. }
  121. $respond = ShopAdminService::generateAdmin($post, $admin);
  122. util::success($respond);
  123. }
  124. //收款通知 ssh 2021.1.14
  125. public function actionUpdateRemind()
  126. {
  127. $get = Yii::$app->request->get();
  128. $id = $get['id'] ?? 0;
  129. $remind = $get['remind'] ?? 0;
  130. $relation = ShopAdminClass::getById($id, true);
  131. if (empty($relation)) {
  132. util::fail('没有找到员工');
  133. }
  134. ShopAdminClass::valid($relation->attributes, $this->shopId);
  135. $relation->remind = $remind;
  136. $relation->save();
  137. util::complete();
  138. }
  139. //启用与停用 ssh 2021.1.14
  140. public function actionUpdateStatus()
  141. {
  142. $get = Yii::$app->request->get();
  143. $id = $get['id'] ?? 0;
  144. $status = $get['status'] ?? 0;
  145. $relation = ShopAdminClass::getById($id, true);
  146. if (empty($relation)) {
  147. util::fail('没有找到员工');
  148. }
  149. ShopAdminClass::valid($relation->attributes, $this->shopId);
  150. $relation->status = $status;
  151. $relation->save();
  152. util::complete();
  153. }
  154. //员工列表 ssh 2019.12.8
  155. public function actionList()
  156. {
  157. $where = [];
  158. $where['mainId'] = $this->mainId;
  159. $where['delStatus'] = 0;
  160. $where['isPt'] = 0;
  161. $list = ShopAdminService::getAdminList($where);
  162. util::success($list);
  163. }
  164. //获取员工信息 ssh 2021.1.13
  165. public function actionDetail()
  166. {
  167. $get = Yii::$app->request->get();
  168. $id = isset($get['id']) ? $get['id'] : 0;
  169. $respond = ShopAdminService::getAdminInfo($id);
  170. ShopAdminClass::valid($respond, $this->shopId);
  171. util::success($respond);
  172. }
  173. //修改员工信息
  174. public function actionUpdate()
  175. {
  176. $post = Yii::$app->request->post();
  177. $id = $post['id'] ?? 0;
  178. $shopAdmin = $this->shopAdmin;
  179. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  180. util::fail('没有权限');
  181. }
  182. if (empty($id)) {
  183. util::fail('请选择员工');
  184. }
  185. $relation = ShopAdminClass::getById($id, true);
  186. if (empty($relation)) {
  187. util::fail('没有找到员工');
  188. }
  189. ShopAdminClass::valid($relation, $this->shopId);
  190. unset($post['id']);
  191. $roleId = $post['roleId'] ?? 0;
  192. $remind = $post['remind'] ?? 0;
  193. $status = $post['status'] ?? 1;
  194. $super = $post['super'] ?? 0;
  195. $founder = $relation->founder ?? 1;
  196. if ($founder == 2 && $super == 0) {
  197. util::fail('不能关闭超管权限');
  198. }
  199. $relation->roleId = $roleId;
  200. $relation->remind = $remind;
  201. $relation->status = $status;
  202. $relation->super = $super;
  203. if (isset($post['name']) && !empty($post['name'])) {
  204. $relation->name = $post['name'];
  205. }
  206. $relation->save();
  207. //若有传密码,则修改密码
  208. $password = $post['password'] ?? '';
  209. if (!empty($password)) {
  210. if (ctype_alnum($password)) {
  211. util::fail('密码必须是字母、数字和符号的组合');
  212. }
  213. $adminId = $relation->adminId;
  214. $password = password_hash($password, PASSWORD_BCRYPT);
  215. AdminClass::updateById($adminId, ['password' => $password]);
  216. }
  217. util::complete('修改成功');
  218. }
  219. //删除员工关系 ssh 2021.1.14
  220. public function actionDelete()
  221. {
  222. $id = Yii::$app->request->get('id');
  223. $shopAdmin = $this->shopAdmin;
  224. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  225. util::fail('没有权限');
  226. }
  227. $relation = ShopAdminClass::getById($id, true);
  228. \biz\shop\classes\ShopAdminClass::deleteShopAdmin($relation, $this->shopAdminId, $this->shopId);
  229. util::complete();
  230. }
  231. }