UserController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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\classes\UserClass;
  7. use biz\user\services\UserAssetService;
  8. use biz\user\services\UserGrowthService;
  9. use biz\user\services\UserService;
  10. use biz\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. use yii\helpers\Json;
  20. class UserController extends BaseController
  21. {
  22. //客户列表 shish 2019.11.30
  23. public function actionList()
  24. {
  25. $get = Yii::$app->request->get();
  26. //组建where
  27. $type = isset($get['type']) ? $get['type'] : -1;
  28. $search = isset($get['search']) ? $get['search'] : '';
  29. $where = ['merchantId' => $this->merchantId];
  30. switch ($type) {
  31. case 1:
  32. //粉丝
  33. $where['subscribe'] = 1;
  34. break;
  35. case 2:
  36. //会员
  37. $where['member>'] = -1;
  38. break;
  39. default:
  40. }
  41. if (!empty($search)) {
  42. if (stringUtil::isMobile($search)) {
  43. $where['mobile'] = $search;
  44. } else {
  45. $where['userName'] = ['like', $search];
  46. }
  47. }
  48. $list = UserService::getUserList($where);
  49. $list['asset'] = MerchantAssetService::getByMerchantId($this->merchantId);
  50. util::success($list);
  51. }
  52. //获取客户基本和资产信息 shish 2019.11.30
  53. public function actionDetail()
  54. {
  55. $userId = Yii::$app->request->get('userId');
  56. $info = UserService::getUserInfo($userId);
  57. if (isset($info['merchantId']) == false || $this->merchantId != $info['merchantId']) {
  58. util::fail('没有权限');
  59. }
  60. util::success($info);
  61. }
  62. //获取客户详情,包括基本信息、资产、订单和备注 2019.11.30
  63. public function actionUserDetail()
  64. {
  65. $userId = Yii::$app->request->get('userId');
  66. $info = UserService::getFullUserInfo($userId);
  67. UserService::valid($info, $this->merchantId);
  68. $list = MerchantRemarkService::getRemarkList($userId);
  69. $info['remarkList'] = $list;
  70. util::success($info);
  71. }
  72. //添加客户,申请会员时会自动同步资产 shish 2019.11.30
  73. public function actionAdd()
  74. {
  75. $post = Yii::$app->request->post();
  76. if (isset($post['mobile']) == false || stringUtil::isMobile($post['mobile']) == false) {
  77. util::fail('请输入手机号');
  78. }
  79. if ($post['mobile'] != $post['confirmMobile']) {
  80. util::fail('二次输入手机号不一样');
  81. }
  82. $confirmPassword = isset($post['confirmPassword']) ? $post['confirmPassword'] : '';
  83. AdminService::verifyConfirmPassword($this->adminId, $confirmPassword);
  84. $mobile = $post['mobile'];
  85. $user = UserService::getByMobile($mobile, $this->merchantId);
  86. if (!empty($user)) {
  87. util::fail('手机号已使用,客户已存在');
  88. }
  89. $post['merchantId'] = $this->merchantId;
  90. $post['member'] = $post['memberLevel'];
  91. UserService::addUser($post);
  92. util::complete();
  93. }
  94. //充值 shish 2019.12.1
  95. public function actionRecharge()
  96. {
  97. $post = Yii::$app->request->post();
  98. $confirmPassword = isset($post['confirmPassword']) ? $post['confirmPassword'] : '';
  99. AdminService::verifyConfirmPassword($this->adminId, $confirmPassword);
  100. $userId = isset($post['userId']) ? $post['userId'] : 0;
  101. $rechargeAmount = isset($post['rechargeAmount']) ? $post['rechargeAmount'] : 1;
  102. $user = UserService::getById($userId);
  103. if (empty($user)) {
  104. util::fail('找不到客户');
  105. }
  106. if ($user['merchantId'] != $this->merchantId) {
  107. util::fail('您没有权限操作');
  108. }
  109. $userAsset = UserAssetService::getByUserId($userId);
  110. $totalBalance = $userAsset['balance'] + $rechargeAmount;
  111. $totalRecharge = $userAsset['totalRecharge'] + $rechargeAmount;
  112. $upData['balance'] = $totalBalance;
  113. $upData['totalRecharge'] = $totalRecharge;
  114. $merchantAsset = MerchantAssetService::getByMerchantId($this->merchantId);
  115. $return = xhRechargeService::recharge($rechargeAmount, $user, $this->adminId, $this->merchant, $this->merchantExtend, $merchantAsset);
  116. if ($return['code'] == 'A0001') {
  117. util::complete();
  118. }
  119. util::fail();
  120. }
  121. //升级 shish 2019.12.1
  122. public function actionUpgrade()
  123. {
  124. $post = Yii::$app->request->post();
  125. $userId = isset($post['userId']) ? $post['userId'] : 0;
  126. $member = isset($post['member']) ? $post['member'] : 0;
  127. $user = UserService::getById($userId);
  128. if (empty($user)) {
  129. util::fail('客户不存在');
  130. }
  131. //验证是否商家客户
  132. UserService::valid($user, $this->merchantId);
  133. //验证确认密码
  134. $confirmPassword = $post['confirmPassword'];
  135. AdminService::verifyConfirmPassword($this->adminId, $confirmPassword);
  136. $grade = xhMerchantExtendService::getGradeList($this->merchantExtend, 'desc');
  137. //升级后的成长值
  138. $newLevelGrowth = $grade[$member]['growth'];
  139. $newDiscount = $grade[$member]['discount'];
  140. $userAsset = UserAssetService::getByUserId($userId);
  141. if ($userAsset['member'] >= $member) {
  142. util::fail('客户已经达到此级别了');
  143. }
  144. $now = time();
  145. $date = date("Y-m-d H:i", $now);
  146. $connection = Yii::$app->db;//事务处理
  147. $transaction = $connection->beginTransaction();
  148. try {
  149. $addGrowth = $newLevelGrowth - $userAsset['growth'];
  150. $upData['growth'] = $newLevelGrowth;
  151. $upData['member'] = $member;
  152. $upData['discount'] = $newDiscount;
  153. UserAssetService::updateByUserId($userId, $upData);
  154. UserService::updateById($userId, ['member' => $member]);
  155. $upgradeData = [
  156. 'member' => $member,
  157. 'prevMember' => $userAsset['member'],
  158. 'merchantId' => $this->merchantId,
  159. 'gainType' => 0,
  160. 'addTime' => $now,
  161. 'userId' => $userId,
  162. ];
  163. $upgradeRespond = UserUpgradeService::add($upgradeData);
  164. $growthData = [
  165. 'userId' => $userId,
  166. 'userName' => $user['userName'],
  167. 'merchantId' => $this->merchantId,
  168. 'relateId' => $upgradeRespond['id'],
  169. 'growth' => $newLevelGrowth,
  170. 'num' => $addGrowth,
  171. 'io' => 1,
  172. 'payWay' => 4,
  173. 'alipayId' => '',
  174. 'event' => '管理员操作升到' . $member . '级增加成长值',
  175. 'operatorId' => $userId,
  176. 'createTime' => $date,
  177. 'gainType' => 0,
  178. 'addTime' => $now,
  179. ];
  180. UserGrowthService::add($growthData);
  181. $transaction->commit();
  182. } catch (Exception $e) {
  183. $transaction->rollBack();
  184. util::fail();
  185. }
  186. $allTM = xhTMessageService::getList($this->merchantId);
  187. $openId = isset($user['openId']) ? $user['openId'] : '';
  188. if (empty($openId)) {
  189. util::complete();
  190. }
  191. $shortTempId = 'OPENTM205211943';//升级通知
  192. if (isset($allTM[$shortTempId]) == false) {
  193. util::complete();
  194. }
  195. $tempId = $allTM[$shortTempId];
  196. $data = [
  197. "touser" => $openId, "template_id" => $tempId, "url" => '',
  198. "data" => [
  199. "first" => ["value" => "恭喜,您已经升为{$member}级会员。", "color" => "#173177"],
  200. "keyword1" => ["value" => $user['userName'], "color" => "#173177"],
  201. "keyword2" => ["value" => $date, "color" => "#173177"],
  202. "remark" => ["value" => "点击查看会员权益", "color" => "#173177"],
  203. ]];
  204. wxUtil::sendTaskInform($data, $this->merchant);
  205. util::complete();
  206. }
  207. //重置客户密码
  208. public function actionResetPayPassword()
  209. {
  210. $post = Yii::$app->request->post();
  211. $userId = isset($post['userId']) ? $post['userId'] : 0;
  212. $password = isset($post['password']) ? $post['password'] : 0;
  213. $info = UserService::getById($userId);
  214. UserService::valid($info, $this->merchantId);
  215. UserService::updateById($userId, ['payPassword' => password_hash($password, PASSWORD_BCRYPT)]);
  216. util::complete();
  217. }
  218. //小程序用户完整信息 shish 2019.12.12
  219. public function actionMiniFullInfo()
  220. {
  221. $post = Yii::$app->request->post();
  222. $iv = isset($post['iv']) ? $post['iv'] : '';
  223. $encryptedData = isset($post['encryptedData']) ? $post['encryptedData'] : '';
  224. $merchant = $this->openMerchant;
  225. $appId = $merchant['miniAppId'];
  226. $admin = $this->admin;
  227. $miniOpenId = $admin['miniOpenId'];
  228. if (empty($miniOpenId)) {
  229. util::fail('mini_open_id empty');
  230. }
  231. $cacheKey = 'SHOP_MINI_SESSION_KEY_' . $miniOpenId;
  232. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  233. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  234. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  235. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  236. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  237. if ($errCode != 0) {
  238. Yii::info($result . ' ' . $errCode);
  239. util::fail('获取用户信息失败');
  240. }
  241. Yii::info('小程序获取用户信息:' . $result);
  242. $originalInfo = Json::decode($result);
  243. $source = UserClass::$userSourceId['mini']['name'];
  244. $admin = AdminService::replaceAdmin($originalInfo, $source);
  245. $adminId = $admin['id'];
  246. $admin = AdminService::getAdminById($adminId);
  247. util::success($admin);
  248. }
  249. //小程序用户手机号
  250. public function actionMiniMobile()
  251. {
  252. $post = Yii::$app->request->post();
  253. $iv = $post['iv'];
  254. $encryptedData = $post['encryptedData'];
  255. $merchant = $this->openMerchant;
  256. $appId = $merchant['miniAppId'];
  257. $admin = $this->admin;
  258. $miniOpenId = $admin['miniOpenId'];
  259. $cacheKey = 'SHOP_MINI_SESSION_KEY_' . $miniOpenId;
  260. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  261. if (empty($sessionKey)) {
  262. util::fail('sesstion key empty');
  263. }
  264. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  265. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  266. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  267. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  268. if ($errCode != 0) {
  269. util::fail('解密失败');
  270. }
  271. $arr = Json::decode($result);
  272. $mobile = isset($arr['purePhoneNumber']) ? $arr['purePhoneNumber'] : '';
  273. util::success(['mobile' => $mobile]);
  274. }
  275. }