UserController.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. namespace business\controllers;
  3. use biz\admin\services\AdminService;
  4. use biz\merchant\services\MerchantExtendService;
  5. use biz\user\services\UserAssetService;
  6. use biz\user\services\UserIntegralService;
  7. use biz\user\services\UserService;
  8. use common\components\weixinUtil;
  9. use common\services\xhMerchantExtendService;
  10. use common\services\xhRechargeService;
  11. use common\services\xhTMessageService;
  12. use common\services\xhUserIntegralService;
  13. use Yii;
  14. use common\components\stringUtil;
  15. use common\components\util;
  16. class UserController extends BaseController
  17. {
  18. //客户列表 shish 2019.11.30
  19. public function actionList()
  20. {
  21. $get = Yii::$app->request->get();
  22. //组建where
  23. $type = isset($get['type']) ? $get['type'] : 0;
  24. $search = isset($get['search']) ? $get['search'] : '';
  25. $where = ['merchantId' => $this->merchantId];
  26. switch ($type) {
  27. case 1:
  28. //粉丝
  29. $where['subscribe'] = 1;
  30. break;
  31. case 2:
  32. //会员
  33. $where['isMember'] = 1;
  34. break;
  35. default:
  36. }
  37. if (!empty($search)) {
  38. if (stringUtil::isMobieNumber($search)) {
  39. $where['mobile'] = $search;
  40. } else {
  41. $where['userName'] = ['like', $search];
  42. }
  43. }
  44. $list = UserService::getUserList($where);
  45. util::success($list);
  46. }
  47. //获取客户基本和资产信息 shish 2019.11.30
  48. public function actionDetail()
  49. {
  50. $userId = Yii::$app->request->get('userId');
  51. $info = UserService::getUserInfo($userId);
  52. if (isset($info['merchantId']) == false || $this->merchantId != $info['merchantId']) {
  53. util::fail('没有权限');
  54. }
  55. util::success($info);
  56. }
  57. //获取客户基本、资产和订单完整信息 2019.11.30
  58. public function actionFullDetail()
  59. {
  60. $userId = Yii::$app->request->get('userId');
  61. $info = UserService::getFullUserInfo($userId);
  62. UserService::valid($info, $this->merchantId);
  63. util::success($info);
  64. }
  65. //添加客户,申请会员时会自动同步资产 shish 2019.11.30
  66. public function actionAdd()
  67. {
  68. $post = Yii::$app->request->post();
  69. if ($post['mobile'] != $post['confirmMobile']) {
  70. util::fail('二次输入手机号不一样');
  71. }
  72. $confirmPassword = isset($post['confirmPassword']) ? $post['confirmPassword'] : '';
  73. AdminService::verifyConfirmPassword($this->adminId, $confirmPassword);
  74. $mobile = $post['mobile'];
  75. $user = UserService::getByMobile($mobile, $this->merchantId);
  76. if (!empty($user)) {
  77. util::fail('手机号已使用,客户已存在');
  78. }
  79. $originalInfo = [];
  80. $merchantId = $this->merchantId;
  81. $originalInfo['merchantId'] = $merchantId;
  82. $originalInfo['mobile'] = $mobile;
  83. $originalInfo['realName'] = $post['realName'];
  84. $userSource = Yii::$app->dict->getValue('userSourceGetId', 'official');
  85. $source = $userSource['name'];
  86. $user = UserService::replaceUser($originalInfo, $source, $merchantId);
  87. //等级
  88. $memberLevel = isset($post['memberLevel']) ? $post['memberLevel'] : 0;
  89. $extend = MerchantExtendService::getByMerchantId($this->merchantId);
  90. $gradeList = xhMerchantExtendService::getGradeList($extend, 'desc');
  91. $growth = isset($gradeList[$memberLevel]['growth']) ? $gradeList[$memberLevel]['growth'] : 0;
  92. //余额成长值更新
  93. $userId = $user['id'];
  94. $balance = isset($post['balance']) && is_numeric($post['balance']) ? $post['balance'] : 0;
  95. $data = ['balance' => $balance, 'growth' => $growth];
  96. UserAssetService::updateByUserId($userId, $data);
  97. //会员升级
  98. UserIntegralService::increaseToUpgrade(0, $growth, 0, $userId, $merchantId, $extend);
  99. util::ok();
  100. }
  101. //充值 shish 2019.12.1
  102. public function actionRecharge()
  103. {
  104. $post = Yii::$app->request->post();
  105. $confirmPassword = isset($post['confirmPassword']) ? $post['confirmPassword'] : '';
  106. AdminService::verifyConfirmPassword($this->adminId, $confirmPassword);
  107. $userId = isset($post['userId']) ? $post['userId'] : 0;
  108. $rechargeAmount = isset($post['rechargeAmount']) ? $post['rechargeAmount'] : 1;
  109. $user = UserService::getById($userId);
  110. if (empty($user)) {
  111. util::fail('找不到客户');
  112. }
  113. if ($user['merchantId'] != $this->merchantId) {
  114. util::fail('您没有权限操作');
  115. }
  116. $userAsset = UserAssetService::getByUserId($userId);
  117. $totalBalance = $userAsset['balance'] + $rechargeAmount;
  118. $totalRecharge = $userAsset['totalRecharge'] + $rechargeAmount;
  119. $upData['balance'] = $totalBalance;
  120. $upData['totalRecharge'] = $totalRecharge;
  121. $return = xhRechargeService::recharge($rechargeAmount, $user, $this->adminId, $this->merchant, $this->merchantExtend, $this->merchantAsset);
  122. if ($return['code'] == 'A0001') {
  123. util::ok();
  124. }
  125. util::fail();
  126. }
  127. //升级 shish 2019.12.1
  128. public function actionUpgrade()
  129. {
  130. $post = Yii::$app->request->post();
  131. $userId = isset($post['userId']) ? $post['userId'] : 0;
  132. $memberLevel = isset($post['memberLevel']) ? $post['memberLevel'] : 0;
  133. $user = UserService::getById($userId);
  134. if (empty($user)) {
  135. util::fail('客户不存在');
  136. }
  137. if ($user['merchantId'] != $this->merchantId) {
  138. util::fail('您没有权限操作');
  139. }
  140. $confirmPassword = $post['confirmPassword'];
  141. AdminService::verifyConfirmPassword($this->adminId, $confirmPassword);
  142. $grade = xhMerchantExtendService::getGradeList($this->merchantExtend, 'desc');
  143. $newLevelGrowth = $grade[$memberLevel]['growth'];//升级到当前级别需要的积分数
  144. $userAsset = UserAssetService::getByUserId($userId);//用户资产
  145. if (!empty($userAsset['isMember']) && $userAsset['memberLevel'] >= $memberLevel) {
  146. util::fail('客户已经达到此级别了');
  147. }
  148. $now = time();
  149. $date = date("Y-m-d H:i", $now);
  150. $connection = Yii::$app->db;//事务处理
  151. $transaction = $connection->beginTransaction();
  152. try {
  153. $addGrowth = $newLevelGrowth - $userAsset['growth'];
  154. //升级后的成长值
  155. $upData['growth'] = $newLevelGrowth;
  156. $upData['memberLevel'] = $memberLevel;
  157. $upData['isMember'] = 1;
  158. UserAssetService::updateByUserId($userId, $upData);
  159. $integralData = [
  160. 'userId' => $userId,
  161. 'userName' => $user['userName'],
  162. 'merchantId' => $this->merchantId,
  163. 'relateId' => '',
  164. 'integral' => $newLevelGrowth,
  165. 'num' => $addGrowth,
  166. 'io' => 1,
  167. 'payWay' => 4,
  168. 'alipayId' => '',
  169. 'event' => '店长操作升级',
  170. 'operatorId' => $userId,
  171. 'createTime' => $date,
  172. 'capitalType' => 0,
  173. 'addTime' => $now,
  174. ];
  175. //用户兑换型积分流水增加
  176. xhUserIntegralService::add($integralData);
  177. $transaction->commit();
  178. } catch (Exception $e) {
  179. $transaction->rollBack();
  180. util::fail();
  181. }
  182. $allTM = xhTMessageService::getList($this->merchantId);
  183. $openId = isset($user['openId']) ? $user['openId'] : '';
  184. if (empty($openId)) {
  185. util::ok();
  186. }
  187. $shortTempId = 'OPENTM205211943';//升级通知
  188. if (isset($allTM[$shortTempId]) == false) {
  189. util::ok();
  190. }
  191. $tempId = $allTM[$shortTempId];
  192. $data = [
  193. "touser" => $openId, "template_id" => $tempId, "url" => '',
  194. "data" => [
  195. "first" => ["value" => "恭喜,您已经升为{$memberLevel}级会员。", "color" => "#173177"],
  196. "keyword1" => ["value" => $user['userName'], "color" => "#173177"],
  197. "keyword2" => ["value" => $date, "color" => "#173177"],
  198. "remark" => ["value" => "点击查看会员权益", "color" => "#173177"],
  199. ]];
  200. weixinUtil::sendTaskInform($data, $this->merchant);
  201. util::ok();
  202. }
  203. }