UserController.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. namespace shop\controllers;
  3. use biz\admin\services\AdminService;
  4. use biz\merchant\services\MerchantAssetService;
  5. use biz\merchant\services\MerchantRemarkService;
  6. use biz\user\services\UserAssetService;
  7. use biz\user\services\UserGrowthService;
  8. use biz\user\services\UserService;
  9. use biz\user\services\UserUpgradeService;
  10. use common\components\wxUtil;
  11. use common\services\xhMerchantExtendService;
  12. use common\services\xhRechargeService;
  13. use common\services\xhTMessageService;
  14. use common\services\xhUserIntegralService;
  15. use Yii;
  16. use common\components\stringUtil;
  17. use common\components\util;
  18. class UserController extends BaseController
  19. {
  20. //客户列表 shish 2019.11.30
  21. public function actionList()
  22. {
  23. $get = Yii::$app->request->get();
  24. //组建where
  25. $type = isset($get['type']) ? $get['type'] : -1;
  26. $search = isset($get['search']) ? $get['search'] : '';
  27. $where = ['merchantId' => $this->merchantId];
  28. switch ($type) {
  29. case 1:
  30. //粉丝
  31. $where['subscribe'] = 1;
  32. break;
  33. case 2:
  34. //会员
  35. $where['member>'] = -1;
  36. break;
  37. default:
  38. }
  39. if (!empty($search)) {
  40. if (stringUtil::isMobile($search)) {
  41. $where['mobile'] = $search;
  42. } else {
  43. $where['userName'] = ['like', $search];
  44. }
  45. }
  46. $list = UserService::getUserList($where);
  47. $list['asset'] = MerchantAssetService::getByMerchantId($this->merchantId);
  48. util::success($list);
  49. }
  50. //获取客户基本和资产信息 shish 2019.11.30
  51. public function actionDetail()
  52. {
  53. $userId = Yii::$app->request->get('userId');
  54. $info = UserService::getUserInfo($userId);
  55. if (isset($info['merchantId']) == false || $this->merchantId != $info['merchantId']) {
  56. util::fail('没有权限');
  57. }
  58. util::success($info);
  59. }
  60. //获取客户详情,包括基本信息、资产、订单和备注 2019.11.30
  61. public function actionUserDetail()
  62. {
  63. $userId = Yii::$app->request->get('userId');
  64. $info = UserService::getFullUserInfo($userId);
  65. UserService::valid($info, $this->merchantId);
  66. $list = MerchantRemarkService::getRemarkList($userId);
  67. $info['remarkList'] = $list;
  68. util::success($info);
  69. }
  70. //添加客户,申请会员时会自动同步资产 shish 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. $confirmPassword = isset($post['confirmPassword']) ? $post['confirmPassword'] : '';
  81. AdminService::verifyConfirmPassword($this->adminId, $confirmPassword);
  82. $mobile = $post['mobile'];
  83. $user = UserService::getByMobile($mobile, $this->merchantId);
  84. if (!empty($user)) {
  85. util::fail('手机号已使用');
  86. }
  87. $post['merchantId'] = $this->merchantId;
  88. $post['member'] = $post['memberLevel'];
  89. UserService::addUser($post);
  90. util::complete();
  91. }
  92. //充值 shish 2019.12.1
  93. public function actionRecharge()
  94. {
  95. $post = Yii::$app->request->post();
  96. $confirmPassword = isset($post['confirmPassword']) ? $post['confirmPassword'] : '';
  97. AdminService::verifyConfirmPassword($this->adminId, $confirmPassword);
  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['merchantId'] != $this->merchantId) {
  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::getByMerchantId($this->merchantId);
  113. $return = xhRechargeService::recharge($rechargeAmount, $user, $this->adminId, $this->merchant, $this->merchantExtend, $merchantAsset);
  114. if ($return['code'] == 'A0001') {
  115. util::complete();
  116. }
  117. util::fail();
  118. }
  119. //升级 shish 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->merchantId);
  131. //验证确认密码
  132. $confirmPassword = $post['confirmPassword'];
  133. AdminService::verifyConfirmPassword($this->adminId, $confirmPassword);
  134. $grade = xhMerchantExtendService::getGradeList($this->merchantExtend, '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. 'merchantId' => $this->merchantId,
  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. 'merchantId' => $this->merchantId,
  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->merchantId);
  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->merchant);
  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->merchantId);
  213. UserService::updateById($userId, ['payPassword' => password_hash($password, PASSWORD_BCRYPT)]);
  214. util::complete();
  215. }
  216. }