CustomController.php 15 KB

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