UserController.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <?php
  2. namespace ghs\controllers;
  3. use bizGhs\custom\classes\CustomClass;
  4. use bizHd\admin\services\AdminService;
  5. use biz\sj\services\MerchantAssetService;
  6. use biz\sj\services\MerchantRemarkService;
  7. use bizHd\user\services\UserAssetService;
  8. use bizHd\user\services\UserGrowthService;
  9. use bizHd\user\services\UserService;
  10. use bizHd\user\services\UserUpgradeService;
  11. use common\components\wxUtil;
  12. use common\services\xhMerchantExtendService;
  13. use common\services\xhRechargeService;
  14. use common\services\xhTMessageService;
  15. use common\services\xhUserIntegralService;
  16. use Yii;
  17. use common\components\stringUtil;
  18. use common\components\util;
  19. class UserController extends BaseController
  20. {
  21. //客户列表 ssh 2019.11.30
  22. public function actionList()
  23. {
  24. $get = Yii::$app->request->get();
  25. //组建where
  26. $type = isset($get['type']) ? $get['type'] : -1;
  27. $search = isset($get['search']) ? $get['search'] : '';
  28. $where = ['sjId' => $this->sjId];
  29. switch ($type) {
  30. case 1:
  31. //粉丝
  32. $where['subscribe'] = 1;
  33. break;
  34. case 2:
  35. //会员
  36. $where['member>'] = 0;
  37. break;
  38. default:
  39. }
  40. if (!empty($search)) {
  41. if (stringUtil::isMobile($search)) {
  42. $where['mobile'] = $search;
  43. } else {
  44. $where['userName'] = ['like', $search];
  45. }
  46. }
  47. $list = UserService::getUserList($where);
  48. $list['asset'] = MerchantAssetService::getBySjId($this->sjId);
  49. util::success($list);
  50. }
  51. //获取客户基本和资产信息 ssh 2019.11.30
  52. public function actionDetail()
  53. {
  54. $userId = Yii::$app->request->get('userId');
  55. $info = UserService::getUserInfo($userId);
  56. if (isset($info['sjId']) == false || $this->sjId != $info['sjId']) {
  57. util::fail('没有权限');
  58. }
  59. util::success($info);
  60. }
  61. //获取客户详情,包括基本信息、资产、订单和备注 2019.11.30
  62. public function actionUserDetail()
  63. {
  64. $userId = Yii::$app->request->get('userId');
  65. $info = UserService::getFullUserInfo($userId);
  66. CustomClass::valid($info, $this->shopId);
  67. $info['remarkList'] = [];
  68. util::success($info);
  69. }
  70. //添加客户,申请会员时会自动同步资产 ssh 2019.11.30
  71. public function actionAdd()
  72. {
  73. $post = Yii::$app->request->post();
  74. if (isset($post['mobile']) == false || stringUtil::isMobile($post['mobile']) == false) {
  75. util::fail('请输入手机号');
  76. }
  77. if ($post['mobile'] != $post['confirmMobile']) {
  78. util::fail('二次输入手机号不一样');
  79. }
  80. $payPassword = isset($post['payPassword']) ? $post['payPassword'] : '';
  81. AdminService::verifyPayPassword($this->adminId, $payPassword);
  82. $mobile = $post['mobile'];
  83. $user = UserService::getByMobile($mobile, $this->sjId);
  84. if (!empty($user)) {
  85. util::fail('手机号已使用');
  86. }
  87. $post['sjId'] = $this->sjId;
  88. $post['member'] = $post['memberLevel'];
  89. UserService::addUser($post);
  90. util::complete();
  91. }
  92. //充值 ssh 2019.12.1
  93. public function actionRecharge()
  94. {
  95. $post = Yii::$app->request->post();
  96. $payPassword = isset($post['payPassword']) ? $post['payPassword'] : '';
  97. AdminService::verifyPayPassword($this->adminId, $payPassword);
  98. $userId = isset($post['userId']) ? $post['userId'] : 0;
  99. $rechargeAmount = isset($post['rechargeAmount']) ? $post['rechargeAmount'] : 1;
  100. $user = UserService::getById($userId);
  101. if (empty($user)) {
  102. util::fail('找不到客户');
  103. }
  104. if ($user['sjId'] != $this->sjId) {
  105. util::fail('您无法操作');
  106. }
  107. $userAsset = UserAssetService::getByUserId($userId);
  108. $totalBalance = $userAsset['balance'] + $rechargeAmount;
  109. $totalRecharge = $userAsset['totalRecharge'] + $rechargeAmount;
  110. $upData['balance'] = $totalBalance;
  111. $upData['totalRecharge'] = $totalRecharge;
  112. $merchantAsset = MerchantAssetService::getBySjId($this->sjId);
  113. $return = xhRechargeService::recharge($rechargeAmount, $user, $this->adminId, $this->sj->attributes, $this->sjExtend->attributes, $merchantAsset);
  114. if ($return['code'] == 'A0001') {
  115. util::complete();
  116. }
  117. util::fail();
  118. }
  119. //升级 ssh 2019.12.1
  120. public function actionUpgrade()
  121. {
  122. $post = Yii::$app->request->post();
  123. $userId = isset($post['userId']) ? $post['userId'] : 0;
  124. $member = isset($post['member']) ? $post['member'] : 0;
  125. $user = UserService::getById($userId);
  126. if (empty($user)) {
  127. util::fail('客户不存在');
  128. }
  129. //验证是否商家客户
  130. UserService::valid($user, $this->sjId);
  131. //验证确认密码
  132. $payPassword = $post['payPassword'];
  133. AdminService::verifyPayPassword($this->adminId, $payPassword);
  134. $grade = xhMerchantExtendService::getGradeList($this->sjExtend->attributes, 'desc');
  135. //升级后的成长值
  136. $newLevelGrowth = $grade[$member]['growth'];
  137. $newDiscount = $grade[$member]['discount'];
  138. $userAsset = UserAssetService::getByUserId($userId);
  139. if ($userAsset['member'] >= $member) {
  140. util::fail('客户已经达到此级别了');
  141. }
  142. $now = time();
  143. $date = date("Y-m-d H:i", $now);
  144. $connection = Yii::$app->db;
  145. $transaction = $connection->beginTransaction();
  146. try {
  147. $addGrowth = $newLevelGrowth - $userAsset['growth'];
  148. $upData['growth'] = $newLevelGrowth;
  149. $upData['member'] = $member;
  150. $upData['discount'] = $newDiscount;
  151. UserAssetService::updateByUserId($userId, $upData);
  152. UserService::updateById($userId, ['member' => $member]);
  153. $upgradeData = [
  154. 'member' => $member,
  155. 'prevMember' => $userAsset['member'],
  156. 'sjId' => $this->sjId,
  157. 'gainType' => 0,
  158. 'addTime' => $now,
  159. 'userId' => $userId,
  160. ];
  161. $upgradeRespond = UserUpgradeService::add($upgradeData);
  162. $growthData = [
  163. 'userId' => $userId,
  164. 'userName' => $user['userName'],
  165. 'sjId' => $this->sjId,
  166. 'relateId' => $upgradeRespond['id'],
  167. 'growth' => $newLevelGrowth,
  168. 'num' => $addGrowth,
  169. 'io' => 1,
  170. 'payWay' => 4,
  171. 'alipayId' => '',
  172. 'event' => '管理员操作升到' . $member . '级增加成长值',
  173. 'operatorId' => $userId,
  174. 'createTime' => $date,
  175. 'gainType' => 0,
  176. 'addTime' => $now,
  177. ];
  178. UserGrowthService::add($growthData);
  179. $transaction->commit();
  180. } catch (Exception $e) {
  181. $transaction->rollBack();
  182. util::fail();
  183. }
  184. $allTM = xhTMessageService::getList($this->sjId);
  185. $openId = isset($user['openId']) ? $user['openId'] : '';
  186. if (empty($openId)) {
  187. util::complete();
  188. }
  189. $shortTempId = 'OPENTM205211943';//升级通知
  190. if (isset($allTM[$shortTempId]) == false) {
  191. util::complete();
  192. }
  193. $tempId = $allTM[$shortTempId];
  194. $data = [
  195. "touser" => $openId, "template_id" => $tempId, "url" => '',
  196. "data" => [
  197. "first" => ["value" => "恭喜,您已经升为{$member}级会员。", "color" => "#173177"],
  198. "keyword1" => ["value" => $user['userName'], "color" => "#173177"],
  199. "keyword2" => ["value" => $date, "color" => "#173177"],
  200. "remark" => ["value" => "点击查看会员权益", "color" => "#173177"],
  201. ]];
  202. wxUtil::sendTaskInform($data, $this->sj);
  203. util::complete();
  204. }
  205. //重置客户密码
  206. public function actionResetPayPassword()
  207. {
  208. $post = Yii::$app->request->post();
  209. $userId = isset($post['userId']) ? $post['userId'] : 0;
  210. $password = isset($post['password']) ? $post['password'] : 0;
  211. $info = UserService::getById($userId);
  212. UserService::valid($info, $this->sjId);
  213. UserService::updateById($userId, ['payPassword' => password_hash($password, PASSWORD_BCRYPT)]);
  214. util::complete();
  215. }
  216. }