CustomController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. <?php
  2. namespace hd\controllers;
  3. use bizHd\admin\services\AdminService;
  4. use bizHd\custom\classes\CustomClass;
  5. use bizHd\custom\services\CustomService;
  6. use biz\sj\services\MerchantAssetService;
  7. use biz\sj\services\MerchantRemarkService;
  8. use bizHd\user\services\UserAssetService;
  9. use bizHd\user\services\UserGrowthService;
  10. use bizHd\user\services\UserService;
  11. use bizHd\user\services\UserUpgradeService;
  12. use common\components\wxUtil;
  13. use common\services\xhMerchantExtendService;
  14. use common\services\xhRechargeService;
  15. use common\services\xhTMessageService;
  16. use common\services\xhUserIntegralService;
  17. use Yii;
  18. use common\components\stringUtil;
  19. use common\components\util;
  20. use biz\sj\services\MerchantExtendService;
  21. use bizHd\message\services\SmsService;
  22. use bizHd\user\classes\UserClass;
  23. use yii\helpers\Json;
  24. class CustomController extends BaseController
  25. {
  26. //客户列表 ssh 2019.11.30
  27. public function actionList()
  28. {
  29. $where = ['sjId' => $this->sjId];
  30. $list = CustomService::getCustomList($where);
  31. util::success($list);
  32. }
  33. //获取客户基本和资产信息 ssh 2019.11.30
  34. public function actionDetail()
  35. {
  36. $userId = Yii::$app->request->get('userId');
  37. $info = CustomClass::getCustomInfo($userId);
  38. CustomClass::valid($info, $this->sjId);
  39. util::success($info);
  40. }
  41. //获取客户详情,包括基本信息、资产、订单和备注 2019.11.30
  42. public function actionUserDetail()
  43. {
  44. $userId = Yii::$app->request->get('userId');
  45. $info = UserService::getFullUserInfo($userId);
  46. UserService::valid($info, $this->sjId);
  47. $list = MerchantRemarkService::getRemarkList($userId);
  48. $info['remarkList'] = $list;
  49. util::success($info);
  50. }
  51. //添加客户,申请会员时会自动同步资产 ssh 2019.11.30
  52. public function actionAdd()
  53. {
  54. $post = Yii::$app->request->post();
  55. if (isset($post['mobile']) == false || stringUtil::isMobile($post['mobile']) == false) {
  56. util::fail('请输入手机号');
  57. }
  58. if ($post['mobile'] != $post['confirmMobile']) {
  59. util::fail('二次输入手机号不一样');
  60. }
  61. $confirmPassword = isset($post['confirmPassword']) ? $post['confirmPassword'] : '';
  62. AdminService::verifyConfirmPassword($this->adminId, $confirmPassword);
  63. $mobile = $post['mobile'];
  64. $user = UserService::getByMobile($mobile, $this->sjId);
  65. if (!empty($user)) {
  66. util::fail('手机号已使用');
  67. }
  68. $post['merchantId'] = $this->sjId;
  69. $post['member'] = $post['memberLevel'];
  70. UserService::addUser($post);
  71. util::complete();
  72. }
  73. //充值 ssh 2019.12.1
  74. public function actionRecharge()
  75. {
  76. $post = Yii::$app->request->post();
  77. $confirmPassword = isset($post['confirmPassword']) ? $post['confirmPassword'] : '';
  78. AdminService::verifyConfirmPassword($this->adminId, $confirmPassword);
  79. $userId = isset($post['userId']) ? $post['userId'] : 0;
  80. $rechargeAmount = isset($post['rechargeAmount']) ? $post['rechargeAmount'] : 1;
  81. $user = UserService::getById($userId);
  82. if (empty($user)) {
  83. util::fail('找不到客户');
  84. }
  85. if ($user['merchantId'] != $this->sjId) {
  86. util::fail('您没有权限操作');
  87. }
  88. $userAsset = UserAssetService::getByUserId($userId);
  89. $totalBalance = $userAsset['balance'] + $rechargeAmount;
  90. $totalRecharge = $userAsset['totalRecharge'] + $rechargeAmount;
  91. $upData['balance'] = $totalBalance;
  92. $upData['totalRecharge'] = $totalRecharge;
  93. $merchantAsset = MerchantAssetService::getByMerchantId($this->sjId);
  94. $return = xhRechargeService::recharge($rechargeAmount, $user, $this->adminId, $this->sj, $this->sjExtend, $merchantAsset);
  95. if ($return['code'] == 'A0001') {
  96. util::complete();
  97. }
  98. util::fail();
  99. }
  100. //升级 ssh 2019.12.1
  101. public function actionUpgrade()
  102. {
  103. $post = Yii::$app->request->post();
  104. $userId = isset($post['userId']) ? $post['userId'] : 0;
  105. $member = isset($post['member']) ? $post['member'] : 0;
  106. $user = UserService::getById($userId);
  107. if (empty($user)) {
  108. util::fail('客户不存在');
  109. }
  110. //验证是否商家客户
  111. UserService::valid($user, $this->sjId);
  112. //验证确认密码
  113. $confirmPassword = $post['confirmPassword'];
  114. AdminService::verifyConfirmPassword($this->adminId, $confirmPassword);
  115. $grade = xhMerchantExtendService::getGradeList($this->sjExtend, 'desc');
  116. //升级后的成长值
  117. $newLevelGrowth = $grade[$member]['growth'];
  118. $newDiscount = $grade[$member]['discount'];
  119. $userAsset = UserAssetService::getByUserId($userId);
  120. if ($userAsset['member'] >= $member) {
  121. util::fail('客户已经达到此级别了');
  122. }
  123. $now = time();
  124. $date = date("Y-m-d H:i", $now);
  125. $connection = Yii::$app->db;//事务处理
  126. $transaction = $connection->beginTransaction();
  127. try {
  128. $addGrowth = $newLevelGrowth - $userAsset['growth'];
  129. $upData['growth'] = $newLevelGrowth;
  130. $upData['member'] = $member;
  131. $upData['discount'] = $newDiscount;
  132. UserAssetService::updateByUserId($userId, $upData);
  133. UserService::updateById($userId, ['member' => $member]);
  134. $upgradeData = [
  135. 'member' => $member,
  136. 'prevMember' => $userAsset['member'],
  137. 'merchantId' => $this->sjId,
  138. 'gainType' => 0,
  139. 'addTime' => $now,
  140. 'userId' => $userId,
  141. ];
  142. $upgradeRespond = UserUpgradeService::add($upgradeData);
  143. $growthData = [
  144. 'userId' => $userId,
  145. 'userName' => $user['userName'],
  146. 'merchantId' => $this->sjId,
  147. 'relateId' => $upgradeRespond['id'],
  148. 'growth' => $newLevelGrowth,
  149. 'num' => $addGrowth,
  150. 'io' => 1,
  151. 'payWay' => 4,
  152. 'alipayId' => '',
  153. 'event' => '管理员操作升到' . $member . '级增加成长值',
  154. 'operatorId' => $userId,
  155. 'createTime' => $date,
  156. 'gainType' => 0,
  157. 'addTime' => $now,
  158. ];
  159. UserGrowthService::add($growthData);
  160. $transaction->commit();
  161. } catch (Exception $e) {
  162. $transaction->rollBack();
  163. util::fail();
  164. }
  165. $allTM = xhTMessageService::getList($this->sjId);
  166. $openId = isset($user['openId']) ? $user['openId'] : '';
  167. if (empty($openId)) {
  168. util::complete();
  169. }
  170. $shortTempId = 'OPENTM205211943';//升级通知
  171. if (isset($allTM[$shortTempId]) == false) {
  172. util::complete();
  173. }
  174. $tempId = $allTM[$shortTempId];
  175. $data = [
  176. "touser" => $openId, "template_id" => $tempId, "url" => '',
  177. "data" => [
  178. "first" => ["value" => "恭喜,您已经升为{$member}级会员。", "color" => "#173177"],
  179. "keyword1" => ["value" => $user['userName'], "color" => "#173177"],
  180. "keyword2" => ["value" => $date, "color" => "#173177"],
  181. "remark" => ["value" => "点击查看会员权益", "color" => "#173177"],
  182. ]];
  183. wxUtil::sendTaskInform($data, $this->sj);
  184. util::complete();
  185. }
  186. //重置客户密码
  187. public function actionResetPayPassword()
  188. {
  189. $post = Yii::$app->request->post();
  190. $userId = isset($post['userId']) ? $post['userId'] : 0;
  191. $password = isset($post['password']) ? $post['password'] : 0;
  192. $info = UserService::getById($userId);
  193. UserService::valid($info, $this->sjId);
  194. UserService::updateById($userId, ['payPassword' => password_hash($password, PASSWORD_BCRYPT)]);
  195. util::complete();
  196. }
  197. //获取登陆客户的资产 ssh 2019.11.22
  198. public function actionAsset()
  199. {
  200. $asset = UserAssetService::getByUserId($this->adminId);
  201. util::success($asset);
  202. }
  203. //获取登陆客户的优惠情况 ssh 2019.12.3
  204. public function actionDiscount()
  205. {
  206. $discount = UserAssetService::getDiscount($this->adminId);
  207. util::success($discount);
  208. }
  209. //小程序用户完整信息 ssh 2019.12.12
  210. public function actionMiniFullInfo()
  211. {
  212. $post = Yii::$app->request->post();
  213. $iv = isset($post['iv']) ? $post['iv'] : '';
  214. $encryptedData = isset($post['encryptedData']) ? $post['encryptedData'] : '';
  215. $appId = $this->sj['miniAppId'];
  216. $merchantId = $this->sjId;
  217. $miniOpenId = $this->user['miniOpenId'];
  218. if (empty($miniOpenId)) {
  219. util::fail('mini_open_id empty');
  220. }
  221. $cacheKey = 'MALL_MINI_SESSION_KEY_' . $miniOpenId;
  222. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  223. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  224. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  225. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  226. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  227. if ($errCode != 0) {
  228. Yii::info($result . ' ' . $errCode);
  229. util::fail('获取用户信息失败');
  230. }
  231. Yii::info('小程序获取用户信息:' . $result);
  232. $originalInfo = Json::decode($result);
  233. $source = UserClass::$userSourceId['mini']['name'];
  234. $user = UserService::replaceUser($originalInfo, $source, $merchantId);
  235. $userId = $user['id'];
  236. $userInfo = UserService::getUserInfo($userId);
  237. util::success($userInfo);
  238. }
  239. //小程序用户手机号
  240. public function actionMiniMobile()
  241. {
  242. $post = Yii::$app->request->post();
  243. $iv = $post['iv'];
  244. $encryptedData = $post['encryptedData'];
  245. $merchantId = $this->sjId;
  246. $merchant = $this->sj;
  247. $appId = $merchant['miniAppId'];
  248. $miniOpenId = $this->user['miniOpenId'];
  249. $user = $this->user;
  250. if (!empty($user) && !empty($user['mobile'])) {
  251. $userId = $user['id'];
  252. util::success(['mobile' => $user['mobile']]);
  253. }
  254. $cacheKey = 'MALL_MINI_SESSION_KEY_' . $miniOpenId;
  255. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  256. if (empty($sessionKey)) {
  257. util::fail('系统错误,sesstion empty');
  258. }
  259. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  260. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  261. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  262. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  263. if ($errCode != 0) {
  264. util::fail('解密失败');
  265. }
  266. $arr = Json::decode($result);
  267. $mobile = isset($arr['purePhoneNumber']) ? $arr['purePhoneNumber'] : '';
  268. if (empty($mobile)) {
  269. util::fail('没有获取手机号,请重试');
  270. }
  271. $userId = $user['id'];
  272. UserService::becomeMember($user, $merchant, ['mobile' => $mobile]);
  273. util::success(['mobile' => $mobile]);
  274. }
  275. //密码修改
  276. public function actionPassword()
  277. {
  278. $post = Yii::$app->request->post();
  279. $old = isset($post['old']) ? $post['old'] : '';
  280. $new = isset($post['new']) ? $post['new'] : '';
  281. $confirm = isset($post['confirm']) ? $post['confirm'] : '';
  282. if (empty($new)) {
  283. util::fail('请输入新密码');
  284. }
  285. if ($new != $confirm) {
  286. util::fail('二次密码不一致');
  287. }
  288. $userId = $this->adminId;
  289. $user = UserService::getUserInfo($userId, false);
  290. if (isset($user['hasPayPwd']) && $user['hasPayPwd'] == 1) {
  291. if (empty($old)) {
  292. util::fail('请填写旧密码');
  293. }
  294. if (password_verify($old, $user['payPassword']) == false) {
  295. util::fail('旧密码错误');
  296. }
  297. }
  298. UserService::updateById($userId, ['payPassword' => password_hash($new, PASSWORD_BCRYPT), 'hasPayPwd' => 1]);
  299. util::complete('修改成功');
  300. }
  301. //申请会员 ssh 2019.12.18
  302. public function actionApplyMember()
  303. {
  304. $post = Yii::$app->request->post();
  305. $mobile = isset($post['mobile']) ? $post['mobile'] : 0;
  306. if (stringUtil::isMobile($mobile) == false) {
  307. util::fail('请输入手机号');
  308. }
  309. $userId = $this->adminId;
  310. $asset = UserAssetService::getByUserId($userId);
  311. if (isset($asset['member']) && $asset['member'] != -1) {
  312. util::fail('已经是会员了');
  313. }
  314. $code = isset($post['code']) ? $post['code'] : '';
  315. $cacheCode = SmsService::getApplyMemberCode($userId, $mobile);
  316. if (empty($cacheCode) || $cacheCode != $code) {
  317. util::fail('验证码错误');
  318. }
  319. $update = [];
  320. if (isset($post['realName']) && !empty($post['realName'])) {
  321. $update['realName'] = $post['realName'];
  322. }
  323. if (isset($post['birthday']) && !empty($post['birthday'])) {
  324. $update['birthday'] = $post['birthday'];
  325. }
  326. $user = UserService::getByCondition(['merchantId' => $this->sjId, 'mobile' => $mobile]);
  327. if ($userId == $user['id']) {
  328. util::fail('手机号已验证');
  329. }
  330. if (!empty($user)) {
  331. util::fail('手机号已经注册过了');
  332. }
  333. $update['mobile'] = $mobile;
  334. UserService::becomeMember($this->user, $this->sj, $update);
  335. util::complete('注册成功');
  336. }
  337. //用户基本信息和资产 ssh 2019.12.19
  338. public function actionMyProfile()
  339. {
  340. $custom = $this->custom;
  341. $user = UserService::getUserInfo($this->adminId);
  342. $growth = $custom['growth'] ?? 0;
  343. $extend = $this->sjExtend;
  344. $grade = MerchantExtendService::getGradeList($extend, 'asc');
  345. //下个等级的积分
  346. $nextLevelGrowth = 0;
  347. foreach ($grade as $val) {
  348. $currentGrowth = $val['growth'];
  349. if ($currentGrowth > $growth) {
  350. $nextLevelGrowth = $currentGrowth;
  351. break;
  352. }
  353. }
  354. $user['userAsset']['nextLevelGrowth'] = $nextLevelGrowth;
  355. util::success($user);
  356. }
  357. //登陆客户的信息 ssh 2020.3.12
  358. public function actionLoginDetail()
  359. {
  360. $user = $this->user;
  361. util::success($user);
  362. }
  363. }